xref: /onnv-gate/usr/src/uts/sun4/io/px/px_dma.c (revision 12027:6c3efe08f39d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51501Sgovinda  * Common Development and Distribution License (the "License").
61501Sgovinda  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*12027SStephen.Hanson@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * PCI Express nexus DVMA and DMA core routines:
280Sstevel@tonic-gate  *	dma_map/dma_bind_handle implementation
290Sstevel@tonic-gate  *	bypass and peer-to-peer support
300Sstevel@tonic-gate  *	fast track DVMA space allocation
310Sstevel@tonic-gate  *	runtime DVMA debug
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <sys/kmem.h>
350Sstevel@tonic-gate #include <sys/async.h>
360Sstevel@tonic-gate #include <sys/sysmacros.h>
370Sstevel@tonic-gate #include <sys/sunddi.h>
380Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
390Sstevel@tonic-gate #include "px_obj.h"
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /*LINTLIBRARY*/
420Sstevel@tonic-gate 
430Sstevel@tonic-gate /*
440Sstevel@tonic-gate  * px_dma_allocmp - Allocate a pci dma implementation structure
450Sstevel@tonic-gate  *
460Sstevel@tonic-gate  * An extra ddi_dma_attr structure is bundled with the usual ddi_dma_impl
470Sstevel@tonic-gate  * to hold unmodified device limits. The ddi_dma_attr inside the
480Sstevel@tonic-gate  * ddi_dma_impl structure is augumented with system limits to enhance
490Sstevel@tonic-gate  * DVMA performance at runtime. The unaugumented device limits saved
500Sstevel@tonic-gate  * right after (accessed through (ddi_dma_attr_t *)(mp + 1)) is used
510Sstevel@tonic-gate  * strictly for peer-to-peer transfers which do not obey system limits.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * return: DDI_SUCCESS DDI_DMA_NORESOURCES
540Sstevel@tonic-gate  */
550Sstevel@tonic-gate ddi_dma_impl_t *
px_dma_allocmp(dev_info_t * dip,dev_info_t * rdip,int (* waitfp)(caddr_t),caddr_t arg)560Sstevel@tonic-gate px_dma_allocmp(dev_info_t *dip, dev_info_t *rdip, int (*waitfp)(caddr_t),
570Sstevel@tonic-gate 	caddr_t arg)
580Sstevel@tonic-gate {
590Sstevel@tonic-gate 	register ddi_dma_impl_t *mp;
600Sstevel@tonic-gate 	int sleep = (waitfp == DDI_DMA_SLEEP) ? KM_SLEEP : KM_NOSLEEP;
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	/* Caution: we don't use zalloc to enhance performance! */
630Sstevel@tonic-gate 	if ((mp = kmem_alloc(sizeof (px_dma_hdl_t), sleep)) == 0) {
640Sstevel@tonic-gate 		DBG(DBG_DMA_MAP, dip, "can't alloc dma_handle\n");
650Sstevel@tonic-gate 		if (waitfp != DDI_DMA_DONTWAIT) {
660Sstevel@tonic-gate 			DBG(DBG_DMA_MAP, dip, "alloc_mp kmem cb\n");
670Sstevel@tonic-gate 			ddi_set_callback(waitfp, arg, &px_kmem_clid);
680Sstevel@tonic-gate 		}
690Sstevel@tonic-gate 		return (mp);
700Sstevel@tonic-gate 	}
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	mp->dmai_rdip = rdip;
730Sstevel@tonic-gate 	mp->dmai_flags = 0;
740Sstevel@tonic-gate 	mp->dmai_pfnlst = NULL;
750Sstevel@tonic-gate 	mp->dmai_winlst = NULL;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	/*
780Sstevel@tonic-gate 	 * kmem_alloc debug: the following fields are not zero-ed
790Sstevel@tonic-gate 	 * mp->dmai_mapping = 0;
800Sstevel@tonic-gate 	 * mp->dmai_size = 0;
810Sstevel@tonic-gate 	 * mp->dmai_offset = 0;
820Sstevel@tonic-gate 	 * mp->dmai_minxfer = 0;
830Sstevel@tonic-gate 	 * mp->dmai_burstsizes = 0;
840Sstevel@tonic-gate 	 * mp->dmai_ndvmapages = 0;
850Sstevel@tonic-gate 	 * mp->dmai_pool/roffset = 0;
860Sstevel@tonic-gate 	 * mp->dmai_rflags = 0;
870Sstevel@tonic-gate 	 * mp->dmai_inuse/flags
880Sstevel@tonic-gate 	 * mp->dmai_nwin = 0;
890Sstevel@tonic-gate 	 * mp->dmai_winsize = 0;
900Sstevel@tonic-gate 	 * mp->dmai_nexus_private/tte = 0;
910Sstevel@tonic-gate 	 * mp->dmai_iopte/pfnlst
920Sstevel@tonic-gate 	 * mp->dmai_sbi/pfn0 = 0;
930Sstevel@tonic-gate 	 * mp->dmai_minfo/winlst/fdvma
940Sstevel@tonic-gate 	 * mp->dmai_rdip
950Sstevel@tonic-gate 	 * bzero(&mp->dmai_object, sizeof (ddi_dma_obj_t));
960Sstevel@tonic-gate 	 * bzero(&mp->dmai_attr, sizeof (ddi_dma_attr_t));
970Sstevel@tonic-gate 	 * mp->dmai_cookie = 0;
980Sstevel@tonic-gate 	 */
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	mp->dmai_attr.dma_attr_version = (uint_t)DMA_ATTR_VERSION;
1010Sstevel@tonic-gate 	mp->dmai_attr.dma_attr_flags = (uint_t)0;
1020Sstevel@tonic-gate 	mp->dmai_fault = 0;
1030Sstevel@tonic-gate 	mp->dmai_fault_check = NULL;
1040Sstevel@tonic-gate 	mp->dmai_fault_notify = NULL;
105758Svgadre 
106758Svgadre 	mp->dmai_error.err_ena = 0;
107758Svgadre 	mp->dmai_error.err_status = DDI_FM_OK;
108758Svgadre 	mp->dmai_error.err_expected = DDI_FM_ERR_UNEXPECTED;
109758Svgadre 	mp->dmai_error.err_ontrap = NULL;
110758Svgadre 	mp->dmai_error.err_fep = NULL;
1111865Sdilpreet 	mp->dmai_error.err_cf = NULL;
112758Svgadre 
1133156Sgirish 	/*
1148413SDaniel.Ice@Sun.COM 	 * The bdf protection value is set to immediate child
1158413SDaniel.Ice@Sun.COM 	 * at first. It gets modified by switch/bridge drivers
1168413SDaniel.Ice@Sun.COM 	 * as the code traverses down the fabric topology.
1178413SDaniel.Ice@Sun.COM 	 *
1188413SDaniel.Ice@Sun.COM 	 * XXX No IOMMU protection for broken devices.
1193156Sgirish 	 */
1208413SDaniel.Ice@Sun.COM 	ASSERT((intptr_t)ddi_get_parent_data(rdip) >> 1 == 0);
1219921SKrishna.Elango@Sun.COM 	mp->dmai_bdf = ((intptr_t)ddi_get_parent_data(rdip) == 1) ?
1229921SKrishna.Elango@Sun.COM 	    PCIE_INVALID_BDF : pcie_get_bdf_for_dma_xfer(dip, rdip);
1233156Sgirish 
124*12027SStephen.Hanson@Sun.COM 	ndi_fmc_insert(rdip, DMA_HANDLE, mp, NULL);
1250Sstevel@tonic-gate 	return (mp);
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate void
px_dma_freemp(ddi_dma_impl_t * mp)1290Sstevel@tonic-gate px_dma_freemp(ddi_dma_impl_t *mp)
1300Sstevel@tonic-gate {
131*12027SStephen.Hanson@Sun.COM 	ndi_fmc_remove(mp->dmai_rdip, DMA_HANDLE, mp);
1320Sstevel@tonic-gate 	if (mp->dmai_ndvmapages > 1)
1330Sstevel@tonic-gate 		px_dma_freepfn(mp);
1340Sstevel@tonic-gate 	if (mp->dmai_winlst)
1350Sstevel@tonic-gate 		px_dma_freewin(mp);
1360Sstevel@tonic-gate 	kmem_free(mp, sizeof (px_dma_hdl_t));
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate void
px_dma_freepfn(ddi_dma_impl_t * mp)1400Sstevel@tonic-gate px_dma_freepfn(ddi_dma_impl_t *mp)
1410Sstevel@tonic-gate {
1420Sstevel@tonic-gate 	void *addr = mp->dmai_pfnlst;
1430Sstevel@tonic-gate 	if (addr) {
1440Sstevel@tonic-gate 		size_t npages = mp->dmai_ndvmapages;
1450Sstevel@tonic-gate 		if (npages > 1)
1460Sstevel@tonic-gate 			kmem_free(addr, npages * sizeof (px_iopfn_t));
1470Sstevel@tonic-gate 		mp->dmai_pfnlst = NULL;
1480Sstevel@tonic-gate 	}
1490Sstevel@tonic-gate 	mp->dmai_ndvmapages = 0;
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate /*
1530Sstevel@tonic-gate  * px_dma_lmts2hdl - alloate a ddi_dma_impl_t, validate practical limits
1540Sstevel@tonic-gate  *			and convert dmareq->dmar_limits to mp->dmai_attr
1550Sstevel@tonic-gate  *
1560Sstevel@tonic-gate  * ddi_dma_impl_t member modified     input
1570Sstevel@tonic-gate  * ------------------------------------------------------------------------
1580Sstevel@tonic-gate  * mp->dmai_minxfer		    - dev
1590Sstevel@tonic-gate  * mp->dmai_burstsizes		    - dev
1600Sstevel@tonic-gate  * mp->dmai_flags		    - no limit? peer-to-peer only?
1610Sstevel@tonic-gate  *
1620Sstevel@tonic-gate  * ddi_dma_attr member modified       input
1630Sstevel@tonic-gate  * ------------------------------------------------------------------------
1640Sstevel@tonic-gate  * mp->dmai_attr.dma_attr_addr_lo   - dev lo, sys lo
1650Sstevel@tonic-gate  * mp->dmai_attr.dma_attr_addr_hi   - dev hi, sys hi
1660Sstevel@tonic-gate  * mp->dmai_attr.dma_attr_count_max - dev count max, dev/sys lo/hi delta
1670Sstevel@tonic-gate  * mp->dmai_attr.dma_attr_seg       - 0         (no nocross   restriction)
1680Sstevel@tonic-gate  * mp->dmai_attr.dma_attr_align     - 1         (no alignment restriction)
1690Sstevel@tonic-gate  *
1700Sstevel@tonic-gate  * The dlim_dmaspeed member of dmareq->dmar_limits is ignored.
1710Sstevel@tonic-gate  */
1720Sstevel@tonic-gate ddi_dma_impl_t *
px_dma_lmts2hdl(dev_info_t * dip,dev_info_t * rdip,px_mmu_t * mmu_p,ddi_dma_req_t * dmareq)1730Sstevel@tonic-gate px_dma_lmts2hdl(dev_info_t *dip, dev_info_t *rdip, px_mmu_t *mmu_p,
1740Sstevel@tonic-gate 	ddi_dma_req_t *dmareq)
1750Sstevel@tonic-gate {
1760Sstevel@tonic-gate 	ddi_dma_impl_t *mp;
1770Sstevel@tonic-gate 	ddi_dma_attr_t *attr_p;
1780Sstevel@tonic-gate 	uint64_t syslo		= mmu_p->mmu_dvma_base;
1790Sstevel@tonic-gate 	uint64_t syshi		= mmu_p->mmu_dvma_end;
1800Sstevel@tonic-gate 	uint64_t fasthi		= mmu_p->mmu_dvma_fast_end;
1810Sstevel@tonic-gate 	ddi_dma_lim_t *lim_p	= dmareq->dmar_limits;
1820Sstevel@tonic-gate 	uint32_t count_max	= lim_p->dlim_cntr_max;
1830Sstevel@tonic-gate 	uint64_t lo		= lim_p->dlim_addr_lo;
1840Sstevel@tonic-gate 	uint64_t hi		= lim_p->dlim_addr_hi;
1850Sstevel@tonic-gate 	if (hi <= lo) {
1860Sstevel@tonic-gate 		DBG(DBG_DMA_MAP, dip, "Bad limits\n");
1870Sstevel@tonic-gate 		return ((ddi_dma_impl_t *)DDI_DMA_NOMAPPING);
1880Sstevel@tonic-gate 	}
1890Sstevel@tonic-gate 	if (!count_max)
1900Sstevel@tonic-gate 		count_max--;
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	if (!(mp = px_dma_allocmp(dip, rdip, dmareq->dmar_fp,
1938413SDaniel.Ice@Sun.COM 	    dmareq->dmar_arg)))
1940Sstevel@tonic-gate 		return (NULL);
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	/* store original dev input at the 2nd ddi_dma_attr */
197909Segillett 	attr_p = PX_DEV_ATTR(mp);
1980Sstevel@tonic-gate 	SET_DMAATTR(attr_p, lo, hi, -1, count_max);
1990Sstevel@tonic-gate 	SET_DMAALIGN(attr_p, 1);
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	lo = MAX(lo, syslo);
2020Sstevel@tonic-gate 	hi = MIN(hi, syshi);
2030Sstevel@tonic-gate 	if (hi <= lo)
204909Segillett 		mp->dmai_flags |= PX_DMAI_FLAGS_PEER_ONLY;
2050Sstevel@tonic-gate 	count_max = MIN(count_max, hi - lo);
2060Sstevel@tonic-gate 
207909Segillett 	if (PX_DEV_NOSYSLIMIT(lo, hi, syslo, fasthi, 1))
208909Segillett 		mp->dmai_flags |= PX_DMAI_FLAGS_NOFASTLIMIT |
2098413SDaniel.Ice@Sun.COM 		    PX_DMAI_FLAGS_NOSYSLIMIT;
2100Sstevel@tonic-gate 	else {
211909Segillett 		if (PX_DEV_NOFASTLIMIT(lo, hi, syslo, syshi, 1))
212909Segillett 			mp->dmai_flags |= PX_DMAI_FLAGS_NOFASTLIMIT;
2130Sstevel@tonic-gate 	}
2140Sstevel@tonic-gate 	if (PX_DMA_NOCTX(rdip))
215909Segillett 		mp->dmai_flags |= PX_DMAI_FLAGS_NOCTX;
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	/* store augumented dev input to mp->dmai_attr */
2180Sstevel@tonic-gate 	mp->dmai_burstsizes	= lim_p->dlim_burstsizes;
2190Sstevel@tonic-gate 	attr_p = &mp->dmai_attr;
2200Sstevel@tonic-gate 	SET_DMAATTR(attr_p, lo, hi, -1, count_max);
2210Sstevel@tonic-gate 	SET_DMAALIGN(attr_p, 1);
2220Sstevel@tonic-gate 	return (mp);
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate /*
2260Sstevel@tonic-gate  * Called from px_attach to check for bypass dma support and set
2270Sstevel@tonic-gate  * flags accordingly.
2280Sstevel@tonic-gate  */
2290Sstevel@tonic-gate int
px_dma_attach(px_t * px_p)2300Sstevel@tonic-gate px_dma_attach(px_t *px_p)
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate 	uint64_t baddr;
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	if (px_lib_iommu_getbypass(px_p->px_dip, 0ull,
2358413SDaniel.Ice@Sun.COM 	    PCI_MAP_ATTR_WRITE|PCI_MAP_ATTR_READ,
2368413SDaniel.Ice@Sun.COM 	    &baddr) != DDI_ENOTSUP)
2370Sstevel@tonic-gate 		/* ignore all other errors */
2381531Skini 		px_p->px_dev_caps |= PX_BYPASS_DMA_ALLOWED;
2390Sstevel@tonic-gate 
2403107Saa72041 	px_p->px_dma_sync_opt = ddi_prop_get_int(DDI_DEV_T_ANY,
2413107Saa72041 	    px_p->px_dip, DDI_PROP_DONTPASS, "dma-sync-options", 0);
2423107Saa72041 
2433107Saa72041 	if (px_p->px_dma_sync_opt != 0)
2443107Saa72041 		px_p->px_dev_caps |= PX_DMA_SYNC_REQUIRED;
2453107Saa72041 
2460Sstevel@tonic-gate 	return (DDI_SUCCESS);
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate /*
2500Sstevel@tonic-gate  * px_dma_attr2hdl
2510Sstevel@tonic-gate  *
2520Sstevel@tonic-gate  * This routine is called from the alloc handle entry point to sanity check the
2530Sstevel@tonic-gate  * dma attribute structure.
2540Sstevel@tonic-gate  *
2550Sstevel@tonic-gate  * use by: px_dma_allochdl()
2560Sstevel@tonic-gate  *
2570Sstevel@tonic-gate  * return value:
2580Sstevel@tonic-gate  *
2590Sstevel@tonic-gate  *	DDI_SUCCESS		- on success
2600Sstevel@tonic-gate  *	DDI_DMA_BADATTR		- attribute has invalid version number
2610Sstevel@tonic-gate  *				  or address limits exclude dvma space
2620Sstevel@tonic-gate  */
2630Sstevel@tonic-gate int
px_dma_attr2hdl(px_t * px_p,ddi_dma_impl_t * mp)2640Sstevel@tonic-gate px_dma_attr2hdl(px_t *px_p, ddi_dma_impl_t *mp)
2650Sstevel@tonic-gate {
2660Sstevel@tonic-gate 	px_mmu_t *mmu_p = px_p->px_mmu_p;
2670Sstevel@tonic-gate 	uint64_t syslo, syshi;
2680Sstevel@tonic-gate 	int	ret;
269909Segillett 	ddi_dma_attr_t *attrp		= PX_DEV_ATTR(mp);
2700Sstevel@tonic-gate 	uint64_t hi			= attrp->dma_attr_addr_hi;
2710Sstevel@tonic-gate 	uint64_t lo			= attrp->dma_attr_addr_lo;
2720Sstevel@tonic-gate 	uint64_t align			= attrp->dma_attr_align;
2730Sstevel@tonic-gate 	uint64_t nocross		= attrp->dma_attr_seg;
2740Sstevel@tonic-gate 	uint64_t count_max		= attrp->dma_attr_count_max;
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	DBG(DBG_DMA_ALLOCH, px_p->px_dip, "attrp=%p cntr_max=%x.%08x\n",
2778413SDaniel.Ice@Sun.COM 	    attrp, HI32(count_max), LO32(count_max));
2780Sstevel@tonic-gate 	DBG(DBG_DMA_ALLOCH, px_p->px_dip, "hi=%x.%08x lo=%x.%08x\n",
2798413SDaniel.Ice@Sun.COM 	    HI32(hi), LO32(hi), HI32(lo), LO32(lo));
2800Sstevel@tonic-gate 	DBG(DBG_DMA_ALLOCH, px_p->px_dip, "seg=%x.%08x align=%x.%08x\n",
2818413SDaniel.Ice@Sun.COM 	    HI32(nocross), LO32(nocross), HI32(align), LO32(align));
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	if (!nocross)
2840Sstevel@tonic-gate 		nocross--;
2850Sstevel@tonic-gate 	if (attrp->dma_attr_flags & DDI_DMA_FORCE_PHYSICAL) { /* BYPASS */
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 		DBG(DBG_DMA_ALLOCH, px_p->px_dip, "bypass mode\n");
2880Sstevel@tonic-gate 		/*
2890Sstevel@tonic-gate 		 * If Bypass DMA is not supported, return error so that
2900Sstevel@tonic-gate 		 * target driver can fall back to dvma mode of operation
2910Sstevel@tonic-gate 		 */
2921531Skini 		if (!(px_p->px_dev_caps & PX_BYPASS_DMA_ALLOWED))
2930Sstevel@tonic-gate 			return (DDI_DMA_BADATTR);
294909Segillett 		mp->dmai_flags |= PX_DMAI_FLAGS_BYPASSREQ;
2950Sstevel@tonic-gate 		if (nocross != UINT64_MAX)
2960Sstevel@tonic-gate 			return (DDI_DMA_BADATTR);
2970Sstevel@tonic-gate 		if (align && (align > MMU_PAGE_SIZE))
2980Sstevel@tonic-gate 			return (DDI_DMA_BADATTR);
2990Sstevel@tonic-gate 		align = 1; /* align on 1 page boundary */
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 		/* do a range check and get the limits */
3021772Sjl139090 		ret = px_lib_dma_bypass_rngchk(px_p->px_dip, attrp,
3038413SDaniel.Ice@Sun.COM 		    &syslo, &syshi);
3040Sstevel@tonic-gate 		if (ret != DDI_SUCCESS)
3050Sstevel@tonic-gate 			return (ret);
3060Sstevel@tonic-gate 	} else { /* MMU_XLATE or PEER_TO_PEER */
3070Sstevel@tonic-gate 		align = MAX(align, MMU_PAGE_SIZE) - 1;
3080Sstevel@tonic-gate 		if ((align & nocross) != align) {
3090Sstevel@tonic-gate 			dev_info_t *rdip = mp->dmai_rdip;
3100Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d dma_attr_seg not aligned",
3118413SDaniel.Ice@Sun.COM 			    NAMEINST(rdip));
3120Sstevel@tonic-gate 			return (DDI_DMA_BADATTR);
3130Sstevel@tonic-gate 		}
3140Sstevel@tonic-gate 		align = MMU_BTOP(align + 1);
3150Sstevel@tonic-gate 		syslo = mmu_p->mmu_dvma_base;
3160Sstevel@tonic-gate 		syshi = mmu_p->mmu_dvma_end;
3170Sstevel@tonic-gate 	}
3180Sstevel@tonic-gate 	if (hi <= lo) {
3190Sstevel@tonic-gate 		dev_info_t *rdip = mp->dmai_rdip;
3200Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d limits out of range", NAMEINST(rdip));
3210Sstevel@tonic-gate 		return (DDI_DMA_BADATTR);
3220Sstevel@tonic-gate 	}
3230Sstevel@tonic-gate 	lo = MAX(lo, syslo);
3240Sstevel@tonic-gate 	hi = MIN(hi, syshi);
3250Sstevel@tonic-gate 	if (!count_max)
3260Sstevel@tonic-gate 		count_max--;
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	DBG(DBG_DMA_ALLOCH, px_p->px_dip, "hi=%x.%08x, lo=%x.%08x\n",
3298413SDaniel.Ice@Sun.COM 	    HI32(hi), LO32(hi), HI32(lo), LO32(lo));
3301923Scjj 	if (hi <= lo) {
3311923Scjj 		/*
3321923Scjj 		 * If this is an IOMMU bypass access, the caller can't use
3331923Scjj 		 * the required addresses, so fail it.  Otherwise, it's
3341923Scjj 		 * peer-to-peer; ensure that the caller has no alignment or
3351923Scjj 		 * segment size restrictions.
3361923Scjj 		 */
3371923Scjj 		if ((mp->dmai_flags & PX_DMAI_FLAGS_BYPASSREQ) ||
3381923Scjj 		    (nocross < UINT32_MAX) || (align > 1))
3390Sstevel@tonic-gate 			return (DDI_DMA_BADATTR);
3401923Scjj 
341909Segillett 		mp->dmai_flags |= PX_DMAI_FLAGS_PEER_ONLY;
3420Sstevel@tonic-gate 	} else /* set practical counter_max value */
3430Sstevel@tonic-gate 		count_max = MIN(count_max, hi - lo);
3440Sstevel@tonic-gate 
345909Segillett 	if (PX_DEV_NOSYSLIMIT(lo, hi, syslo, syshi, align))
346909Segillett 		mp->dmai_flags |= PX_DMAI_FLAGS_NOSYSLIMIT |
3478413SDaniel.Ice@Sun.COM 		    PX_DMAI_FLAGS_NOFASTLIMIT;
3480Sstevel@tonic-gate 	else {
3490Sstevel@tonic-gate 		syshi = mmu_p->mmu_dvma_fast_end;
350909Segillett 		if (PX_DEV_NOFASTLIMIT(lo, hi, syslo, syshi, align))
351909Segillett 			mp->dmai_flags |= PX_DMAI_FLAGS_NOFASTLIMIT;
3520Sstevel@tonic-gate 	}
3530Sstevel@tonic-gate 	if (PX_DMA_NOCTX(mp->dmai_rdip))
354909Segillett 		mp->dmai_flags |= PX_DMAI_FLAGS_NOCTX;
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	mp->dmai_burstsizes	= attrp->dma_attr_burstsizes;
3570Sstevel@tonic-gate 	attrp = &mp->dmai_attr;
3580Sstevel@tonic-gate 	SET_DMAATTR(attrp, lo, hi, nocross, count_max);
3590Sstevel@tonic-gate 	return (DDI_SUCCESS);
3600Sstevel@tonic-gate }
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate #define	TGT_PFN_INBETWEEN(pfn, bgn, end) ((pfn >= bgn) && (pfn <= end))
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate /*
3650Sstevel@tonic-gate  * px_dma_type - determine which of the three types DMA (peer-to-peer,
3660Sstevel@tonic-gate  *		mmu bypass, or mmu translate) we are asked to do.
3670Sstevel@tonic-gate  *		Also checks pfn0 and rejects any non-peer-to-peer
3680Sstevel@tonic-gate  *		requests for peer-only devices.
3690Sstevel@tonic-gate  *
3700Sstevel@tonic-gate  *	return values:
3710Sstevel@tonic-gate  *		DDI_DMA_NOMAPPING - can't get valid pfn0, or bad dma type
3720Sstevel@tonic-gate  *		DDI_SUCCESS
3730Sstevel@tonic-gate  *
3740Sstevel@tonic-gate  *	dma handle members affected (set on exit):
3750Sstevel@tonic-gate  *	mp->dmai_object		- dmareq->dmar_object
3760Sstevel@tonic-gate  *	mp->dmai_rflags		- consistent?, nosync?, dmareq->dmar_flags
3770Sstevel@tonic-gate  *	mp->dmai_flags   	- DMA type
3780Sstevel@tonic-gate  *	mp->dmai_pfn0   	- 1st page pfn (if va/size pair and not shadow)
3790Sstevel@tonic-gate  *	mp->dmai_roffset 	- initialized to starting MMU page offset
3800Sstevel@tonic-gate  *	mp->dmai_ndvmapages	- # of total MMU pages of entire object
3810Sstevel@tonic-gate  */
3820Sstevel@tonic-gate int
px_dma_type(px_t * px_p,ddi_dma_req_t * dmareq,ddi_dma_impl_t * mp)3830Sstevel@tonic-gate px_dma_type(px_t *px_p, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp)
3840Sstevel@tonic-gate {
3850Sstevel@tonic-gate 	dev_info_t *dip = px_p->px_dip;
3860Sstevel@tonic-gate 	ddi_dma_obj_t *dobj_p = &dmareq->dmar_object;
3870Sstevel@tonic-gate 	px_pec_t *pec_p = px_p->px_pec_p;
3880Sstevel@tonic-gate 	uint32_t offset;
3890Sstevel@tonic-gate 	pfn_t pfn0;
3902549Sgovinda 	uint_t redzone;
3910Sstevel@tonic-gate 
3923107Saa72041 	mp->dmai_rflags = dmareq->dmar_flags & DMP_DDIFLAGS;
3933107Saa72041 
3943107Saa72041 	if (!(px_p->px_dev_caps & PX_DMA_SYNC_REQUIRED))
3953107Saa72041 		mp->dmai_rflags |= DMP_NOSYNC;
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	switch (dobj_p->dmao_type) {
3980Sstevel@tonic-gate 	case DMA_OTYP_BUFVADDR:
3990Sstevel@tonic-gate 	case DMA_OTYP_VADDR: {
4000Sstevel@tonic-gate 		page_t **pplist = dobj_p->dmao_obj.virt_obj.v_priv;
4010Sstevel@tonic-gate 		caddr_t vaddr = dobj_p->dmao_obj.virt_obj.v_addr;
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 		DBG(DBG_DMA_MAP, dip, "vaddr=%p pplist=%p\n", vaddr, pplist);
4040Sstevel@tonic-gate 		offset = (ulong_t)vaddr & MMU_PAGE_OFFSET;
4050Sstevel@tonic-gate 		if (pplist) {				/* shadow list */
406909Segillett 			mp->dmai_flags |= PX_DMAI_FLAGS_PGPFN;
4070Sstevel@tonic-gate 			pfn0 = page_pptonum(*pplist);
4080Sstevel@tonic-gate 		} else {
4090Sstevel@tonic-gate 			struct as *as_p = dobj_p->dmao_obj.virt_obj.v_as;
4100Sstevel@tonic-gate 			struct hat *hat_p = as_p ? as_p->a_hat : kas.a_hat;
4110Sstevel@tonic-gate 			pfn0 = hat_getpfnum(hat_p, vaddr);
4120Sstevel@tonic-gate 		}
4130Sstevel@tonic-gate 		}
4140Sstevel@tonic-gate 		break;
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	case DMA_OTYP_PAGES:
4170Sstevel@tonic-gate 		offset = dobj_p->dmao_obj.pp_obj.pp_offset;
418909Segillett 		mp->dmai_flags |= PX_DMAI_FLAGS_PGPFN;
4190Sstevel@tonic-gate 		pfn0 = page_pptonum(dobj_p->dmao_obj.pp_obj.pp_pp);
4200Sstevel@tonic-gate 		break;
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	case DMA_OTYP_PADDR:
4230Sstevel@tonic-gate 	default:
4240Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d requested unsupported dma type %x",
4258413SDaniel.Ice@Sun.COM 		    NAMEINST(mp->dmai_rdip), dobj_p->dmao_type);
4260Sstevel@tonic-gate 		return (DDI_DMA_NOMAPPING);
4270Sstevel@tonic-gate 	}
4280Sstevel@tonic-gate 	if (pfn0 == PFN_INVALID) {
4290Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: invalid pfn0 for DMA object %p",
4308413SDaniel.Ice@Sun.COM 		    NAMEINST(dip), dobj_p);
4310Sstevel@tonic-gate 		return (DDI_DMA_NOMAPPING);
4320Sstevel@tonic-gate 	}
4330Sstevel@tonic-gate 	if (TGT_PFN_INBETWEEN(pfn0, pec_p->pec_base32_pfn,
4348413SDaniel.Ice@Sun.COM 	    pec_p->pec_last32_pfn)) {
435909Segillett 		mp->dmai_flags |= PX_DMAI_FLAGS_PTP|PX_DMAI_FLAGS_PTP32;
4360Sstevel@tonic-gate 		goto done;	/* leave bypass and dvma flag as 0 */
4370Sstevel@tonic-gate 	} else if (TGT_PFN_INBETWEEN(pfn0, pec_p->pec_base64_pfn,
4388413SDaniel.Ice@Sun.COM 	    pec_p->pec_last64_pfn)) {
439909Segillett 		mp->dmai_flags |= PX_DMAI_FLAGS_PTP|PX_DMAI_FLAGS_PTP64;
4400Sstevel@tonic-gate 		goto done;	/* leave bypass and dvma flag as 0 */
4410Sstevel@tonic-gate 	}
4420Sstevel@tonic-gate 	if (PX_DMA_ISPEERONLY(mp)) {
4430Sstevel@tonic-gate 		dev_info_t *rdip = mp->dmai_rdip;
4440Sstevel@tonic-gate 		cmn_err(CE_WARN, "Bad peer-to-peer req %s%d", NAMEINST(rdip));
4450Sstevel@tonic-gate 		return (DDI_DMA_NOMAPPING);
4460Sstevel@tonic-gate 	}
4472549Sgovinda 
4482549Sgovinda 	redzone = (mp->dmai_rflags & DDI_DMA_REDZONE) ||
4492549Sgovinda 	    (mp->dmai_flags & PX_DMAI_FLAGS_MAP_BUFZONE) ?
4502549Sgovinda 	    PX_DMAI_FLAGS_REDZONE : 0;
4512549Sgovinda 
452909Segillett 	mp->dmai_flags |= (mp->dmai_flags & PX_DMAI_FLAGS_BYPASSREQ) ?
4532549Sgovinda 	    PX_DMAI_FLAGS_BYPASS : (PX_DMAI_FLAGS_DVMA | redzone);
4540Sstevel@tonic-gate done:
4550Sstevel@tonic-gate 	mp->dmai_object	 = *dobj_p;			/* whole object    */
4560Sstevel@tonic-gate 	mp->dmai_pfn0	 = (void *)pfn0;		/* cache pfn0	   */
4570Sstevel@tonic-gate 	mp->dmai_roffset = offset;			/* win0 pg0 offset */
4580Sstevel@tonic-gate 	mp->dmai_ndvmapages = MMU_BTOPR(offset + mp->dmai_object.dmao_size);
4590Sstevel@tonic-gate 	return (DDI_SUCCESS);
4600Sstevel@tonic-gate }
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate /*
4630Sstevel@tonic-gate  * px_dma_pgpfn - set up pfnlst array according to pages
4640Sstevel@tonic-gate  *	VA/size pair: <shadow IO, bypass, peer-to-peer>, or OTYP_PAGES
4650Sstevel@tonic-gate  */
4660Sstevel@tonic-gate /*ARGSUSED*/
4670Sstevel@tonic-gate static int
px_dma_pgpfn(px_t * px_p,ddi_dma_impl_t * mp,uint_t npages)4680Sstevel@tonic-gate px_dma_pgpfn(px_t *px_p, ddi_dma_impl_t *mp, uint_t npages)
4690Sstevel@tonic-gate {
4700Sstevel@tonic-gate 	int i;
4710Sstevel@tonic-gate 	dev_info_t *dip = px_p->px_dip;
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	switch (mp->dmai_object.dmao_type) {
4740Sstevel@tonic-gate 	case DMA_OTYP_BUFVADDR:
4750Sstevel@tonic-gate 	case DMA_OTYP_VADDR: {
4760Sstevel@tonic-gate 		page_t **pplist = mp->dmai_object.dmao_obj.virt_obj.v_priv;
4770Sstevel@tonic-gate 		DBG(DBG_DMA_MAP, dip, "shadow pplist=%p, %x pages, pfns=",
4788413SDaniel.Ice@Sun.COM 		    pplist, npages);
4790Sstevel@tonic-gate 		for (i = 1; i < npages; i++) {
4800Sstevel@tonic-gate 			px_iopfn_t pfn = page_pptonum(pplist[i]);
4810Sstevel@tonic-gate 			PX_SET_MP_PFN1(mp, i, pfn);
4820Sstevel@tonic-gate 			DBG(DBG_DMA_MAP|DBG_CONT, dip, "%x ", pfn);
4830Sstevel@tonic-gate 		}
4840Sstevel@tonic-gate 		DBG(DBG_DMA_MAP|DBG_CONT, dip, "\n");
4850Sstevel@tonic-gate 		}
4860Sstevel@tonic-gate 		break;
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	case DMA_OTYP_PAGES: {
4890Sstevel@tonic-gate 		page_t *pp = mp->dmai_object.dmao_obj.pp_obj.pp_pp->p_next;
4900Sstevel@tonic-gate 		DBG(DBG_DMA_MAP, dip, "pp=%p pfns=", pp);
4910Sstevel@tonic-gate 		for (i = 1; i < npages; i++, pp = pp->p_next) {
4920Sstevel@tonic-gate 			px_iopfn_t pfn = page_pptonum(pp);
4930Sstevel@tonic-gate 			PX_SET_MP_PFN1(mp, i, pfn);
4940Sstevel@tonic-gate 			DBG(DBG_DMA_MAP|DBG_CONT, dip, "%x ", pfn);
4950Sstevel@tonic-gate 		}
4960Sstevel@tonic-gate 		DBG(DBG_DMA_MAP|DBG_CONT, dip, "\n");
4970Sstevel@tonic-gate 		}
4980Sstevel@tonic-gate 		break;
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	default:	/* check is already done by px_dma_type */
5010Sstevel@tonic-gate 		ASSERT(0);
5020Sstevel@tonic-gate 		break;
5030Sstevel@tonic-gate 	}
5040Sstevel@tonic-gate 	return (DDI_SUCCESS);
5050Sstevel@tonic-gate }
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate /*
5080Sstevel@tonic-gate  * px_dma_vapfn - set up pfnlst array according to VA
5090Sstevel@tonic-gate  *	VA/size pair: <normal, bypass, peer-to-peer>
5100Sstevel@tonic-gate  *	pfn0 is skipped as it is already done.
5110Sstevel@tonic-gate  *	In this case, the cached pfn0 is used to fill pfnlst[0]
5120Sstevel@tonic-gate  */
5130Sstevel@tonic-gate static int
px_dma_vapfn(px_t * px_p,ddi_dma_impl_t * mp,uint_t npages)5140Sstevel@tonic-gate px_dma_vapfn(px_t *px_p, ddi_dma_impl_t *mp, uint_t npages)
5150Sstevel@tonic-gate {
5160Sstevel@tonic-gate 	dev_info_t *dip = px_p->px_dip;
5170Sstevel@tonic-gate 	int i;
5180Sstevel@tonic-gate 	caddr_t vaddr = (caddr_t)mp->dmai_object.dmao_obj.virt_obj.v_as;
5190Sstevel@tonic-gate 	struct hat *hat_p = vaddr ? ((struct as *)vaddr)->a_hat : kas.a_hat;
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	vaddr = mp->dmai_object.dmao_obj.virt_obj.v_addr + MMU_PAGE_SIZE;
5220Sstevel@tonic-gate 	for (i = 1; i < npages; i++, vaddr += MMU_PAGE_SIZE) {
5230Sstevel@tonic-gate 		px_iopfn_t pfn = hat_getpfnum(hat_p, vaddr);
5240Sstevel@tonic-gate 		if (pfn == PFN_INVALID)
5250Sstevel@tonic-gate 			goto err_badpfn;
5260Sstevel@tonic-gate 		PX_SET_MP_PFN1(mp, i, pfn);
5270Sstevel@tonic-gate 		DBG(DBG_DMA_BINDH, dip, "px_dma_vapfn: mp=%p pfnlst[%x]=%x\n",
5288413SDaniel.Ice@Sun.COM 		    mp, i, pfn);
5290Sstevel@tonic-gate 	}
5300Sstevel@tonic-gate 	return (DDI_SUCCESS);
5310Sstevel@tonic-gate err_badpfn:
5320Sstevel@tonic-gate 	cmn_err(CE_WARN, "%s%d: bad page frame vaddr=%p", NAMEINST(dip), vaddr);
5330Sstevel@tonic-gate 	return (DDI_DMA_NOMAPPING);
5340Sstevel@tonic-gate }
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate /*
5370Sstevel@tonic-gate  * px_dma_pfn - Fills pfn list for all pages being DMA-ed.
5380Sstevel@tonic-gate  *
5390Sstevel@tonic-gate  * dependencies:
5400Sstevel@tonic-gate  *	mp->dmai_ndvmapages	- set to total # of dma pages
5410Sstevel@tonic-gate  *
5420Sstevel@tonic-gate  * return value:
5430Sstevel@tonic-gate  *	DDI_SUCCESS
5440Sstevel@tonic-gate  *	DDI_DMA_NOMAPPING
5450Sstevel@tonic-gate  */
5460Sstevel@tonic-gate int
px_dma_pfn(px_t * px_p,ddi_dma_req_t * dmareq,ddi_dma_impl_t * mp)5470Sstevel@tonic-gate px_dma_pfn(px_t *px_p, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp)
5480Sstevel@tonic-gate {
5490Sstevel@tonic-gate 	uint32_t npages = mp->dmai_ndvmapages;
5500Sstevel@tonic-gate 	int (*waitfp)(caddr_t) = dmareq->dmar_fp;
5510Sstevel@tonic-gate 	int i, ret, peer = PX_DMA_ISPTP(mp);
5520Sstevel@tonic-gate 	int peer32 = PX_DMA_ISPTP32(mp);
5530Sstevel@tonic-gate 	dev_info_t *dip = px_p->px_dip;
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	px_pec_t *pec_p = px_p->px_pec_p;
5560Sstevel@tonic-gate 	px_iopfn_t pfn_base = peer32 ? pec_p->pec_base32_pfn :
5578413SDaniel.Ice@Sun.COM 	    pec_p->pec_base64_pfn;
5580Sstevel@tonic-gate 	px_iopfn_t pfn_last = peer32 ? pec_p->pec_last32_pfn :
5598413SDaniel.Ice@Sun.COM 	    pec_p->pec_last64_pfn;
5600Sstevel@tonic-gate 	px_iopfn_t pfn_adj = peer ? pfn_base : 0;
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	DBG(DBG_DMA_BINDH, dip, "px_dma_pfn: mp=%p pfn0=%x\n",
5638413SDaniel.Ice@Sun.COM 	    mp, PX_MP_PFN0(mp) - pfn_adj);
5640Sstevel@tonic-gate 	/* 1 page: no array alloc/fill, no mixed mode check */
5650Sstevel@tonic-gate 	if (npages == 1) {
566909Segillett 		PX_SET_MP_PFN(mp, 0, PX_MP_PFN0(mp) - pfn_adj);
5670Sstevel@tonic-gate 		return (DDI_SUCCESS);
5680Sstevel@tonic-gate 	}
5690Sstevel@tonic-gate 	/* allocate pfn array */
5700Sstevel@tonic-gate 	if (!(mp->dmai_pfnlst = kmem_alloc(npages * sizeof (px_iopfn_t),
5718413SDaniel.Ice@Sun.COM 	    waitfp == DDI_DMA_SLEEP ? KM_SLEEP : KM_NOSLEEP))) {
5720Sstevel@tonic-gate 		if (waitfp != DDI_DMA_DONTWAIT)
5730Sstevel@tonic-gate 			ddi_set_callback(waitfp, dmareq->dmar_arg,
5748413SDaniel.Ice@Sun.COM 			    &px_kmem_clid);
5750Sstevel@tonic-gate 		return (DDI_DMA_NORESOURCES);
5760Sstevel@tonic-gate 	}
5770Sstevel@tonic-gate 	/* fill pfn array */
578909Segillett 	PX_SET_MP_PFN(mp, 0, PX_MP_PFN0(mp) - pfn_adj);	/* pfnlst[0] */
5790Sstevel@tonic-gate 	if ((ret = PX_DMA_ISPGPFN(mp) ? px_dma_pgpfn(px_p, mp, npages) :
5808413SDaniel.Ice@Sun.COM 	    px_dma_vapfn(px_p, mp, npages)) != DDI_SUCCESS)
5810Sstevel@tonic-gate 		goto err;
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	/* skip pfn0, check mixed mode and adjust peer to peer pfn */
5840Sstevel@tonic-gate 	for (i = 1; i < npages; i++) {
5850Sstevel@tonic-gate 		px_iopfn_t pfn = PX_GET_MP_PFN1(mp, i);
5860Sstevel@tonic-gate 		if (peer ^ TGT_PFN_INBETWEEN(pfn, pfn_base, pfn_last)) {
587671Skrishnae 			cmn_err(CE_WARN, "%s%d mixed mode DMA %lx %lx",
5888413SDaniel.Ice@Sun.COM 			    NAMEINST(mp->dmai_rdip), PX_MP_PFN0(mp), pfn);
5890Sstevel@tonic-gate 			ret = DDI_DMA_NOMAPPING;	/* mixed mode */
5900Sstevel@tonic-gate 			goto err;
5910Sstevel@tonic-gate 		}
5920Sstevel@tonic-gate 		DBG(DBG_DMA_MAP, dip,
5938413SDaniel.Ice@Sun.COM 		    "px_dma_pfn: pfnlst[%x]=%x-%x\n", i, pfn, pfn_adj);
5940Sstevel@tonic-gate 		if (pfn_adj)
5950Sstevel@tonic-gate 			PX_SET_MP_PFN1(mp, i, pfn - pfn_adj);
5960Sstevel@tonic-gate 	}
5970Sstevel@tonic-gate 	return (DDI_SUCCESS);
5980Sstevel@tonic-gate err:
5990Sstevel@tonic-gate 	px_dma_freepfn(mp);
6000Sstevel@tonic-gate 	return (ret);
6010Sstevel@tonic-gate }
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate /*
6040Sstevel@tonic-gate  * px_dvma_win() - trim requested DVMA size down to window size
6050Sstevel@tonic-gate  *	The 1st window starts from offset and ends at page-aligned boundary.
6060Sstevel@tonic-gate  *	From the 2nd window on, each window starts and ends at page-aligned
6070Sstevel@tonic-gate  *	boundary except the last window ends at wherever requested.
6080Sstevel@tonic-gate  *
6090Sstevel@tonic-gate  *	accesses the following mp-> members:
6100Sstevel@tonic-gate  *	mp->dmai_attr.dma_attr_count_max
6110Sstevel@tonic-gate  *	mp->dmai_attr.dma_attr_seg
6120Sstevel@tonic-gate  *	mp->dmai_roffset   - start offset of 1st window
6130Sstevel@tonic-gate  *	mp->dmai_rflags (redzone)
6140Sstevel@tonic-gate  *	mp->dmai_ndvmapages (for 1 page fast path)
6150Sstevel@tonic-gate  *
6160Sstevel@tonic-gate  *	sets the following mp-> members:
6170Sstevel@tonic-gate  *	mp->dmai_size	   - xfer size, != winsize if 1st/last win  (not fixed)
6180Sstevel@tonic-gate  *	mp->dmai_winsize   - window size (no redzone), n * page size    (fixed)
6190Sstevel@tonic-gate  *	mp->dmai_nwin	   - # of DMA windows of entire object		(fixed)
6200Sstevel@tonic-gate  *	mp->dmai_rflags	   - remove partial flag if nwin == 1		(fixed)
6210Sstevel@tonic-gate  *	mp->dmai_winlst	   - NULL, window objects not used for DVMA	(fixed)
6220Sstevel@tonic-gate  *
6230Sstevel@tonic-gate  *	fixed - not changed across different DMA windows
6240Sstevel@tonic-gate  */
6250Sstevel@tonic-gate /*ARGSUSED*/
6260Sstevel@tonic-gate int
px_dvma_win(px_t * px_p,ddi_dma_req_t * dmareq,ddi_dma_impl_t * mp)6270Sstevel@tonic-gate px_dvma_win(px_t *px_p, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp)
6280Sstevel@tonic-gate {
6291501Sgovinda 	uint32_t redzone_sz	= PX_HAS_REDZONE(mp) ? MMU_PAGE_SIZE : 0;
6300Sstevel@tonic-gate 	size_t obj_sz		= mp->dmai_object.dmao_size;
6310Sstevel@tonic-gate 	size_t xfer_sz;
6320Sstevel@tonic-gate 	ulong_t pg_off;
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	if ((mp->dmai_ndvmapages == 1) && !redzone_sz) {
6350Sstevel@tonic-gate 		mp->dmai_rflags &= ~DDI_DMA_PARTIAL;
6360Sstevel@tonic-gate 		mp->dmai_size = obj_sz;
6370Sstevel@tonic-gate 		mp->dmai_winsize = MMU_PAGE_SIZE;
6380Sstevel@tonic-gate 		mp->dmai_nwin = 1;
6390Sstevel@tonic-gate 		goto done;
6400Sstevel@tonic-gate 	}
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	pg_off	= mp->dmai_roffset;
6430Sstevel@tonic-gate 	xfer_sz	= obj_sz + redzone_sz;
6440Sstevel@tonic-gate 
6458413SDaniel.Ice@Sun.COM 	/* include redzone in nocross check */	{
6460Sstevel@tonic-gate 		uint64_t nocross = mp->dmai_attr.dma_attr_seg;
6470Sstevel@tonic-gate 		if (xfer_sz + pg_off - 1 > nocross)
6480Sstevel@tonic-gate 			xfer_sz = nocross - pg_off + 1;
6490Sstevel@tonic-gate 		if (redzone_sz && (xfer_sz <= redzone_sz)) {
6500Sstevel@tonic-gate 			DBG(DBG_DMA_MAP, px_p->px_dip,
6510Sstevel@tonic-gate 			    "nocross too small: "
6520Sstevel@tonic-gate 			    "%lx(%lx)+%lx+%lx < %llx\n",
6530Sstevel@tonic-gate 			    xfer_sz, obj_sz, pg_off, redzone_sz, nocross);
6540Sstevel@tonic-gate 			return (DDI_DMA_TOOBIG);
6550Sstevel@tonic-gate 		}
6560Sstevel@tonic-gate 	}
6570Sstevel@tonic-gate 	xfer_sz -= redzone_sz;		/* restore transfer size  */
6588413SDaniel.Ice@Sun.COM 	/* check counter max */	{
6590Sstevel@tonic-gate 		uint32_t count_max = mp->dmai_attr.dma_attr_count_max;
6600Sstevel@tonic-gate 		if (xfer_sz - 1 > count_max)
6610Sstevel@tonic-gate 			xfer_sz = count_max + 1;
6620Sstevel@tonic-gate 	}
6630Sstevel@tonic-gate 	if (xfer_sz >= obj_sz) {
6640Sstevel@tonic-gate 		mp->dmai_rflags &= ~DDI_DMA_PARTIAL;
6650Sstevel@tonic-gate 		mp->dmai_size = xfer_sz;
6660Sstevel@tonic-gate 		mp->dmai_winsize = P2ROUNDUP(xfer_sz + pg_off, MMU_PAGE_SIZE);
6670Sstevel@tonic-gate 		mp->dmai_nwin = 1;
6680Sstevel@tonic-gate 		goto done;
6690Sstevel@tonic-gate 	}
6700Sstevel@tonic-gate 	if (!(dmareq->dmar_flags & DDI_DMA_PARTIAL)) {
6710Sstevel@tonic-gate 		DBG(DBG_DMA_MAP, px_p->px_dip, "too big: %lx+%lx+%lx > %lx\n",
6728413SDaniel.Ice@Sun.COM 		    obj_sz, pg_off, redzone_sz, xfer_sz);
6730Sstevel@tonic-gate 		return (DDI_DMA_TOOBIG);
6740Sstevel@tonic-gate 	}
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 	xfer_sz = MMU_PTOB(MMU_BTOP(xfer_sz + pg_off)); /* page align */
6770Sstevel@tonic-gate 	mp->dmai_size = xfer_sz - pg_off;	/* 1st window xferrable size */
6780Sstevel@tonic-gate 	mp->dmai_winsize = xfer_sz;		/* redzone not in winsize */
6790Sstevel@tonic-gate 	mp->dmai_nwin = (obj_sz + pg_off + xfer_sz - 1) / xfer_sz;
6800Sstevel@tonic-gate done:
6810Sstevel@tonic-gate 	mp->dmai_winlst = NULL;
6820Sstevel@tonic-gate 	px_dump_dma_handle(DBG_DMA_MAP, px_p->px_dip, mp);
6830Sstevel@tonic-gate 	return (DDI_SUCCESS);
6840Sstevel@tonic-gate }
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate /*
6870Sstevel@tonic-gate  * fast track cache entry to mmu context, inserts 3 0 bits between
6880Sstevel@tonic-gate  * upper 6-bits and lower 3-bits of the 9-bit cache entry
6890Sstevel@tonic-gate  */
6900Sstevel@tonic-gate #define	MMU_FCE_TO_CTX(i)	(((i) << 3) | ((i) & 0x7) | 0x38)
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate /*
6930Sstevel@tonic-gate  * px_dvma_map_fast - attempts to map fast trackable DVMA
6940Sstevel@tonic-gate  */
6950Sstevel@tonic-gate /*ARGSUSED*/
6960Sstevel@tonic-gate int
px_dvma_map_fast(px_mmu_t * mmu_p,ddi_dma_impl_t * mp)6970Sstevel@tonic-gate px_dvma_map_fast(px_mmu_t *mmu_p, ddi_dma_impl_t *mp)
6980Sstevel@tonic-gate {
6990Sstevel@tonic-gate 	uint_t clustsz = px_dvma_page_cache_clustsz;
7000Sstevel@tonic-gate 	uint_t entries = px_dvma_page_cache_entries;
7011772Sjl139090 	io_attributes_t attr = PX_GET_TTE_ATTR(mp->dmai_rflags,
7021772Sjl139090 	    mp->dmai_attr.dma_attr_flags);
7030Sstevel@tonic-gate 	int i = mmu_p->mmu_dvma_addr_scan_start;
7040Sstevel@tonic-gate 	uint8_t *lock_addr = mmu_p->mmu_dvma_cache_locks + i;
7050Sstevel@tonic-gate 	px_dvma_addr_t dvma_pg;
7060Sstevel@tonic-gate 	size_t npages = MMU_BTOP(mp->dmai_winsize);
7071501Sgovinda 	dev_info_t *dip = mmu_p->mmu_px_p->px_dip;
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 	extern uint8_t ldstub(uint8_t *);
7100Sstevel@tonic-gate 	ASSERT(MMU_PTOB(npages) == mp->dmai_winsize);
7111501Sgovinda 	ASSERT(npages + PX_HAS_REDZONE(mp) <= clustsz);
7120Sstevel@tonic-gate 
7138413SDaniel.Ice@Sun.COM 	for (; i < entries && ldstub(lock_addr); i++, lock_addr++)
7148413SDaniel.Ice@Sun.COM 		;
7150Sstevel@tonic-gate 	if (i >= entries) {
7160Sstevel@tonic-gate 		lock_addr = mmu_p->mmu_dvma_cache_locks;
7170Sstevel@tonic-gate 		i = 0;
7188413SDaniel.Ice@Sun.COM 		for (; i < entries && ldstub(lock_addr); i++, lock_addr++)
7198413SDaniel.Ice@Sun.COM 			;
7200Sstevel@tonic-gate 		if (i >= entries) {
7210Sstevel@tonic-gate #ifdef	PX_DMA_PROF
7220Sstevel@tonic-gate 			px_dvmaft_exhaust++;
7230Sstevel@tonic-gate #endif	/* PX_DMA_PROF */
7240Sstevel@tonic-gate 			return (DDI_DMA_NORESOURCES);
7250Sstevel@tonic-gate 		}
7260Sstevel@tonic-gate 	}
7270Sstevel@tonic-gate 	mmu_p->mmu_dvma_addr_scan_start = (i + 1) & (entries - 1);
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 	i *= clustsz;
7300Sstevel@tonic-gate 	dvma_pg = mmu_p->dvma_base_pg + i;
7310Sstevel@tonic-gate 
7323156Sgirish 	if (px_lib_iommu_map(dip, PCI_TSBID(0, i), npages,
7333156Sgirish 	    PX_ADD_ATTR_EXTNS(attr, mp->dmai_bdf), (void *)mp, 0,
7343156Sgirish 	    MMU_MAP_PFN) != DDI_SUCCESS) {
7351501Sgovinda 		DBG(DBG_MAP_WIN, dip, "px_dvma_map_fast: "
7361501Sgovinda 		    "px_lib_iommu_map failed\n");
7370Sstevel@tonic-gate 		return (DDI_FAILURE);
7381501Sgovinda 	}
7390Sstevel@tonic-gate 
7401501Sgovinda 	if (!PX_MAP_BUFZONE(mp))
7411501Sgovinda 		goto done;
7421501Sgovinda 
7431501Sgovinda 	DBG(DBG_MAP_WIN, dip, "px_dvma_map_fast: redzone pg=%x\n", i + npages);
7441501Sgovinda 
7451501Sgovinda 	ASSERT(PX_HAS_REDZONE(mp));
7461501Sgovinda 
7473156Sgirish 	if (px_lib_iommu_map(dip, PCI_TSBID(0, i + npages), 1,
7483156Sgirish 	    PX_ADD_ATTR_EXTNS(attr, mp->dmai_bdf), (void *)mp, npages - 1,
7493156Sgirish 	    MMU_MAP_PFN) != DDI_SUCCESS) {
7501501Sgovinda 		DBG(DBG_MAP_WIN, dip, "px_dvma_map_fast: "
7511501Sgovinda 		    "mapping REDZONE page failed\n");
7521501Sgovinda 
7531501Sgovinda 		(void) px_lib_iommu_demap(dip, PCI_TSBID(0, i), npages);
7541501Sgovinda 		return (DDI_FAILURE);
7551501Sgovinda 	}
7561501Sgovinda 
7571501Sgovinda done:
7580Sstevel@tonic-gate #ifdef PX_DMA_PROF
7590Sstevel@tonic-gate 	px_dvmaft_success++;
7600Sstevel@tonic-gate #endif
7610Sstevel@tonic-gate 	mp->dmai_mapping = mp->dmai_roffset | MMU_PTOB(dvma_pg);
7620Sstevel@tonic-gate 	mp->dmai_offset = 0;
763909Segillett 	mp->dmai_flags |= PX_DMAI_FLAGS_FASTTRACK;
7640Sstevel@tonic-gate 	PX_SAVE_MP_TTE(mp, attr);	/* save TTE template for unmapping */
765909Segillett 	if (PX_DVMA_DBG_ON(mmu_p))
7660Sstevel@tonic-gate 		px_dvma_alloc_debug(mmu_p, (char *)mp->dmai_mapping,
7678413SDaniel.Ice@Sun.COM 		    mp->dmai_size, mp);
7680Sstevel@tonic-gate 	return (DDI_SUCCESS);
7690Sstevel@tonic-gate }
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate /*
7720Sstevel@tonic-gate  * px_dvma_map: map non-fasttrack DMA
7730Sstevel@tonic-gate  *		Use quantum cache if single page DMA.
7740Sstevel@tonic-gate  */
7750Sstevel@tonic-gate int
px_dvma_map(ddi_dma_impl_t * mp,ddi_dma_req_t * dmareq,px_mmu_t * mmu_p)7760Sstevel@tonic-gate px_dvma_map(ddi_dma_impl_t *mp, ddi_dma_req_t *dmareq, px_mmu_t *mmu_p)
7770Sstevel@tonic-gate {
7780Sstevel@tonic-gate 	uint_t npages = PX_DMA_WINNPGS(mp);
7790Sstevel@tonic-gate 	px_dvma_addr_t dvma_pg, dvma_pg_index;
7800Sstevel@tonic-gate 	void *dvma_addr;
7819707SDaniel.Ice@Sun.COM 	io_attributes_t attr = PX_GET_TTE_ATTR(mp->dmai_rflags,
7821772Sjl139090 	    mp->dmai_attr.dma_attr_flags);
7830Sstevel@tonic-gate 	int sleep = dmareq->dmar_fp == DDI_DMA_SLEEP ? VM_SLEEP : VM_NOSLEEP;
7840Sstevel@tonic-gate 	dev_info_t *dip = mp->dmai_rdip;
7850Sstevel@tonic-gate 	int	ret = DDI_SUCCESS;
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	/*
7880Sstevel@tonic-gate 	 * allocate dvma space resource and map in the first window.
7890Sstevel@tonic-gate 	 * (vmem_t *vmp, size_t size,
7900Sstevel@tonic-gate 	 *	size_t align, size_t phase, size_t nocross,
7910Sstevel@tonic-gate 	 *	void *minaddr, void *maxaddr, int vmflag)
7920Sstevel@tonic-gate 	 */
7931501Sgovinda 	if ((npages == 1) && !PX_HAS_REDZONE(mp) && PX_HAS_NOSYSLIMIT(mp)) {
7940Sstevel@tonic-gate 		dvma_addr = vmem_alloc(mmu_p->mmu_dvma_map,
7958413SDaniel.Ice@Sun.COM 		    MMU_PAGE_SIZE, sleep);
796909Segillett 		mp->dmai_flags |= PX_DMAI_FLAGS_VMEMCACHE;
7970Sstevel@tonic-gate #ifdef	PX_DMA_PROF
7980Sstevel@tonic-gate 		px_dvma_vmem_alloc++;
7990Sstevel@tonic-gate #endif	/* PX_DMA_PROF */
8000Sstevel@tonic-gate 	} else {
8010Sstevel@tonic-gate 		dvma_addr = vmem_xalloc(mmu_p->mmu_dvma_map,
8028413SDaniel.Ice@Sun.COM 		    MMU_PTOB(npages + PX_HAS_REDZONE(mp)),
8038413SDaniel.Ice@Sun.COM 		    MAX(mp->dmai_attr.dma_attr_align, MMU_PAGE_SIZE),
8048413SDaniel.Ice@Sun.COM 		    0,
8058413SDaniel.Ice@Sun.COM 		    mp->dmai_attr.dma_attr_seg + 1,
8068413SDaniel.Ice@Sun.COM 		    (void *)mp->dmai_attr.dma_attr_addr_lo,
8078413SDaniel.Ice@Sun.COM 		    (void *)(mp->dmai_attr.dma_attr_addr_hi + 1),
8088413SDaniel.Ice@Sun.COM 		    sleep);
8090Sstevel@tonic-gate #ifdef	PX_DMA_PROF
8100Sstevel@tonic-gate 		px_dvma_vmem_xalloc++;
8110Sstevel@tonic-gate #endif	/* PX_DMA_PROF */
8120Sstevel@tonic-gate 	}
8130Sstevel@tonic-gate 	dvma_pg = MMU_BTOP((ulong_t)dvma_addr);
8140Sstevel@tonic-gate 	dvma_pg_index = dvma_pg - mmu_p->dvma_base_pg;
8150Sstevel@tonic-gate 	DBG(DBG_DMA_MAP, dip, "fallback dvma_pages: dvma_pg=%x index=%x\n",
8168413SDaniel.Ice@Sun.COM 	    dvma_pg, dvma_pg_index);
8170Sstevel@tonic-gate 	if (dvma_pg == 0)
8180Sstevel@tonic-gate 		goto noresource;
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 	mp->dmai_mapping = mp->dmai_roffset | MMU_PTOB(dvma_pg);
8210Sstevel@tonic-gate 	mp->dmai_offset = 0;
8229707SDaniel.Ice@Sun.COM 	PX_SAVE_MP_TTE(mp, attr);	/* mp->dmai_tte = tte */
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	if ((ret = px_mmu_map_pages(mmu_p,
8250Sstevel@tonic-gate 	    mp, dvma_pg, npages, 0)) != DDI_SUCCESS) {
826909Segillett 		if (mp->dmai_flags & PX_DMAI_FLAGS_VMEMCACHE) {
8270Sstevel@tonic-gate 			vmem_free(mmu_p->mmu_dvma_map, (void *)dvma_addr,
8280Sstevel@tonic-gate 			    MMU_PAGE_SIZE);
8290Sstevel@tonic-gate #ifdef PX_DMA_PROF
8300Sstevel@tonic-gate 			px_dvma_vmem_free++;
8310Sstevel@tonic-gate #endif /* PX_DMA_PROF */
8320Sstevel@tonic-gate 		} else {
8330Sstevel@tonic-gate 			vmem_xfree(mmu_p->mmu_dvma_map, (void *)dvma_addr,
8341501Sgovinda 			    MMU_PTOB(npages + PX_HAS_REDZONE(mp)));
8350Sstevel@tonic-gate #ifdef PX_DMA_PROF
8360Sstevel@tonic-gate 			px_dvma_vmem_xfree++;
8370Sstevel@tonic-gate #endif /* PX_DMA_PROF */
8380Sstevel@tonic-gate 		}
8390Sstevel@tonic-gate 	}
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 	return (ret);
8420Sstevel@tonic-gate noresource:
8430Sstevel@tonic-gate 	if (dmareq->dmar_fp != DDI_DMA_DONTWAIT) {
8440Sstevel@tonic-gate 		DBG(DBG_DMA_MAP, dip, "dvma_pg 0 - set callback\n");
8450Sstevel@tonic-gate 		ddi_set_callback(dmareq->dmar_fp, dmareq->dmar_arg,
8468413SDaniel.Ice@Sun.COM 		    &mmu_p->mmu_dvma_clid);
8470Sstevel@tonic-gate 	}
8480Sstevel@tonic-gate 	DBG(DBG_DMA_MAP, dip, "vmem_xalloc - DDI_DMA_NORESOURCES\n");
8490Sstevel@tonic-gate 	return (DDI_DMA_NORESOURCES);
8500Sstevel@tonic-gate }
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate void
px_dvma_unmap(px_mmu_t * mmu_p,ddi_dma_impl_t * mp)8530Sstevel@tonic-gate px_dvma_unmap(px_mmu_t *mmu_p, ddi_dma_impl_t *mp)
8540Sstevel@tonic-gate {
8550Sstevel@tonic-gate 	px_dvma_addr_t dvma_addr = (px_dvma_addr_t)mp->dmai_mapping;
8560Sstevel@tonic-gate 	px_dvma_addr_t dvma_pg = MMU_BTOP(dvma_addr);
8570Sstevel@tonic-gate 	dvma_addr = MMU_PTOB(dvma_pg);
8580Sstevel@tonic-gate 
859909Segillett 	if (mp->dmai_flags & PX_DMAI_FLAGS_FASTTRACK) {
8600Sstevel@tonic-gate 		px_iopfn_t index = dvma_pg - mmu_p->dvma_base_pg;
8610Sstevel@tonic-gate 		ASSERT(index % px_dvma_page_cache_clustsz == 0);
8620Sstevel@tonic-gate 		index /= px_dvma_page_cache_clustsz;
8630Sstevel@tonic-gate 		ASSERT(index < px_dvma_page_cache_entries);
8640Sstevel@tonic-gate 		mmu_p->mmu_dvma_cache_locks[index] = 0;
8650Sstevel@tonic-gate #ifdef	PX_DMA_PROF
8660Sstevel@tonic-gate 		px_dvmaft_free++;
8670Sstevel@tonic-gate #endif	/* PX_DMA_PROF */
8680Sstevel@tonic-gate 		return;
8690Sstevel@tonic-gate 	}
8700Sstevel@tonic-gate 
871909Segillett 	if (mp->dmai_flags & PX_DMAI_FLAGS_VMEMCACHE) {
8720Sstevel@tonic-gate 		vmem_free(mmu_p->mmu_dvma_map, (void *)dvma_addr,
8738413SDaniel.Ice@Sun.COM 		    MMU_PAGE_SIZE);
8740Sstevel@tonic-gate #ifdef PX_DMA_PROF
8750Sstevel@tonic-gate 		px_dvma_vmem_free++;
8760Sstevel@tonic-gate #endif /* PX_DMA_PROF */
8770Sstevel@tonic-gate 	} else {
8781501Sgovinda 		size_t npages = MMU_BTOP(mp->dmai_winsize) + PX_HAS_REDZONE(mp);
8790Sstevel@tonic-gate 		vmem_xfree(mmu_p->mmu_dvma_map, (void *)dvma_addr,
8808413SDaniel.Ice@Sun.COM 		    MMU_PTOB(npages));
8810Sstevel@tonic-gate #ifdef PX_DMA_PROF
8820Sstevel@tonic-gate 		px_dvma_vmem_xfree++;
8830Sstevel@tonic-gate #endif /* PX_DMA_PROF */
8840Sstevel@tonic-gate 	}
8850Sstevel@tonic-gate }
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate /*
8880Sstevel@tonic-gate  * DVMA mappings may have multiple windows, but each window always have
8890Sstevel@tonic-gate  * one segment.
8900Sstevel@tonic-gate  */
8910Sstevel@tonic-gate int
px_dvma_ctl(dev_info_t * dip,dev_info_t * rdip,ddi_dma_impl_t * mp,enum ddi_dma_ctlops cmd,off_t * offp,size_t * lenp,caddr_t * objp,uint_t cache_flags)8920Sstevel@tonic-gate px_dvma_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_impl_t *mp,
8930Sstevel@tonic-gate 	enum ddi_dma_ctlops cmd, off_t *offp, size_t *lenp, caddr_t *objp,
8940Sstevel@tonic-gate 	uint_t cache_flags)
8950Sstevel@tonic-gate {
8960Sstevel@tonic-gate 	switch (cmd) {
8970Sstevel@tonic-gate 	case DDI_DMA_SYNC:
8980Sstevel@tonic-gate 		return (px_lib_dma_sync(dip, rdip, (ddi_dma_handle_t)mp,
8990Sstevel@tonic-gate 		    *offp, *lenp, cache_flags));
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 	case DDI_DMA_HTOC: {
9020Sstevel@tonic-gate 		int ret;
9030Sstevel@tonic-gate 		off_t wo_off, off = *offp;	/* wo_off: wnd's obj offset */
9040Sstevel@tonic-gate 		uint_t win_size = mp->dmai_winsize;
9050Sstevel@tonic-gate 		ddi_dma_cookie_t *cp = (ddi_dma_cookie_t *)objp;
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 		if (off >= mp->dmai_object.dmao_size) {
9080Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d invalid dma_htoc offset %lx",
9098413SDaniel.Ice@Sun.COM 			    NAMEINST(mp->dmai_rdip), off);
9100Sstevel@tonic-gate 			return (DDI_FAILURE);
9110Sstevel@tonic-gate 		}
9120Sstevel@tonic-gate 		off += mp->dmai_roffset;
9130Sstevel@tonic-gate 		ret = px_dma_win(dip, rdip, (ddi_dma_handle_t)mp,
9140Sstevel@tonic-gate 		    off / win_size, &wo_off, NULL, cp, NULL); /* lenp == NULL */
9150Sstevel@tonic-gate 		if (ret)
9160Sstevel@tonic-gate 			return (ret);
9170Sstevel@tonic-gate 		DBG(DBG_DMA_CTL, dip, "HTOC:cookie=%x+%lx off=%lx,%lx\n",
9188413SDaniel.Ice@Sun.COM 		    cp->dmac_address, cp->dmac_size, off, *offp);
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 		/* adjust cookie addr/len if we are not on window boundary */
9210Sstevel@tonic-gate 		ASSERT((off % win_size) == (off -
9228413SDaniel.Ice@Sun.COM 		    (PX_DMA_CURWIN(mp) ? mp->dmai_roffset : 0) - wo_off));
9230Sstevel@tonic-gate 		off = PX_DMA_CURWIN(mp) ? off % win_size : *offp;
9240Sstevel@tonic-gate 		ASSERT(cp->dmac_size > off);
9250Sstevel@tonic-gate 		cp->dmac_laddress += off;
9260Sstevel@tonic-gate 		cp->dmac_size -= off;
9270Sstevel@tonic-gate 		DBG(DBG_DMA_CTL, dip, "HTOC:mp=%p cookie=%x+%lx off=%lx,%lx\n",
9288413SDaniel.Ice@Sun.COM 		    mp, cp->dmac_address, cp->dmac_size, off, wo_off);
9290Sstevel@tonic-gate 		}
9300Sstevel@tonic-gate 		return (DDI_SUCCESS);
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 	case DDI_DMA_REPWIN:
9330Sstevel@tonic-gate 		*offp = mp->dmai_offset;
9340Sstevel@tonic-gate 		*lenp = mp->dmai_size;
9350Sstevel@tonic-gate 		return (DDI_SUCCESS);
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 	case DDI_DMA_MOVWIN: {
9380Sstevel@tonic-gate 		off_t off = *offp;
9390Sstevel@tonic-gate 		if (off >= mp->dmai_object.dmao_size)
9400Sstevel@tonic-gate 			return (DDI_FAILURE);
9410Sstevel@tonic-gate 		off += mp->dmai_roffset;
9420Sstevel@tonic-gate 		return (px_dma_win(dip, rdip, (ddi_dma_handle_t)mp,
9430Sstevel@tonic-gate 		    off / mp->dmai_winsize, offp, lenp,
9440Sstevel@tonic-gate 		    (ddi_dma_cookie_t *)objp, NULL));
9450Sstevel@tonic-gate 		}
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 	case DDI_DMA_NEXTWIN: {
9480Sstevel@tonic-gate 		px_window_t win = PX_DMA_CURWIN(mp);
9490Sstevel@tonic-gate 		if (offp) {
9500Sstevel@tonic-gate 			if (*(px_window_t *)offp != win) {
9510Sstevel@tonic-gate 				/* window not active */
9520Sstevel@tonic-gate 				*(px_window_t *)objp = win; /* return cur win */
9530Sstevel@tonic-gate 				return (DDI_DMA_STALE);
9540Sstevel@tonic-gate 			}
9550Sstevel@tonic-gate 			win++;
9560Sstevel@tonic-gate 		} else	/* map win 0 */
9570Sstevel@tonic-gate 			win = 0;
9580Sstevel@tonic-gate 		if (win >= mp->dmai_nwin) {
9590Sstevel@tonic-gate 			*(px_window_t *)objp = win - 1;
9600Sstevel@tonic-gate 			return (DDI_DMA_DONE);
9610Sstevel@tonic-gate 		}
9620Sstevel@tonic-gate 		if (px_dma_win(dip, rdip, (ddi_dma_handle_t)mp,
9630Sstevel@tonic-gate 		    win, 0, 0, 0, 0)) {
9640Sstevel@tonic-gate 			*(px_window_t *)objp = win - 1;
9650Sstevel@tonic-gate 			return (DDI_FAILURE);
9660Sstevel@tonic-gate 		}
9670Sstevel@tonic-gate 		*(px_window_t *)objp = win;
9680Sstevel@tonic-gate 		}
9690Sstevel@tonic-gate 		return (DDI_SUCCESS);
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate 	case DDI_DMA_NEXTSEG:
9720Sstevel@tonic-gate 		if (*(px_window_t *)offp != PX_DMA_CURWIN(mp))
9730Sstevel@tonic-gate 			return (DDI_DMA_STALE);
9740Sstevel@tonic-gate 		if (lenp)				/* only 1 seg allowed */
9750Sstevel@tonic-gate 			return (DDI_DMA_DONE);
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 		/* return mp as seg 0 */
9780Sstevel@tonic-gate 		*(ddi_dma_seg_t *)objp = (ddi_dma_seg_t)mp;
9790Sstevel@tonic-gate 		return (DDI_SUCCESS);
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 	case DDI_DMA_SEGTOC:
9820Sstevel@tonic-gate 		MAKE_DMA_COOKIE((ddi_dma_cookie_t *)objp, mp->dmai_mapping,
9838413SDaniel.Ice@Sun.COM 		    mp->dmai_size);
9840Sstevel@tonic-gate 		*offp = mp->dmai_offset;
9850Sstevel@tonic-gate 		*lenp = mp->dmai_size;
9860Sstevel@tonic-gate 		return (DDI_SUCCESS);
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate 	case DDI_DMA_COFF: {
9890Sstevel@tonic-gate 		ddi_dma_cookie_t *cp = (ddi_dma_cookie_t *)offp;
9900Sstevel@tonic-gate 		if (cp->dmac_address < mp->dmai_mapping ||
9918413SDaniel.Ice@Sun.COM 		    (cp->dmac_address + cp->dmac_size) >
9928413SDaniel.Ice@Sun.COM 		    (mp->dmai_mapping + mp->dmai_size))
9930Sstevel@tonic-gate 			return (DDI_FAILURE);
9940Sstevel@tonic-gate 		*objp = (caddr_t)(cp->dmac_address - mp->dmai_mapping +
9958413SDaniel.Ice@Sun.COM 		    mp->dmai_offset);
9960Sstevel@tonic-gate 		}
9970Sstevel@tonic-gate 		return (DDI_SUCCESS);
9980Sstevel@tonic-gate 	default:
9990Sstevel@tonic-gate 		DBG(DBG_DMA_CTL, dip, "unknown command (%x): rdip=%s%d\n",
10008413SDaniel.Ice@Sun.COM 		    cmd, ddi_driver_name(rdip), ddi_get_instance(rdip));
10010Sstevel@tonic-gate 		break;
10020Sstevel@tonic-gate 	}
10030Sstevel@tonic-gate 	return (DDI_FAILURE);
10040Sstevel@tonic-gate }
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate void
px_dma_freewin(ddi_dma_impl_t * mp)10070Sstevel@tonic-gate px_dma_freewin(ddi_dma_impl_t *mp)
10080Sstevel@tonic-gate {
10090Sstevel@tonic-gate 	px_dma_win_t *win_p = mp->dmai_winlst, *win2_p;
10100Sstevel@tonic-gate 	for (win2_p = win_p; win_p; win2_p = win_p) {
10110Sstevel@tonic-gate 		win_p = win2_p->win_next;
10120Sstevel@tonic-gate 		kmem_free(win2_p, sizeof (px_dma_win_t) +
10138413SDaniel.Ice@Sun.COM 		    sizeof (ddi_dma_cookie_t) * win2_p->win_ncookies);
10140Sstevel@tonic-gate 	}
10150Sstevel@tonic-gate 	mp->dmai_nwin = 0;
10160Sstevel@tonic-gate 	mp->dmai_winlst = NULL;
10170Sstevel@tonic-gate }
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate /*
10200Sstevel@tonic-gate  * px_dma_newwin - create a dma window object and cookies
10210Sstevel@tonic-gate  *
10220Sstevel@tonic-gate  *	After the initial scan in px_dma_physwin(), which identifies
10230Sstevel@tonic-gate  *	a portion of the pfn array that belongs to a dma window,
10240Sstevel@tonic-gate  *	we are called to allocate and initialize representing memory
10250Sstevel@tonic-gate  *	resources. We know from the 1st scan the number of cookies
10260Sstevel@tonic-gate  *	or dma segment in this window so we can allocate a contiguous
10270Sstevel@tonic-gate  *	memory array for the dma cookies (The implementation of
10280Sstevel@tonic-gate  *	ddi_dma_nextcookie(9f) dictates dma cookies be contiguous).
10290Sstevel@tonic-gate  *
10300Sstevel@tonic-gate  *	A second round scan is done on the pfn array to identify
10310Sstevel@tonic-gate  *	each dma segment and initialize its corresponding dma cookie.
10320Sstevel@tonic-gate  *	We don't need to do all the safety checking and we know they
10330Sstevel@tonic-gate  *	all belong to the same dma window.
10340Sstevel@tonic-gate  *
10350Sstevel@tonic-gate  *	Input:	cookie_no - # of cookies identified by the 1st scan
10360Sstevel@tonic-gate  *		start_idx - subscript of the pfn array for the starting pfn
10370Sstevel@tonic-gate  *		end_idx   - subscript of the last pfn in dma window
10380Sstevel@tonic-gate  *		win_pp    - pointer to win_next member of previous window
10390Sstevel@tonic-gate  *	Return:	DDI_SUCCESS - with **win_pp as newly created window object
10400Sstevel@tonic-gate  *		DDI_DMA_NORESROUCE - caller frees all previous window objs
10410Sstevel@tonic-gate  *	Note:	Each cookie and window size are all initialized on page
10420Sstevel@tonic-gate  *		boundary. This is not true for the 1st cookie of the 1st
10430Sstevel@tonic-gate  *		window and the last cookie of the last window.
10440Sstevel@tonic-gate  *		We fix that later in upper layer which has access to size
10450Sstevel@tonic-gate  *		and offset info.
10460Sstevel@tonic-gate  *
10470Sstevel@tonic-gate  */
10480Sstevel@tonic-gate /*ARGSUSED*/
10490Sstevel@tonic-gate static int
px_dma_newwin(dev_info_t * dip,ddi_dma_req_t * dmareq,ddi_dma_impl_t * mp,uint32_t cookie_no,uint32_t start_idx,uint32_t end_idx,px_dma_win_t ** win_pp,uint64_t count_max,uint64_t bypass)10500Sstevel@tonic-gate px_dma_newwin(dev_info_t *dip, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp,
10510Sstevel@tonic-gate 	uint32_t cookie_no, uint32_t start_idx, uint32_t end_idx,
10520Sstevel@tonic-gate 	px_dma_win_t **win_pp, uint64_t count_max, uint64_t bypass)
10530Sstevel@tonic-gate {
10540Sstevel@tonic-gate 	int (*waitfp)(caddr_t) = dmareq->dmar_fp;
10550Sstevel@tonic-gate 	ddi_dma_cookie_t *cookie_p;
10560Sstevel@tonic-gate 	uint32_t pfn_no = 1;
10570Sstevel@tonic-gate 	px_iopfn_t pfn = PX_GET_MP_PFN(mp, start_idx);
10580Sstevel@tonic-gate 	px_iopfn_t prev_pfn = pfn;
10590Sstevel@tonic-gate 	uint64_t baddr, seg_pfn0 = pfn;
10600Sstevel@tonic-gate 	size_t sz = cookie_no * sizeof (ddi_dma_cookie_t);
10610Sstevel@tonic-gate 	px_dma_win_t *win_p = kmem_zalloc(sizeof (px_dma_win_t) + sz,
10628413SDaniel.Ice@Sun.COM 	    waitfp == DDI_DMA_SLEEP ? KM_SLEEP : KM_NOSLEEP);
10631772Sjl139090 	io_attributes_t	attr = PX_GET_TTE_ATTR(mp->dmai_rflags,
10641772Sjl139090 	    mp->dmai_attr.dma_attr_flags);
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 	if (!win_p)
10670Sstevel@tonic-gate 		goto noresource;
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate 	win_p->win_next = NULL;
10700Sstevel@tonic-gate 	win_p->win_ncookies = cookie_no;
10710Sstevel@tonic-gate 	win_p->win_curseg = 0;	/* start from segment 0 */
10720Sstevel@tonic-gate 	win_p->win_size = MMU_PTOB(end_idx - start_idx + 1);
10730Sstevel@tonic-gate 	/* win_p->win_offset is left uninitialized */
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 	cookie_p = (ddi_dma_cookie_t *)(win_p + 1);
10760Sstevel@tonic-gate 	start_idx++;
10770Sstevel@tonic-gate 	for (; start_idx <= end_idx; start_idx++, prev_pfn = pfn, pfn_no++) {
10780Sstevel@tonic-gate 		pfn = PX_GET_MP_PFN1(mp, start_idx);
10790Sstevel@tonic-gate 		if ((pfn == prev_pfn + 1) &&
10808413SDaniel.Ice@Sun.COM 		    (MMU_PTOB(pfn_no + 1) - 1 <= count_max))
10810Sstevel@tonic-gate 			continue;
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 		/* close up the cookie up to (including) prev_pfn */
10840Sstevel@tonic-gate 		baddr = MMU_PTOB(seg_pfn0);
10858691SLida.Horn@Sun.COM 		if (bypass) {
10868691SLida.Horn@Sun.COM 			if (px_lib_iommu_getbypass(dip, baddr, attr, &baddr)
10878691SLida.Horn@Sun.COM 			    == DDI_SUCCESS)
10888691SLida.Horn@Sun.COM 				baddr = px_lib_ro_bypass(dip, attr, baddr);
10898691SLida.Horn@Sun.COM 			else
10908691SLida.Horn@Sun.COM 				return (DDI_FAILURE);
10918691SLida.Horn@Sun.COM 		}
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 		MAKE_DMA_COOKIE(cookie_p, baddr, MMU_PTOB(pfn_no));
10940Sstevel@tonic-gate 		DBG(DBG_BYPASS, mp->dmai_rdip, "cookie %p (%x pages)\n",
10958413SDaniel.Ice@Sun.COM 		    MMU_PTOB(seg_pfn0), pfn_no);
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 		cookie_p++;	/* advance to next available cookie cell */
10980Sstevel@tonic-gate 		pfn_no = 0;
10990Sstevel@tonic-gate 		seg_pfn0 = pfn;	/* start a new segment from current pfn */
11000Sstevel@tonic-gate 	}
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 	baddr = MMU_PTOB(seg_pfn0);
11038691SLida.Horn@Sun.COM 	if (bypass) {
11048691SLida.Horn@Sun.COM 		if (px_lib_iommu_getbypass(dip, baddr, attr, &baddr)
11058691SLida.Horn@Sun.COM 		    == DDI_SUCCESS)
11068691SLida.Horn@Sun.COM 			baddr = px_lib_ro_bypass(dip, attr, baddr);
11078691SLida.Horn@Sun.COM 		else
11088691SLida.Horn@Sun.COM 			return (DDI_FAILURE);
11098691SLida.Horn@Sun.COM 	}
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 	MAKE_DMA_COOKIE(cookie_p, baddr, MMU_PTOB(pfn_no));
11120Sstevel@tonic-gate 	DBG(DBG_BYPASS, mp->dmai_rdip, "cookie %p (%x pages) of total %x\n",
11138413SDaniel.Ice@Sun.COM 	    MMU_PTOB(seg_pfn0), pfn_no, cookie_no);
11140Sstevel@tonic-gate #ifdef	DEBUG
11150Sstevel@tonic-gate 	cookie_p++;
11160Sstevel@tonic-gate 	ASSERT((cookie_p - (ddi_dma_cookie_t *)(win_p + 1)) == cookie_no);
11170Sstevel@tonic-gate #endif	/* DEBUG */
11180Sstevel@tonic-gate 	*win_pp = win_p;
11190Sstevel@tonic-gate 	return (DDI_SUCCESS);
11200Sstevel@tonic-gate noresource:
11210Sstevel@tonic-gate 	if (waitfp != DDI_DMA_DONTWAIT)
11220Sstevel@tonic-gate 		ddi_set_callback(waitfp, dmareq->dmar_arg, &px_kmem_clid);
11230Sstevel@tonic-gate 	return (DDI_DMA_NORESOURCES);
11240Sstevel@tonic-gate }
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate /*
11270Sstevel@tonic-gate  * px_dma_adjust - adjust 1st and last cookie and window sizes
11280Sstevel@tonic-gate  *	remove initial dma page offset from 1st cookie and window size
11290Sstevel@tonic-gate  *	remove last dma page remainder from last cookie and window size
11300Sstevel@tonic-gate  *	fill win_offset of each dma window according to just fixed up
11310Sstevel@tonic-gate  *		each window sizes
11320Sstevel@tonic-gate  *	px_dma_win_t members modified:
11330Sstevel@tonic-gate  *	win_p->win_offset - this window's offset within entire DMA object
11340Sstevel@tonic-gate  *	win_p->win_size	  - xferrable size (in bytes) for this window
11350Sstevel@tonic-gate  *
11360Sstevel@tonic-gate  *	ddi_dma_impl_t members modified:
11370Sstevel@tonic-gate  *	mp->dmai_size	  - 1st window xferrable size
11380Sstevel@tonic-gate  *	mp->dmai_offset   - 0, which is the dma offset of the 1st window
11390Sstevel@tonic-gate  *
11400Sstevel@tonic-gate  *	ddi_dma_cookie_t members modified:
11410Sstevel@tonic-gate  *	cookie_p->dmac_size - 1st and last cookie remove offset or remainder
11420Sstevel@tonic-gate  *	cookie_p->dmac_laddress - 1st cookie add page offset
11430Sstevel@tonic-gate  */
11440Sstevel@tonic-gate static void
px_dma_adjust(ddi_dma_req_t * dmareq,ddi_dma_impl_t * mp,px_dma_win_t * win_p)11450Sstevel@tonic-gate px_dma_adjust(ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp, px_dma_win_t *win_p)
11460Sstevel@tonic-gate {
11470Sstevel@tonic-gate 	ddi_dma_cookie_t *cookie_p = (ddi_dma_cookie_t *)(win_p + 1);
11480Sstevel@tonic-gate 	size_t pg_offset = mp->dmai_roffset;
11490Sstevel@tonic-gate 	size_t win_offset = 0;
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate 	cookie_p->dmac_size -= pg_offset;
11520Sstevel@tonic-gate 	cookie_p->dmac_laddress |= pg_offset;
11530Sstevel@tonic-gate 	win_p->win_size -= pg_offset;
11540Sstevel@tonic-gate 	DBG(DBG_BYPASS, mp->dmai_rdip, "pg0 adjust %lx\n", pg_offset);
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate 	mp->dmai_size = win_p->win_size;
11570Sstevel@tonic-gate 	mp->dmai_offset = 0;
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 	pg_offset += mp->dmai_object.dmao_size;
11600Sstevel@tonic-gate 	pg_offset &= MMU_PAGE_OFFSET;
11610Sstevel@tonic-gate 	if (pg_offset)
11620Sstevel@tonic-gate 		pg_offset = MMU_PAGE_SIZE - pg_offset;
11630Sstevel@tonic-gate 	DBG(DBG_BYPASS, mp->dmai_rdip, "last pg adjust %lx\n", pg_offset);
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate 	for (; win_p->win_next; win_p = win_p->win_next) {
11660Sstevel@tonic-gate 		DBG(DBG_BYPASS, mp->dmai_rdip, "win off %p\n", win_offset);
11670Sstevel@tonic-gate 		win_p->win_offset = win_offset;
11680Sstevel@tonic-gate 		win_offset += win_p->win_size;
11690Sstevel@tonic-gate 	}
11700Sstevel@tonic-gate 	/* last window */
11710Sstevel@tonic-gate 	win_p->win_offset = win_offset;
11720Sstevel@tonic-gate 	cookie_p = (ddi_dma_cookie_t *)(win_p + 1);
11730Sstevel@tonic-gate 	cookie_p[win_p->win_ncookies - 1].dmac_size -= pg_offset;
11740Sstevel@tonic-gate 	win_p->win_size -= pg_offset;
11750Sstevel@tonic-gate 	ASSERT((win_offset + win_p->win_size) == mp->dmai_object.dmao_size);
11760Sstevel@tonic-gate }
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate /*
11790Sstevel@tonic-gate  * px_dma_physwin() - carve up dma windows using physical addresses.
11800Sstevel@tonic-gate  *	Called to handle mmu bypass and pci peer-to-peer transfers.
11810Sstevel@tonic-gate  *	Calls px_dma_newwin() to allocate window objects.
11820Sstevel@tonic-gate  *
11830Sstevel@tonic-gate  * Dependency: mp->dmai_pfnlst points to an array of pfns
11840Sstevel@tonic-gate  *
11850Sstevel@tonic-gate  * 1. Each dma window is represented by a px_dma_win_t object.
11860Sstevel@tonic-gate  *	The object will be casted to ddi_dma_win_t and returned
11870Sstevel@tonic-gate  *	to leaf driver through the DDI interface.
11880Sstevel@tonic-gate  * 2. Each dma window can have several dma segments with each
11890Sstevel@tonic-gate  *	segment representing a physically contiguous either memory
11900Sstevel@tonic-gate  *	space (if we are doing an mmu bypass transfer) or pci address
11910Sstevel@tonic-gate  *	space (if we are doing a peer-to-peer transfer).
11920Sstevel@tonic-gate  * 3. Each segment has a DMA cookie to program the DMA engine.
11930Sstevel@tonic-gate  *	The cookies within each DMA window must be located in a
11940Sstevel@tonic-gate  *	contiguous array per ddi_dma_nextcookie(9f).
11950Sstevel@tonic-gate  * 4. The number of DMA segments within each DMA window cannot exceed
11960Sstevel@tonic-gate  *	mp->dmai_attr.dma_attr_sgllen. If the transfer size is
11970Sstevel@tonic-gate  *	too large to fit in the sgllen, the rest needs to be
11980Sstevel@tonic-gate  *	relocated to the next dma window.
11990Sstevel@tonic-gate  * 5. Peer-to-peer DMA segment follows device hi, lo, count_max,
12000Sstevel@tonic-gate  *	and nocross restrictions while bypass DMA follows the set of
12010Sstevel@tonic-gate  *	restrictions with system limits factored in.
12020Sstevel@tonic-gate  *
12030Sstevel@tonic-gate  * Return:
12040Sstevel@tonic-gate  *	mp->dmai_winlst	 - points to a link list of px_dma_win_t objects.
12050Sstevel@tonic-gate  *		Each px_dma_win_t object on the link list contains
12060Sstevel@tonic-gate  *		infomation such as its window size (# of pages),
12070Sstevel@tonic-gate  *		starting offset (also see Restriction), an array of
12080Sstevel@tonic-gate  *		DMA cookies, and # of cookies in the array.
12090Sstevel@tonic-gate  *	mp->dmai_pfnlst	 - NULL, the pfn list is freed to conserve memory.
12100Sstevel@tonic-gate  *	mp->dmai_nwin	 - # of total DMA windows on mp->dmai_winlst.
12110Sstevel@tonic-gate  *	mp->dmai_mapping - starting cookie address
12120Sstevel@tonic-gate  *	mp->dmai_rflags	 - consistent, nosync, no redzone
12130Sstevel@tonic-gate  *	mp->dmai_cookie	 - start of cookie table of the 1st DMA window
12140Sstevel@tonic-gate  *
12150Sstevel@tonic-gate  * Restriction:
12160Sstevel@tonic-gate  *	Each px_dma_win_t object can theoratically start from any offset
12170Sstevel@tonic-gate  *	since the mmu is not involved. However, this implementation
12180Sstevel@tonic-gate  *	always make windows start from page aligned offset (except
12190Sstevel@tonic-gate  *	the 1st window, which follows the requested offset) due to the
12200Sstevel@tonic-gate  *	fact that we are handed a pfn list. This does require device's
12210Sstevel@tonic-gate  *	count_max and attr_seg to be at least MMU_PAGE_SIZE aligned.
12220Sstevel@tonic-gate  */
12230Sstevel@tonic-gate int
px_dma_physwin(px_t * px_p,ddi_dma_req_t * dmareq,ddi_dma_impl_t * mp)12240Sstevel@tonic-gate px_dma_physwin(px_t *px_p, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp)
12250Sstevel@tonic-gate {
12260Sstevel@tonic-gate 	uint_t npages = mp->dmai_ndvmapages;
12270Sstevel@tonic-gate 	int ret, sgllen = mp->dmai_attr.dma_attr_sgllen;
12280Sstevel@tonic-gate 	px_iopfn_t pfn_lo, pfn_hi, prev_pfn;
12290Sstevel@tonic-gate 	px_iopfn_t pfn = PX_GET_MP_PFN(mp, 0);
12300Sstevel@tonic-gate 	uint32_t i, win_no = 0, pfn_no = 1, win_pfn0_index = 0, cookie_no = 0;
12310Sstevel@tonic-gate 	uint64_t count_max, bypass_addr = 0;
12320Sstevel@tonic-gate 	px_dma_win_t **win_pp = (px_dma_win_t **)&mp->dmai_winlst;
12330Sstevel@tonic-gate 	ddi_dma_cookie_t *cookie0_p;
12341772Sjl139090 	io_attributes_t attr = PX_GET_TTE_ATTR(mp->dmai_rflags,
12351772Sjl139090 	    mp->dmai_attr.dma_attr_flags);
12360Sstevel@tonic-gate 	dev_info_t *dip = px_p->px_dip;
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 	ASSERT(PX_DMA_ISPTP(mp) || PX_DMA_ISBYPASS(mp));
12390Sstevel@tonic-gate 	if (PX_DMA_ISPTP(mp)) { /* ignore sys limits for peer-to-peer */
1240909Segillett 		ddi_dma_attr_t *dev_attr_p = PX_DEV_ATTR(mp);
12410Sstevel@tonic-gate 		uint64_t nocross = dev_attr_p->dma_attr_seg;
12420Sstevel@tonic-gate 		px_pec_t *pec_p = px_p->px_pec_p;
12430Sstevel@tonic-gate 		px_iopfn_t pfn_last = PX_DMA_ISPTP32(mp) ?
12448413SDaniel.Ice@Sun.COM 		    pec_p->pec_last32_pfn - pec_p->pec_base32_pfn :
12458413SDaniel.Ice@Sun.COM 		    pec_p->pec_last64_pfn - pec_p->pec_base64_pfn;
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate 		if (nocross && (nocross < UINT32_MAX))
12480Sstevel@tonic-gate 			return (DDI_DMA_NOMAPPING);
12490Sstevel@tonic-gate 		if (dev_attr_p->dma_attr_align > MMU_PAGE_SIZE)
12500Sstevel@tonic-gate 			return (DDI_DMA_NOMAPPING);
12510Sstevel@tonic-gate 		pfn_lo = MMU_BTOP(dev_attr_p->dma_attr_addr_lo);
12520Sstevel@tonic-gate 		pfn_hi = MMU_BTOP(dev_attr_p->dma_attr_addr_hi);
12530Sstevel@tonic-gate 		pfn_hi = MIN(pfn_hi, pfn_last);
12540Sstevel@tonic-gate 		if ((pfn_lo > pfn_hi) || (pfn < pfn_lo))
12550Sstevel@tonic-gate 			return (DDI_DMA_NOMAPPING);
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate 		count_max = dev_attr_p->dma_attr_count_max;
12580Sstevel@tonic-gate 		count_max = MIN(count_max, nocross);
12590Sstevel@tonic-gate 		/*
12600Sstevel@tonic-gate 		 * the following count_max trim is not done because we are
12610Sstevel@tonic-gate 		 * making sure pfn_lo <= pfn <= pfn_hi inside the loop
12620Sstevel@tonic-gate 		 * count_max=MIN(count_max, MMU_PTOB(pfn_hi - pfn_lo + 1)-1);
12630Sstevel@tonic-gate 		 */
12640Sstevel@tonic-gate 	} else { /* bypass hi/lo/count_max have been processed by attr2hdl() */
12650Sstevel@tonic-gate 		count_max = mp->dmai_attr.dma_attr_count_max;
12660Sstevel@tonic-gate 		pfn_lo = MMU_BTOP(mp->dmai_attr.dma_attr_addr_lo);
12670Sstevel@tonic-gate 		pfn_hi = MMU_BTOP(mp->dmai_attr.dma_attr_addr_hi);
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate 		if (px_lib_iommu_getbypass(dip, MMU_PTOB(pfn),
12708413SDaniel.Ice@Sun.COM 		    attr, &bypass_addr) != DDI_SUCCESS) {
12718580SBill.Taylor@Sun.COM 			DBG(DBG_BYPASS, mp->dmai_rdip,
12728580SBill.Taylor@Sun.COM 			    "bypass cookie failure %lx\n", pfn);
12730Sstevel@tonic-gate 			return (DDI_DMA_NOMAPPING);
12740Sstevel@tonic-gate 		}
12750Sstevel@tonic-gate 		pfn = MMU_BTOP(bypass_addr);
12760Sstevel@tonic-gate 	}
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 	/* pfn: absolute (bypass mode) or relative (p2p mode) */
12790Sstevel@tonic-gate 	for (prev_pfn = pfn, i = 1; i < npages;
12800Sstevel@tonic-gate 	    i++, prev_pfn = pfn, pfn_no++) {
12810Sstevel@tonic-gate 		pfn = PX_GET_MP_PFN1(mp, i);
12820Sstevel@tonic-gate 		if (bypass_addr) {
12830Sstevel@tonic-gate 			if (px_lib_iommu_getbypass(dip, MMU_PTOB(pfn), attr,
12848413SDaniel.Ice@Sun.COM 			    &bypass_addr) != DDI_SUCCESS) {
12850Sstevel@tonic-gate 				ret = DDI_DMA_NOMAPPING;
12860Sstevel@tonic-gate 				goto err;
12870Sstevel@tonic-gate 			}
12880Sstevel@tonic-gate 			pfn = MMU_BTOP(bypass_addr);
12890Sstevel@tonic-gate 		}
12900Sstevel@tonic-gate 		if ((pfn == prev_pfn + 1) &&
12918413SDaniel.Ice@Sun.COM 		    (MMU_PTOB(pfn_no + 1) - 1 <= count_max))
12920Sstevel@tonic-gate 			continue;
12930Sstevel@tonic-gate 		if ((pfn < pfn_lo) || (prev_pfn > pfn_hi)) {
12940Sstevel@tonic-gate 			ret = DDI_DMA_NOMAPPING;
12950Sstevel@tonic-gate 			goto err;
12960Sstevel@tonic-gate 		}
12970Sstevel@tonic-gate 		cookie_no++;
12980Sstevel@tonic-gate 		pfn_no = 0;
12990Sstevel@tonic-gate 		if (cookie_no < sgllen)
13000Sstevel@tonic-gate 			continue;
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 		DBG(DBG_BYPASS, mp->dmai_rdip, "newwin pfn[%x-%x] %x cks\n",
13038413SDaniel.Ice@Sun.COM 		    win_pfn0_index, i - 1, cookie_no);
13040Sstevel@tonic-gate 		if (ret = px_dma_newwin(dip, dmareq, mp, cookie_no,
13058413SDaniel.Ice@Sun.COM 		    win_pfn0_index, i - 1, win_pp, count_max, bypass_addr))
13060Sstevel@tonic-gate 			goto err;
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate 		win_pp = &(*win_pp)->win_next;	/* win_pp = *(win_pp) */
13090Sstevel@tonic-gate 		win_no++;
13100Sstevel@tonic-gate 		win_pfn0_index = i;
13110Sstevel@tonic-gate 		cookie_no = 0;
13120Sstevel@tonic-gate 	}
13130Sstevel@tonic-gate 	if (pfn > pfn_hi) {
13140Sstevel@tonic-gate 		ret = DDI_DMA_NOMAPPING;
13150Sstevel@tonic-gate 		goto err;
13160Sstevel@tonic-gate 	}
13170Sstevel@tonic-gate 	cookie_no++;
13180Sstevel@tonic-gate 	DBG(DBG_BYPASS, mp->dmai_rdip, "newwin pfn[%x-%x] %x cks\n",
13198413SDaniel.Ice@Sun.COM 	    win_pfn0_index, i - 1, cookie_no);
13200Sstevel@tonic-gate 	if (ret = px_dma_newwin(dip, dmareq, mp, cookie_no, win_pfn0_index,
13218413SDaniel.Ice@Sun.COM 	    i - 1, win_pp, count_max, bypass_addr))
13220Sstevel@tonic-gate 		goto err;
13230Sstevel@tonic-gate 	win_no++;
13240Sstevel@tonic-gate 	px_dma_adjust(dmareq, mp, mp->dmai_winlst);
13250Sstevel@tonic-gate 	mp->dmai_nwin = win_no;
13260Sstevel@tonic-gate 	mp->dmai_rflags |= DDI_DMA_CONSISTENT | DMP_NOSYNC;
13270Sstevel@tonic-gate 	mp->dmai_rflags &= ~DDI_DMA_REDZONE;
1328909Segillett 	mp->dmai_flags |= PX_DMAI_FLAGS_NOSYNC;
1329909Segillett 	cookie0_p = (ddi_dma_cookie_t *)(PX_WINLST(mp) + 1);
1330909Segillett 	mp->dmai_cookie = PX_WINLST(mp)->win_ncookies > 1 ? cookie0_p + 1 : 0;
13310Sstevel@tonic-gate 	mp->dmai_mapping = cookie0_p->dmac_laddress;
13320Sstevel@tonic-gate 
13330Sstevel@tonic-gate 	px_dma_freepfn(mp);
13340Sstevel@tonic-gate 	return (DDI_DMA_MAPPED);
13350Sstevel@tonic-gate err:
13360Sstevel@tonic-gate 	px_dma_freewin(mp);
13370Sstevel@tonic-gate 	return (ret);
13380Sstevel@tonic-gate }
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate int
px_dma_ctl(dev_info_t * dip,dev_info_t * rdip,ddi_dma_impl_t * mp,enum ddi_dma_ctlops cmd,off_t * offp,size_t * lenp,caddr_t * objp,uint_t cache_flags)13410Sstevel@tonic-gate px_dma_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_impl_t *mp,
13420Sstevel@tonic-gate 	enum ddi_dma_ctlops cmd, off_t *offp, size_t *lenp, caddr_t *objp,
13430Sstevel@tonic-gate 	uint_t cache_flags)
13440Sstevel@tonic-gate {
13450Sstevel@tonic-gate 	switch (cmd) {
13460Sstevel@tonic-gate 	case DDI_DMA_SYNC:
13470Sstevel@tonic-gate 		return (DDI_SUCCESS);
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate 	case DDI_DMA_HTOC: {
13500Sstevel@tonic-gate 		off_t off = *offp;
13510Sstevel@tonic-gate 		ddi_dma_cookie_t *loop_cp, *cp;
13520Sstevel@tonic-gate 		px_dma_win_t *win_p = mp->dmai_winlst;
13530Sstevel@tonic-gate 
13540Sstevel@tonic-gate 		if (off >= mp->dmai_object.dmao_size)
13550Sstevel@tonic-gate 			return (DDI_FAILURE);
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate 		/* locate window */
13580Sstevel@tonic-gate 		while (win_p->win_offset + win_p->win_size <= off)
13590Sstevel@tonic-gate 			win_p = win_p->win_next;
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate 		loop_cp = cp = (ddi_dma_cookie_t *)(win_p + 1);
13620Sstevel@tonic-gate 		mp->dmai_offset = win_p->win_offset;
13630Sstevel@tonic-gate 		mp->dmai_size   = win_p->win_size;
13640Sstevel@tonic-gate 		mp->dmai_mapping = cp->dmac_laddress; /* cookie0 start addr */
13650Sstevel@tonic-gate 
13660Sstevel@tonic-gate 		/* adjust cookie addr/len if we are not on cookie boundary */
13670Sstevel@tonic-gate 		off -= win_p->win_offset;	   /* offset within window */
13680Sstevel@tonic-gate 		for (; off >= loop_cp->dmac_size; loop_cp++)
13690Sstevel@tonic-gate 			off -= loop_cp->dmac_size; /* offset within cookie */
13700Sstevel@tonic-gate 
13710Sstevel@tonic-gate 		mp->dmai_cookie = loop_cp + 1;
13720Sstevel@tonic-gate 		win_p->win_curseg = loop_cp - cp;
13730Sstevel@tonic-gate 		cp = (ddi_dma_cookie_t *)objp;
13740Sstevel@tonic-gate 		MAKE_DMA_COOKIE(cp, loop_cp->dmac_laddress + off,
13758413SDaniel.Ice@Sun.COM 		    loop_cp->dmac_size - off);
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate 		DBG(DBG_DMA_CTL, dip,
13788413SDaniel.Ice@Sun.COM 		    "HTOC: cookie - dmac_laddress=%p dmac_size=%x\n",
13798413SDaniel.Ice@Sun.COM 		    cp->dmac_laddress, cp->dmac_size);
13800Sstevel@tonic-gate 		}
13810Sstevel@tonic-gate 		return (DDI_SUCCESS);
13820Sstevel@tonic-gate 
13830Sstevel@tonic-gate 	case DDI_DMA_REPWIN:
13840Sstevel@tonic-gate 		*offp = mp->dmai_offset;
13850Sstevel@tonic-gate 		*lenp = mp->dmai_size;
13860Sstevel@tonic-gate 		return (DDI_SUCCESS);
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate 	case DDI_DMA_MOVWIN: {
13890Sstevel@tonic-gate 		off_t off = *offp;
13900Sstevel@tonic-gate 		ddi_dma_cookie_t *cp;
13910Sstevel@tonic-gate 		px_dma_win_t *win_p = mp->dmai_winlst;
13920Sstevel@tonic-gate 
13930Sstevel@tonic-gate 		if (off >= mp->dmai_object.dmao_size)
13940Sstevel@tonic-gate 			return (DDI_FAILURE);
13950Sstevel@tonic-gate 
13960Sstevel@tonic-gate 		/* locate window */
13970Sstevel@tonic-gate 		while (win_p->win_offset + win_p->win_size <= off)
13980Sstevel@tonic-gate 			win_p = win_p->win_next;
13990Sstevel@tonic-gate 
14000Sstevel@tonic-gate 		cp = (ddi_dma_cookie_t *)(win_p + 1);
14010Sstevel@tonic-gate 		mp->dmai_offset = win_p->win_offset;
14020Sstevel@tonic-gate 		mp->dmai_size   = win_p->win_size;
14030Sstevel@tonic-gate 		mp->dmai_mapping = cp->dmac_laddress;	/* cookie0 star addr */
14040Sstevel@tonic-gate 		mp->dmai_cookie = cp + 1;
14050Sstevel@tonic-gate 		win_p->win_curseg = 0;
14060Sstevel@tonic-gate 
14070Sstevel@tonic-gate 		*(ddi_dma_cookie_t *)objp = *cp;
14080Sstevel@tonic-gate 		*offp = win_p->win_offset;
14090Sstevel@tonic-gate 		*lenp = win_p->win_size;
14100Sstevel@tonic-gate 		DBG(DBG_DMA_CTL, dip,
14118413SDaniel.Ice@Sun.COM 		    "HTOC: cookie - dmac_laddress=%p dmac_size=%x\n",
14128413SDaniel.Ice@Sun.COM 		    cp->dmac_laddress, cp->dmac_size);
14130Sstevel@tonic-gate 		}
14140Sstevel@tonic-gate 		return (DDI_SUCCESS);
14150Sstevel@tonic-gate 
14160Sstevel@tonic-gate 	case DDI_DMA_NEXTWIN: {
14170Sstevel@tonic-gate 		px_dma_win_t *win_p = *(px_dma_win_t **)offp;
14180Sstevel@tonic-gate 		px_dma_win_t **nw_pp = (px_dma_win_t **)objp;
14190Sstevel@tonic-gate 		ddi_dma_cookie_t *cp;
14200Sstevel@tonic-gate 		if (!win_p) {
14210Sstevel@tonic-gate 			*nw_pp = mp->dmai_winlst;
14220Sstevel@tonic-gate 			return (DDI_SUCCESS);
14230Sstevel@tonic-gate 		}
14240Sstevel@tonic-gate 
14250Sstevel@tonic-gate 		if (win_p->win_offset != mp->dmai_offset)
14260Sstevel@tonic-gate 			return (DDI_DMA_STALE);
14270Sstevel@tonic-gate 		if (!win_p->win_next)
14280Sstevel@tonic-gate 			return (DDI_DMA_DONE);
14290Sstevel@tonic-gate 		win_p = win_p->win_next;
14300Sstevel@tonic-gate 		cp = (ddi_dma_cookie_t *)(win_p + 1);
14310Sstevel@tonic-gate 		mp->dmai_offset = win_p->win_offset;
14320Sstevel@tonic-gate 		mp->dmai_size   = win_p->win_size;
14330Sstevel@tonic-gate 		mp->dmai_mapping = cp->dmac_laddress;   /* cookie0 star addr */
14340Sstevel@tonic-gate 		mp->dmai_cookie = cp + 1;
14350Sstevel@tonic-gate 		win_p->win_curseg = 0;
14360Sstevel@tonic-gate 		*nw_pp = win_p;
14370Sstevel@tonic-gate 		}
14380Sstevel@tonic-gate 		return (DDI_SUCCESS);
14390Sstevel@tonic-gate 
14400Sstevel@tonic-gate 	case DDI_DMA_NEXTSEG: {
14410Sstevel@tonic-gate 		px_dma_win_t *w_p = *(px_dma_win_t **)offp;
14420Sstevel@tonic-gate 		if (w_p->win_offset != mp->dmai_offset)
14430Sstevel@tonic-gate 			return (DDI_DMA_STALE);
14440Sstevel@tonic-gate 		if (w_p->win_curseg + 1 >= w_p->win_ncookies)
14450Sstevel@tonic-gate 			return (DDI_DMA_DONE);
14460Sstevel@tonic-gate 		w_p->win_curseg++;
14470Sstevel@tonic-gate 		}
14480Sstevel@tonic-gate 		*(ddi_dma_seg_t *)objp = (ddi_dma_seg_t)mp;
14490Sstevel@tonic-gate 		return (DDI_SUCCESS);
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate 	case DDI_DMA_SEGTOC: {
14520Sstevel@tonic-gate 		px_dma_win_t *win_p = mp->dmai_winlst;
14530Sstevel@tonic-gate 		off_t off = mp->dmai_offset;
14540Sstevel@tonic-gate 		ddi_dma_cookie_t *cp;
14550Sstevel@tonic-gate 		int i;
14560Sstevel@tonic-gate 
14570Sstevel@tonic-gate 		/* locate active window */
14588413SDaniel.Ice@Sun.COM 		for (; win_p->win_offset != off; win_p = win_p->win_next)
14598413SDaniel.Ice@Sun.COM 			;
14600Sstevel@tonic-gate 		cp = (ddi_dma_cookie_t *)(win_p + 1);
14610Sstevel@tonic-gate 		for (i = 0; i < win_p->win_curseg; i++, cp++)
14620Sstevel@tonic-gate 			off += cp->dmac_size;
14630Sstevel@tonic-gate 		*offp = off;
14640Sstevel@tonic-gate 		*lenp = cp->dmac_size;
14650Sstevel@tonic-gate 		*(ddi_dma_cookie_t *)objp = *cp;	/* copy cookie */
14660Sstevel@tonic-gate 		}
14670Sstevel@tonic-gate 		return (DDI_SUCCESS);
14680Sstevel@tonic-gate 
14690Sstevel@tonic-gate 	case DDI_DMA_COFF: {
14700Sstevel@tonic-gate 		px_dma_win_t *win_p;
14710Sstevel@tonic-gate 		ddi_dma_cookie_t *cp;
14720Sstevel@tonic-gate 		uint64_t addr, key = ((ddi_dma_cookie_t *)offp)->dmac_laddress;
14730Sstevel@tonic-gate 		size_t win_off;
14740Sstevel@tonic-gate 
14750Sstevel@tonic-gate 		for (win_p = mp->dmai_winlst; win_p; win_p = win_p->win_next) {
14760Sstevel@tonic-gate 			int i;
14770Sstevel@tonic-gate 			win_off = 0;
14780Sstevel@tonic-gate 			cp = (ddi_dma_cookie_t *)(win_p + 1);
14790Sstevel@tonic-gate 			for (i = 0; i < win_p->win_ncookies; i++, cp++) {
14800Sstevel@tonic-gate 				size_t sz = cp->dmac_size;
14810Sstevel@tonic-gate 
14820Sstevel@tonic-gate 				addr = cp->dmac_laddress;
14830Sstevel@tonic-gate 				if ((addr <= key) && (addr + sz >= key))
14840Sstevel@tonic-gate 					goto found;
14850Sstevel@tonic-gate 				win_off += sz;
14860Sstevel@tonic-gate 			}
14870Sstevel@tonic-gate 		}
14880Sstevel@tonic-gate 		return (DDI_FAILURE);
14890Sstevel@tonic-gate found:
14900Sstevel@tonic-gate 		*objp = (caddr_t)(win_p->win_offset + win_off + (key - addr));
14910Sstevel@tonic-gate 		return (DDI_SUCCESS);
14920Sstevel@tonic-gate 		}
14930Sstevel@tonic-gate 	default:
14940Sstevel@tonic-gate 		DBG(DBG_DMA_CTL, dip, "unknown command (%x): rdip=%s%d\n",
14958413SDaniel.Ice@Sun.COM 		    cmd, ddi_driver_name(rdip), ddi_get_instance(rdip));
14960Sstevel@tonic-gate 		break;
14970Sstevel@tonic-gate 	}
14980Sstevel@tonic-gate 	return (DDI_FAILURE);
14990Sstevel@tonic-gate }
15000Sstevel@tonic-gate 
15010Sstevel@tonic-gate static void
px_dvma_debug_init(px_mmu_t * mmu_p)15020Sstevel@tonic-gate px_dvma_debug_init(px_mmu_t *mmu_p)
15030Sstevel@tonic-gate {
15040Sstevel@tonic-gate 	size_t sz = sizeof (struct px_dvma_rec) * px_dvma_debug_rec;
15050Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&mmu_p->dvma_debug_lock));
15060Sstevel@tonic-gate 	cmn_err(CE_NOTE, "PCI Express DVMA %p stat ON", mmu_p);
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate 	mmu_p->dvma_alloc_rec = kmem_alloc(sz, KM_SLEEP);
15090Sstevel@tonic-gate 	mmu_p->dvma_free_rec = kmem_alloc(sz, KM_SLEEP);
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate 	mmu_p->dvma_active_list = NULL;
15120Sstevel@tonic-gate 	mmu_p->dvma_alloc_rec_index = 0;
15130Sstevel@tonic-gate 	mmu_p->dvma_free_rec_index = 0;
15140Sstevel@tonic-gate 	mmu_p->dvma_active_count = 0;
15150Sstevel@tonic-gate }
15160Sstevel@tonic-gate 
15170Sstevel@tonic-gate void
px_dvma_debug_fini(px_mmu_t * mmu_p)15180Sstevel@tonic-gate px_dvma_debug_fini(px_mmu_t *mmu_p)
15190Sstevel@tonic-gate {
15200Sstevel@tonic-gate 	struct px_dvma_rec *prev, *ptr;
15210Sstevel@tonic-gate 	size_t sz = sizeof (struct px_dvma_rec) * px_dvma_debug_rec;
15220Sstevel@tonic-gate 	uint64_t mask = ~(1ull << mmu_p->mmu_inst);
15230Sstevel@tonic-gate 	cmn_err(CE_NOTE, "PCI Express DVMA %p stat OFF", mmu_p);
15240Sstevel@tonic-gate 
15255328Sdanice 	if (mmu_p->dvma_alloc_rec) {
15265328Sdanice 		kmem_free(mmu_p->dvma_alloc_rec, sz);
15275328Sdanice 		mmu_p->dvma_alloc_rec = NULL;
15285328Sdanice 	}
15295328Sdanice 	if (mmu_p->dvma_free_rec) {
15305328Sdanice 		kmem_free(mmu_p->dvma_free_rec, sz);
15315328Sdanice 		mmu_p->dvma_free_rec = NULL;
15325328Sdanice 	}
15330Sstevel@tonic-gate 
15340Sstevel@tonic-gate 	prev = mmu_p->dvma_active_list;
15350Sstevel@tonic-gate 	if (!prev)
15360Sstevel@tonic-gate 		return;
15370Sstevel@tonic-gate 	for (ptr = prev->next; ptr; prev = ptr, ptr = ptr->next)
15380Sstevel@tonic-gate 		kmem_free(prev, sizeof (struct px_dvma_rec));
15390Sstevel@tonic-gate 	kmem_free(prev, sizeof (struct px_dvma_rec));
15400Sstevel@tonic-gate 
15410Sstevel@tonic-gate 	mmu_p->dvma_active_list = NULL;
15420Sstevel@tonic-gate 	mmu_p->dvma_alloc_rec_index = 0;
15430Sstevel@tonic-gate 	mmu_p->dvma_free_rec_index = 0;
15440Sstevel@tonic-gate 	mmu_p->dvma_active_count = 0;
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate 	px_dvma_debug_off &= mask;
15470Sstevel@tonic-gate 	px_dvma_debug_on &= mask;
15480Sstevel@tonic-gate }
15490Sstevel@tonic-gate 
15500Sstevel@tonic-gate void
px_dvma_alloc_debug(px_mmu_t * mmu_p,char * address,uint_t len,ddi_dma_impl_t * mp)15510Sstevel@tonic-gate px_dvma_alloc_debug(px_mmu_t *mmu_p, char *address, uint_t len,
15520Sstevel@tonic-gate 	ddi_dma_impl_t *mp)
15530Sstevel@tonic-gate {
15540Sstevel@tonic-gate 	struct px_dvma_rec *ptr;
15550Sstevel@tonic-gate 	mutex_enter(&mmu_p->dvma_debug_lock);
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate 	if (!mmu_p->dvma_alloc_rec)
15580Sstevel@tonic-gate 		px_dvma_debug_init(mmu_p);
1559909Segillett 	if (PX_DVMA_DBG_OFF(mmu_p)) {
15600Sstevel@tonic-gate 		px_dvma_debug_fini(mmu_p);
15610Sstevel@tonic-gate 		goto done;
15620Sstevel@tonic-gate 	}
15630Sstevel@tonic-gate 
15640Sstevel@tonic-gate 	ptr = &mmu_p->dvma_alloc_rec[mmu_p->dvma_alloc_rec_index];
15650Sstevel@tonic-gate 	ptr->dvma_addr = address;
15660Sstevel@tonic-gate 	ptr->len = len;
15670Sstevel@tonic-gate 	ptr->mp = mp;
15680Sstevel@tonic-gate 	if (++mmu_p->dvma_alloc_rec_index == px_dvma_debug_rec)
15690Sstevel@tonic-gate 		mmu_p->dvma_alloc_rec_index = 0;
15700Sstevel@tonic-gate 
15710Sstevel@tonic-gate 	ptr = kmem_alloc(sizeof (struct px_dvma_rec), KM_SLEEP);
15720Sstevel@tonic-gate 	ptr->dvma_addr = address;
15730Sstevel@tonic-gate 	ptr->len = len;
15740Sstevel@tonic-gate 	ptr->mp = mp;
15750Sstevel@tonic-gate 
15760Sstevel@tonic-gate 	ptr->next = mmu_p->dvma_active_list;
15770Sstevel@tonic-gate 	mmu_p->dvma_active_list = ptr;
15780Sstevel@tonic-gate 	mmu_p->dvma_active_count++;
15790Sstevel@tonic-gate done:
15800Sstevel@tonic-gate 	mutex_exit(&mmu_p->dvma_debug_lock);
15810Sstevel@tonic-gate }
15820Sstevel@tonic-gate 
15830Sstevel@tonic-gate void
px_dvma_free_debug(px_mmu_t * mmu_p,char * address,uint_t len,ddi_dma_impl_t * mp)15840Sstevel@tonic-gate px_dvma_free_debug(px_mmu_t *mmu_p, char *address, uint_t len,
15850Sstevel@tonic-gate     ddi_dma_impl_t *mp)
15860Sstevel@tonic-gate {
15870Sstevel@tonic-gate 	struct px_dvma_rec *ptr, *ptr_save;
15880Sstevel@tonic-gate 	mutex_enter(&mmu_p->dvma_debug_lock);
15890Sstevel@tonic-gate 
15900Sstevel@tonic-gate 	if (!mmu_p->dvma_alloc_rec)
15910Sstevel@tonic-gate 		px_dvma_debug_init(mmu_p);
1592909Segillett 	if (PX_DVMA_DBG_OFF(mmu_p)) {
15930Sstevel@tonic-gate 		px_dvma_debug_fini(mmu_p);
15940Sstevel@tonic-gate 		goto done;
15950Sstevel@tonic-gate 	}
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate 	ptr = &mmu_p->dvma_free_rec[mmu_p->dvma_free_rec_index];
15980Sstevel@tonic-gate 	ptr->dvma_addr = address;
15990Sstevel@tonic-gate 	ptr->len = len;
16000Sstevel@tonic-gate 	ptr->mp = mp;
16010Sstevel@tonic-gate 	if (++mmu_p->dvma_free_rec_index == px_dvma_debug_rec)
16020Sstevel@tonic-gate 		mmu_p->dvma_free_rec_index = 0;
16030Sstevel@tonic-gate 
16040Sstevel@tonic-gate 	ptr_save = mmu_p->dvma_active_list;
16050Sstevel@tonic-gate 	for (ptr = ptr_save; ptr; ptr = ptr->next) {
16060Sstevel@tonic-gate 		if ((ptr->dvma_addr == address) && (ptr->len = len))
16070Sstevel@tonic-gate 			break;
16080Sstevel@tonic-gate 		ptr_save = ptr;
16090Sstevel@tonic-gate 	}
16100Sstevel@tonic-gate 	if (!ptr) {
16110Sstevel@tonic-gate 		cmn_err(CE_WARN, "bad dvma free addr=%lx len=%x",
16128413SDaniel.Ice@Sun.COM 		    (long)address, len);
16130Sstevel@tonic-gate 		goto done;
16140Sstevel@tonic-gate 	}
16150Sstevel@tonic-gate 	if (ptr == mmu_p->dvma_active_list)
16160Sstevel@tonic-gate 		mmu_p->dvma_active_list = ptr->next;
16170Sstevel@tonic-gate 	else
16180Sstevel@tonic-gate 		ptr_save->next = ptr->next;
16190Sstevel@tonic-gate 	kmem_free(ptr, sizeof (struct px_dvma_rec));
16200Sstevel@tonic-gate 	mmu_p->dvma_active_count--;
16210Sstevel@tonic-gate done:
16220Sstevel@tonic-gate 	mutex_exit(&mmu_p->dvma_debug_lock);
16230Sstevel@tonic-gate }
16240Sstevel@tonic-gate 
16250Sstevel@tonic-gate #ifdef	DEBUG
16260Sstevel@tonic-gate void
px_dump_dma_handle(uint64_t flag,dev_info_t * dip,ddi_dma_impl_t * hp)16270Sstevel@tonic-gate px_dump_dma_handle(uint64_t flag, dev_info_t *dip, ddi_dma_impl_t *hp)
16280Sstevel@tonic-gate {
16290Sstevel@tonic-gate 	DBG(flag, dip, "mp(%p): flags=%x mapping=%lx xfer_size=%x\n",
16308413SDaniel.Ice@Sun.COM 	    hp, hp->dmai_inuse, hp->dmai_mapping, hp->dmai_size);
16310Sstevel@tonic-gate 	DBG(flag|DBG_CONT, dip, "\tnpages=%x roffset=%x rflags=%x nwin=%x\n",
16328413SDaniel.Ice@Sun.COM 	    hp->dmai_ndvmapages, hp->dmai_roffset, hp->dmai_rflags,
16338413SDaniel.Ice@Sun.COM 	    hp->dmai_nwin);
16340Sstevel@tonic-gate 	DBG(flag|DBG_CONT, dip, "\twinsize=%x tte=%p pfnlst=%p pfn0=%p\n",
16358413SDaniel.Ice@Sun.COM 	    hp->dmai_winsize, hp->dmai_tte, hp->dmai_pfnlst, hp->dmai_pfn0);
16360Sstevel@tonic-gate 	DBG(flag|DBG_CONT, dip, "\twinlst=%x obj=%p attr=%p ckp=%p\n",
16378413SDaniel.Ice@Sun.COM 	    hp->dmai_winlst, &hp->dmai_object, &hp->dmai_attr,
16388413SDaniel.Ice@Sun.COM 	    hp->dmai_cookie);
16390Sstevel@tonic-gate }
16400Sstevel@tonic-gate #endif	/* DEBUG */
1641