xref: /onnv-gate/usr/src/uts/sun4/io/px/px_dma.c (revision 1772:78cca3d2cc4b)
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 /*
221501Sgovinda  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * PCI Express nexus DVMA and DMA core routines:
300Sstevel@tonic-gate  *	dma_map/dma_bind_handle implementation
310Sstevel@tonic-gate  *	bypass and peer-to-peer support
320Sstevel@tonic-gate  *	fast track DVMA space allocation
330Sstevel@tonic-gate  *	runtime DVMA debug
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate #include <sys/types.h>
360Sstevel@tonic-gate #include <sys/kmem.h>
370Sstevel@tonic-gate #include <sys/async.h>
380Sstevel@tonic-gate #include <sys/sysmacros.h>
390Sstevel@tonic-gate #include <sys/sunddi.h>
400Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
410Sstevel@tonic-gate #include "px_obj.h"
420Sstevel@tonic-gate 
430Sstevel@tonic-gate /*LINTLIBRARY*/
440Sstevel@tonic-gate 
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate  * px_dma_allocmp - Allocate a pci dma implementation structure
470Sstevel@tonic-gate  *
480Sstevel@tonic-gate  * An extra ddi_dma_attr structure is bundled with the usual ddi_dma_impl
490Sstevel@tonic-gate  * to hold unmodified device limits. The ddi_dma_attr inside the
500Sstevel@tonic-gate  * ddi_dma_impl structure is augumented with system limits to enhance
510Sstevel@tonic-gate  * DVMA performance at runtime. The unaugumented device limits saved
520Sstevel@tonic-gate  * right after (accessed through (ddi_dma_attr_t *)(mp + 1)) is used
530Sstevel@tonic-gate  * strictly for peer-to-peer transfers which do not obey system limits.
540Sstevel@tonic-gate  *
550Sstevel@tonic-gate  * return: DDI_SUCCESS DDI_DMA_NORESOURCES
560Sstevel@tonic-gate  */
570Sstevel@tonic-gate ddi_dma_impl_t *
580Sstevel@tonic-gate px_dma_allocmp(dev_info_t *dip, dev_info_t *rdip, int (*waitfp)(caddr_t),
590Sstevel@tonic-gate 	caddr_t arg)
600Sstevel@tonic-gate {
610Sstevel@tonic-gate 	register ddi_dma_impl_t *mp;
620Sstevel@tonic-gate 	int sleep = (waitfp == DDI_DMA_SLEEP) ? KM_SLEEP : KM_NOSLEEP;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 	/* Caution: we don't use zalloc to enhance performance! */
650Sstevel@tonic-gate 	if ((mp = kmem_alloc(sizeof (px_dma_hdl_t), sleep)) == 0) {
660Sstevel@tonic-gate 		DBG(DBG_DMA_MAP, dip, "can't alloc dma_handle\n");
670Sstevel@tonic-gate 		if (waitfp != DDI_DMA_DONTWAIT) {
680Sstevel@tonic-gate 			DBG(DBG_DMA_MAP, dip, "alloc_mp kmem cb\n");
690Sstevel@tonic-gate 			ddi_set_callback(waitfp, arg, &px_kmem_clid);
700Sstevel@tonic-gate 		}
710Sstevel@tonic-gate 		return (mp);
720Sstevel@tonic-gate 	}
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 	mp->dmai_rdip = rdip;
750Sstevel@tonic-gate 	mp->dmai_flags = 0;
760Sstevel@tonic-gate 	mp->dmai_pfnlst = NULL;
770Sstevel@tonic-gate 	mp->dmai_winlst = NULL;
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	/*
800Sstevel@tonic-gate 	 * kmem_alloc debug: the following fields are not zero-ed
810Sstevel@tonic-gate 	 * mp->dmai_mapping = 0;
820Sstevel@tonic-gate 	 * mp->dmai_size = 0;
830Sstevel@tonic-gate 	 * mp->dmai_offset = 0;
840Sstevel@tonic-gate 	 * mp->dmai_minxfer = 0;
850Sstevel@tonic-gate 	 * mp->dmai_burstsizes = 0;
860Sstevel@tonic-gate 	 * mp->dmai_ndvmapages = 0;
870Sstevel@tonic-gate 	 * mp->dmai_pool/roffset = 0;
880Sstevel@tonic-gate 	 * mp->dmai_rflags = 0;
890Sstevel@tonic-gate 	 * mp->dmai_inuse/flags
900Sstevel@tonic-gate 	 * mp->dmai_nwin = 0;
910Sstevel@tonic-gate 	 * mp->dmai_winsize = 0;
920Sstevel@tonic-gate 	 * mp->dmai_nexus_private/tte = 0;
930Sstevel@tonic-gate 	 * mp->dmai_iopte/pfnlst
940Sstevel@tonic-gate 	 * mp->dmai_sbi/pfn0 = 0;
950Sstevel@tonic-gate 	 * mp->dmai_minfo/winlst/fdvma
960Sstevel@tonic-gate 	 * mp->dmai_rdip
970Sstevel@tonic-gate 	 * bzero(&mp->dmai_object, sizeof (ddi_dma_obj_t));
980Sstevel@tonic-gate 	 * bzero(&mp->dmai_attr, sizeof (ddi_dma_attr_t));
990Sstevel@tonic-gate 	 * mp->dmai_cookie = 0;
1000Sstevel@tonic-gate 	 */
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	mp->dmai_attr.dma_attr_version = (uint_t)DMA_ATTR_VERSION;
1030Sstevel@tonic-gate 	mp->dmai_attr.dma_attr_flags = (uint_t)0;
1040Sstevel@tonic-gate 	mp->dmai_fault = 0;
1050Sstevel@tonic-gate 	mp->dmai_fault_check = NULL;
1060Sstevel@tonic-gate 	mp->dmai_fault_notify = NULL;
107758Svgadre 
108758Svgadre 	mp->dmai_error.err_ena = 0;
109758Svgadre 	mp->dmai_error.err_status = DDI_FM_OK;
110758Svgadre 	mp->dmai_error.err_expected = DDI_FM_ERR_UNEXPECTED;
111758Svgadre 	mp->dmai_error.err_ontrap = NULL;
112758Svgadre 	mp->dmai_error.err_fep = NULL;
113758Svgadre 
1141501Sgovinda 	if (px_child_prefetch(mp->dmai_rdip))
1151501Sgovinda 		mp->dmai_flags |= (PX_DMAI_FLAGS_MAP_BUFZONE |
1161501Sgovinda 		    PX_DMAI_FLAGS_REDZONE);
1171501Sgovinda 
1180Sstevel@tonic-gate 	return (mp);
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate void
1220Sstevel@tonic-gate px_dma_freemp(ddi_dma_impl_t *mp)
1230Sstevel@tonic-gate {
1240Sstevel@tonic-gate 	if (mp->dmai_ndvmapages > 1)
1250Sstevel@tonic-gate 		px_dma_freepfn(mp);
1260Sstevel@tonic-gate 	if (mp->dmai_winlst)
1270Sstevel@tonic-gate 		px_dma_freewin(mp);
1280Sstevel@tonic-gate 	kmem_free(mp, sizeof (px_dma_hdl_t));
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate void
1320Sstevel@tonic-gate px_dma_freepfn(ddi_dma_impl_t *mp)
1330Sstevel@tonic-gate {
1340Sstevel@tonic-gate 	void *addr = mp->dmai_pfnlst;
1350Sstevel@tonic-gate 	if (addr) {
1360Sstevel@tonic-gate 		size_t npages = mp->dmai_ndvmapages;
1370Sstevel@tonic-gate 		if (npages > 1)
1380Sstevel@tonic-gate 			kmem_free(addr, npages * sizeof (px_iopfn_t));
1390Sstevel@tonic-gate 		mp->dmai_pfnlst = NULL;
1400Sstevel@tonic-gate 	}
1410Sstevel@tonic-gate 	mp->dmai_ndvmapages = 0;
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate /*
1450Sstevel@tonic-gate  * px_dma_lmts2hdl - alloate a ddi_dma_impl_t, validate practical limits
1460Sstevel@tonic-gate  *			and convert dmareq->dmar_limits to mp->dmai_attr
1470Sstevel@tonic-gate  *
1480Sstevel@tonic-gate  * ddi_dma_impl_t member modified     input
1490Sstevel@tonic-gate  * ------------------------------------------------------------------------
1500Sstevel@tonic-gate  * mp->dmai_minxfer		    - dev
1510Sstevel@tonic-gate  * mp->dmai_burstsizes		    - dev
1520Sstevel@tonic-gate  * mp->dmai_flags		    - no limit? peer-to-peer only?
1530Sstevel@tonic-gate  *
1540Sstevel@tonic-gate  * ddi_dma_attr member modified       input
1550Sstevel@tonic-gate  * ------------------------------------------------------------------------
1560Sstevel@tonic-gate  * mp->dmai_attr.dma_attr_addr_lo   - dev lo, sys lo
1570Sstevel@tonic-gate  * mp->dmai_attr.dma_attr_addr_hi   - dev hi, sys hi
1580Sstevel@tonic-gate  * mp->dmai_attr.dma_attr_count_max - dev count max, dev/sys lo/hi delta
1590Sstevel@tonic-gate  * mp->dmai_attr.dma_attr_seg       - 0         (no nocross   restriction)
1600Sstevel@tonic-gate  * mp->dmai_attr.dma_attr_align     - 1         (no alignment restriction)
1610Sstevel@tonic-gate  *
1620Sstevel@tonic-gate  * The dlim_dmaspeed member of dmareq->dmar_limits is ignored.
1630Sstevel@tonic-gate  */
1640Sstevel@tonic-gate ddi_dma_impl_t *
1650Sstevel@tonic-gate px_dma_lmts2hdl(dev_info_t *dip, dev_info_t *rdip, px_mmu_t *mmu_p,
1660Sstevel@tonic-gate 	ddi_dma_req_t *dmareq)
1670Sstevel@tonic-gate {
1680Sstevel@tonic-gate 	ddi_dma_impl_t *mp;
1690Sstevel@tonic-gate 	ddi_dma_attr_t *attr_p;
1700Sstevel@tonic-gate 	uint64_t syslo		= mmu_p->mmu_dvma_base;
1710Sstevel@tonic-gate 	uint64_t syshi		= mmu_p->mmu_dvma_end;
1720Sstevel@tonic-gate 	uint64_t fasthi		= mmu_p->mmu_dvma_fast_end;
1730Sstevel@tonic-gate 	ddi_dma_lim_t *lim_p	= dmareq->dmar_limits;
1740Sstevel@tonic-gate 	uint32_t count_max	= lim_p->dlim_cntr_max;
1750Sstevel@tonic-gate 	uint64_t lo		= lim_p->dlim_addr_lo;
1760Sstevel@tonic-gate 	uint64_t hi		= lim_p->dlim_addr_hi;
1770Sstevel@tonic-gate 	if (hi <= lo) {
1780Sstevel@tonic-gate 		DBG(DBG_DMA_MAP, dip, "Bad limits\n");
1790Sstevel@tonic-gate 		return ((ddi_dma_impl_t *)DDI_DMA_NOMAPPING);
1800Sstevel@tonic-gate 	}
1810Sstevel@tonic-gate 	if (!count_max)
1820Sstevel@tonic-gate 		count_max--;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	if (!(mp = px_dma_allocmp(dip, rdip, dmareq->dmar_fp,
1850Sstevel@tonic-gate 		dmareq->dmar_arg)))
1860Sstevel@tonic-gate 		return (NULL);
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	/* store original dev input at the 2nd ddi_dma_attr */
189909Segillett 	attr_p = PX_DEV_ATTR(mp);
1900Sstevel@tonic-gate 	SET_DMAATTR(attr_p, lo, hi, -1, count_max);
1910Sstevel@tonic-gate 	SET_DMAALIGN(attr_p, 1);
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	lo = MAX(lo, syslo);
1940Sstevel@tonic-gate 	hi = MIN(hi, syshi);
1950Sstevel@tonic-gate 	if (hi <= lo)
196909Segillett 		mp->dmai_flags |= PX_DMAI_FLAGS_PEER_ONLY;
1970Sstevel@tonic-gate 	count_max = MIN(count_max, hi - lo);
1980Sstevel@tonic-gate 
199909Segillett 	if (PX_DEV_NOSYSLIMIT(lo, hi, syslo, fasthi, 1))
200909Segillett 		mp->dmai_flags |= PX_DMAI_FLAGS_NOFASTLIMIT |
201909Segillett 			PX_DMAI_FLAGS_NOSYSLIMIT;
2020Sstevel@tonic-gate 	else {
203909Segillett 		if (PX_DEV_NOFASTLIMIT(lo, hi, syslo, syshi, 1))
204909Segillett 			mp->dmai_flags |= PX_DMAI_FLAGS_NOFASTLIMIT;
2050Sstevel@tonic-gate 	}
2060Sstevel@tonic-gate 	if (PX_DMA_NOCTX(rdip))
207909Segillett 		mp->dmai_flags |= PX_DMAI_FLAGS_NOCTX;
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	/* store augumented dev input to mp->dmai_attr */
2100Sstevel@tonic-gate 	mp->dmai_minxfer	= lim_p->dlim_minxfer;
2110Sstevel@tonic-gate 	mp->dmai_burstsizes	= lim_p->dlim_burstsizes;
2120Sstevel@tonic-gate 	attr_p = &mp->dmai_attr;
2130Sstevel@tonic-gate 	SET_DMAATTR(attr_p, lo, hi, -1, count_max);
2140Sstevel@tonic-gate 	SET_DMAALIGN(attr_p, 1);
2150Sstevel@tonic-gate 	return (mp);
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate /*
2190Sstevel@tonic-gate  * Called from px_attach to check for bypass dma support and set
2200Sstevel@tonic-gate  * flags accordingly.
2210Sstevel@tonic-gate  */
2220Sstevel@tonic-gate int
2230Sstevel@tonic-gate px_dma_attach(px_t *px_p)
2240Sstevel@tonic-gate {
2250Sstevel@tonic-gate 	uint64_t baddr;
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	if (px_lib_iommu_getbypass(px_p->px_dip, 0ull,
2280Sstevel@tonic-gate 			PCI_MAP_ATTR_WRITE|PCI_MAP_ATTR_READ,
2290Sstevel@tonic-gate 			&baddr) != DDI_ENOTSUP)
2300Sstevel@tonic-gate 		/* ignore all other errors */
2311531Skini 		px_p->px_dev_caps |= PX_BYPASS_DMA_ALLOWED;
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	return (DDI_SUCCESS);
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate /*
2370Sstevel@tonic-gate  * px_dma_attr2hdl
2380Sstevel@tonic-gate  *
2390Sstevel@tonic-gate  * This routine is called from the alloc handle entry point to sanity check the
2400Sstevel@tonic-gate  * dma attribute structure.
2410Sstevel@tonic-gate  *
2420Sstevel@tonic-gate  * use by: px_dma_allochdl()
2430Sstevel@tonic-gate  *
2440Sstevel@tonic-gate  * return value:
2450Sstevel@tonic-gate  *
2460Sstevel@tonic-gate  *	DDI_SUCCESS		- on success
2470Sstevel@tonic-gate  *	DDI_DMA_BADATTR		- attribute has invalid version number
2480Sstevel@tonic-gate  *				  or address limits exclude dvma space
2490Sstevel@tonic-gate  */
2500Sstevel@tonic-gate int
2510Sstevel@tonic-gate px_dma_attr2hdl(px_t *px_p, ddi_dma_impl_t *mp)
2520Sstevel@tonic-gate {
2530Sstevel@tonic-gate 	px_mmu_t *mmu_p = px_p->px_mmu_p;
2540Sstevel@tonic-gate 	uint64_t syslo, syshi;
2550Sstevel@tonic-gate 	int	ret;
256909Segillett 	ddi_dma_attr_t *attrp		= PX_DEV_ATTR(mp);
2570Sstevel@tonic-gate 	uint64_t hi			= attrp->dma_attr_addr_hi;
2580Sstevel@tonic-gate 	uint64_t lo			= attrp->dma_attr_addr_lo;
2590Sstevel@tonic-gate 	uint64_t align			= attrp->dma_attr_align;
2600Sstevel@tonic-gate 	uint64_t nocross		= attrp->dma_attr_seg;
2610Sstevel@tonic-gate 	uint64_t count_max		= attrp->dma_attr_count_max;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	DBG(DBG_DMA_ALLOCH, px_p->px_dip, "attrp=%p cntr_max=%x.%08x\n",
2640Sstevel@tonic-gate 		attrp, HI32(count_max), LO32(count_max));
2650Sstevel@tonic-gate 	DBG(DBG_DMA_ALLOCH, px_p->px_dip, "hi=%x.%08x lo=%x.%08x\n",
2660Sstevel@tonic-gate 		HI32(hi), LO32(hi), HI32(lo), LO32(lo));
2670Sstevel@tonic-gate 	DBG(DBG_DMA_ALLOCH, px_p->px_dip, "seg=%x.%08x align=%x.%08x\n",
2680Sstevel@tonic-gate 		HI32(nocross), LO32(nocross), HI32(align), LO32(align));
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	if (!nocross)
2710Sstevel@tonic-gate 		nocross--;
2720Sstevel@tonic-gate 	if (attrp->dma_attr_flags & DDI_DMA_FORCE_PHYSICAL) { /* BYPASS */
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 		DBG(DBG_DMA_ALLOCH, px_p->px_dip, "bypass mode\n");
2750Sstevel@tonic-gate 		/*
2760Sstevel@tonic-gate 		 * If Bypass DMA is not supported, return error so that
2770Sstevel@tonic-gate 		 * target driver can fall back to dvma mode of operation
2780Sstevel@tonic-gate 		 */
2791531Skini 		if (!(px_p->px_dev_caps & PX_BYPASS_DMA_ALLOWED))
2800Sstevel@tonic-gate 			return (DDI_DMA_BADATTR);
281909Segillett 		mp->dmai_flags |= PX_DMAI_FLAGS_BYPASSREQ;
2820Sstevel@tonic-gate 		if (nocross != UINT64_MAX)
2830Sstevel@tonic-gate 			return (DDI_DMA_BADATTR);
2840Sstevel@tonic-gate 		if (align && (align > MMU_PAGE_SIZE))
2850Sstevel@tonic-gate 			return (DDI_DMA_BADATTR);
2860Sstevel@tonic-gate 		align = 1; /* align on 1 page boundary */
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 		/* do a range check and get the limits */
289*1772Sjl139090 		ret = px_lib_dma_bypass_rngchk(px_p->px_dip, attrp,
290*1772Sjl139090 				&syslo, &syshi);
2910Sstevel@tonic-gate 		if (ret != DDI_SUCCESS)
2920Sstevel@tonic-gate 			return (ret);
2930Sstevel@tonic-gate 	} else { /* MMU_XLATE or PEER_TO_PEER */
2940Sstevel@tonic-gate 		align = MAX(align, MMU_PAGE_SIZE) - 1;
2950Sstevel@tonic-gate 		if ((align & nocross) != align) {
2960Sstevel@tonic-gate 			dev_info_t *rdip = mp->dmai_rdip;
2970Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d dma_attr_seg not aligned",
2980Sstevel@tonic-gate 				NAMEINST(rdip));
2990Sstevel@tonic-gate 			return (DDI_DMA_BADATTR);
3000Sstevel@tonic-gate 		}
3010Sstevel@tonic-gate 		align = MMU_BTOP(align + 1);
3020Sstevel@tonic-gate 		syslo = mmu_p->mmu_dvma_base;
3030Sstevel@tonic-gate 		syshi = mmu_p->mmu_dvma_end;
3040Sstevel@tonic-gate 	}
3050Sstevel@tonic-gate 	if (hi <= lo) {
3060Sstevel@tonic-gate 		dev_info_t *rdip = mp->dmai_rdip;
3070Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d limits out of range", NAMEINST(rdip));
3080Sstevel@tonic-gate 		return (DDI_DMA_BADATTR);
3090Sstevel@tonic-gate 	}
3100Sstevel@tonic-gate 	lo = MAX(lo, syslo);
3110Sstevel@tonic-gate 	hi = MIN(hi, syshi);
3120Sstevel@tonic-gate 	if (!count_max)
3130Sstevel@tonic-gate 		count_max--;
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	DBG(DBG_DMA_ALLOCH, px_p->px_dip, "hi=%x.%08x, lo=%x.%08x\n",
3160Sstevel@tonic-gate 		HI32(hi), LO32(hi), HI32(lo), LO32(lo));
3170Sstevel@tonic-gate 	if (hi <= lo) { /* peer transfers cannot have alignment & nocross */
3180Sstevel@tonic-gate 		dev_info_t *rdip = mp->dmai_rdip;
3190Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d peer only dev %p", NAMEINST(rdip), mp);
3200Sstevel@tonic-gate 		if ((nocross < UINT32_MAX) || (align > 1)) {
3210Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d peer only device bad attr",
3220Sstevel@tonic-gate 				NAMEINST(rdip));
3230Sstevel@tonic-gate 			return (DDI_DMA_BADATTR);
3240Sstevel@tonic-gate 		}
325909Segillett 		mp->dmai_flags |= PX_DMAI_FLAGS_PEER_ONLY;
3260Sstevel@tonic-gate 	} else /* set practical counter_max value */
3270Sstevel@tonic-gate 		count_max = MIN(count_max, hi - lo);
3280Sstevel@tonic-gate 
329909Segillett 	if (PX_DEV_NOSYSLIMIT(lo, hi, syslo, syshi, align))
330909Segillett 		mp->dmai_flags |= PX_DMAI_FLAGS_NOSYSLIMIT |
331909Segillett 			PX_DMAI_FLAGS_NOFASTLIMIT;
3320Sstevel@tonic-gate 	else {
3330Sstevel@tonic-gate 		syshi = mmu_p->mmu_dvma_fast_end;
334909Segillett 		if (PX_DEV_NOFASTLIMIT(lo, hi, syslo, syshi, align))
335909Segillett 			mp->dmai_flags |= PX_DMAI_FLAGS_NOFASTLIMIT;
3360Sstevel@tonic-gate 	}
3370Sstevel@tonic-gate 	if (PX_DMA_NOCTX(mp->dmai_rdip))
338909Segillett 		mp->dmai_flags |= PX_DMAI_FLAGS_NOCTX;
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	mp->dmai_minxfer	= attrp->dma_attr_minxfer;
3410Sstevel@tonic-gate 	mp->dmai_burstsizes	= attrp->dma_attr_burstsizes;
3420Sstevel@tonic-gate 	attrp = &mp->dmai_attr;
3430Sstevel@tonic-gate 	SET_DMAATTR(attrp, lo, hi, nocross, count_max);
3440Sstevel@tonic-gate 	return (DDI_SUCCESS);
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate #define	TGT_PFN_INBETWEEN(pfn, bgn, end) ((pfn >= bgn) && (pfn <= end))
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate /*
3500Sstevel@tonic-gate  * px_dma_type - determine which of the three types DMA (peer-to-peer,
3510Sstevel@tonic-gate  *		mmu bypass, or mmu translate) we are asked to do.
3520Sstevel@tonic-gate  *		Also checks pfn0 and rejects any non-peer-to-peer
3530Sstevel@tonic-gate  *		requests for peer-only devices.
3540Sstevel@tonic-gate  *
3550Sstevel@tonic-gate  *	return values:
3560Sstevel@tonic-gate  *		DDI_DMA_NOMAPPING - can't get valid pfn0, or bad dma type
3570Sstevel@tonic-gate  *		DDI_SUCCESS
3580Sstevel@tonic-gate  *
3590Sstevel@tonic-gate  *	dma handle members affected (set on exit):
3600Sstevel@tonic-gate  *	mp->dmai_object		- dmareq->dmar_object
3610Sstevel@tonic-gate  *	mp->dmai_rflags		- consistent?, nosync?, dmareq->dmar_flags
3620Sstevel@tonic-gate  *	mp->dmai_flags   	- DMA type
3630Sstevel@tonic-gate  *	mp->dmai_pfn0   	- 1st page pfn (if va/size pair and not shadow)
3640Sstevel@tonic-gate  *	mp->dmai_roffset 	- initialized to starting MMU page offset
3650Sstevel@tonic-gate  *	mp->dmai_ndvmapages	- # of total MMU pages of entire object
3660Sstevel@tonic-gate  */
3670Sstevel@tonic-gate int
3680Sstevel@tonic-gate px_dma_type(px_t *px_p, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp)
3690Sstevel@tonic-gate {
3700Sstevel@tonic-gate 	dev_info_t *dip = px_p->px_dip;
3710Sstevel@tonic-gate 	ddi_dma_obj_t *dobj_p = &dmareq->dmar_object;
3720Sstevel@tonic-gate 	px_pec_t *pec_p = px_p->px_pec_p;
3730Sstevel@tonic-gate 	uint32_t offset;
3740Sstevel@tonic-gate 	pfn_t pfn0;
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	mp->dmai_rflags = dmareq->dmar_flags & DMP_DDIFLAGS | DMP_NOSYNC;
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	switch (dobj_p->dmao_type) {
3790Sstevel@tonic-gate 	case DMA_OTYP_BUFVADDR:
3800Sstevel@tonic-gate 	case DMA_OTYP_VADDR: {
3810Sstevel@tonic-gate 		page_t **pplist = dobj_p->dmao_obj.virt_obj.v_priv;
3820Sstevel@tonic-gate 		caddr_t vaddr = dobj_p->dmao_obj.virt_obj.v_addr;
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 		DBG(DBG_DMA_MAP, dip, "vaddr=%p pplist=%p\n", vaddr, pplist);
3850Sstevel@tonic-gate 		offset = (ulong_t)vaddr & MMU_PAGE_OFFSET;
3860Sstevel@tonic-gate 		if (pplist) {				/* shadow list */
387909Segillett 			mp->dmai_flags |= PX_DMAI_FLAGS_PGPFN;
3880Sstevel@tonic-gate 			pfn0 = page_pptonum(*pplist);
3890Sstevel@tonic-gate 		} else {
3900Sstevel@tonic-gate 			struct as *as_p = dobj_p->dmao_obj.virt_obj.v_as;
3910Sstevel@tonic-gate 			struct hat *hat_p = as_p ? as_p->a_hat : kas.a_hat;
3920Sstevel@tonic-gate 			pfn0 = hat_getpfnum(hat_p, vaddr);
3930Sstevel@tonic-gate 		}
3940Sstevel@tonic-gate 		}
3950Sstevel@tonic-gate 		break;
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	case DMA_OTYP_PAGES:
3980Sstevel@tonic-gate 		offset = dobj_p->dmao_obj.pp_obj.pp_offset;
399909Segillett 		mp->dmai_flags |= PX_DMAI_FLAGS_PGPFN;
4000Sstevel@tonic-gate 		pfn0 = page_pptonum(dobj_p->dmao_obj.pp_obj.pp_pp);
4010Sstevel@tonic-gate 		break;
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 	case DMA_OTYP_PADDR:
4040Sstevel@tonic-gate 	default:
4050Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d requested unsupported dma type %x",
4060Sstevel@tonic-gate 			NAMEINST(mp->dmai_rdip), dobj_p->dmao_type);
4070Sstevel@tonic-gate 		return (DDI_DMA_NOMAPPING);
4080Sstevel@tonic-gate 	}
4090Sstevel@tonic-gate 	if (pfn0 == PFN_INVALID) {
4100Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: invalid pfn0 for DMA object %p",
4110Sstevel@tonic-gate 			NAMEINST(dip), dobj_p);
4120Sstevel@tonic-gate 		return (DDI_DMA_NOMAPPING);
4130Sstevel@tonic-gate 	}
4140Sstevel@tonic-gate 	if (TGT_PFN_INBETWEEN(pfn0, pec_p->pec_base32_pfn,
4150Sstevel@tonic-gate 			pec_p->pec_last32_pfn)) {
416909Segillett 		mp->dmai_flags |= PX_DMAI_FLAGS_PTP|PX_DMAI_FLAGS_PTP32;
4170Sstevel@tonic-gate 		goto done;	/* leave bypass and dvma flag as 0 */
4180Sstevel@tonic-gate 	} else if (TGT_PFN_INBETWEEN(pfn0, pec_p->pec_base64_pfn,
4190Sstevel@tonic-gate 			pec_p->pec_last64_pfn)) {
420909Segillett 		mp->dmai_flags |= PX_DMAI_FLAGS_PTP|PX_DMAI_FLAGS_PTP64;
4210Sstevel@tonic-gate 		goto done;	/* leave bypass and dvma flag as 0 */
4220Sstevel@tonic-gate 	}
4230Sstevel@tonic-gate 	if (PX_DMA_ISPEERONLY(mp)) {
4240Sstevel@tonic-gate 		dev_info_t *rdip = mp->dmai_rdip;
4250Sstevel@tonic-gate 		cmn_err(CE_WARN, "Bad peer-to-peer req %s%d", NAMEINST(rdip));
4260Sstevel@tonic-gate 		return (DDI_DMA_NOMAPPING);
4270Sstevel@tonic-gate 	}
428909Segillett 	mp->dmai_flags |= (mp->dmai_flags & PX_DMAI_FLAGS_BYPASSREQ) ?
4291501Sgovinda 	    PX_DMAI_FLAGS_BYPASS : PX_DMAI_FLAGS_DVMA |
4301501Sgovinda 	    (mp->dmai_rflags & DDI_DMA_REDZONE ? PX_DMAI_FLAGS_REDZONE : 0);
4310Sstevel@tonic-gate done:
4320Sstevel@tonic-gate 	mp->dmai_object	 = *dobj_p;			/* whole object    */
4330Sstevel@tonic-gate 	mp->dmai_pfn0	 = (void *)pfn0;		/* cache pfn0	   */
4340Sstevel@tonic-gate 	mp->dmai_roffset = offset;			/* win0 pg0 offset */
4350Sstevel@tonic-gate 	mp->dmai_ndvmapages = MMU_BTOPR(offset + mp->dmai_object.dmao_size);
4360Sstevel@tonic-gate 	return (DDI_SUCCESS);
4370Sstevel@tonic-gate }
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate /*
4400Sstevel@tonic-gate  * px_dma_pgpfn - set up pfnlst array according to pages
4410Sstevel@tonic-gate  *	VA/size pair: <shadow IO, bypass, peer-to-peer>, or OTYP_PAGES
4420Sstevel@tonic-gate  */
4430Sstevel@tonic-gate /*ARGSUSED*/
4440Sstevel@tonic-gate static int
4450Sstevel@tonic-gate px_dma_pgpfn(px_t *px_p, ddi_dma_impl_t *mp, uint_t npages)
4460Sstevel@tonic-gate {
4470Sstevel@tonic-gate 	int i;
4480Sstevel@tonic-gate 	dev_info_t *dip = px_p->px_dip;
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	switch (mp->dmai_object.dmao_type) {
4510Sstevel@tonic-gate 	case DMA_OTYP_BUFVADDR:
4520Sstevel@tonic-gate 	case DMA_OTYP_VADDR: {
4530Sstevel@tonic-gate 		page_t **pplist = mp->dmai_object.dmao_obj.virt_obj.v_priv;
4540Sstevel@tonic-gate 		DBG(DBG_DMA_MAP, dip, "shadow pplist=%p, %x pages, pfns=",
4550Sstevel@tonic-gate 			pplist, npages);
4560Sstevel@tonic-gate 		for (i = 1; i < npages; i++) {
4570Sstevel@tonic-gate 			px_iopfn_t pfn = page_pptonum(pplist[i]);
4580Sstevel@tonic-gate 			PX_SET_MP_PFN1(mp, i, pfn);
4590Sstevel@tonic-gate 			DBG(DBG_DMA_MAP|DBG_CONT, dip, "%x ", pfn);
4600Sstevel@tonic-gate 		}
4610Sstevel@tonic-gate 		DBG(DBG_DMA_MAP|DBG_CONT, dip, "\n");
4620Sstevel@tonic-gate 		}
4630Sstevel@tonic-gate 		break;
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	case DMA_OTYP_PAGES: {
4660Sstevel@tonic-gate 		page_t *pp = mp->dmai_object.dmao_obj.pp_obj.pp_pp->p_next;
4670Sstevel@tonic-gate 		DBG(DBG_DMA_MAP, dip, "pp=%p pfns=", pp);
4680Sstevel@tonic-gate 		for (i = 1; i < npages; i++, pp = pp->p_next) {
4690Sstevel@tonic-gate 			px_iopfn_t pfn = page_pptonum(pp);
4700Sstevel@tonic-gate 			PX_SET_MP_PFN1(mp, i, pfn);
4710Sstevel@tonic-gate 			DBG(DBG_DMA_MAP|DBG_CONT, dip, "%x ", pfn);
4720Sstevel@tonic-gate 		}
4730Sstevel@tonic-gate 		DBG(DBG_DMA_MAP|DBG_CONT, dip, "\n");
4740Sstevel@tonic-gate 		}
4750Sstevel@tonic-gate 		break;
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 	default:	/* check is already done by px_dma_type */
4780Sstevel@tonic-gate 		ASSERT(0);
4790Sstevel@tonic-gate 		break;
4800Sstevel@tonic-gate 	}
4810Sstevel@tonic-gate 	return (DDI_SUCCESS);
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate /*
4850Sstevel@tonic-gate  * px_dma_vapfn - set up pfnlst array according to VA
4860Sstevel@tonic-gate  *	VA/size pair: <normal, bypass, peer-to-peer>
4870Sstevel@tonic-gate  *	pfn0 is skipped as it is already done.
4880Sstevel@tonic-gate  *	In this case, the cached pfn0 is used to fill pfnlst[0]
4890Sstevel@tonic-gate  */
4900Sstevel@tonic-gate static int
4910Sstevel@tonic-gate px_dma_vapfn(px_t *px_p, ddi_dma_impl_t *mp, uint_t npages)
4920Sstevel@tonic-gate {
4930Sstevel@tonic-gate 	dev_info_t *dip = px_p->px_dip;
4940Sstevel@tonic-gate 	int i;
4950Sstevel@tonic-gate 	caddr_t vaddr = (caddr_t)mp->dmai_object.dmao_obj.virt_obj.v_as;
4960Sstevel@tonic-gate 	struct hat *hat_p = vaddr ? ((struct as *)vaddr)->a_hat : kas.a_hat;
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	vaddr = mp->dmai_object.dmao_obj.virt_obj.v_addr + MMU_PAGE_SIZE;
4990Sstevel@tonic-gate 	for (i = 1; i < npages; i++, vaddr += MMU_PAGE_SIZE) {
5000Sstevel@tonic-gate 		px_iopfn_t pfn = hat_getpfnum(hat_p, vaddr);
5010Sstevel@tonic-gate 		if (pfn == PFN_INVALID)
5020Sstevel@tonic-gate 			goto err_badpfn;
5030Sstevel@tonic-gate 		PX_SET_MP_PFN1(mp, i, pfn);
5040Sstevel@tonic-gate 		DBG(DBG_DMA_BINDH, dip, "px_dma_vapfn: mp=%p pfnlst[%x]=%x\n",
5050Sstevel@tonic-gate 			mp, i, pfn);
5060Sstevel@tonic-gate 	}
5070Sstevel@tonic-gate 	return (DDI_SUCCESS);
5080Sstevel@tonic-gate err_badpfn:
5090Sstevel@tonic-gate 	cmn_err(CE_WARN, "%s%d: bad page frame vaddr=%p", NAMEINST(dip), vaddr);
5100Sstevel@tonic-gate 	return (DDI_DMA_NOMAPPING);
5110Sstevel@tonic-gate }
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate /*
5140Sstevel@tonic-gate  * px_dma_pfn - Fills pfn list for all pages being DMA-ed.
5150Sstevel@tonic-gate  *
5160Sstevel@tonic-gate  * dependencies:
5170Sstevel@tonic-gate  *	mp->dmai_ndvmapages	- set to total # of dma pages
5180Sstevel@tonic-gate  *
5190Sstevel@tonic-gate  * return value:
5200Sstevel@tonic-gate  *	DDI_SUCCESS
5210Sstevel@tonic-gate  *	DDI_DMA_NOMAPPING
5220Sstevel@tonic-gate  */
5230Sstevel@tonic-gate int
5240Sstevel@tonic-gate px_dma_pfn(px_t *px_p, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp)
5250Sstevel@tonic-gate {
5260Sstevel@tonic-gate 	uint32_t npages = mp->dmai_ndvmapages;
5270Sstevel@tonic-gate 	int (*waitfp)(caddr_t) = dmareq->dmar_fp;
5280Sstevel@tonic-gate 	int i, ret, peer = PX_DMA_ISPTP(mp);
5290Sstevel@tonic-gate 	int peer32 = PX_DMA_ISPTP32(mp);
5300Sstevel@tonic-gate 	dev_info_t *dip = px_p->px_dip;
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	px_pec_t *pec_p = px_p->px_pec_p;
5330Sstevel@tonic-gate 	px_iopfn_t pfn_base = peer32 ? pec_p->pec_base32_pfn :
5340Sstevel@tonic-gate 					pec_p->pec_base64_pfn;
5350Sstevel@tonic-gate 	px_iopfn_t pfn_last = peer32 ? pec_p->pec_last32_pfn :
5360Sstevel@tonic-gate 					pec_p->pec_last64_pfn;
5370Sstevel@tonic-gate 	px_iopfn_t pfn_adj = peer ? pfn_base : 0;
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	DBG(DBG_DMA_BINDH, dip, "px_dma_pfn: mp=%p pfn0=%x\n",
540909Segillett 		mp, PX_MP_PFN0(mp) - pfn_adj);
5410Sstevel@tonic-gate 	/* 1 page: no array alloc/fill, no mixed mode check */
5420Sstevel@tonic-gate 	if (npages == 1) {
543909Segillett 		PX_SET_MP_PFN(mp, 0, PX_MP_PFN0(mp) - pfn_adj);
5440Sstevel@tonic-gate 		return (DDI_SUCCESS);
5450Sstevel@tonic-gate 	}
5460Sstevel@tonic-gate 	/* allocate pfn array */
5470Sstevel@tonic-gate 	if (!(mp->dmai_pfnlst = kmem_alloc(npages * sizeof (px_iopfn_t),
5480Sstevel@tonic-gate 		waitfp == DDI_DMA_SLEEP ? KM_SLEEP : KM_NOSLEEP))) {
5490Sstevel@tonic-gate 		if (waitfp != DDI_DMA_DONTWAIT)
5500Sstevel@tonic-gate 			ddi_set_callback(waitfp, dmareq->dmar_arg,
5510Sstevel@tonic-gate 				&px_kmem_clid);
5520Sstevel@tonic-gate 		return (DDI_DMA_NORESOURCES);
5530Sstevel@tonic-gate 	}
5540Sstevel@tonic-gate 	/* fill pfn array */
555909Segillett 	PX_SET_MP_PFN(mp, 0, PX_MP_PFN0(mp) - pfn_adj);	/* pfnlst[0] */
5560Sstevel@tonic-gate 	if ((ret = PX_DMA_ISPGPFN(mp) ? px_dma_pgpfn(px_p, mp, npages) :
5570Sstevel@tonic-gate 		px_dma_vapfn(px_p, mp, npages)) != DDI_SUCCESS)
5580Sstevel@tonic-gate 		goto err;
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	/* skip pfn0, check mixed mode and adjust peer to peer pfn */
5610Sstevel@tonic-gate 	for (i = 1; i < npages; i++) {
5620Sstevel@tonic-gate 		px_iopfn_t pfn = PX_GET_MP_PFN1(mp, i);
5630Sstevel@tonic-gate 		if (peer ^ TGT_PFN_INBETWEEN(pfn, pfn_base, pfn_last)) {
564671Skrishnae 			cmn_err(CE_WARN, "%s%d mixed mode DMA %lx %lx",
565909Segillett 				NAMEINST(mp->dmai_rdip), PX_MP_PFN0(mp), pfn);
5660Sstevel@tonic-gate 			ret = DDI_DMA_NOMAPPING;	/* mixed mode */
5670Sstevel@tonic-gate 			goto err;
5680Sstevel@tonic-gate 		}
5690Sstevel@tonic-gate 		DBG(DBG_DMA_MAP, dip,
5700Sstevel@tonic-gate 			"px_dma_pfn: pfnlst[%x]=%x-%x\n", i, pfn, pfn_adj);
5710Sstevel@tonic-gate 		if (pfn_adj)
5720Sstevel@tonic-gate 			PX_SET_MP_PFN1(mp, i, pfn - pfn_adj);
5730Sstevel@tonic-gate 	}
5740Sstevel@tonic-gate 	return (DDI_SUCCESS);
5750Sstevel@tonic-gate err:
5760Sstevel@tonic-gate 	px_dma_freepfn(mp);
5770Sstevel@tonic-gate 	return (ret);
5780Sstevel@tonic-gate }
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate /*
5810Sstevel@tonic-gate  * px_dvma_win() - trim requested DVMA size down to window size
5820Sstevel@tonic-gate  *	The 1st window starts from offset and ends at page-aligned boundary.
5830Sstevel@tonic-gate  *	From the 2nd window on, each window starts and ends at page-aligned
5840Sstevel@tonic-gate  *	boundary except the last window ends at wherever requested.
5850Sstevel@tonic-gate  *
5860Sstevel@tonic-gate  *	accesses the following mp-> members:
5870Sstevel@tonic-gate  *	mp->dmai_attr.dma_attr_count_max
5880Sstevel@tonic-gate  *	mp->dmai_attr.dma_attr_seg
5890Sstevel@tonic-gate  *	mp->dmai_roffset   - start offset of 1st window
5900Sstevel@tonic-gate  *	mp->dmai_rflags (redzone)
5910Sstevel@tonic-gate  *	mp->dmai_ndvmapages (for 1 page fast path)
5920Sstevel@tonic-gate  *
5930Sstevel@tonic-gate  *	sets the following mp-> members:
5940Sstevel@tonic-gate  *	mp->dmai_size	   - xfer size, != winsize if 1st/last win  (not fixed)
5950Sstevel@tonic-gate  *	mp->dmai_winsize   - window size (no redzone), n * page size    (fixed)
5960Sstevel@tonic-gate  *	mp->dmai_nwin	   - # of DMA windows of entire object		(fixed)
5970Sstevel@tonic-gate  *	mp->dmai_rflags	   - remove partial flag if nwin == 1		(fixed)
5980Sstevel@tonic-gate  *	mp->dmai_winlst	   - NULL, window objects not used for DVMA	(fixed)
5990Sstevel@tonic-gate  *
6000Sstevel@tonic-gate  *	fixed - not changed across different DMA windows
6010Sstevel@tonic-gate  */
6020Sstevel@tonic-gate /*ARGSUSED*/
6030Sstevel@tonic-gate int
6040Sstevel@tonic-gate px_dvma_win(px_t *px_p, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp)
6050Sstevel@tonic-gate {
6061501Sgovinda 	uint32_t redzone_sz	= PX_HAS_REDZONE(mp) ? MMU_PAGE_SIZE : 0;
6070Sstevel@tonic-gate 	size_t obj_sz		= mp->dmai_object.dmao_size;
6080Sstevel@tonic-gate 	size_t xfer_sz;
6090Sstevel@tonic-gate 	ulong_t pg_off;
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	if ((mp->dmai_ndvmapages == 1) && !redzone_sz) {
6120Sstevel@tonic-gate 		mp->dmai_rflags &= ~DDI_DMA_PARTIAL;
6130Sstevel@tonic-gate 		mp->dmai_size = obj_sz;
6140Sstevel@tonic-gate 		mp->dmai_winsize = MMU_PAGE_SIZE;
6150Sstevel@tonic-gate 		mp->dmai_nwin = 1;
6160Sstevel@tonic-gate 		goto done;
6170Sstevel@tonic-gate 	}
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	pg_off	= mp->dmai_roffset;
6200Sstevel@tonic-gate 	xfer_sz	= obj_sz + redzone_sz;
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	/* include redzone in nocross check */ {
6230Sstevel@tonic-gate 		uint64_t nocross = mp->dmai_attr.dma_attr_seg;
6240Sstevel@tonic-gate 		if (xfer_sz + pg_off - 1 > nocross)
6250Sstevel@tonic-gate 			xfer_sz = nocross - pg_off + 1;
6260Sstevel@tonic-gate 		if (redzone_sz && (xfer_sz <= redzone_sz)) {
6270Sstevel@tonic-gate 			DBG(DBG_DMA_MAP, px_p->px_dip,
6280Sstevel@tonic-gate 			    "nocross too small: "
6290Sstevel@tonic-gate 			    "%lx(%lx)+%lx+%lx < %llx\n",
6300Sstevel@tonic-gate 			    xfer_sz, obj_sz, pg_off, redzone_sz, nocross);
6310Sstevel@tonic-gate 			return (DDI_DMA_TOOBIG);
6320Sstevel@tonic-gate 		}
6330Sstevel@tonic-gate 	}
6340Sstevel@tonic-gate 	xfer_sz -= redzone_sz;		/* restore transfer size  */
6350Sstevel@tonic-gate 	/* check counter max */ {
6360Sstevel@tonic-gate 		uint32_t count_max = mp->dmai_attr.dma_attr_count_max;
6370Sstevel@tonic-gate 		if (xfer_sz - 1 > count_max)
6380Sstevel@tonic-gate 			xfer_sz = count_max + 1;
6390Sstevel@tonic-gate 	}
6400Sstevel@tonic-gate 	if (xfer_sz >= obj_sz) {
6410Sstevel@tonic-gate 		mp->dmai_rflags &= ~DDI_DMA_PARTIAL;
6420Sstevel@tonic-gate 		mp->dmai_size = xfer_sz;
6430Sstevel@tonic-gate 		mp->dmai_winsize = P2ROUNDUP(xfer_sz + pg_off, MMU_PAGE_SIZE);
6440Sstevel@tonic-gate 		mp->dmai_nwin = 1;
6450Sstevel@tonic-gate 		goto done;
6460Sstevel@tonic-gate 	}
6470Sstevel@tonic-gate 	if (!(dmareq->dmar_flags & DDI_DMA_PARTIAL)) {
6480Sstevel@tonic-gate 		DBG(DBG_DMA_MAP, px_p->px_dip, "too big: %lx+%lx+%lx > %lx\n",
6490Sstevel@tonic-gate 			obj_sz, pg_off, redzone_sz, xfer_sz);
6500Sstevel@tonic-gate 		return (DDI_DMA_TOOBIG);
6510Sstevel@tonic-gate 	}
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	xfer_sz = MMU_PTOB(MMU_BTOP(xfer_sz + pg_off)); /* page align */
6540Sstevel@tonic-gate 	mp->dmai_size = xfer_sz - pg_off;	/* 1st window xferrable size */
6550Sstevel@tonic-gate 	mp->dmai_winsize = xfer_sz;		/* redzone not in winsize */
6560Sstevel@tonic-gate 	mp->dmai_nwin = (obj_sz + pg_off + xfer_sz - 1) / xfer_sz;
6570Sstevel@tonic-gate done:
6580Sstevel@tonic-gate 	mp->dmai_winlst = NULL;
6590Sstevel@tonic-gate 	px_dump_dma_handle(DBG_DMA_MAP, px_p->px_dip, mp);
6600Sstevel@tonic-gate 	return (DDI_SUCCESS);
6610Sstevel@tonic-gate }
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate /*
6640Sstevel@tonic-gate  * fast track cache entry to mmu context, inserts 3 0 bits between
6650Sstevel@tonic-gate  * upper 6-bits and lower 3-bits of the 9-bit cache entry
6660Sstevel@tonic-gate  */
6670Sstevel@tonic-gate #define	MMU_FCE_TO_CTX(i)	(((i) << 3) | ((i) & 0x7) | 0x38)
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate /*
6700Sstevel@tonic-gate  * px_dvma_map_fast - attempts to map fast trackable DVMA
6710Sstevel@tonic-gate  */
6720Sstevel@tonic-gate /*ARGSUSED*/
6730Sstevel@tonic-gate int
6740Sstevel@tonic-gate px_dvma_map_fast(px_mmu_t *mmu_p, ddi_dma_impl_t *mp)
6750Sstevel@tonic-gate {
6760Sstevel@tonic-gate 	uint_t clustsz = px_dvma_page_cache_clustsz;
6770Sstevel@tonic-gate 	uint_t entries = px_dvma_page_cache_entries;
678*1772Sjl139090 	io_attributes_t attr = PX_GET_TTE_ATTR(mp->dmai_rflags,
679*1772Sjl139090 	    mp->dmai_attr.dma_attr_flags);
6800Sstevel@tonic-gate 	int i = mmu_p->mmu_dvma_addr_scan_start;
6810Sstevel@tonic-gate 	uint8_t *lock_addr = mmu_p->mmu_dvma_cache_locks + i;
6820Sstevel@tonic-gate 	px_dvma_addr_t dvma_pg;
6830Sstevel@tonic-gate 	size_t npages = MMU_BTOP(mp->dmai_winsize);
6841501Sgovinda 	dev_info_t *dip = mmu_p->mmu_px_p->px_dip;
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 	extern uint8_t ldstub(uint8_t *);
6870Sstevel@tonic-gate 	ASSERT(MMU_PTOB(npages) == mp->dmai_winsize);
6881501Sgovinda 	ASSERT(npages + PX_HAS_REDZONE(mp) <= clustsz);
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	for (; i < entries && ldstub(lock_addr); i++, lock_addr++);
6910Sstevel@tonic-gate 	if (i >= entries) {
6920Sstevel@tonic-gate 		lock_addr = mmu_p->mmu_dvma_cache_locks;
6930Sstevel@tonic-gate 		i = 0;
6940Sstevel@tonic-gate 		for (; i < entries && ldstub(lock_addr); i++, lock_addr++);
6950Sstevel@tonic-gate 		if (i >= entries) {
6960Sstevel@tonic-gate #ifdef	PX_DMA_PROF
6970Sstevel@tonic-gate 			px_dvmaft_exhaust++;
6980Sstevel@tonic-gate #endif	/* PX_DMA_PROF */
6990Sstevel@tonic-gate 			return (DDI_DMA_NORESOURCES);
7000Sstevel@tonic-gate 		}
7010Sstevel@tonic-gate 	}
7020Sstevel@tonic-gate 	mmu_p->mmu_dvma_addr_scan_start = (i + 1) & (entries - 1);
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 	i *= clustsz;
7050Sstevel@tonic-gate 	dvma_pg = mmu_p->dvma_base_pg + i;
7060Sstevel@tonic-gate 
7071501Sgovinda 	if (px_lib_iommu_map(dip, PCI_TSBID(0, i), npages, attr,
7081617Sgovinda 	    (void *)mp, 0, MMU_MAP_PFN) != DDI_SUCCESS) {
7091501Sgovinda 		DBG(DBG_MAP_WIN, dip, "px_dvma_map_fast: "
7101501Sgovinda 		    "px_lib_iommu_map failed\n");
7111501Sgovinda 
7120Sstevel@tonic-gate 		return (DDI_FAILURE);
7131501Sgovinda 	}
7140Sstevel@tonic-gate 
7151501Sgovinda 	if (!PX_MAP_BUFZONE(mp))
7161501Sgovinda 		goto done;
7171501Sgovinda 
7181501Sgovinda 	DBG(DBG_MAP_WIN, dip, "px_dvma_map_fast: redzone pg=%x\n", i + npages);
7191501Sgovinda 
7201501Sgovinda 	ASSERT(PX_HAS_REDZONE(mp));
7211501Sgovinda 
7221501Sgovinda 	if (px_lib_iommu_map(dip, PCI_TSBID(0, i + npages), 1, attr,
7231617Sgovinda 	    (void *)mp, npages - 1, MMU_MAP_PFN) != DDI_SUCCESS) {
7241501Sgovinda 		DBG(DBG_MAP_WIN, dip, "px_dvma_map_fast: "
7251501Sgovinda 		    "mapping REDZONE page failed\n");
7261501Sgovinda 
7271501Sgovinda 		(void) px_lib_iommu_demap(dip, PCI_TSBID(0, i), npages);
7281501Sgovinda 		return (DDI_FAILURE);
7291501Sgovinda 	}
7301501Sgovinda 
7311501Sgovinda done:
7320Sstevel@tonic-gate #ifdef PX_DMA_PROF
7330Sstevel@tonic-gate 	px_dvmaft_success++;
7340Sstevel@tonic-gate #endif
7350Sstevel@tonic-gate 	mp->dmai_mapping = mp->dmai_roffset | MMU_PTOB(dvma_pg);
7360Sstevel@tonic-gate 	mp->dmai_offset = 0;
737909Segillett 	mp->dmai_flags |= PX_DMAI_FLAGS_FASTTRACK;
7380Sstevel@tonic-gate 	PX_SAVE_MP_TTE(mp, attr);	/* save TTE template for unmapping */
739909Segillett 	if (PX_DVMA_DBG_ON(mmu_p))
7400Sstevel@tonic-gate 		px_dvma_alloc_debug(mmu_p, (char *)mp->dmai_mapping,
7410Sstevel@tonic-gate 			mp->dmai_size, mp);
7420Sstevel@tonic-gate 	return (DDI_SUCCESS);
7430Sstevel@tonic-gate }
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate /*
7460Sstevel@tonic-gate  * px_dvma_map: map non-fasttrack DMA
7470Sstevel@tonic-gate  *		Use quantum cache if single page DMA.
7480Sstevel@tonic-gate  */
7490Sstevel@tonic-gate int
7500Sstevel@tonic-gate px_dvma_map(ddi_dma_impl_t *mp, ddi_dma_req_t *dmareq, px_mmu_t *mmu_p)
7510Sstevel@tonic-gate {
7520Sstevel@tonic-gate 	uint_t npages = PX_DMA_WINNPGS(mp);
7530Sstevel@tonic-gate 	px_dvma_addr_t dvma_pg, dvma_pg_index;
7540Sstevel@tonic-gate 	void *dvma_addr;
755*1772Sjl139090 	uint64_t tte = PX_GET_TTE_ATTR(mp->dmai_rflags,
756*1772Sjl139090 	    mp->dmai_attr.dma_attr_flags);
7570Sstevel@tonic-gate 	int sleep = dmareq->dmar_fp == DDI_DMA_SLEEP ? VM_SLEEP : VM_NOSLEEP;
7580Sstevel@tonic-gate 	dev_info_t *dip = mp->dmai_rdip;
7590Sstevel@tonic-gate 	int	ret = DDI_SUCCESS;
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 	/*
7620Sstevel@tonic-gate 	 * allocate dvma space resource and map in the first window.
7630Sstevel@tonic-gate 	 * (vmem_t *vmp, size_t size,
7640Sstevel@tonic-gate 	 *	size_t align, size_t phase, size_t nocross,
7650Sstevel@tonic-gate 	 *	void *minaddr, void *maxaddr, int vmflag)
7660Sstevel@tonic-gate 	 */
7671501Sgovinda 	if ((npages == 1) && !PX_HAS_REDZONE(mp) && PX_HAS_NOSYSLIMIT(mp)) {
7680Sstevel@tonic-gate 		dvma_addr = vmem_alloc(mmu_p->mmu_dvma_map,
7690Sstevel@tonic-gate 			MMU_PAGE_SIZE, sleep);
770909Segillett 		mp->dmai_flags |= PX_DMAI_FLAGS_VMEMCACHE;
7710Sstevel@tonic-gate #ifdef	PX_DMA_PROF
7720Sstevel@tonic-gate 		px_dvma_vmem_alloc++;
7730Sstevel@tonic-gate #endif	/* PX_DMA_PROF */
7740Sstevel@tonic-gate 	} else {
7750Sstevel@tonic-gate 		dvma_addr = vmem_xalloc(mmu_p->mmu_dvma_map,
7761501Sgovinda 			MMU_PTOB(npages + PX_HAS_REDZONE(mp)),
7770Sstevel@tonic-gate 			MAX(mp->dmai_attr.dma_attr_align, MMU_PAGE_SIZE),
7780Sstevel@tonic-gate 			0,
7790Sstevel@tonic-gate 			mp->dmai_attr.dma_attr_seg + 1,
7800Sstevel@tonic-gate 			(void *)mp->dmai_attr.dma_attr_addr_lo,
7810Sstevel@tonic-gate 			(void *)(mp->dmai_attr.dma_attr_addr_hi + 1),
7820Sstevel@tonic-gate 			sleep);
7830Sstevel@tonic-gate #ifdef	PX_DMA_PROF
7840Sstevel@tonic-gate 		px_dvma_vmem_xalloc++;
7850Sstevel@tonic-gate #endif	/* PX_DMA_PROF */
7860Sstevel@tonic-gate 	}
7870Sstevel@tonic-gate 	dvma_pg = MMU_BTOP((ulong_t)dvma_addr);
7880Sstevel@tonic-gate 	dvma_pg_index = dvma_pg - mmu_p->dvma_base_pg;
7890Sstevel@tonic-gate 	DBG(DBG_DMA_MAP, dip, "fallback dvma_pages: dvma_pg=%x index=%x\n",
7900Sstevel@tonic-gate 		dvma_pg, dvma_pg_index);
7910Sstevel@tonic-gate 	if (dvma_pg == 0)
7920Sstevel@tonic-gate 		goto noresource;
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	mp->dmai_mapping = mp->dmai_roffset | MMU_PTOB(dvma_pg);
7950Sstevel@tonic-gate 	mp->dmai_offset = 0;
7960Sstevel@tonic-gate 	PX_SAVE_MP_TTE(mp, tte);	/* mp->dmai_tte = tte */
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	if ((ret = px_mmu_map_pages(mmu_p,
7990Sstevel@tonic-gate 	    mp, dvma_pg, npages, 0)) != DDI_SUCCESS) {
800909Segillett 		if (mp->dmai_flags & PX_DMAI_FLAGS_VMEMCACHE) {
8010Sstevel@tonic-gate 			vmem_free(mmu_p->mmu_dvma_map, (void *)dvma_addr,
8020Sstevel@tonic-gate 			    MMU_PAGE_SIZE);
8030Sstevel@tonic-gate #ifdef PX_DMA_PROF
8040Sstevel@tonic-gate 			px_dvma_vmem_free++;
8050Sstevel@tonic-gate #endif /* PX_DMA_PROF */
8060Sstevel@tonic-gate 		} else {
8070Sstevel@tonic-gate 			vmem_xfree(mmu_p->mmu_dvma_map, (void *)dvma_addr,
8081501Sgovinda 			    MMU_PTOB(npages + PX_HAS_REDZONE(mp)));
8090Sstevel@tonic-gate #ifdef PX_DMA_PROF
8100Sstevel@tonic-gate 			px_dvma_vmem_xfree++;
8110Sstevel@tonic-gate #endif /* PX_DMA_PROF */
8120Sstevel@tonic-gate 		}
8130Sstevel@tonic-gate 	}
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	return (ret);
8160Sstevel@tonic-gate noresource:
8170Sstevel@tonic-gate 	if (dmareq->dmar_fp != DDI_DMA_DONTWAIT) {
8180Sstevel@tonic-gate 		DBG(DBG_DMA_MAP, dip, "dvma_pg 0 - set callback\n");
8190Sstevel@tonic-gate 		ddi_set_callback(dmareq->dmar_fp, dmareq->dmar_arg,
8200Sstevel@tonic-gate 			&mmu_p->mmu_dvma_clid);
8210Sstevel@tonic-gate 	}
8220Sstevel@tonic-gate 	DBG(DBG_DMA_MAP, dip, "vmem_xalloc - DDI_DMA_NORESOURCES\n");
8230Sstevel@tonic-gate 	return (DDI_DMA_NORESOURCES);
8240Sstevel@tonic-gate }
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate void
8270Sstevel@tonic-gate px_dvma_unmap(px_mmu_t *mmu_p, ddi_dma_impl_t *mp)
8280Sstevel@tonic-gate {
8290Sstevel@tonic-gate 	px_dvma_addr_t dvma_addr = (px_dvma_addr_t)mp->dmai_mapping;
8300Sstevel@tonic-gate 	px_dvma_addr_t dvma_pg = MMU_BTOP(dvma_addr);
8310Sstevel@tonic-gate 	dvma_addr = MMU_PTOB(dvma_pg);
8320Sstevel@tonic-gate 
833909Segillett 	if (mp->dmai_flags & PX_DMAI_FLAGS_FASTTRACK) {
8340Sstevel@tonic-gate 		px_iopfn_t index = dvma_pg - mmu_p->dvma_base_pg;
8350Sstevel@tonic-gate 		ASSERT(index % px_dvma_page_cache_clustsz == 0);
8360Sstevel@tonic-gate 		index /= px_dvma_page_cache_clustsz;
8370Sstevel@tonic-gate 		ASSERT(index < px_dvma_page_cache_entries);
8380Sstevel@tonic-gate 		mmu_p->mmu_dvma_cache_locks[index] = 0;
8390Sstevel@tonic-gate #ifdef	PX_DMA_PROF
8400Sstevel@tonic-gate 		px_dvmaft_free++;
8410Sstevel@tonic-gate #endif	/* PX_DMA_PROF */
8420Sstevel@tonic-gate 		return;
8430Sstevel@tonic-gate 	}
8440Sstevel@tonic-gate 
845909Segillett 	if (mp->dmai_flags & PX_DMAI_FLAGS_VMEMCACHE) {
8460Sstevel@tonic-gate 		vmem_free(mmu_p->mmu_dvma_map, (void *)dvma_addr,
8470Sstevel@tonic-gate 			MMU_PAGE_SIZE);
8480Sstevel@tonic-gate #ifdef PX_DMA_PROF
8490Sstevel@tonic-gate 		px_dvma_vmem_free++;
8500Sstevel@tonic-gate #endif /* PX_DMA_PROF */
8510Sstevel@tonic-gate 	} else {
8521501Sgovinda 		size_t npages = MMU_BTOP(mp->dmai_winsize) + PX_HAS_REDZONE(mp);
8530Sstevel@tonic-gate 		vmem_xfree(mmu_p->mmu_dvma_map, (void *)dvma_addr,
8540Sstevel@tonic-gate 			MMU_PTOB(npages));
8550Sstevel@tonic-gate #ifdef PX_DMA_PROF
8560Sstevel@tonic-gate 		px_dvma_vmem_xfree++;
8570Sstevel@tonic-gate #endif /* PX_DMA_PROF */
8580Sstevel@tonic-gate 	}
8590Sstevel@tonic-gate }
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate /*
8620Sstevel@tonic-gate  * DVMA mappings may have multiple windows, but each window always have
8630Sstevel@tonic-gate  * one segment.
8640Sstevel@tonic-gate  */
8650Sstevel@tonic-gate int
8660Sstevel@tonic-gate px_dvma_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_impl_t *mp,
8670Sstevel@tonic-gate 	enum ddi_dma_ctlops cmd, off_t *offp, size_t *lenp, caddr_t *objp,
8680Sstevel@tonic-gate 	uint_t cache_flags)
8690Sstevel@tonic-gate {
8700Sstevel@tonic-gate 	switch (cmd) {
8710Sstevel@tonic-gate 	case DDI_DMA_SYNC:
8720Sstevel@tonic-gate 		return (px_lib_dma_sync(dip, rdip, (ddi_dma_handle_t)mp,
8730Sstevel@tonic-gate 		    *offp, *lenp, cache_flags));
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 	case DDI_DMA_HTOC: {
8760Sstevel@tonic-gate 		int ret;
8770Sstevel@tonic-gate 		off_t wo_off, off = *offp;	/* wo_off: wnd's obj offset */
8780Sstevel@tonic-gate 		uint_t win_size = mp->dmai_winsize;
8790Sstevel@tonic-gate 		ddi_dma_cookie_t *cp = (ddi_dma_cookie_t *)objp;
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 		if (off >= mp->dmai_object.dmao_size) {
8820Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d invalid dma_htoc offset %lx",
8830Sstevel@tonic-gate 				NAMEINST(mp->dmai_rdip), off);
8840Sstevel@tonic-gate 			return (DDI_FAILURE);
8850Sstevel@tonic-gate 		}
8860Sstevel@tonic-gate 		off += mp->dmai_roffset;
8870Sstevel@tonic-gate 		ret = px_dma_win(dip, rdip, (ddi_dma_handle_t)mp,
8880Sstevel@tonic-gate 		    off / win_size, &wo_off, NULL, cp, NULL); /* lenp == NULL */
8890Sstevel@tonic-gate 		if (ret)
8900Sstevel@tonic-gate 			return (ret);
8910Sstevel@tonic-gate 		DBG(DBG_DMA_CTL, dip, "HTOC:cookie=%x+%lx off=%lx,%lx\n",
8920Sstevel@tonic-gate 			cp->dmac_address, cp->dmac_size, off, *offp);
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 		/* adjust cookie addr/len if we are not on window boundary */
8950Sstevel@tonic-gate 		ASSERT((off % win_size) == (off -
8960Sstevel@tonic-gate 			(PX_DMA_CURWIN(mp) ? mp->dmai_roffset : 0) - wo_off));
8970Sstevel@tonic-gate 		off = PX_DMA_CURWIN(mp) ? off % win_size : *offp;
8980Sstevel@tonic-gate 		ASSERT(cp->dmac_size > off);
8990Sstevel@tonic-gate 		cp->dmac_laddress += off;
9000Sstevel@tonic-gate 		cp->dmac_size -= off;
9010Sstevel@tonic-gate 		DBG(DBG_DMA_CTL, dip, "HTOC:mp=%p cookie=%x+%lx off=%lx,%lx\n",
9020Sstevel@tonic-gate 			mp, cp->dmac_address, cp->dmac_size, off, wo_off);
9030Sstevel@tonic-gate 		}
9040Sstevel@tonic-gate 		return (DDI_SUCCESS);
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 	case DDI_DMA_REPWIN:
9070Sstevel@tonic-gate 		*offp = mp->dmai_offset;
9080Sstevel@tonic-gate 		*lenp = mp->dmai_size;
9090Sstevel@tonic-gate 		return (DDI_SUCCESS);
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 	case DDI_DMA_MOVWIN: {
9120Sstevel@tonic-gate 		off_t off = *offp;
9130Sstevel@tonic-gate 		if (off >= mp->dmai_object.dmao_size)
9140Sstevel@tonic-gate 			return (DDI_FAILURE);
9150Sstevel@tonic-gate 		off += mp->dmai_roffset;
9160Sstevel@tonic-gate 		return (px_dma_win(dip, rdip, (ddi_dma_handle_t)mp,
9170Sstevel@tonic-gate 		    off / mp->dmai_winsize, offp, lenp,
9180Sstevel@tonic-gate 		    (ddi_dma_cookie_t *)objp, NULL));
9190Sstevel@tonic-gate 		}
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 	case DDI_DMA_NEXTWIN: {
9220Sstevel@tonic-gate 		px_window_t win = PX_DMA_CURWIN(mp);
9230Sstevel@tonic-gate 		if (offp) {
9240Sstevel@tonic-gate 			if (*(px_window_t *)offp != win) {
9250Sstevel@tonic-gate 				/* window not active */
9260Sstevel@tonic-gate 				*(px_window_t *)objp = win; /* return cur win */
9270Sstevel@tonic-gate 				return (DDI_DMA_STALE);
9280Sstevel@tonic-gate 			}
9290Sstevel@tonic-gate 			win++;
9300Sstevel@tonic-gate 		} else	/* map win 0 */
9310Sstevel@tonic-gate 			win = 0;
9320Sstevel@tonic-gate 		if (win >= mp->dmai_nwin) {
9330Sstevel@tonic-gate 			*(px_window_t *)objp = win - 1;
9340Sstevel@tonic-gate 			return (DDI_DMA_DONE);
9350Sstevel@tonic-gate 		}
9360Sstevel@tonic-gate 		if (px_dma_win(dip, rdip, (ddi_dma_handle_t)mp,
9370Sstevel@tonic-gate 		    win, 0, 0, 0, 0)) {
9380Sstevel@tonic-gate 			*(px_window_t *)objp = win - 1;
9390Sstevel@tonic-gate 			return (DDI_FAILURE);
9400Sstevel@tonic-gate 		}
9410Sstevel@tonic-gate 		*(px_window_t *)objp = win;
9420Sstevel@tonic-gate 		}
9430Sstevel@tonic-gate 		return (DDI_SUCCESS);
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 	case DDI_DMA_NEXTSEG:
9460Sstevel@tonic-gate 		if (*(px_window_t *)offp != PX_DMA_CURWIN(mp))
9470Sstevel@tonic-gate 			return (DDI_DMA_STALE);
9480Sstevel@tonic-gate 		if (lenp)				/* only 1 seg allowed */
9490Sstevel@tonic-gate 			return (DDI_DMA_DONE);
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 		/* return mp as seg 0 */
9520Sstevel@tonic-gate 		*(ddi_dma_seg_t *)objp = (ddi_dma_seg_t)mp;
9530Sstevel@tonic-gate 		return (DDI_SUCCESS);
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	case DDI_DMA_SEGTOC:
9560Sstevel@tonic-gate 		MAKE_DMA_COOKIE((ddi_dma_cookie_t *)objp, mp->dmai_mapping,
9570Sstevel@tonic-gate 			mp->dmai_size);
9580Sstevel@tonic-gate 		*offp = mp->dmai_offset;
9590Sstevel@tonic-gate 		*lenp = mp->dmai_size;
9600Sstevel@tonic-gate 		return (DDI_SUCCESS);
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 	case DDI_DMA_COFF: {
9630Sstevel@tonic-gate 		ddi_dma_cookie_t *cp = (ddi_dma_cookie_t *)offp;
9640Sstevel@tonic-gate 		if (cp->dmac_address < mp->dmai_mapping ||
9650Sstevel@tonic-gate 			(cp->dmac_address + cp->dmac_size) >
9660Sstevel@tonic-gate 			(mp->dmai_mapping + mp->dmai_size))
9670Sstevel@tonic-gate 			return (DDI_FAILURE);
9680Sstevel@tonic-gate 		*objp = (caddr_t)(cp->dmac_address - mp->dmai_mapping +
9690Sstevel@tonic-gate 			mp->dmai_offset);
9700Sstevel@tonic-gate 		}
9710Sstevel@tonic-gate 		return (DDI_SUCCESS);
9720Sstevel@tonic-gate 	default:
9730Sstevel@tonic-gate 		DBG(DBG_DMA_CTL, dip, "unknown command (%x): rdip=%s%d\n",
9740Sstevel@tonic-gate 			cmd, ddi_driver_name(rdip), ddi_get_instance(rdip));
9750Sstevel@tonic-gate 		break;
9760Sstevel@tonic-gate 	}
9770Sstevel@tonic-gate 	return (DDI_FAILURE);
9780Sstevel@tonic-gate }
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate void
9810Sstevel@tonic-gate px_dma_freewin(ddi_dma_impl_t *mp)
9820Sstevel@tonic-gate {
9830Sstevel@tonic-gate 	px_dma_win_t *win_p = mp->dmai_winlst, *win2_p;
9840Sstevel@tonic-gate 	for (win2_p = win_p; win_p; win2_p = win_p) {
9850Sstevel@tonic-gate 		win_p = win2_p->win_next;
9860Sstevel@tonic-gate 		kmem_free(win2_p, sizeof (px_dma_win_t) +
9870Sstevel@tonic-gate 			sizeof (ddi_dma_cookie_t) * win2_p->win_ncookies);
9880Sstevel@tonic-gate 	}
9890Sstevel@tonic-gate 	mp->dmai_nwin = 0;
9900Sstevel@tonic-gate 	mp->dmai_winlst = NULL;
9910Sstevel@tonic-gate }
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate /*
9940Sstevel@tonic-gate  * px_dma_newwin - create a dma window object and cookies
9950Sstevel@tonic-gate  *
9960Sstevel@tonic-gate  *	After the initial scan in px_dma_physwin(), which identifies
9970Sstevel@tonic-gate  *	a portion of the pfn array that belongs to a dma window,
9980Sstevel@tonic-gate  *	we are called to allocate and initialize representing memory
9990Sstevel@tonic-gate  *	resources. We know from the 1st scan the number of cookies
10000Sstevel@tonic-gate  *	or dma segment in this window so we can allocate a contiguous
10010Sstevel@tonic-gate  *	memory array for the dma cookies (The implementation of
10020Sstevel@tonic-gate  *	ddi_dma_nextcookie(9f) dictates dma cookies be contiguous).
10030Sstevel@tonic-gate  *
10040Sstevel@tonic-gate  *	A second round scan is done on the pfn array to identify
10050Sstevel@tonic-gate  *	each dma segment and initialize its corresponding dma cookie.
10060Sstevel@tonic-gate  *	We don't need to do all the safety checking and we know they
10070Sstevel@tonic-gate  *	all belong to the same dma window.
10080Sstevel@tonic-gate  *
10090Sstevel@tonic-gate  *	Input:	cookie_no - # of cookies identified by the 1st scan
10100Sstevel@tonic-gate  *		start_idx - subscript of the pfn array for the starting pfn
10110Sstevel@tonic-gate  *		end_idx   - subscript of the last pfn in dma window
10120Sstevel@tonic-gate  *		win_pp    - pointer to win_next member of previous window
10130Sstevel@tonic-gate  *	Return:	DDI_SUCCESS - with **win_pp as newly created window object
10140Sstevel@tonic-gate  *		DDI_DMA_NORESROUCE - caller frees all previous window objs
10150Sstevel@tonic-gate  *	Note:	Each cookie and window size are all initialized on page
10160Sstevel@tonic-gate  *		boundary. This is not true for the 1st cookie of the 1st
10170Sstevel@tonic-gate  *		window and the last cookie of the last window.
10180Sstevel@tonic-gate  *		We fix that later in upper layer which has access to size
10190Sstevel@tonic-gate  *		and offset info.
10200Sstevel@tonic-gate  *
10210Sstevel@tonic-gate  */
10220Sstevel@tonic-gate /*ARGSUSED*/
10230Sstevel@tonic-gate static int
10240Sstevel@tonic-gate px_dma_newwin(dev_info_t *dip, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp,
10250Sstevel@tonic-gate 	uint32_t cookie_no, uint32_t start_idx, uint32_t end_idx,
10260Sstevel@tonic-gate 	px_dma_win_t **win_pp, uint64_t count_max, uint64_t bypass)
10270Sstevel@tonic-gate {
10280Sstevel@tonic-gate 	int (*waitfp)(caddr_t) = dmareq->dmar_fp;
10290Sstevel@tonic-gate 	ddi_dma_cookie_t *cookie_p;
10300Sstevel@tonic-gate 	uint32_t pfn_no = 1;
10310Sstevel@tonic-gate 	px_iopfn_t pfn = PX_GET_MP_PFN(mp, start_idx);
10320Sstevel@tonic-gate 	px_iopfn_t prev_pfn = pfn;
10330Sstevel@tonic-gate 	uint64_t baddr, seg_pfn0 = pfn;
10340Sstevel@tonic-gate 	size_t sz = cookie_no * sizeof (ddi_dma_cookie_t);
10350Sstevel@tonic-gate 	px_dma_win_t *win_p = kmem_zalloc(sizeof (px_dma_win_t) + sz,
10360Sstevel@tonic-gate 		waitfp == DDI_DMA_SLEEP ? KM_SLEEP : KM_NOSLEEP);
1037*1772Sjl139090 	io_attributes_t	attr = PX_GET_TTE_ATTR(mp->dmai_rflags,
1038*1772Sjl139090 	    mp->dmai_attr.dma_attr_flags);
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate 	if (!win_p)
10410Sstevel@tonic-gate 		goto noresource;
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 	win_p->win_next = NULL;
10440Sstevel@tonic-gate 	win_p->win_ncookies = cookie_no;
10450Sstevel@tonic-gate 	win_p->win_curseg = 0;	/* start from segment 0 */
10460Sstevel@tonic-gate 	win_p->win_size = MMU_PTOB(end_idx - start_idx + 1);
10470Sstevel@tonic-gate 	/* win_p->win_offset is left uninitialized */
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 	cookie_p = (ddi_dma_cookie_t *)(win_p + 1);
10500Sstevel@tonic-gate 	start_idx++;
10510Sstevel@tonic-gate 	for (; start_idx <= end_idx; start_idx++, prev_pfn = pfn, pfn_no++) {
10520Sstevel@tonic-gate 		pfn = PX_GET_MP_PFN1(mp, start_idx);
10530Sstevel@tonic-gate 		if ((pfn == prev_pfn + 1) &&
10540Sstevel@tonic-gate 			(MMU_PTOB(pfn_no + 1) - 1 <= count_max))
10550Sstevel@tonic-gate 			continue;
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate 		/* close up the cookie up to (including) prev_pfn */
10580Sstevel@tonic-gate 		baddr = MMU_PTOB(seg_pfn0);
10590Sstevel@tonic-gate 		if (bypass && (px_lib_iommu_getbypass(dip,
10600Sstevel@tonic-gate 				baddr, attr, &baddr) != DDI_SUCCESS))
10610Sstevel@tonic-gate 			return (DDI_FAILURE);
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate 		MAKE_DMA_COOKIE(cookie_p, baddr, MMU_PTOB(pfn_no));
10640Sstevel@tonic-gate 		DBG(DBG_BYPASS, mp->dmai_rdip, "cookie %p (%x pages)\n",
10650Sstevel@tonic-gate 			MMU_PTOB(seg_pfn0), pfn_no);
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 		cookie_p++;	/* advance to next available cookie cell */
10680Sstevel@tonic-gate 		pfn_no = 0;
10690Sstevel@tonic-gate 		seg_pfn0 = pfn;	/* start a new segment from current pfn */
10700Sstevel@tonic-gate 	}
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 	baddr = MMU_PTOB(seg_pfn0);
10730Sstevel@tonic-gate 	if (bypass && (px_lib_iommu_getbypass(dip,
10740Sstevel@tonic-gate 			baddr, attr, &baddr) != DDI_SUCCESS))
10750Sstevel@tonic-gate 		return (DDI_FAILURE);
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 	MAKE_DMA_COOKIE(cookie_p, baddr, MMU_PTOB(pfn_no));
10780Sstevel@tonic-gate 	DBG(DBG_BYPASS, mp->dmai_rdip, "cookie %p (%x pages) of total %x\n",
10790Sstevel@tonic-gate 		MMU_PTOB(seg_pfn0), pfn_no, cookie_no);
10800Sstevel@tonic-gate #ifdef	DEBUG
10810Sstevel@tonic-gate 	cookie_p++;
10820Sstevel@tonic-gate 	ASSERT((cookie_p - (ddi_dma_cookie_t *)(win_p + 1)) == cookie_no);
10830Sstevel@tonic-gate #endif	/* DEBUG */
10840Sstevel@tonic-gate 	*win_pp = win_p;
10850Sstevel@tonic-gate 	return (DDI_SUCCESS);
10860Sstevel@tonic-gate noresource:
10870Sstevel@tonic-gate 	if (waitfp != DDI_DMA_DONTWAIT)
10880Sstevel@tonic-gate 		ddi_set_callback(waitfp, dmareq->dmar_arg, &px_kmem_clid);
10890Sstevel@tonic-gate 	return (DDI_DMA_NORESOURCES);
10900Sstevel@tonic-gate }
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate /*
10930Sstevel@tonic-gate  * px_dma_adjust - adjust 1st and last cookie and window sizes
10940Sstevel@tonic-gate  *	remove initial dma page offset from 1st cookie and window size
10950Sstevel@tonic-gate  *	remove last dma page remainder from last cookie and window size
10960Sstevel@tonic-gate  *	fill win_offset of each dma window according to just fixed up
10970Sstevel@tonic-gate  *		each window sizes
10980Sstevel@tonic-gate  *	px_dma_win_t members modified:
10990Sstevel@tonic-gate  *	win_p->win_offset - this window's offset within entire DMA object
11000Sstevel@tonic-gate  *	win_p->win_size	  - xferrable size (in bytes) for this window
11010Sstevel@tonic-gate  *
11020Sstevel@tonic-gate  *	ddi_dma_impl_t members modified:
11030Sstevel@tonic-gate  *	mp->dmai_size	  - 1st window xferrable size
11040Sstevel@tonic-gate  *	mp->dmai_offset   - 0, which is the dma offset of the 1st window
11050Sstevel@tonic-gate  *
11060Sstevel@tonic-gate  *	ddi_dma_cookie_t members modified:
11070Sstevel@tonic-gate  *	cookie_p->dmac_size - 1st and last cookie remove offset or remainder
11080Sstevel@tonic-gate  *	cookie_p->dmac_laddress - 1st cookie add page offset
11090Sstevel@tonic-gate  */
11100Sstevel@tonic-gate static void
11110Sstevel@tonic-gate px_dma_adjust(ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp, px_dma_win_t *win_p)
11120Sstevel@tonic-gate {
11130Sstevel@tonic-gate 	ddi_dma_cookie_t *cookie_p = (ddi_dma_cookie_t *)(win_p + 1);
11140Sstevel@tonic-gate 	size_t pg_offset = mp->dmai_roffset;
11150Sstevel@tonic-gate 	size_t win_offset = 0;
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 	cookie_p->dmac_size -= pg_offset;
11180Sstevel@tonic-gate 	cookie_p->dmac_laddress |= pg_offset;
11190Sstevel@tonic-gate 	win_p->win_size -= pg_offset;
11200Sstevel@tonic-gate 	DBG(DBG_BYPASS, mp->dmai_rdip, "pg0 adjust %lx\n", pg_offset);
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 	mp->dmai_size = win_p->win_size;
11230Sstevel@tonic-gate 	mp->dmai_offset = 0;
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate 	pg_offset += mp->dmai_object.dmao_size;
11260Sstevel@tonic-gate 	pg_offset &= MMU_PAGE_OFFSET;
11270Sstevel@tonic-gate 	if (pg_offset)
11280Sstevel@tonic-gate 		pg_offset = MMU_PAGE_SIZE - pg_offset;
11290Sstevel@tonic-gate 	DBG(DBG_BYPASS, mp->dmai_rdip, "last pg adjust %lx\n", pg_offset);
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 	for (; win_p->win_next; win_p = win_p->win_next) {
11320Sstevel@tonic-gate 		DBG(DBG_BYPASS, mp->dmai_rdip, "win off %p\n", win_offset);
11330Sstevel@tonic-gate 		win_p->win_offset = win_offset;
11340Sstevel@tonic-gate 		win_offset += win_p->win_size;
11350Sstevel@tonic-gate 	}
11360Sstevel@tonic-gate 	/* last window */
11370Sstevel@tonic-gate 	win_p->win_offset = win_offset;
11380Sstevel@tonic-gate 	cookie_p = (ddi_dma_cookie_t *)(win_p + 1);
11390Sstevel@tonic-gate 	cookie_p[win_p->win_ncookies - 1].dmac_size -= pg_offset;
11400Sstevel@tonic-gate 	win_p->win_size -= pg_offset;
11410Sstevel@tonic-gate 	ASSERT((win_offset + win_p->win_size) == mp->dmai_object.dmao_size);
11420Sstevel@tonic-gate }
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate /*
11450Sstevel@tonic-gate  * px_dma_physwin() - carve up dma windows using physical addresses.
11460Sstevel@tonic-gate  *	Called to handle mmu bypass and pci peer-to-peer transfers.
11470Sstevel@tonic-gate  *	Calls px_dma_newwin() to allocate window objects.
11480Sstevel@tonic-gate  *
11490Sstevel@tonic-gate  * Dependency: mp->dmai_pfnlst points to an array of pfns
11500Sstevel@tonic-gate  *
11510Sstevel@tonic-gate  * 1. Each dma window is represented by a px_dma_win_t object.
11520Sstevel@tonic-gate  *	The object will be casted to ddi_dma_win_t and returned
11530Sstevel@tonic-gate  *	to leaf driver through the DDI interface.
11540Sstevel@tonic-gate  * 2. Each dma window can have several dma segments with each
11550Sstevel@tonic-gate  *	segment representing a physically contiguous either memory
11560Sstevel@tonic-gate  *	space (if we are doing an mmu bypass transfer) or pci address
11570Sstevel@tonic-gate  *	space (if we are doing a peer-to-peer transfer).
11580Sstevel@tonic-gate  * 3. Each segment has a DMA cookie to program the DMA engine.
11590Sstevel@tonic-gate  *	The cookies within each DMA window must be located in a
11600Sstevel@tonic-gate  *	contiguous array per ddi_dma_nextcookie(9f).
11610Sstevel@tonic-gate  * 4. The number of DMA segments within each DMA window cannot exceed
11620Sstevel@tonic-gate  *	mp->dmai_attr.dma_attr_sgllen. If the transfer size is
11630Sstevel@tonic-gate  *	too large to fit in the sgllen, the rest needs to be
11640Sstevel@tonic-gate  *	relocated to the next dma window.
11650Sstevel@tonic-gate  * 5. Peer-to-peer DMA segment follows device hi, lo, count_max,
11660Sstevel@tonic-gate  *	and nocross restrictions while bypass DMA follows the set of
11670Sstevel@tonic-gate  *	restrictions with system limits factored in.
11680Sstevel@tonic-gate  *
11690Sstevel@tonic-gate  * Return:
11700Sstevel@tonic-gate  *	mp->dmai_winlst	 - points to a link list of px_dma_win_t objects.
11710Sstevel@tonic-gate  *		Each px_dma_win_t object on the link list contains
11720Sstevel@tonic-gate  *		infomation such as its window size (# of pages),
11730Sstevel@tonic-gate  *		starting offset (also see Restriction), an array of
11740Sstevel@tonic-gate  *		DMA cookies, and # of cookies in the array.
11750Sstevel@tonic-gate  *	mp->dmai_pfnlst	 - NULL, the pfn list is freed to conserve memory.
11760Sstevel@tonic-gate  *	mp->dmai_nwin	 - # of total DMA windows on mp->dmai_winlst.
11770Sstevel@tonic-gate  *	mp->dmai_mapping - starting cookie address
11780Sstevel@tonic-gate  *	mp->dmai_rflags	 - consistent, nosync, no redzone
11790Sstevel@tonic-gate  *	mp->dmai_cookie	 - start of cookie table of the 1st DMA window
11800Sstevel@tonic-gate  *
11810Sstevel@tonic-gate  * Restriction:
11820Sstevel@tonic-gate  *	Each px_dma_win_t object can theoratically start from any offset
11830Sstevel@tonic-gate  *	since the mmu is not involved. However, this implementation
11840Sstevel@tonic-gate  *	always make windows start from page aligned offset (except
11850Sstevel@tonic-gate  *	the 1st window, which follows the requested offset) due to the
11860Sstevel@tonic-gate  *	fact that we are handed a pfn list. This does require device's
11870Sstevel@tonic-gate  *	count_max and attr_seg to be at least MMU_PAGE_SIZE aligned.
11880Sstevel@tonic-gate  */
11890Sstevel@tonic-gate int
11900Sstevel@tonic-gate px_dma_physwin(px_t *px_p, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp)
11910Sstevel@tonic-gate {
11920Sstevel@tonic-gate 	uint_t npages = mp->dmai_ndvmapages;
11930Sstevel@tonic-gate 	int ret, sgllen = mp->dmai_attr.dma_attr_sgllen;
11940Sstevel@tonic-gate 	px_iopfn_t pfn_lo, pfn_hi, prev_pfn;
11950Sstevel@tonic-gate 	px_iopfn_t pfn = PX_GET_MP_PFN(mp, 0);
11960Sstevel@tonic-gate 	uint32_t i, win_no = 0, pfn_no = 1, win_pfn0_index = 0, cookie_no = 0;
11970Sstevel@tonic-gate 	uint64_t count_max, bypass_addr = 0;
11980Sstevel@tonic-gate 	px_dma_win_t **win_pp = (px_dma_win_t **)&mp->dmai_winlst;
11990Sstevel@tonic-gate 	ddi_dma_cookie_t *cookie0_p;
1200*1772Sjl139090 	io_attributes_t attr = PX_GET_TTE_ATTR(mp->dmai_rflags,
1201*1772Sjl139090 	    mp->dmai_attr.dma_attr_flags);
12020Sstevel@tonic-gate 	dev_info_t *dip = px_p->px_dip;
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 	ASSERT(PX_DMA_ISPTP(mp) || PX_DMA_ISBYPASS(mp));
12050Sstevel@tonic-gate 	if (PX_DMA_ISPTP(mp)) { /* ignore sys limits for peer-to-peer */
1206909Segillett 		ddi_dma_attr_t *dev_attr_p = PX_DEV_ATTR(mp);
12070Sstevel@tonic-gate 		uint64_t nocross = dev_attr_p->dma_attr_seg;
12080Sstevel@tonic-gate 		px_pec_t *pec_p = px_p->px_pec_p;
12090Sstevel@tonic-gate 		px_iopfn_t pfn_last = PX_DMA_ISPTP32(mp) ?
12100Sstevel@tonic-gate 				pec_p->pec_last32_pfn - pec_p->pec_base32_pfn :
12110Sstevel@tonic-gate 				pec_p->pec_last64_pfn - pec_p->pec_base64_pfn;
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 		if (nocross && (nocross < UINT32_MAX))
12140Sstevel@tonic-gate 			return (DDI_DMA_NOMAPPING);
12150Sstevel@tonic-gate 		if (dev_attr_p->dma_attr_align > MMU_PAGE_SIZE)
12160Sstevel@tonic-gate 			return (DDI_DMA_NOMAPPING);
12170Sstevel@tonic-gate 		pfn_lo = MMU_BTOP(dev_attr_p->dma_attr_addr_lo);
12180Sstevel@tonic-gate 		pfn_hi = MMU_BTOP(dev_attr_p->dma_attr_addr_hi);
12190Sstevel@tonic-gate 		pfn_hi = MIN(pfn_hi, pfn_last);
12200Sstevel@tonic-gate 		if ((pfn_lo > pfn_hi) || (pfn < pfn_lo))
12210Sstevel@tonic-gate 			return (DDI_DMA_NOMAPPING);
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 		count_max = dev_attr_p->dma_attr_count_max;
12240Sstevel@tonic-gate 		count_max = MIN(count_max, nocross);
12250Sstevel@tonic-gate 		/*
12260Sstevel@tonic-gate 		 * the following count_max trim is not done because we are
12270Sstevel@tonic-gate 		 * making sure pfn_lo <= pfn <= pfn_hi inside the loop
12280Sstevel@tonic-gate 		 * count_max=MIN(count_max, MMU_PTOB(pfn_hi - pfn_lo + 1)-1);
12290Sstevel@tonic-gate 		 */
12300Sstevel@tonic-gate 	} else { /* bypass hi/lo/count_max have been processed by attr2hdl() */
12310Sstevel@tonic-gate 		count_max = mp->dmai_attr.dma_attr_count_max;
12320Sstevel@tonic-gate 		pfn_lo = MMU_BTOP(mp->dmai_attr.dma_attr_addr_lo);
12330Sstevel@tonic-gate 		pfn_hi = MMU_BTOP(mp->dmai_attr.dma_attr_addr_hi);
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate 		if (px_lib_iommu_getbypass(dip, MMU_PTOB(pfn),
12360Sstevel@tonic-gate 				attr, &bypass_addr) != DDI_SUCCESS) {
1237671Skrishnae 			cmn_err(CE_WARN, "bypass cookie failure %lx\n", pfn);
12380Sstevel@tonic-gate 			return (DDI_DMA_NOMAPPING);
12390Sstevel@tonic-gate 		}
12400Sstevel@tonic-gate 		pfn = MMU_BTOP(bypass_addr);
12410Sstevel@tonic-gate 	}
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 	/* pfn: absolute (bypass mode) or relative (p2p mode) */
12440Sstevel@tonic-gate 	for (prev_pfn = pfn, i = 1; i < npages;
12450Sstevel@tonic-gate 	    i++, prev_pfn = pfn, pfn_no++) {
12460Sstevel@tonic-gate 		pfn = PX_GET_MP_PFN1(mp, i);
12470Sstevel@tonic-gate 		if (bypass_addr) {
12480Sstevel@tonic-gate 			if (px_lib_iommu_getbypass(dip, MMU_PTOB(pfn), attr,
12490Sstevel@tonic-gate 					&bypass_addr) != DDI_SUCCESS) {
12500Sstevel@tonic-gate 				ret = DDI_DMA_NOMAPPING;
12510Sstevel@tonic-gate 				goto err;
12520Sstevel@tonic-gate 			}
12530Sstevel@tonic-gate 			pfn = MMU_BTOP(bypass_addr);
12540Sstevel@tonic-gate 		}
12550Sstevel@tonic-gate 		if ((pfn == prev_pfn + 1) &&
12560Sstevel@tonic-gate 				(MMU_PTOB(pfn_no + 1) - 1 <= count_max))
12570Sstevel@tonic-gate 			continue;
12580Sstevel@tonic-gate 		if ((pfn < pfn_lo) || (prev_pfn > pfn_hi)) {
12590Sstevel@tonic-gate 			ret = DDI_DMA_NOMAPPING;
12600Sstevel@tonic-gate 			goto err;
12610Sstevel@tonic-gate 		}
12620Sstevel@tonic-gate 		cookie_no++;
12630Sstevel@tonic-gate 		pfn_no = 0;
12640Sstevel@tonic-gate 		if (cookie_no < sgllen)
12650Sstevel@tonic-gate 			continue;
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 		DBG(DBG_BYPASS, mp->dmai_rdip, "newwin pfn[%x-%x] %x cks\n",
12680Sstevel@tonic-gate 			win_pfn0_index, i - 1, cookie_no);
12690Sstevel@tonic-gate 		if (ret = px_dma_newwin(dip, dmareq, mp, cookie_no,
12700Sstevel@tonic-gate 			win_pfn0_index, i - 1, win_pp, count_max, bypass_addr))
12710Sstevel@tonic-gate 			goto err;
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate 		win_pp = &(*win_pp)->win_next;	/* win_pp = *(win_pp) */
12740Sstevel@tonic-gate 		win_no++;
12750Sstevel@tonic-gate 		win_pfn0_index = i;
12760Sstevel@tonic-gate 		cookie_no = 0;
12770Sstevel@tonic-gate 	}
12780Sstevel@tonic-gate 	if (pfn > pfn_hi) {
12790Sstevel@tonic-gate 		ret = DDI_DMA_NOMAPPING;
12800Sstevel@tonic-gate 		goto err;
12810Sstevel@tonic-gate 	}
12820Sstevel@tonic-gate 	cookie_no++;
12830Sstevel@tonic-gate 	DBG(DBG_BYPASS, mp->dmai_rdip, "newwin pfn[%x-%x] %x cks\n",
12840Sstevel@tonic-gate 		win_pfn0_index, i - 1, cookie_no);
12850Sstevel@tonic-gate 	if (ret = px_dma_newwin(dip, dmareq, mp, cookie_no, win_pfn0_index,
12860Sstevel@tonic-gate 		i - 1, win_pp, count_max, bypass_addr))
12870Sstevel@tonic-gate 		goto err;
12880Sstevel@tonic-gate 	win_no++;
12890Sstevel@tonic-gate 	px_dma_adjust(dmareq, mp, mp->dmai_winlst);
12900Sstevel@tonic-gate 	mp->dmai_nwin = win_no;
12910Sstevel@tonic-gate 	mp->dmai_rflags |= DDI_DMA_CONSISTENT | DMP_NOSYNC;
12920Sstevel@tonic-gate 	mp->dmai_rflags &= ~DDI_DMA_REDZONE;
1293909Segillett 	mp->dmai_flags |= PX_DMAI_FLAGS_NOSYNC;
1294909Segillett 	cookie0_p = (ddi_dma_cookie_t *)(PX_WINLST(mp) + 1);
1295909Segillett 	mp->dmai_cookie = PX_WINLST(mp)->win_ncookies > 1 ? cookie0_p + 1 : 0;
12960Sstevel@tonic-gate 	mp->dmai_mapping = cookie0_p->dmac_laddress;
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate 	px_dma_freepfn(mp);
12990Sstevel@tonic-gate 	return (DDI_DMA_MAPPED);
13000Sstevel@tonic-gate err:
13010Sstevel@tonic-gate 	px_dma_freewin(mp);
13020Sstevel@tonic-gate 	return (ret);
13030Sstevel@tonic-gate }
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate int
13060Sstevel@tonic-gate px_dma_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_impl_t *mp,
13070Sstevel@tonic-gate 	enum ddi_dma_ctlops cmd, off_t *offp, size_t *lenp, caddr_t *objp,
13080Sstevel@tonic-gate 	uint_t cache_flags)
13090Sstevel@tonic-gate {
13100Sstevel@tonic-gate 	switch (cmd) {
13110Sstevel@tonic-gate 	case DDI_DMA_SYNC:
13120Sstevel@tonic-gate 		return (DDI_SUCCESS);
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate 	case DDI_DMA_HTOC: {
13150Sstevel@tonic-gate 		off_t off = *offp;
13160Sstevel@tonic-gate 		ddi_dma_cookie_t *loop_cp, *cp;
13170Sstevel@tonic-gate 		px_dma_win_t *win_p = mp->dmai_winlst;
13180Sstevel@tonic-gate 
13190Sstevel@tonic-gate 		if (off >= mp->dmai_object.dmao_size)
13200Sstevel@tonic-gate 			return (DDI_FAILURE);
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate 		/* locate window */
13230Sstevel@tonic-gate 		while (win_p->win_offset + win_p->win_size <= off)
13240Sstevel@tonic-gate 			win_p = win_p->win_next;
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate 		loop_cp = cp = (ddi_dma_cookie_t *)(win_p + 1);
13270Sstevel@tonic-gate 		mp->dmai_offset = win_p->win_offset;
13280Sstevel@tonic-gate 		mp->dmai_size   = win_p->win_size;
13290Sstevel@tonic-gate 		mp->dmai_mapping = cp->dmac_laddress; /* cookie0 start addr */
13300Sstevel@tonic-gate 
13310Sstevel@tonic-gate 		/* adjust cookie addr/len if we are not on cookie boundary */
13320Sstevel@tonic-gate 		off -= win_p->win_offset;	   /* offset within window */
13330Sstevel@tonic-gate 		for (; off >= loop_cp->dmac_size; loop_cp++)
13340Sstevel@tonic-gate 			off -= loop_cp->dmac_size; /* offset within cookie */
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 		mp->dmai_cookie = loop_cp + 1;
13370Sstevel@tonic-gate 		win_p->win_curseg = loop_cp - cp;
13380Sstevel@tonic-gate 		cp = (ddi_dma_cookie_t *)objp;
13390Sstevel@tonic-gate 		MAKE_DMA_COOKIE(cp, loop_cp->dmac_laddress + off,
13400Sstevel@tonic-gate 			loop_cp->dmac_size - off);
13410Sstevel@tonic-gate 
13420Sstevel@tonic-gate 		DBG(DBG_DMA_CTL, dip,
13430Sstevel@tonic-gate 			"HTOC: cookie - dmac_laddress=%p dmac_size=%x\n",
13440Sstevel@tonic-gate 			cp->dmac_laddress, cp->dmac_size);
13450Sstevel@tonic-gate 		}
13460Sstevel@tonic-gate 		return (DDI_SUCCESS);
13470Sstevel@tonic-gate 
13480Sstevel@tonic-gate 	case DDI_DMA_REPWIN:
13490Sstevel@tonic-gate 		*offp = mp->dmai_offset;
13500Sstevel@tonic-gate 		*lenp = mp->dmai_size;
13510Sstevel@tonic-gate 		return (DDI_SUCCESS);
13520Sstevel@tonic-gate 
13530Sstevel@tonic-gate 	case DDI_DMA_MOVWIN: {
13540Sstevel@tonic-gate 		off_t off = *offp;
13550Sstevel@tonic-gate 		ddi_dma_cookie_t *cp;
13560Sstevel@tonic-gate 		px_dma_win_t *win_p = mp->dmai_winlst;
13570Sstevel@tonic-gate 
13580Sstevel@tonic-gate 		if (off >= mp->dmai_object.dmao_size)
13590Sstevel@tonic-gate 			return (DDI_FAILURE);
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate 		/* locate window */
13620Sstevel@tonic-gate 		while (win_p->win_offset + win_p->win_size <= off)
13630Sstevel@tonic-gate 			win_p = win_p->win_next;
13640Sstevel@tonic-gate 
13650Sstevel@tonic-gate 		cp = (ddi_dma_cookie_t *)(win_p + 1);
13660Sstevel@tonic-gate 		mp->dmai_offset = win_p->win_offset;
13670Sstevel@tonic-gate 		mp->dmai_size   = win_p->win_size;
13680Sstevel@tonic-gate 		mp->dmai_mapping = cp->dmac_laddress;	/* cookie0 star addr */
13690Sstevel@tonic-gate 		mp->dmai_cookie = cp + 1;
13700Sstevel@tonic-gate 		win_p->win_curseg = 0;
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate 		*(ddi_dma_cookie_t *)objp = *cp;
13730Sstevel@tonic-gate 		*offp = win_p->win_offset;
13740Sstevel@tonic-gate 		*lenp = win_p->win_size;
13750Sstevel@tonic-gate 		DBG(DBG_DMA_CTL, dip,
13760Sstevel@tonic-gate 			"HTOC: cookie - dmac_laddress=%p dmac_size=%x\n",
13770Sstevel@tonic-gate 			cp->dmac_laddress, cp->dmac_size);
13780Sstevel@tonic-gate 		}
13790Sstevel@tonic-gate 		return (DDI_SUCCESS);
13800Sstevel@tonic-gate 
13810Sstevel@tonic-gate 	case DDI_DMA_NEXTWIN: {
13820Sstevel@tonic-gate 		px_dma_win_t *win_p = *(px_dma_win_t **)offp;
13830Sstevel@tonic-gate 		px_dma_win_t **nw_pp = (px_dma_win_t **)objp;
13840Sstevel@tonic-gate 		ddi_dma_cookie_t *cp;
13850Sstevel@tonic-gate 		if (!win_p) {
13860Sstevel@tonic-gate 			*nw_pp = mp->dmai_winlst;
13870Sstevel@tonic-gate 			return (DDI_SUCCESS);
13880Sstevel@tonic-gate 		}
13890Sstevel@tonic-gate 
13900Sstevel@tonic-gate 		if (win_p->win_offset != mp->dmai_offset)
13910Sstevel@tonic-gate 			return (DDI_DMA_STALE);
13920Sstevel@tonic-gate 		if (!win_p->win_next)
13930Sstevel@tonic-gate 			return (DDI_DMA_DONE);
13940Sstevel@tonic-gate 		win_p = win_p->win_next;
13950Sstevel@tonic-gate 		cp = (ddi_dma_cookie_t *)(win_p + 1);
13960Sstevel@tonic-gate 		mp->dmai_offset = win_p->win_offset;
13970Sstevel@tonic-gate 		mp->dmai_size   = win_p->win_size;
13980Sstevel@tonic-gate 		mp->dmai_mapping = cp->dmac_laddress;   /* cookie0 star addr */
13990Sstevel@tonic-gate 		mp->dmai_cookie = cp + 1;
14000Sstevel@tonic-gate 		win_p->win_curseg = 0;
14010Sstevel@tonic-gate 		*nw_pp = win_p;
14020Sstevel@tonic-gate 		}
14030Sstevel@tonic-gate 		return (DDI_SUCCESS);
14040Sstevel@tonic-gate 
14050Sstevel@tonic-gate 	case DDI_DMA_NEXTSEG: {
14060Sstevel@tonic-gate 		px_dma_win_t *w_p = *(px_dma_win_t **)offp;
14070Sstevel@tonic-gate 		if (w_p->win_offset != mp->dmai_offset)
14080Sstevel@tonic-gate 			return (DDI_DMA_STALE);
14090Sstevel@tonic-gate 		if (w_p->win_curseg + 1 >= w_p->win_ncookies)
14100Sstevel@tonic-gate 			return (DDI_DMA_DONE);
14110Sstevel@tonic-gate 		w_p->win_curseg++;
14120Sstevel@tonic-gate 		}
14130Sstevel@tonic-gate 		*(ddi_dma_seg_t *)objp = (ddi_dma_seg_t)mp;
14140Sstevel@tonic-gate 		return (DDI_SUCCESS);
14150Sstevel@tonic-gate 
14160Sstevel@tonic-gate 	case DDI_DMA_SEGTOC: {
14170Sstevel@tonic-gate 		px_dma_win_t *win_p = mp->dmai_winlst;
14180Sstevel@tonic-gate 		off_t off = mp->dmai_offset;
14190Sstevel@tonic-gate 		ddi_dma_cookie_t *cp;
14200Sstevel@tonic-gate 		int i;
14210Sstevel@tonic-gate 
14220Sstevel@tonic-gate 		/* locate active window */
14230Sstevel@tonic-gate 		for (; win_p->win_offset != off; win_p = win_p->win_next);
14240Sstevel@tonic-gate 		cp = (ddi_dma_cookie_t *)(win_p + 1);
14250Sstevel@tonic-gate 		for (i = 0; i < win_p->win_curseg; i++, cp++)
14260Sstevel@tonic-gate 			off += cp->dmac_size;
14270Sstevel@tonic-gate 		*offp = off;
14280Sstevel@tonic-gate 		*lenp = cp->dmac_size;
14290Sstevel@tonic-gate 		*(ddi_dma_cookie_t *)objp = *cp;	/* copy cookie */
14300Sstevel@tonic-gate 		}
14310Sstevel@tonic-gate 		return (DDI_SUCCESS);
14320Sstevel@tonic-gate 
14330Sstevel@tonic-gate 	case DDI_DMA_COFF: {
14340Sstevel@tonic-gate 		px_dma_win_t *win_p;
14350Sstevel@tonic-gate 		ddi_dma_cookie_t *cp;
14360Sstevel@tonic-gate 		uint64_t addr, key = ((ddi_dma_cookie_t *)offp)->dmac_laddress;
14370Sstevel@tonic-gate 		size_t win_off;
14380Sstevel@tonic-gate 
14390Sstevel@tonic-gate 		for (win_p = mp->dmai_winlst; win_p; win_p = win_p->win_next) {
14400Sstevel@tonic-gate 			int i;
14410Sstevel@tonic-gate 			win_off = 0;
14420Sstevel@tonic-gate 			cp = (ddi_dma_cookie_t *)(win_p + 1);
14430Sstevel@tonic-gate 			for (i = 0; i < win_p->win_ncookies; i++, cp++) {
14440Sstevel@tonic-gate 				size_t sz = cp->dmac_size;
14450Sstevel@tonic-gate 
14460Sstevel@tonic-gate 				addr = cp->dmac_laddress;
14470Sstevel@tonic-gate 				if ((addr <= key) && (addr + sz >= key))
14480Sstevel@tonic-gate 					goto found;
14490Sstevel@tonic-gate 				win_off += sz;
14500Sstevel@tonic-gate 			}
14510Sstevel@tonic-gate 		}
14520Sstevel@tonic-gate 		return (DDI_FAILURE);
14530Sstevel@tonic-gate found:
14540Sstevel@tonic-gate 		*objp = (caddr_t)(win_p->win_offset + win_off + (key - addr));
14550Sstevel@tonic-gate 		return (DDI_SUCCESS);
14560Sstevel@tonic-gate 		}
14570Sstevel@tonic-gate 	default:
14580Sstevel@tonic-gate 		DBG(DBG_DMA_CTL, dip, "unknown command (%x): rdip=%s%d\n",
14590Sstevel@tonic-gate 			cmd, ddi_driver_name(rdip), ddi_get_instance(rdip));
14600Sstevel@tonic-gate 		break;
14610Sstevel@tonic-gate 	}
14620Sstevel@tonic-gate 	return (DDI_FAILURE);
14630Sstevel@tonic-gate }
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate static void
14660Sstevel@tonic-gate px_dvma_debug_init(px_mmu_t *mmu_p)
14670Sstevel@tonic-gate {
14680Sstevel@tonic-gate 	size_t sz = sizeof (struct px_dvma_rec) * px_dvma_debug_rec;
14690Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&mmu_p->dvma_debug_lock));
14700Sstevel@tonic-gate 	cmn_err(CE_NOTE, "PCI Express DVMA %p stat ON", mmu_p);
14710Sstevel@tonic-gate 
14720Sstevel@tonic-gate 	mmu_p->dvma_alloc_rec = kmem_alloc(sz, KM_SLEEP);
14730Sstevel@tonic-gate 	mmu_p->dvma_free_rec = kmem_alloc(sz, KM_SLEEP);
14740Sstevel@tonic-gate 
14750Sstevel@tonic-gate 	mmu_p->dvma_active_list = NULL;
14760Sstevel@tonic-gate 	mmu_p->dvma_alloc_rec_index = 0;
14770Sstevel@tonic-gate 	mmu_p->dvma_free_rec_index = 0;
14780Sstevel@tonic-gate 	mmu_p->dvma_active_count = 0;
14790Sstevel@tonic-gate }
14800Sstevel@tonic-gate 
14810Sstevel@tonic-gate void
14820Sstevel@tonic-gate px_dvma_debug_fini(px_mmu_t *mmu_p)
14830Sstevel@tonic-gate {
14840Sstevel@tonic-gate 	struct px_dvma_rec *prev, *ptr;
14850Sstevel@tonic-gate 	size_t sz = sizeof (struct px_dvma_rec) * px_dvma_debug_rec;
14860Sstevel@tonic-gate 	uint64_t mask = ~(1ull << mmu_p->mmu_inst);
14870Sstevel@tonic-gate 	cmn_err(CE_NOTE, "PCI Express DVMA %p stat OFF", mmu_p);
14880Sstevel@tonic-gate 
14890Sstevel@tonic-gate 	kmem_free(mmu_p->dvma_alloc_rec, sz);
14900Sstevel@tonic-gate 	kmem_free(mmu_p->dvma_free_rec, sz);
14910Sstevel@tonic-gate 	mmu_p->dvma_alloc_rec = mmu_p->dvma_free_rec = NULL;
14920Sstevel@tonic-gate 
14930Sstevel@tonic-gate 	prev = mmu_p->dvma_active_list;
14940Sstevel@tonic-gate 	if (!prev)
14950Sstevel@tonic-gate 		return;
14960Sstevel@tonic-gate 	for (ptr = prev->next; ptr; prev = ptr, ptr = ptr->next)
14970Sstevel@tonic-gate 		kmem_free(prev, sizeof (struct px_dvma_rec));
14980Sstevel@tonic-gate 	kmem_free(prev, sizeof (struct px_dvma_rec));
14990Sstevel@tonic-gate 
15000Sstevel@tonic-gate 	mmu_p->dvma_active_list = NULL;
15010Sstevel@tonic-gate 	mmu_p->dvma_alloc_rec_index = 0;
15020Sstevel@tonic-gate 	mmu_p->dvma_free_rec_index = 0;
15030Sstevel@tonic-gate 	mmu_p->dvma_active_count = 0;
15040Sstevel@tonic-gate 
15050Sstevel@tonic-gate 	px_dvma_debug_off &= mask;
15060Sstevel@tonic-gate 	px_dvma_debug_on &= mask;
15070Sstevel@tonic-gate }
15080Sstevel@tonic-gate 
15090Sstevel@tonic-gate void
15100Sstevel@tonic-gate px_dvma_alloc_debug(px_mmu_t *mmu_p, char *address, uint_t len,
15110Sstevel@tonic-gate 	ddi_dma_impl_t *mp)
15120Sstevel@tonic-gate {
15130Sstevel@tonic-gate 	struct px_dvma_rec *ptr;
15140Sstevel@tonic-gate 	mutex_enter(&mmu_p->dvma_debug_lock);
15150Sstevel@tonic-gate 
15160Sstevel@tonic-gate 	if (!mmu_p->dvma_alloc_rec)
15170Sstevel@tonic-gate 		px_dvma_debug_init(mmu_p);
1518909Segillett 	if (PX_DVMA_DBG_OFF(mmu_p)) {
15190Sstevel@tonic-gate 		px_dvma_debug_fini(mmu_p);
15200Sstevel@tonic-gate 		goto done;
15210Sstevel@tonic-gate 	}
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate 	ptr = &mmu_p->dvma_alloc_rec[mmu_p->dvma_alloc_rec_index];
15240Sstevel@tonic-gate 	ptr->dvma_addr = address;
15250Sstevel@tonic-gate 	ptr->len = len;
15260Sstevel@tonic-gate 	ptr->mp = mp;
15270Sstevel@tonic-gate 	if (++mmu_p->dvma_alloc_rec_index == px_dvma_debug_rec)
15280Sstevel@tonic-gate 		mmu_p->dvma_alloc_rec_index = 0;
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate 	ptr = kmem_alloc(sizeof (struct px_dvma_rec), KM_SLEEP);
15310Sstevel@tonic-gate 	ptr->dvma_addr = address;
15320Sstevel@tonic-gate 	ptr->len = len;
15330Sstevel@tonic-gate 	ptr->mp = mp;
15340Sstevel@tonic-gate 
15350Sstevel@tonic-gate 	ptr->next = mmu_p->dvma_active_list;
15360Sstevel@tonic-gate 	mmu_p->dvma_active_list = ptr;
15370Sstevel@tonic-gate 	mmu_p->dvma_active_count++;
15380Sstevel@tonic-gate done:
15390Sstevel@tonic-gate 	mutex_exit(&mmu_p->dvma_debug_lock);
15400Sstevel@tonic-gate }
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate void
15430Sstevel@tonic-gate px_dvma_free_debug(px_mmu_t *mmu_p, char *address, uint_t len,
15440Sstevel@tonic-gate     ddi_dma_impl_t *mp)
15450Sstevel@tonic-gate {
15460Sstevel@tonic-gate 	struct px_dvma_rec *ptr, *ptr_save;
15470Sstevel@tonic-gate 	mutex_enter(&mmu_p->dvma_debug_lock);
15480Sstevel@tonic-gate 
15490Sstevel@tonic-gate 	if (!mmu_p->dvma_alloc_rec)
15500Sstevel@tonic-gate 		px_dvma_debug_init(mmu_p);
1551909Segillett 	if (PX_DVMA_DBG_OFF(mmu_p)) {
15520Sstevel@tonic-gate 		px_dvma_debug_fini(mmu_p);
15530Sstevel@tonic-gate 		goto done;
15540Sstevel@tonic-gate 	}
15550Sstevel@tonic-gate 
15560Sstevel@tonic-gate 	ptr = &mmu_p->dvma_free_rec[mmu_p->dvma_free_rec_index];
15570Sstevel@tonic-gate 	ptr->dvma_addr = address;
15580Sstevel@tonic-gate 	ptr->len = len;
15590Sstevel@tonic-gate 	ptr->mp = mp;
15600Sstevel@tonic-gate 	if (++mmu_p->dvma_free_rec_index == px_dvma_debug_rec)
15610Sstevel@tonic-gate 		mmu_p->dvma_free_rec_index = 0;
15620Sstevel@tonic-gate 
15630Sstevel@tonic-gate 	ptr_save = mmu_p->dvma_active_list;
15640Sstevel@tonic-gate 	for (ptr = ptr_save; ptr; ptr = ptr->next) {
15650Sstevel@tonic-gate 		if ((ptr->dvma_addr == address) && (ptr->len = len))
15660Sstevel@tonic-gate 			break;
15670Sstevel@tonic-gate 		ptr_save = ptr;
15680Sstevel@tonic-gate 	}
15690Sstevel@tonic-gate 	if (!ptr) {
15700Sstevel@tonic-gate 		cmn_err(CE_WARN, "bad dvma free addr=%lx len=%x",
15710Sstevel@tonic-gate 			(long)address, len);
15720Sstevel@tonic-gate 		goto done;
15730Sstevel@tonic-gate 	}
15740Sstevel@tonic-gate 	if (ptr == mmu_p->dvma_active_list)
15750Sstevel@tonic-gate 		mmu_p->dvma_active_list = ptr->next;
15760Sstevel@tonic-gate 	else
15770Sstevel@tonic-gate 		ptr_save->next = ptr->next;
15780Sstevel@tonic-gate 	kmem_free(ptr, sizeof (struct px_dvma_rec));
15790Sstevel@tonic-gate 	mmu_p->dvma_active_count--;
15800Sstevel@tonic-gate done:
15810Sstevel@tonic-gate 	mutex_exit(&mmu_p->dvma_debug_lock);
15820Sstevel@tonic-gate }
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate #ifdef	DEBUG
15850Sstevel@tonic-gate void
15860Sstevel@tonic-gate px_dump_dma_handle(uint64_t flag, dev_info_t *dip, ddi_dma_impl_t *hp)
15870Sstevel@tonic-gate {
15880Sstevel@tonic-gate 	DBG(flag, dip, "mp(%p): flags=%x mapping=%lx xfer_size=%x\n",
15890Sstevel@tonic-gate 		hp, hp->dmai_inuse, hp->dmai_mapping, hp->dmai_size);
15900Sstevel@tonic-gate 	DBG(flag|DBG_CONT, dip, "\tnpages=%x roffset=%x rflags=%x nwin=%x\n",
15910Sstevel@tonic-gate 		hp->dmai_ndvmapages, hp->dmai_roffset, hp->dmai_rflags,
15920Sstevel@tonic-gate 		hp->dmai_nwin);
15930Sstevel@tonic-gate 	DBG(flag|DBG_CONT, dip, "\twinsize=%x tte=%p pfnlst=%p pfn0=%p\n",
15940Sstevel@tonic-gate 		hp->dmai_winsize, hp->dmai_tte, hp->dmai_pfnlst, hp->dmai_pfn0);
15950Sstevel@tonic-gate 	DBG(flag|DBG_CONT, dip, "\twinlst=%x obj=%p attr=%p ckp=%p\n",
15960Sstevel@tonic-gate 		hp->dmai_winlst, &hp->dmai_object, &hp->dmai_attr,
15970Sstevel@tonic-gate 		hp->dmai_cookie);
15980Sstevel@tonic-gate }
15990Sstevel@tonic-gate #endif	/* DEBUG */
1600