xref: /illumos-gate/usr/src/uts/sun4/io/px/px_dma.c (revision b65731f1)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51de45cd9Sgovinda  * Common Development and Distribution License (the "License").
61de45cd9Sgovinda  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
221de45cd9Sgovinda  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * PCI Express nexus DVMA and DMA core routines:
307c478bd9Sstevel@tonic-gate  *	dma_map/dma_bind_handle implementation
317c478bd9Sstevel@tonic-gate  *	bypass and peer-to-peer support
327c478bd9Sstevel@tonic-gate  *	fast track DVMA space allocation
337c478bd9Sstevel@tonic-gate  *	runtime DVMA debug
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
377c478bd9Sstevel@tonic-gate #include <sys/async.h>
387c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
397c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
407c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
417c478bd9Sstevel@tonic-gate #include "px_obj.h"
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * px_dma_allocmp - Allocate a pci dma implementation structure
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  * An extra ddi_dma_attr structure is bundled with the usual ddi_dma_impl
497c478bd9Sstevel@tonic-gate  * to hold unmodified device limits. The ddi_dma_attr inside the
507c478bd9Sstevel@tonic-gate  * ddi_dma_impl structure is augumented with system limits to enhance
517c478bd9Sstevel@tonic-gate  * DVMA performance at runtime. The unaugumented device limits saved
527c478bd9Sstevel@tonic-gate  * right after (accessed through (ddi_dma_attr_t *)(mp + 1)) is used
537c478bd9Sstevel@tonic-gate  * strictly for peer-to-peer transfers which do not obey system limits.
547c478bd9Sstevel@tonic-gate  *
557c478bd9Sstevel@tonic-gate  * return: DDI_SUCCESS DDI_DMA_NORESOURCES
567c478bd9Sstevel@tonic-gate  */
577c478bd9Sstevel@tonic-gate ddi_dma_impl_t *
587c478bd9Sstevel@tonic-gate px_dma_allocmp(dev_info_t *dip, dev_info_t *rdip, int (*waitfp)(caddr_t),
597c478bd9Sstevel@tonic-gate 	caddr_t arg)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate 	register ddi_dma_impl_t *mp;
627c478bd9Sstevel@tonic-gate 	int sleep = (waitfp == DDI_DMA_SLEEP) ? KM_SLEEP : KM_NOSLEEP;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	/* Caution: we don't use zalloc to enhance performance! */
657c478bd9Sstevel@tonic-gate 	if ((mp = kmem_alloc(sizeof (px_dma_hdl_t), sleep)) == 0) {
667c478bd9Sstevel@tonic-gate 		DBG(DBG_DMA_MAP, dip, "can't alloc dma_handle\n");
677c478bd9Sstevel@tonic-gate 		if (waitfp != DDI_DMA_DONTWAIT) {
687c478bd9Sstevel@tonic-gate 			DBG(DBG_DMA_MAP, dip, "alloc_mp kmem cb\n");
697c478bd9Sstevel@tonic-gate 			ddi_set_callback(waitfp, arg, &px_kmem_clid);
707c478bd9Sstevel@tonic-gate 		}
717c478bd9Sstevel@tonic-gate 		return (mp);
727c478bd9Sstevel@tonic-gate 	}
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	mp->dmai_rdip = rdip;
757c478bd9Sstevel@tonic-gate 	mp->dmai_flags = 0;
767c478bd9Sstevel@tonic-gate 	mp->dmai_pfnlst = NULL;
777c478bd9Sstevel@tonic-gate 	mp->dmai_winlst = NULL;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	/*
807c478bd9Sstevel@tonic-gate 	 * kmem_alloc debug: the following fields are not zero-ed
817c478bd9Sstevel@tonic-gate 	 * mp->dmai_mapping = 0;
827c478bd9Sstevel@tonic-gate 	 * mp->dmai_size = 0;
837c478bd9Sstevel@tonic-gate 	 * mp->dmai_offset = 0;
847c478bd9Sstevel@tonic-gate 	 * mp->dmai_minxfer = 0;
857c478bd9Sstevel@tonic-gate 	 * mp->dmai_burstsizes = 0;
867c478bd9Sstevel@tonic-gate 	 * mp->dmai_ndvmapages = 0;
877c478bd9Sstevel@tonic-gate 	 * mp->dmai_pool/roffset = 0;
887c478bd9Sstevel@tonic-gate 	 * mp->dmai_rflags = 0;
897c478bd9Sstevel@tonic-gate 	 * mp->dmai_inuse/flags
907c478bd9Sstevel@tonic-gate 	 * mp->dmai_nwin = 0;
917c478bd9Sstevel@tonic-gate 	 * mp->dmai_winsize = 0;
927c478bd9Sstevel@tonic-gate 	 * mp->dmai_nexus_private/tte = 0;
937c478bd9Sstevel@tonic-gate 	 * mp->dmai_iopte/pfnlst
947c478bd9Sstevel@tonic-gate 	 * mp->dmai_sbi/pfn0 = 0;
957c478bd9Sstevel@tonic-gate 	 * mp->dmai_minfo/winlst/fdvma
967c478bd9Sstevel@tonic-gate 	 * mp->dmai_rdip
977c478bd9Sstevel@tonic-gate 	 * bzero(&mp->dmai_object, sizeof (ddi_dma_obj_t));
987c478bd9Sstevel@tonic-gate 	 * bzero(&mp->dmai_attr, sizeof (ddi_dma_attr_t));
997c478bd9Sstevel@tonic-gate 	 * mp->dmai_cookie = 0;
1007c478bd9Sstevel@tonic-gate 	 */
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	mp->dmai_attr.dma_attr_version = (uint_t)DMA_ATTR_VERSION;
1037c478bd9Sstevel@tonic-gate 	mp->dmai_attr.dma_attr_flags = (uint_t)0;
1047c478bd9Sstevel@tonic-gate 	mp->dmai_fault = 0;
1057c478bd9Sstevel@tonic-gate 	mp->dmai_fault_check = NULL;
1067c478bd9Sstevel@tonic-gate 	mp->dmai_fault_notify = NULL;
107b6ec8a57Svgadre 
108b6ec8a57Svgadre 	mp->dmai_error.err_ena = 0;
109b6ec8a57Svgadre 	mp->dmai_error.err_status = DDI_FM_OK;
110b6ec8a57Svgadre 	mp->dmai_error.err_expected = DDI_FM_ERR_UNEXPECTED;
111b6ec8a57Svgadre 	mp->dmai_error.err_ontrap = NULL;
112b6ec8a57Svgadre 	mp->dmai_error.err_fep = NULL;
113b6ec8a57Svgadre 
1141de45cd9Sgovinda 	if (px_child_prefetch(mp->dmai_rdip))
1151de45cd9Sgovinda 		mp->dmai_flags |= (PX_DMAI_FLAGS_MAP_BUFZONE |
1161de45cd9Sgovinda 		    PX_DMAI_FLAGS_REDZONE);
1171de45cd9Sgovinda 
1187c478bd9Sstevel@tonic-gate 	return (mp);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate void
1227c478bd9Sstevel@tonic-gate px_dma_freemp(ddi_dma_impl_t *mp)
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate 	if (mp->dmai_ndvmapages > 1)
1257c478bd9Sstevel@tonic-gate 		px_dma_freepfn(mp);
1267c478bd9Sstevel@tonic-gate 	if (mp->dmai_winlst)
1277c478bd9Sstevel@tonic-gate 		px_dma_freewin(mp);
1287c478bd9Sstevel@tonic-gate 	kmem_free(mp, sizeof (px_dma_hdl_t));
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate void
1327c478bd9Sstevel@tonic-gate px_dma_freepfn(ddi_dma_impl_t *mp)
1337c478bd9Sstevel@tonic-gate {
1347c478bd9Sstevel@tonic-gate 	void *addr = mp->dmai_pfnlst;
1357c478bd9Sstevel@tonic-gate 	if (addr) {
1367c478bd9Sstevel@tonic-gate 		size_t npages = mp->dmai_ndvmapages;
1377c478bd9Sstevel@tonic-gate 		if (npages > 1)
1387c478bd9Sstevel@tonic-gate 			kmem_free(addr, npages * sizeof (px_iopfn_t));
1397c478bd9Sstevel@tonic-gate 		mp->dmai_pfnlst = NULL;
1407c478bd9Sstevel@tonic-gate 	}
1417c478bd9Sstevel@tonic-gate 	mp->dmai_ndvmapages = 0;
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate /*
1457c478bd9Sstevel@tonic-gate  * px_dma_lmts2hdl - alloate a ddi_dma_impl_t, validate practical limits
1467c478bd9Sstevel@tonic-gate  *			and convert dmareq->dmar_limits to mp->dmai_attr
1477c478bd9Sstevel@tonic-gate  *
1487c478bd9Sstevel@tonic-gate  * ddi_dma_impl_t member modified     input
1497c478bd9Sstevel@tonic-gate  * ------------------------------------------------------------------------
1507c478bd9Sstevel@tonic-gate  * mp->dmai_minxfer		    - dev
1517c478bd9Sstevel@tonic-gate  * mp->dmai_burstsizes		    - dev
1527c478bd9Sstevel@tonic-gate  * mp->dmai_flags		    - no limit? peer-to-peer only?
1537c478bd9Sstevel@tonic-gate  *
1547c478bd9Sstevel@tonic-gate  * ddi_dma_attr member modified       input
1557c478bd9Sstevel@tonic-gate  * ------------------------------------------------------------------------
1567c478bd9Sstevel@tonic-gate  * mp->dmai_attr.dma_attr_addr_lo   - dev lo, sys lo
1577c478bd9Sstevel@tonic-gate  * mp->dmai_attr.dma_attr_addr_hi   - dev hi, sys hi
1587c478bd9Sstevel@tonic-gate  * mp->dmai_attr.dma_attr_count_max - dev count max, dev/sys lo/hi delta
1597c478bd9Sstevel@tonic-gate  * mp->dmai_attr.dma_attr_seg       - 0         (no nocross   restriction)
1607c478bd9Sstevel@tonic-gate  * mp->dmai_attr.dma_attr_align     - 1         (no alignment restriction)
1617c478bd9Sstevel@tonic-gate  *
1627c478bd9Sstevel@tonic-gate  * The dlim_dmaspeed member of dmareq->dmar_limits is ignored.
1637c478bd9Sstevel@tonic-gate  */
1647c478bd9Sstevel@tonic-gate ddi_dma_impl_t *
1657c478bd9Sstevel@tonic-gate px_dma_lmts2hdl(dev_info_t *dip, dev_info_t *rdip, px_mmu_t *mmu_p,
1667c478bd9Sstevel@tonic-gate 	ddi_dma_req_t *dmareq)
1677c478bd9Sstevel@tonic-gate {
1687c478bd9Sstevel@tonic-gate 	ddi_dma_impl_t *mp;
1697c478bd9Sstevel@tonic-gate 	ddi_dma_attr_t *attr_p;
1707c478bd9Sstevel@tonic-gate 	uint64_t syslo		= mmu_p->mmu_dvma_base;
1717c478bd9Sstevel@tonic-gate 	uint64_t syshi		= mmu_p->mmu_dvma_end;
1727c478bd9Sstevel@tonic-gate 	uint64_t fasthi		= mmu_p->mmu_dvma_fast_end;
1737c478bd9Sstevel@tonic-gate 	ddi_dma_lim_t *lim_p	= dmareq->dmar_limits;
1747c478bd9Sstevel@tonic-gate 	uint32_t count_max	= lim_p->dlim_cntr_max;
1757c478bd9Sstevel@tonic-gate 	uint64_t lo		= lim_p->dlim_addr_lo;
1767c478bd9Sstevel@tonic-gate 	uint64_t hi		= lim_p->dlim_addr_hi;
1777c478bd9Sstevel@tonic-gate 	if (hi <= lo) {
1787c478bd9Sstevel@tonic-gate 		DBG(DBG_DMA_MAP, dip, "Bad limits\n");
1797c478bd9Sstevel@tonic-gate 		return ((ddi_dma_impl_t *)DDI_DMA_NOMAPPING);
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 	if (!count_max)
1827c478bd9Sstevel@tonic-gate 		count_max--;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	if (!(mp = px_dma_allocmp(dip, rdip, dmareq->dmar_fp,
1857c478bd9Sstevel@tonic-gate 		dmareq->dmar_arg)))
1867c478bd9Sstevel@tonic-gate 		return (NULL);
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	/* store original dev input at the 2nd ddi_dma_attr */
18936fe4a92Segillett 	attr_p = PX_DEV_ATTR(mp);
1907c478bd9Sstevel@tonic-gate 	SET_DMAATTR(attr_p, lo, hi, -1, count_max);
1917c478bd9Sstevel@tonic-gate 	SET_DMAALIGN(attr_p, 1);
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	lo = MAX(lo, syslo);
1947c478bd9Sstevel@tonic-gate 	hi = MIN(hi, syshi);
1957c478bd9Sstevel@tonic-gate 	if (hi <= lo)
19636fe4a92Segillett 		mp->dmai_flags |= PX_DMAI_FLAGS_PEER_ONLY;
1977c478bd9Sstevel@tonic-gate 	count_max = MIN(count_max, hi - lo);
1987c478bd9Sstevel@tonic-gate 
19936fe4a92Segillett 	if (PX_DEV_NOSYSLIMIT(lo, hi, syslo, fasthi, 1))
20036fe4a92Segillett 		mp->dmai_flags |= PX_DMAI_FLAGS_NOFASTLIMIT |
20136fe4a92Segillett 			PX_DMAI_FLAGS_NOSYSLIMIT;
2027c478bd9Sstevel@tonic-gate 	else {
20336fe4a92Segillett 		if (PX_DEV_NOFASTLIMIT(lo, hi, syslo, syshi, 1))
20436fe4a92Segillett 			mp->dmai_flags |= PX_DMAI_FLAGS_NOFASTLIMIT;
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate 	if (PX_DMA_NOCTX(rdip))
20736fe4a92Segillett 		mp->dmai_flags |= PX_DMAI_FLAGS_NOCTX;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	/* store augumented dev input to mp->dmai_attr */
2107c478bd9Sstevel@tonic-gate 	mp->dmai_minxfer	= lim_p->dlim_minxfer;
2117c478bd9Sstevel@tonic-gate 	mp->dmai_burstsizes	= lim_p->dlim_burstsizes;
2127c478bd9Sstevel@tonic-gate 	attr_p = &mp->dmai_attr;
2137c478bd9Sstevel@tonic-gate 	SET_DMAATTR(attr_p, lo, hi, -1, count_max);
2147c478bd9Sstevel@tonic-gate 	SET_DMAALIGN(attr_p, 1);
2157c478bd9Sstevel@tonic-gate 	return (mp);
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate /*
2197c478bd9Sstevel@tonic-gate  * Called from px_attach to check for bypass dma support and set
2207c478bd9Sstevel@tonic-gate  * flags accordingly.
2217c478bd9Sstevel@tonic-gate  */
2227c478bd9Sstevel@tonic-gate int
2237c478bd9Sstevel@tonic-gate px_dma_attach(px_t *px_p)
2247c478bd9Sstevel@tonic-gate {
2257c478bd9Sstevel@tonic-gate 	uint64_t baddr;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	if (px_lib_iommu_getbypass(px_p->px_dip, 0ull,
2287c478bd9Sstevel@tonic-gate 			PCI_MAP_ATTR_WRITE|PCI_MAP_ATTR_READ,
2297c478bd9Sstevel@tonic-gate 			&baddr) != DDI_ENOTSUP)
2307c478bd9Sstevel@tonic-gate 		/* ignore all other errors */
231*b65731f1Skini 		px_p->px_dev_caps |= PX_BYPASS_DMA_ALLOWED;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate /*
2377c478bd9Sstevel@tonic-gate  * px_dma_attr2hdl
2387c478bd9Sstevel@tonic-gate  *
2397c478bd9Sstevel@tonic-gate  * This routine is called from the alloc handle entry point to sanity check the
2407c478bd9Sstevel@tonic-gate  * dma attribute structure.
2417c478bd9Sstevel@tonic-gate  *
2427c478bd9Sstevel@tonic-gate  * use by: px_dma_allochdl()
2437c478bd9Sstevel@tonic-gate  *
2447c478bd9Sstevel@tonic-gate  * return value:
2457c478bd9Sstevel@tonic-gate  *
2467c478bd9Sstevel@tonic-gate  *	DDI_SUCCESS		- on success
2477c478bd9Sstevel@tonic-gate  *	DDI_DMA_BADATTR		- attribute has invalid version number
2487c478bd9Sstevel@tonic-gate  *				  or address limits exclude dvma space
2497c478bd9Sstevel@tonic-gate  */
2507c478bd9Sstevel@tonic-gate int
2517c478bd9Sstevel@tonic-gate px_dma_attr2hdl(px_t *px_p, ddi_dma_impl_t *mp)
2527c478bd9Sstevel@tonic-gate {
2537c478bd9Sstevel@tonic-gate 	px_mmu_t *mmu_p = px_p->px_mmu_p;
2547c478bd9Sstevel@tonic-gate 	uint64_t syslo, syshi;
2557c478bd9Sstevel@tonic-gate 	int	ret;
25636fe4a92Segillett 	ddi_dma_attr_t *attrp		= PX_DEV_ATTR(mp);
2577c478bd9Sstevel@tonic-gate 	uint64_t hi			= attrp->dma_attr_addr_hi;
2587c478bd9Sstevel@tonic-gate 	uint64_t lo			= attrp->dma_attr_addr_lo;
2597c478bd9Sstevel@tonic-gate 	uint64_t align			= attrp->dma_attr_align;
2607c478bd9Sstevel@tonic-gate 	uint64_t nocross		= attrp->dma_attr_seg;
2617c478bd9Sstevel@tonic-gate 	uint64_t count_max		= attrp->dma_attr_count_max;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	DBG(DBG_DMA_ALLOCH, px_p->px_dip, "attrp=%p cntr_max=%x.%08x\n",
2647c478bd9Sstevel@tonic-gate 		attrp, HI32(count_max), LO32(count_max));
2657c478bd9Sstevel@tonic-gate 	DBG(DBG_DMA_ALLOCH, px_p->px_dip, "hi=%x.%08x lo=%x.%08x\n",
2667c478bd9Sstevel@tonic-gate 		HI32(hi), LO32(hi), HI32(lo), LO32(lo));
2677c478bd9Sstevel@tonic-gate 	DBG(DBG_DMA_ALLOCH, px_p->px_dip, "seg=%x.%08x align=%x.%08x\n",
2687c478bd9Sstevel@tonic-gate 		HI32(nocross), LO32(nocross), HI32(align), LO32(align));
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	if (!nocross)
2717c478bd9Sstevel@tonic-gate 		nocross--;
2727c478bd9Sstevel@tonic-gate 	if (attrp->dma_attr_flags & DDI_DMA_FORCE_PHYSICAL) { /* BYPASS */
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 		DBG(DBG_DMA_ALLOCH, px_p->px_dip, "bypass mode\n");
2757c478bd9Sstevel@tonic-gate 		/*
2767c478bd9Sstevel@tonic-gate 		 * If Bypass DMA is not supported, return error so that
2777c478bd9Sstevel@tonic-gate 		 * target driver can fall back to dvma mode of operation
2787c478bd9Sstevel@tonic-gate 		 */
279*b65731f1Skini 		if (!(px_p->px_dev_caps & PX_BYPASS_DMA_ALLOWED))
2807c478bd9Sstevel@tonic-gate 			return (DDI_DMA_BADATTR);
28136fe4a92Segillett 		mp->dmai_flags |= PX_DMAI_FLAGS_BYPASSREQ;
2827c478bd9Sstevel@tonic-gate 		if (nocross != UINT64_MAX)
2837c478bd9Sstevel@tonic-gate 			return (DDI_DMA_BADATTR);
2847c478bd9Sstevel@tonic-gate 		if (align && (align > MMU_PAGE_SIZE))
2857c478bd9Sstevel@tonic-gate 			return (DDI_DMA_BADATTR);
2867c478bd9Sstevel@tonic-gate 		align = 1; /* align on 1 page boundary */
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 		/* do a range check and get the limits */
2897c478bd9Sstevel@tonic-gate 		ret = px_lib_dma_bypass_rngchk(attrp, &syslo, &syshi);
2907c478bd9Sstevel@tonic-gate 		if (ret != DDI_SUCCESS)
2917c478bd9Sstevel@tonic-gate 			return (ret);
2927c478bd9Sstevel@tonic-gate 	} else { /* MMU_XLATE or PEER_TO_PEER */
2937c478bd9Sstevel@tonic-gate 		align = MAX(align, MMU_PAGE_SIZE) - 1;
2947c478bd9Sstevel@tonic-gate 		if ((align & nocross) != align) {
2957c478bd9Sstevel@tonic-gate 			dev_info_t *rdip = mp->dmai_rdip;
2967c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d dma_attr_seg not aligned",
2977c478bd9Sstevel@tonic-gate 				NAMEINST(rdip));
2987c478bd9Sstevel@tonic-gate 			return (DDI_DMA_BADATTR);
2997c478bd9Sstevel@tonic-gate 		}
3007c478bd9Sstevel@tonic-gate 		align = MMU_BTOP(align + 1);
3017c478bd9Sstevel@tonic-gate 		syslo = mmu_p->mmu_dvma_base;
3027c478bd9Sstevel@tonic-gate 		syshi = mmu_p->mmu_dvma_end;
3037c478bd9Sstevel@tonic-gate 	}
3047c478bd9Sstevel@tonic-gate 	if (hi <= lo) {
3057c478bd9Sstevel@tonic-gate 		dev_info_t *rdip = mp->dmai_rdip;
3067c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d limits out of range", NAMEINST(rdip));
3077c478bd9Sstevel@tonic-gate 		return (DDI_DMA_BADATTR);
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate 	lo = MAX(lo, syslo);
3107c478bd9Sstevel@tonic-gate 	hi = MIN(hi, syshi);
3117c478bd9Sstevel@tonic-gate 	if (!count_max)
3127c478bd9Sstevel@tonic-gate 		count_max--;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	DBG(DBG_DMA_ALLOCH, px_p->px_dip, "hi=%x.%08x, lo=%x.%08x\n",
3157c478bd9Sstevel@tonic-gate 		HI32(hi), LO32(hi), HI32(lo), LO32(lo));
3167c478bd9Sstevel@tonic-gate 	if (hi <= lo) { /* peer transfers cannot have alignment & nocross */
3177c478bd9Sstevel@tonic-gate 		dev_info_t *rdip = mp->dmai_rdip;
3187c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d peer only dev %p", NAMEINST(rdip), mp);
3197c478bd9Sstevel@tonic-gate 		if ((nocross < UINT32_MAX) || (align > 1)) {
3207c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d peer only device bad attr",
3217c478bd9Sstevel@tonic-gate 				NAMEINST(rdip));
3227c478bd9Sstevel@tonic-gate 			return (DDI_DMA_BADATTR);
3237c478bd9Sstevel@tonic-gate 		}
32436fe4a92Segillett 		mp->dmai_flags |= PX_DMAI_FLAGS_PEER_ONLY;
3257c478bd9Sstevel@tonic-gate 	} else /* set practical counter_max value */
3267c478bd9Sstevel@tonic-gate 		count_max = MIN(count_max, hi - lo);
3277c478bd9Sstevel@tonic-gate 
32836fe4a92Segillett 	if (PX_DEV_NOSYSLIMIT(lo, hi, syslo, syshi, align))
32936fe4a92Segillett 		mp->dmai_flags |= PX_DMAI_FLAGS_NOSYSLIMIT |
33036fe4a92Segillett 			PX_DMAI_FLAGS_NOFASTLIMIT;
3317c478bd9Sstevel@tonic-gate 	else {
3327c478bd9Sstevel@tonic-gate 		syshi = mmu_p->mmu_dvma_fast_end;
33336fe4a92Segillett 		if (PX_DEV_NOFASTLIMIT(lo, hi, syslo, syshi, align))
33436fe4a92Segillett 			mp->dmai_flags |= PX_DMAI_FLAGS_NOFASTLIMIT;
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 	if (PX_DMA_NOCTX(mp->dmai_rdip))
33736fe4a92Segillett 		mp->dmai_flags |= PX_DMAI_FLAGS_NOCTX;
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	mp->dmai_minxfer	= attrp->dma_attr_minxfer;
3407c478bd9Sstevel@tonic-gate 	mp->dmai_burstsizes	= attrp->dma_attr_burstsizes;
3417c478bd9Sstevel@tonic-gate 	attrp = &mp->dmai_attr;
3427c478bd9Sstevel@tonic-gate 	SET_DMAATTR(attrp, lo, hi, nocross, count_max);
3437c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate #define	TGT_PFN_INBETWEEN(pfn, bgn, end) ((pfn >= bgn) && (pfn <= end))
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate /*
3497c478bd9Sstevel@tonic-gate  * px_dma_type - determine which of the three types DMA (peer-to-peer,
3507c478bd9Sstevel@tonic-gate  *		mmu bypass, or mmu translate) we are asked to do.
3517c478bd9Sstevel@tonic-gate  *		Also checks pfn0 and rejects any non-peer-to-peer
3527c478bd9Sstevel@tonic-gate  *		requests for peer-only devices.
3537c478bd9Sstevel@tonic-gate  *
3547c478bd9Sstevel@tonic-gate  *	return values:
3557c478bd9Sstevel@tonic-gate  *		DDI_DMA_NOMAPPING - can't get valid pfn0, or bad dma type
3567c478bd9Sstevel@tonic-gate  *		DDI_SUCCESS
3577c478bd9Sstevel@tonic-gate  *
3587c478bd9Sstevel@tonic-gate  *	dma handle members affected (set on exit):
3597c478bd9Sstevel@tonic-gate  *	mp->dmai_object		- dmareq->dmar_object
3607c478bd9Sstevel@tonic-gate  *	mp->dmai_rflags		- consistent?, nosync?, dmareq->dmar_flags
3617c478bd9Sstevel@tonic-gate  *	mp->dmai_flags   	- DMA type
3627c478bd9Sstevel@tonic-gate  *	mp->dmai_pfn0   	- 1st page pfn (if va/size pair and not shadow)
3637c478bd9Sstevel@tonic-gate  *	mp->dmai_roffset 	- initialized to starting MMU page offset
3647c478bd9Sstevel@tonic-gate  *	mp->dmai_ndvmapages	- # of total MMU pages of entire object
3657c478bd9Sstevel@tonic-gate  */
3667c478bd9Sstevel@tonic-gate int
3677c478bd9Sstevel@tonic-gate px_dma_type(px_t *px_p, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp)
3687c478bd9Sstevel@tonic-gate {
3697c478bd9Sstevel@tonic-gate 	dev_info_t *dip = px_p->px_dip;
3707c478bd9Sstevel@tonic-gate 	ddi_dma_obj_t *dobj_p = &dmareq->dmar_object;
3717c478bd9Sstevel@tonic-gate 	px_pec_t *pec_p = px_p->px_pec_p;
3727c478bd9Sstevel@tonic-gate 	uint32_t offset;
3737c478bd9Sstevel@tonic-gate 	pfn_t pfn0;
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	mp->dmai_rflags = dmareq->dmar_flags & DMP_DDIFLAGS | DMP_NOSYNC;
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	switch (dobj_p->dmao_type) {
3787c478bd9Sstevel@tonic-gate 	case DMA_OTYP_BUFVADDR:
3797c478bd9Sstevel@tonic-gate 	case DMA_OTYP_VADDR: {
3807c478bd9Sstevel@tonic-gate 		page_t **pplist = dobj_p->dmao_obj.virt_obj.v_priv;
3817c478bd9Sstevel@tonic-gate 		caddr_t vaddr = dobj_p->dmao_obj.virt_obj.v_addr;
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 		DBG(DBG_DMA_MAP, dip, "vaddr=%p pplist=%p\n", vaddr, pplist);
3847c478bd9Sstevel@tonic-gate 		offset = (ulong_t)vaddr & MMU_PAGE_OFFSET;
3857c478bd9Sstevel@tonic-gate 		if (pplist) {				/* shadow list */
38636fe4a92Segillett 			mp->dmai_flags |= PX_DMAI_FLAGS_PGPFN;
3877c478bd9Sstevel@tonic-gate 			pfn0 = page_pptonum(*pplist);
3887c478bd9Sstevel@tonic-gate 		} else {
3897c478bd9Sstevel@tonic-gate 			struct as *as_p = dobj_p->dmao_obj.virt_obj.v_as;
3907c478bd9Sstevel@tonic-gate 			struct hat *hat_p = as_p ? as_p->a_hat : kas.a_hat;
3917c478bd9Sstevel@tonic-gate 			pfn0 = hat_getpfnum(hat_p, vaddr);
3927c478bd9Sstevel@tonic-gate 		}
3937c478bd9Sstevel@tonic-gate 		}
3947c478bd9Sstevel@tonic-gate 		break;
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	case DMA_OTYP_PAGES:
3977c478bd9Sstevel@tonic-gate 		offset = dobj_p->dmao_obj.pp_obj.pp_offset;
39836fe4a92Segillett 		mp->dmai_flags |= PX_DMAI_FLAGS_PGPFN;
3997c478bd9Sstevel@tonic-gate 		pfn0 = page_pptonum(dobj_p->dmao_obj.pp_obj.pp_pp);
4007c478bd9Sstevel@tonic-gate 		break;
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	case DMA_OTYP_PADDR:
4037c478bd9Sstevel@tonic-gate 	default:
4047c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d requested unsupported dma type %x",
4057c478bd9Sstevel@tonic-gate 			NAMEINST(mp->dmai_rdip), dobj_p->dmao_type);
4067c478bd9Sstevel@tonic-gate 		return (DDI_DMA_NOMAPPING);
4077c478bd9Sstevel@tonic-gate 	}
4087c478bd9Sstevel@tonic-gate 	if (pfn0 == PFN_INVALID) {
4097c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: invalid pfn0 for DMA object %p",
4107c478bd9Sstevel@tonic-gate 			NAMEINST(dip), dobj_p);
4117c478bd9Sstevel@tonic-gate 		return (DDI_DMA_NOMAPPING);
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate 	if (TGT_PFN_INBETWEEN(pfn0, pec_p->pec_base32_pfn,
4147c478bd9Sstevel@tonic-gate 			pec_p->pec_last32_pfn)) {
41536fe4a92Segillett 		mp->dmai_flags |= PX_DMAI_FLAGS_PTP|PX_DMAI_FLAGS_PTP32;
4167c478bd9Sstevel@tonic-gate 		goto done;	/* leave bypass and dvma flag as 0 */
4177c478bd9Sstevel@tonic-gate 	} else if (TGT_PFN_INBETWEEN(pfn0, pec_p->pec_base64_pfn,
4187c478bd9Sstevel@tonic-gate 			pec_p->pec_last64_pfn)) {
41936fe4a92Segillett 		mp->dmai_flags |= PX_DMAI_FLAGS_PTP|PX_DMAI_FLAGS_PTP64;
4207c478bd9Sstevel@tonic-gate 		goto done;	/* leave bypass and dvma flag as 0 */
4217c478bd9Sstevel@tonic-gate 	}
4227c478bd9Sstevel@tonic-gate 	if (PX_DMA_ISPEERONLY(mp)) {
4237c478bd9Sstevel@tonic-gate 		dev_info_t *rdip = mp->dmai_rdip;
4247c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "Bad peer-to-peer req %s%d", NAMEINST(rdip));
4257c478bd9Sstevel@tonic-gate 		return (DDI_DMA_NOMAPPING);
4267c478bd9Sstevel@tonic-gate 	}
42736fe4a92Segillett 	mp->dmai_flags |= (mp->dmai_flags & PX_DMAI_FLAGS_BYPASSREQ) ?
4281de45cd9Sgovinda 	    PX_DMAI_FLAGS_BYPASS : PX_DMAI_FLAGS_DVMA |
4291de45cd9Sgovinda 	    (mp->dmai_rflags & DDI_DMA_REDZONE ? PX_DMAI_FLAGS_REDZONE : 0);
4307c478bd9Sstevel@tonic-gate done:
4317c478bd9Sstevel@tonic-gate 	mp->dmai_object	 = *dobj_p;			/* whole object    */
4327c478bd9Sstevel@tonic-gate 	mp->dmai_pfn0	 = (void *)pfn0;		/* cache pfn0	   */
4337c478bd9Sstevel@tonic-gate 	mp->dmai_roffset = offset;			/* win0 pg0 offset */
4347c478bd9Sstevel@tonic-gate 	mp->dmai_ndvmapages = MMU_BTOPR(offset + mp->dmai_object.dmao_size);
4357c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate /*
4397c478bd9Sstevel@tonic-gate  * px_dma_pgpfn - set up pfnlst array according to pages
4407c478bd9Sstevel@tonic-gate  *	VA/size pair: <shadow IO, bypass, peer-to-peer>, or OTYP_PAGES
4417c478bd9Sstevel@tonic-gate  */
4427c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4437c478bd9Sstevel@tonic-gate static int
4447c478bd9Sstevel@tonic-gate px_dma_pgpfn(px_t *px_p, ddi_dma_impl_t *mp, uint_t npages)
4457c478bd9Sstevel@tonic-gate {
4467c478bd9Sstevel@tonic-gate 	int i;
4477c478bd9Sstevel@tonic-gate 	dev_info_t *dip = px_p->px_dip;
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	switch (mp->dmai_object.dmao_type) {
4507c478bd9Sstevel@tonic-gate 	case DMA_OTYP_BUFVADDR:
4517c478bd9Sstevel@tonic-gate 	case DMA_OTYP_VADDR: {
4527c478bd9Sstevel@tonic-gate 		page_t **pplist = mp->dmai_object.dmao_obj.virt_obj.v_priv;
4537c478bd9Sstevel@tonic-gate 		DBG(DBG_DMA_MAP, dip, "shadow pplist=%p, %x pages, pfns=",
4547c478bd9Sstevel@tonic-gate 			pplist, npages);
4557c478bd9Sstevel@tonic-gate 		for (i = 1; i < npages; i++) {
4567c478bd9Sstevel@tonic-gate 			px_iopfn_t pfn = page_pptonum(pplist[i]);
4577c478bd9Sstevel@tonic-gate 			PX_SET_MP_PFN1(mp, i, pfn);
4587c478bd9Sstevel@tonic-gate 			DBG(DBG_DMA_MAP|DBG_CONT, dip, "%x ", pfn);
4597c478bd9Sstevel@tonic-gate 		}
4607c478bd9Sstevel@tonic-gate 		DBG(DBG_DMA_MAP|DBG_CONT, dip, "\n");
4617c478bd9Sstevel@tonic-gate 		}
4627c478bd9Sstevel@tonic-gate 		break;
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	case DMA_OTYP_PAGES: {
4657c478bd9Sstevel@tonic-gate 		page_t *pp = mp->dmai_object.dmao_obj.pp_obj.pp_pp->p_next;
4667c478bd9Sstevel@tonic-gate 		DBG(DBG_DMA_MAP, dip, "pp=%p pfns=", pp);
4677c478bd9Sstevel@tonic-gate 		for (i = 1; i < npages; i++, pp = pp->p_next) {
4687c478bd9Sstevel@tonic-gate 			px_iopfn_t pfn = page_pptonum(pp);
4697c478bd9Sstevel@tonic-gate 			PX_SET_MP_PFN1(mp, i, pfn);
4707c478bd9Sstevel@tonic-gate 			DBG(DBG_DMA_MAP|DBG_CONT, dip, "%x ", pfn);
4717c478bd9Sstevel@tonic-gate 		}
4727c478bd9Sstevel@tonic-gate 		DBG(DBG_DMA_MAP|DBG_CONT, dip, "\n");
4737c478bd9Sstevel@tonic-gate 		}
4747c478bd9Sstevel@tonic-gate 		break;
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	default:	/* check is already done by px_dma_type */
4777c478bd9Sstevel@tonic-gate 		ASSERT(0);
4787c478bd9Sstevel@tonic-gate 		break;
4797c478bd9Sstevel@tonic-gate 	}
4807c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
4817c478bd9Sstevel@tonic-gate }
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate /*
4847c478bd9Sstevel@tonic-gate  * px_dma_vapfn - set up pfnlst array according to VA
4857c478bd9Sstevel@tonic-gate  *	VA/size pair: <normal, bypass, peer-to-peer>
4867c478bd9Sstevel@tonic-gate  *	pfn0 is skipped as it is already done.
4877c478bd9Sstevel@tonic-gate  *	In this case, the cached pfn0 is used to fill pfnlst[0]
4887c478bd9Sstevel@tonic-gate  */
4897c478bd9Sstevel@tonic-gate static int
4907c478bd9Sstevel@tonic-gate px_dma_vapfn(px_t *px_p, ddi_dma_impl_t *mp, uint_t npages)
4917c478bd9Sstevel@tonic-gate {
4927c478bd9Sstevel@tonic-gate 	dev_info_t *dip = px_p->px_dip;
4937c478bd9Sstevel@tonic-gate 	int i;
4947c478bd9Sstevel@tonic-gate 	caddr_t vaddr = (caddr_t)mp->dmai_object.dmao_obj.virt_obj.v_as;
4957c478bd9Sstevel@tonic-gate 	struct hat *hat_p = vaddr ? ((struct as *)vaddr)->a_hat : kas.a_hat;
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	vaddr = mp->dmai_object.dmao_obj.virt_obj.v_addr + MMU_PAGE_SIZE;
4987c478bd9Sstevel@tonic-gate 	for (i = 1; i < npages; i++, vaddr += MMU_PAGE_SIZE) {
4997c478bd9Sstevel@tonic-gate 		px_iopfn_t pfn = hat_getpfnum(hat_p, vaddr);
5007c478bd9Sstevel@tonic-gate 		if (pfn == PFN_INVALID)
5017c478bd9Sstevel@tonic-gate 			goto err_badpfn;
5027c478bd9Sstevel@tonic-gate 		PX_SET_MP_PFN1(mp, i, pfn);
5037c478bd9Sstevel@tonic-gate 		DBG(DBG_DMA_BINDH, dip, "px_dma_vapfn: mp=%p pfnlst[%x]=%x\n",
5047c478bd9Sstevel@tonic-gate 			mp, i, pfn);
5057c478bd9Sstevel@tonic-gate 	}
5067c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
5077c478bd9Sstevel@tonic-gate err_badpfn:
5087c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "%s%d: bad page frame vaddr=%p", NAMEINST(dip), vaddr);
5097c478bd9Sstevel@tonic-gate 	return (DDI_DMA_NOMAPPING);
5107c478bd9Sstevel@tonic-gate }
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate /*
5137c478bd9Sstevel@tonic-gate  * px_dma_pfn - Fills pfn list for all pages being DMA-ed.
5147c478bd9Sstevel@tonic-gate  *
5157c478bd9Sstevel@tonic-gate  * dependencies:
5167c478bd9Sstevel@tonic-gate  *	mp->dmai_ndvmapages	- set to total # of dma pages
5177c478bd9Sstevel@tonic-gate  *
5187c478bd9Sstevel@tonic-gate  * return value:
5197c478bd9Sstevel@tonic-gate  *	DDI_SUCCESS
5207c478bd9Sstevel@tonic-gate  *	DDI_DMA_NOMAPPING
5217c478bd9Sstevel@tonic-gate  */
5227c478bd9Sstevel@tonic-gate int
5237c478bd9Sstevel@tonic-gate px_dma_pfn(px_t *px_p, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp)
5247c478bd9Sstevel@tonic-gate {
5257c478bd9Sstevel@tonic-gate 	uint32_t npages = mp->dmai_ndvmapages;
5267c478bd9Sstevel@tonic-gate 	int (*waitfp)(caddr_t) = dmareq->dmar_fp;
5277c478bd9Sstevel@tonic-gate 	int i, ret, peer = PX_DMA_ISPTP(mp);
5287c478bd9Sstevel@tonic-gate 	int peer32 = PX_DMA_ISPTP32(mp);
5297c478bd9Sstevel@tonic-gate 	dev_info_t *dip = px_p->px_dip;
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	px_pec_t *pec_p = px_p->px_pec_p;
5327c478bd9Sstevel@tonic-gate 	px_iopfn_t pfn_base = peer32 ? pec_p->pec_base32_pfn :
5337c478bd9Sstevel@tonic-gate 					pec_p->pec_base64_pfn;
5347c478bd9Sstevel@tonic-gate 	px_iopfn_t pfn_last = peer32 ? pec_p->pec_last32_pfn :
5357c478bd9Sstevel@tonic-gate 					pec_p->pec_last64_pfn;
5367c478bd9Sstevel@tonic-gate 	px_iopfn_t pfn_adj = peer ? pfn_base : 0;
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	DBG(DBG_DMA_BINDH, dip, "px_dma_pfn: mp=%p pfn0=%x\n",
53936fe4a92Segillett 		mp, PX_MP_PFN0(mp) - pfn_adj);
5407c478bd9Sstevel@tonic-gate 	/* 1 page: no array alloc/fill, no mixed mode check */
5417c478bd9Sstevel@tonic-gate 	if (npages == 1) {
54236fe4a92Segillett 		PX_SET_MP_PFN(mp, 0, PX_MP_PFN0(mp) - pfn_adj);
5437c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
5447c478bd9Sstevel@tonic-gate 	}
5457c478bd9Sstevel@tonic-gate 	/* allocate pfn array */
5467c478bd9Sstevel@tonic-gate 	if (!(mp->dmai_pfnlst = kmem_alloc(npages * sizeof (px_iopfn_t),
5477c478bd9Sstevel@tonic-gate 		waitfp == DDI_DMA_SLEEP ? KM_SLEEP : KM_NOSLEEP))) {
5487c478bd9Sstevel@tonic-gate 		if (waitfp != DDI_DMA_DONTWAIT)
5497c478bd9Sstevel@tonic-gate 			ddi_set_callback(waitfp, dmareq->dmar_arg,
5507c478bd9Sstevel@tonic-gate 				&px_kmem_clid);
5517c478bd9Sstevel@tonic-gate 		return (DDI_DMA_NORESOURCES);
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate 	/* fill pfn array */
55436fe4a92Segillett 	PX_SET_MP_PFN(mp, 0, PX_MP_PFN0(mp) - pfn_adj);	/* pfnlst[0] */
5557c478bd9Sstevel@tonic-gate 	if ((ret = PX_DMA_ISPGPFN(mp) ? px_dma_pgpfn(px_p, mp, npages) :
5567c478bd9Sstevel@tonic-gate 		px_dma_vapfn(px_p, mp, npages)) != DDI_SUCCESS)
5577c478bd9Sstevel@tonic-gate 		goto err;
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	/* skip pfn0, check mixed mode and adjust peer to peer pfn */
5607c478bd9Sstevel@tonic-gate 	for (i = 1; i < npages; i++) {
5617c478bd9Sstevel@tonic-gate 		px_iopfn_t pfn = PX_GET_MP_PFN1(mp, i);
5627c478bd9Sstevel@tonic-gate 		if (peer ^ TGT_PFN_INBETWEEN(pfn, pfn_base, pfn_last)) {
563b40cec45Skrishnae 			cmn_err(CE_WARN, "%s%d mixed mode DMA %lx %lx",
56436fe4a92Segillett 				NAMEINST(mp->dmai_rdip), PX_MP_PFN0(mp), pfn);
5657c478bd9Sstevel@tonic-gate 			ret = DDI_DMA_NOMAPPING;	/* mixed mode */
5667c478bd9Sstevel@tonic-gate 			goto err;
5677c478bd9Sstevel@tonic-gate 		}
5687c478bd9Sstevel@tonic-gate 		DBG(DBG_DMA_MAP, dip,
5697c478bd9Sstevel@tonic-gate 			"px_dma_pfn: pfnlst[%x]=%x-%x\n", i, pfn, pfn_adj);
5707c478bd9Sstevel@tonic-gate 		if (pfn_adj)
5717c478bd9Sstevel@tonic-gate 			PX_SET_MP_PFN1(mp, i, pfn - pfn_adj);
5727c478bd9Sstevel@tonic-gate 	}
5737c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
5747c478bd9Sstevel@tonic-gate err:
5757c478bd9Sstevel@tonic-gate 	px_dma_freepfn(mp);
5767c478bd9Sstevel@tonic-gate 	return (ret);
5777c478bd9Sstevel@tonic-gate }
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate /*
5807c478bd9Sstevel@tonic-gate  * px_dvma_win() - trim requested DVMA size down to window size
5817c478bd9Sstevel@tonic-gate  *	The 1st window starts from offset and ends at page-aligned boundary.
5827c478bd9Sstevel@tonic-gate  *	From the 2nd window on, each window starts and ends at page-aligned
5837c478bd9Sstevel@tonic-gate  *	boundary except the last window ends at wherever requested.
5847c478bd9Sstevel@tonic-gate  *
5857c478bd9Sstevel@tonic-gate  *	accesses the following mp-> members:
5867c478bd9Sstevel@tonic-gate  *	mp->dmai_attr.dma_attr_count_max
5877c478bd9Sstevel@tonic-gate  *	mp->dmai_attr.dma_attr_seg
5887c478bd9Sstevel@tonic-gate  *	mp->dmai_roffset   - start offset of 1st window
5897c478bd9Sstevel@tonic-gate  *	mp->dmai_rflags (redzone)
5907c478bd9Sstevel@tonic-gate  *	mp->dmai_ndvmapages (for 1 page fast path)
5917c478bd9Sstevel@tonic-gate  *
5927c478bd9Sstevel@tonic-gate  *	sets the following mp-> members:
5937c478bd9Sstevel@tonic-gate  *	mp->dmai_size	   - xfer size, != winsize if 1st/last win  (not fixed)
5947c478bd9Sstevel@tonic-gate  *	mp->dmai_winsize   - window size (no redzone), n * page size    (fixed)
5957c478bd9Sstevel@tonic-gate  *	mp->dmai_nwin	   - # of DMA windows of entire object		(fixed)
5967c478bd9Sstevel@tonic-gate  *	mp->dmai_rflags	   - remove partial flag if nwin == 1		(fixed)
5977c478bd9Sstevel@tonic-gate  *	mp->dmai_winlst	   - NULL, window objects not used for DVMA	(fixed)
5987c478bd9Sstevel@tonic-gate  *
5997c478bd9Sstevel@tonic-gate  *	fixed - not changed across different DMA windows
6007c478bd9Sstevel@tonic-gate  */
6017c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6027c478bd9Sstevel@tonic-gate int
6037c478bd9Sstevel@tonic-gate px_dvma_win(px_t *px_p, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp)
6047c478bd9Sstevel@tonic-gate {
6051de45cd9Sgovinda 	uint32_t redzone_sz	= PX_HAS_REDZONE(mp) ? MMU_PAGE_SIZE : 0;
6067c478bd9Sstevel@tonic-gate 	size_t obj_sz		= mp->dmai_object.dmao_size;
6077c478bd9Sstevel@tonic-gate 	size_t xfer_sz;
6087c478bd9Sstevel@tonic-gate 	ulong_t pg_off;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	if ((mp->dmai_ndvmapages == 1) && !redzone_sz) {
6117c478bd9Sstevel@tonic-gate 		mp->dmai_rflags &= ~DDI_DMA_PARTIAL;
6127c478bd9Sstevel@tonic-gate 		mp->dmai_size = obj_sz;
6137c478bd9Sstevel@tonic-gate 		mp->dmai_winsize = MMU_PAGE_SIZE;
6147c478bd9Sstevel@tonic-gate 		mp->dmai_nwin = 1;
6157c478bd9Sstevel@tonic-gate 		goto done;
6167c478bd9Sstevel@tonic-gate 	}
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	pg_off	= mp->dmai_roffset;
6197c478bd9Sstevel@tonic-gate 	xfer_sz	= obj_sz + redzone_sz;
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	/* include redzone in nocross check */ {
6227c478bd9Sstevel@tonic-gate 		uint64_t nocross = mp->dmai_attr.dma_attr_seg;
6237c478bd9Sstevel@tonic-gate 		if (xfer_sz + pg_off - 1 > nocross)
6247c478bd9Sstevel@tonic-gate 			xfer_sz = nocross - pg_off + 1;
6257c478bd9Sstevel@tonic-gate 		if (redzone_sz && (xfer_sz <= redzone_sz)) {
6267c478bd9Sstevel@tonic-gate 			DBG(DBG_DMA_MAP, px_p->px_dip,
6277c478bd9Sstevel@tonic-gate 			    "nocross too small: "
6287c478bd9Sstevel@tonic-gate 			    "%lx(%lx)+%lx+%lx < %llx\n",
6297c478bd9Sstevel@tonic-gate 			    xfer_sz, obj_sz, pg_off, redzone_sz, nocross);
6307c478bd9Sstevel@tonic-gate 			return (DDI_DMA_TOOBIG);
6317c478bd9Sstevel@tonic-gate 		}
6327c478bd9Sstevel@tonic-gate 	}
6337c478bd9Sstevel@tonic-gate 	xfer_sz -= redzone_sz;		/* restore transfer size  */
6347c478bd9Sstevel@tonic-gate 	/* check counter max */ {
6357c478bd9Sstevel@tonic-gate 		uint32_t count_max = mp->dmai_attr.dma_attr_count_max;
6367c478bd9Sstevel@tonic-gate 		if (xfer_sz - 1 > count_max)
6377c478bd9Sstevel@tonic-gate 			xfer_sz = count_max + 1;
6387c478bd9Sstevel@tonic-gate 	}
6397c478bd9Sstevel@tonic-gate 	if (xfer_sz >= obj_sz) {
6407c478bd9Sstevel@tonic-gate 		mp->dmai_rflags &= ~DDI_DMA_PARTIAL;
6417c478bd9Sstevel@tonic-gate 		mp->dmai_size = xfer_sz;
6427c478bd9Sstevel@tonic-gate 		mp->dmai_winsize = P2ROUNDUP(xfer_sz + pg_off, MMU_PAGE_SIZE);
6437c478bd9Sstevel@tonic-gate 		mp->dmai_nwin = 1;
6447c478bd9Sstevel@tonic-gate 		goto done;
6457c478bd9Sstevel@tonic-gate 	}
6467c478bd9Sstevel@tonic-gate 	if (!(dmareq->dmar_flags & DDI_DMA_PARTIAL)) {
6477c478bd9Sstevel@tonic-gate 		DBG(DBG_DMA_MAP, px_p->px_dip, "too big: %lx+%lx+%lx > %lx\n",
6487c478bd9Sstevel@tonic-gate 			obj_sz, pg_off, redzone_sz, xfer_sz);
6497c478bd9Sstevel@tonic-gate 		return (DDI_DMA_TOOBIG);
6507c478bd9Sstevel@tonic-gate 	}
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	xfer_sz = MMU_PTOB(MMU_BTOP(xfer_sz + pg_off)); /* page align */
6537c478bd9Sstevel@tonic-gate 	mp->dmai_size = xfer_sz - pg_off;	/* 1st window xferrable size */
6547c478bd9Sstevel@tonic-gate 	mp->dmai_winsize = xfer_sz;		/* redzone not in winsize */
6557c478bd9Sstevel@tonic-gate 	mp->dmai_nwin = (obj_sz + pg_off + xfer_sz - 1) / xfer_sz;
6567c478bd9Sstevel@tonic-gate done:
6577c478bd9Sstevel@tonic-gate 	mp->dmai_winlst = NULL;
6587c478bd9Sstevel@tonic-gate 	px_dump_dma_handle(DBG_DMA_MAP, px_p->px_dip, mp);
6597c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
6607c478bd9Sstevel@tonic-gate }
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate /*
6637c478bd9Sstevel@tonic-gate  * fast track cache entry to mmu context, inserts 3 0 bits between
6647c478bd9Sstevel@tonic-gate  * upper 6-bits and lower 3-bits of the 9-bit cache entry
6657c478bd9Sstevel@tonic-gate  */
6667c478bd9Sstevel@tonic-gate #define	MMU_FCE_TO_CTX(i)	(((i) << 3) | ((i) & 0x7) | 0x38)
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate /*
6697c478bd9Sstevel@tonic-gate  * px_dvma_map_fast - attempts to map fast trackable DVMA
6707c478bd9Sstevel@tonic-gate  */
6717c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6727c478bd9Sstevel@tonic-gate int
6737c478bd9Sstevel@tonic-gate px_dvma_map_fast(px_mmu_t *mmu_p, ddi_dma_impl_t *mp)
6747c478bd9Sstevel@tonic-gate {
6757c478bd9Sstevel@tonic-gate 	uint_t clustsz = px_dvma_page_cache_clustsz;
6767c478bd9Sstevel@tonic-gate 	uint_t entries = px_dvma_page_cache_entries;
6777c478bd9Sstevel@tonic-gate 	io_attributes_t attr = PX_GET_TTE_ATTR(mp->dmai_rflags);
6787c478bd9Sstevel@tonic-gate 	int i = mmu_p->mmu_dvma_addr_scan_start;
6797c478bd9Sstevel@tonic-gate 	uint8_t *lock_addr = mmu_p->mmu_dvma_cache_locks + i;
6807c478bd9Sstevel@tonic-gate 	px_dvma_addr_t dvma_pg;
6817c478bd9Sstevel@tonic-gate 	size_t npages = MMU_BTOP(mp->dmai_winsize);
6821de45cd9Sgovinda 	dev_info_t *dip = mmu_p->mmu_px_p->px_dip;
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	extern uint8_t ldstub(uint8_t *);
6857c478bd9Sstevel@tonic-gate 	ASSERT(MMU_PTOB(npages) == mp->dmai_winsize);
6861de45cd9Sgovinda 	ASSERT(npages + PX_HAS_REDZONE(mp) <= clustsz);
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	for (; i < entries && ldstub(lock_addr); i++, lock_addr++);
6897c478bd9Sstevel@tonic-gate 	if (i >= entries) {
6907c478bd9Sstevel@tonic-gate 		lock_addr = mmu_p->mmu_dvma_cache_locks;
6917c478bd9Sstevel@tonic-gate 		i = 0;
6927c478bd9Sstevel@tonic-gate 		for (; i < entries && ldstub(lock_addr); i++, lock_addr++);
6937c478bd9Sstevel@tonic-gate 		if (i >= entries) {
6947c478bd9Sstevel@tonic-gate #ifdef	PX_DMA_PROF
6957c478bd9Sstevel@tonic-gate 			px_dvmaft_exhaust++;
6967c478bd9Sstevel@tonic-gate #endif	/* PX_DMA_PROF */
6977c478bd9Sstevel@tonic-gate 			return (DDI_DMA_NORESOURCES);
6987c478bd9Sstevel@tonic-gate 		}
6997c478bd9Sstevel@tonic-gate 	}
7007c478bd9Sstevel@tonic-gate 	mmu_p->mmu_dvma_addr_scan_start = (i + 1) & (entries - 1);
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	i *= clustsz;
7037c478bd9Sstevel@tonic-gate 	dvma_pg = mmu_p->dvma_base_pg + i;
7047c478bd9Sstevel@tonic-gate 
7051de45cd9Sgovinda 	if (px_lib_iommu_map(dip, PCI_TSBID(0, i), npages, attr,
7061de45cd9Sgovinda 	    (void *)mp, 0, MMU_MAP_MP) != DDI_SUCCESS) {
7071de45cd9Sgovinda 		DBG(DBG_MAP_WIN, dip, "px_dvma_map_fast: "
7081de45cd9Sgovinda 		    "px_lib_iommu_map failed\n");
7091de45cd9Sgovinda 
7101de45cd9Sgovinda 		return (DDI_FAILURE);
7111de45cd9Sgovinda 	}
7121de45cd9Sgovinda 
7131de45cd9Sgovinda 	if (!PX_MAP_BUFZONE(mp))
7141de45cd9Sgovinda 		goto done;
7151de45cd9Sgovinda 
7161de45cd9Sgovinda 	DBG(DBG_MAP_WIN, dip, "px_dvma_map_fast: redzone pg=%x\n", i + npages);
7171de45cd9Sgovinda 
7181de45cd9Sgovinda 	ASSERT(PX_HAS_REDZONE(mp));
7191de45cd9Sgovinda 
7201de45cd9Sgovinda 	if (px_lib_iommu_map(dip, PCI_TSBID(0, i + npages), 1, attr,
7211de45cd9Sgovinda 	    (void *)mp, npages, MMU_MAP_MP) != DDI_SUCCESS) {
7221de45cd9Sgovinda 		DBG(DBG_MAP_WIN, dip, "px_dvma_map_fast: "
7231de45cd9Sgovinda 		    "mapping REDZONE page failed\n");
7241de45cd9Sgovinda 
7251de45cd9Sgovinda 		(void) px_lib_iommu_demap(dip, PCI_TSBID(0, i), npages);
7267c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
7271de45cd9Sgovinda 	}
7287c478bd9Sstevel@tonic-gate 
7291de45cd9Sgovinda done:
7307c478bd9Sstevel@tonic-gate #ifdef PX_DMA_PROF
7317c478bd9Sstevel@tonic-gate 	px_dvmaft_success++;
7327c478bd9Sstevel@tonic-gate #endif
7337c478bd9Sstevel@tonic-gate 	mp->dmai_mapping = mp->dmai_roffset | MMU_PTOB(dvma_pg);
7347c478bd9Sstevel@tonic-gate 	mp->dmai_offset = 0;
73536fe4a92Segillett 	mp->dmai_flags |= PX_DMAI_FLAGS_FASTTRACK;
7367c478bd9Sstevel@tonic-gate 	PX_SAVE_MP_TTE(mp, attr);	/* save TTE template for unmapping */
73736fe4a92Segillett 	if (PX_DVMA_DBG_ON(mmu_p))
7387c478bd9Sstevel@tonic-gate 		px_dvma_alloc_debug(mmu_p, (char *)mp->dmai_mapping,
7397c478bd9Sstevel@tonic-gate 			mp->dmai_size, mp);
7407c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
7417c478bd9Sstevel@tonic-gate }
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate /*
7447c478bd9Sstevel@tonic-gate  * px_dvma_map: map non-fasttrack DMA
7457c478bd9Sstevel@tonic-gate  *		Use quantum cache if single page DMA.
7467c478bd9Sstevel@tonic-gate  */
7477c478bd9Sstevel@tonic-gate int
7487c478bd9Sstevel@tonic-gate px_dvma_map(ddi_dma_impl_t *mp, ddi_dma_req_t *dmareq, px_mmu_t *mmu_p)
7497c478bd9Sstevel@tonic-gate {
7507c478bd9Sstevel@tonic-gate 	uint_t npages = PX_DMA_WINNPGS(mp);
7517c478bd9Sstevel@tonic-gate 	px_dvma_addr_t dvma_pg, dvma_pg_index;
7527c478bd9Sstevel@tonic-gate 	void *dvma_addr;
7537c478bd9Sstevel@tonic-gate 	uint64_t tte = PX_GET_TTE_ATTR(mp->dmai_rflags);
7547c478bd9Sstevel@tonic-gate 	int sleep = dmareq->dmar_fp == DDI_DMA_SLEEP ? VM_SLEEP : VM_NOSLEEP;
7557c478bd9Sstevel@tonic-gate 	dev_info_t *dip = mp->dmai_rdip;
7567c478bd9Sstevel@tonic-gate 	int	ret = DDI_SUCCESS;
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	/*
7597c478bd9Sstevel@tonic-gate 	 * allocate dvma space resource and map in the first window.
7607c478bd9Sstevel@tonic-gate 	 * (vmem_t *vmp, size_t size,
7617c478bd9Sstevel@tonic-gate 	 *	size_t align, size_t phase, size_t nocross,
7627c478bd9Sstevel@tonic-gate 	 *	void *minaddr, void *maxaddr, int vmflag)
7637c478bd9Sstevel@tonic-gate 	 */
7641de45cd9Sgovinda 	if ((npages == 1) && !PX_HAS_REDZONE(mp) && PX_HAS_NOSYSLIMIT(mp)) {
7657c478bd9Sstevel@tonic-gate 		dvma_addr = vmem_alloc(mmu_p->mmu_dvma_map,
7667c478bd9Sstevel@tonic-gate 			MMU_PAGE_SIZE, sleep);
76736fe4a92Segillett 		mp->dmai_flags |= PX_DMAI_FLAGS_VMEMCACHE;
7687c478bd9Sstevel@tonic-gate #ifdef	PX_DMA_PROF
7697c478bd9Sstevel@tonic-gate 		px_dvma_vmem_alloc++;
7707c478bd9Sstevel@tonic-gate #endif	/* PX_DMA_PROF */
7717c478bd9Sstevel@tonic-gate 	} else {
7727c478bd9Sstevel@tonic-gate 		dvma_addr = vmem_xalloc(mmu_p->mmu_dvma_map,
7731de45cd9Sgovinda 			MMU_PTOB(npages + PX_HAS_REDZONE(mp)),
7747c478bd9Sstevel@tonic-gate 			MAX(mp->dmai_attr.dma_attr_align, MMU_PAGE_SIZE),
7757c478bd9Sstevel@tonic-gate 			0,
7767c478bd9Sstevel@tonic-gate 			mp->dmai_attr.dma_attr_seg + 1,
7777c478bd9Sstevel@tonic-gate 			(void *)mp->dmai_attr.dma_attr_addr_lo,
7787c478bd9Sstevel@tonic-gate 			(void *)(mp->dmai_attr.dma_attr_addr_hi + 1),
7797c478bd9Sstevel@tonic-gate 			sleep);
7807c478bd9Sstevel@tonic-gate #ifdef	PX_DMA_PROF
7817c478bd9Sstevel@tonic-gate 		px_dvma_vmem_xalloc++;
7827c478bd9Sstevel@tonic-gate #endif	/* PX_DMA_PROF */
7837c478bd9Sstevel@tonic-gate 	}
7847c478bd9Sstevel@tonic-gate 	dvma_pg = MMU_BTOP((ulong_t)dvma_addr);
7857c478bd9Sstevel@tonic-gate 	dvma_pg_index = dvma_pg - mmu_p->dvma_base_pg;
7867c478bd9Sstevel@tonic-gate 	DBG(DBG_DMA_MAP, dip, "fallback dvma_pages: dvma_pg=%x index=%x\n",
7877c478bd9Sstevel@tonic-gate 		dvma_pg, dvma_pg_index);
7887c478bd9Sstevel@tonic-gate 	if (dvma_pg == 0)
7897c478bd9Sstevel@tonic-gate 		goto noresource;
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	mp->dmai_mapping = mp->dmai_roffset | MMU_PTOB(dvma_pg);
7927c478bd9Sstevel@tonic-gate 	mp->dmai_offset = 0;
7937c478bd9Sstevel@tonic-gate 	PX_SAVE_MP_TTE(mp, tte);	/* mp->dmai_tte = tte */
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	if ((ret = px_mmu_map_pages(mmu_p,
7967c478bd9Sstevel@tonic-gate 	    mp, dvma_pg, npages, 0)) != DDI_SUCCESS) {
79736fe4a92Segillett 		if (mp->dmai_flags & PX_DMAI_FLAGS_VMEMCACHE) {
7987c478bd9Sstevel@tonic-gate 			vmem_free(mmu_p->mmu_dvma_map, (void *)dvma_addr,
7997c478bd9Sstevel@tonic-gate 			    MMU_PAGE_SIZE);
8007c478bd9Sstevel@tonic-gate #ifdef PX_DMA_PROF
8017c478bd9Sstevel@tonic-gate 			px_dvma_vmem_free++;
8027c478bd9Sstevel@tonic-gate #endif /* PX_DMA_PROF */
8037c478bd9Sstevel@tonic-gate 		} else {
8047c478bd9Sstevel@tonic-gate 			vmem_xfree(mmu_p->mmu_dvma_map, (void *)dvma_addr,
8051de45cd9Sgovinda 			    MMU_PTOB(npages + PX_HAS_REDZONE(mp)));
8067c478bd9Sstevel@tonic-gate #ifdef PX_DMA_PROF
8077c478bd9Sstevel@tonic-gate 			px_dvma_vmem_xfree++;
8087c478bd9Sstevel@tonic-gate #endif /* PX_DMA_PROF */
8097c478bd9Sstevel@tonic-gate 		}
8107c478bd9Sstevel@tonic-gate 	}
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 	return (ret);
8137c478bd9Sstevel@tonic-gate noresource:
8147c478bd9Sstevel@tonic-gate 	if (dmareq->dmar_fp != DDI_DMA_DONTWAIT) {
8157c478bd9Sstevel@tonic-gate 		DBG(DBG_DMA_MAP, dip, "dvma_pg 0 - set callback\n");
8167c478bd9Sstevel@tonic-gate 		ddi_set_callback(dmareq->dmar_fp, dmareq->dmar_arg,
8177c478bd9Sstevel@tonic-gate 			&mmu_p->mmu_dvma_clid);
8187c478bd9Sstevel@tonic-gate 	}
8197c478bd9Sstevel@tonic-gate 	DBG(DBG_DMA_MAP, dip, "vmem_xalloc - DDI_DMA_NORESOURCES\n");
8207c478bd9Sstevel@tonic-gate 	return (DDI_DMA_NORESOURCES);
8217c478bd9Sstevel@tonic-gate }
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate void
8247c478bd9Sstevel@tonic-gate px_dvma_unmap(px_mmu_t *mmu_p, ddi_dma_impl_t *mp)
8257c478bd9Sstevel@tonic-gate {
8267c478bd9Sstevel@tonic-gate 	px_dvma_addr_t dvma_addr = (px_dvma_addr_t)mp->dmai_mapping;
8277c478bd9Sstevel@tonic-gate 	px_dvma_addr_t dvma_pg = MMU_BTOP(dvma_addr);
8287c478bd9Sstevel@tonic-gate 	dvma_addr = MMU_PTOB(dvma_pg);
8297c478bd9Sstevel@tonic-gate 
83036fe4a92Segillett 	if (mp->dmai_flags & PX_DMAI_FLAGS_FASTTRACK) {
8317c478bd9Sstevel@tonic-gate 		px_iopfn_t index = dvma_pg - mmu_p->dvma_base_pg;
8327c478bd9Sstevel@tonic-gate 		ASSERT(index % px_dvma_page_cache_clustsz == 0);
8337c478bd9Sstevel@tonic-gate 		index /= px_dvma_page_cache_clustsz;
8347c478bd9Sstevel@tonic-gate 		ASSERT(index < px_dvma_page_cache_entries);
8357c478bd9Sstevel@tonic-gate 		mmu_p->mmu_dvma_cache_locks[index] = 0;
8367c478bd9Sstevel@tonic-gate #ifdef	PX_DMA_PROF
8377c478bd9Sstevel@tonic-gate 		px_dvmaft_free++;
8387c478bd9Sstevel@tonic-gate #endif	/* PX_DMA_PROF */
8397c478bd9Sstevel@tonic-gate 		return;
8407c478bd9Sstevel@tonic-gate 	}
8417c478bd9Sstevel@tonic-gate 
84236fe4a92Segillett 	if (mp->dmai_flags & PX_DMAI_FLAGS_VMEMCACHE) {
8437c478bd9Sstevel@tonic-gate 		vmem_free(mmu_p->mmu_dvma_map, (void *)dvma_addr,
8447c478bd9Sstevel@tonic-gate 			MMU_PAGE_SIZE);
8457c478bd9Sstevel@tonic-gate #ifdef PX_DMA_PROF
8467c478bd9Sstevel@tonic-gate 		px_dvma_vmem_free++;
8477c478bd9Sstevel@tonic-gate #endif /* PX_DMA_PROF */
8487c478bd9Sstevel@tonic-gate 	} else {
8491de45cd9Sgovinda 		size_t npages = MMU_BTOP(mp->dmai_winsize) + PX_HAS_REDZONE(mp);
8507c478bd9Sstevel@tonic-gate 		vmem_xfree(mmu_p->mmu_dvma_map, (void *)dvma_addr,
8517c478bd9Sstevel@tonic-gate 			MMU_PTOB(npages));
8527c478bd9Sstevel@tonic-gate #ifdef PX_DMA_PROF
8537c478bd9Sstevel@tonic-gate 		px_dvma_vmem_xfree++;
8547c478bd9Sstevel@tonic-gate #endif /* PX_DMA_PROF */
8557c478bd9Sstevel@tonic-gate 	}
8567c478bd9Sstevel@tonic-gate }
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate /*
8597c478bd9Sstevel@tonic-gate  * DVMA mappings may have multiple windows, but each window always have
8607c478bd9Sstevel@tonic-gate  * one segment.
8617c478bd9Sstevel@tonic-gate  */
8627c478bd9Sstevel@tonic-gate int
8637c478bd9Sstevel@tonic-gate px_dvma_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_impl_t *mp,
8647c478bd9Sstevel@tonic-gate 	enum ddi_dma_ctlops cmd, off_t *offp, size_t *lenp, caddr_t *objp,
8657c478bd9Sstevel@tonic-gate 	uint_t cache_flags)
8667c478bd9Sstevel@tonic-gate {
8677c478bd9Sstevel@tonic-gate 	switch (cmd) {
8687c478bd9Sstevel@tonic-gate 	case DDI_DMA_SYNC:
8697c478bd9Sstevel@tonic-gate 		return (px_lib_dma_sync(dip, rdip, (ddi_dma_handle_t)mp,
8707c478bd9Sstevel@tonic-gate 		    *offp, *lenp, cache_flags));
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	case DDI_DMA_HTOC: {
8737c478bd9Sstevel@tonic-gate 		int ret;
8747c478bd9Sstevel@tonic-gate 		off_t wo_off, off = *offp;	/* wo_off: wnd's obj offset */
8757c478bd9Sstevel@tonic-gate 		uint_t win_size = mp->dmai_winsize;
8767c478bd9Sstevel@tonic-gate 		ddi_dma_cookie_t *cp = (ddi_dma_cookie_t *)objp;
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 		if (off >= mp->dmai_object.dmao_size) {
8797c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d invalid dma_htoc offset %lx",
8807c478bd9Sstevel@tonic-gate 				NAMEINST(mp->dmai_rdip), off);
8817c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
8827c478bd9Sstevel@tonic-gate 		}
8837c478bd9Sstevel@tonic-gate 		off += mp->dmai_roffset;
8847c478bd9Sstevel@tonic-gate 		ret = px_dma_win(dip, rdip, (ddi_dma_handle_t)mp,
8857c478bd9Sstevel@tonic-gate 		    off / win_size, &wo_off, NULL, cp, NULL); /* lenp == NULL */
8867c478bd9Sstevel@tonic-gate 		if (ret)
8877c478bd9Sstevel@tonic-gate 			return (ret);
8887c478bd9Sstevel@tonic-gate 		DBG(DBG_DMA_CTL, dip, "HTOC:cookie=%x+%lx off=%lx,%lx\n",
8897c478bd9Sstevel@tonic-gate 			cp->dmac_address, cp->dmac_size, off, *offp);
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 		/* adjust cookie addr/len if we are not on window boundary */
8927c478bd9Sstevel@tonic-gate 		ASSERT((off % win_size) == (off -
8937c478bd9Sstevel@tonic-gate 			(PX_DMA_CURWIN(mp) ? mp->dmai_roffset : 0) - wo_off));
8947c478bd9Sstevel@tonic-gate 		off = PX_DMA_CURWIN(mp) ? off % win_size : *offp;
8957c478bd9Sstevel@tonic-gate 		ASSERT(cp->dmac_size > off);
8967c478bd9Sstevel@tonic-gate 		cp->dmac_laddress += off;
8977c478bd9Sstevel@tonic-gate 		cp->dmac_size -= off;
8987c478bd9Sstevel@tonic-gate 		DBG(DBG_DMA_CTL, dip, "HTOC:mp=%p cookie=%x+%lx off=%lx,%lx\n",
8997c478bd9Sstevel@tonic-gate 			mp, cp->dmac_address, cp->dmac_size, off, wo_off);
9007c478bd9Sstevel@tonic-gate 		}
9017c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	case DDI_DMA_REPWIN:
9047c478bd9Sstevel@tonic-gate 		*offp = mp->dmai_offset;
9057c478bd9Sstevel@tonic-gate 		*lenp = mp->dmai_size;
9067c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	case DDI_DMA_MOVWIN: {
9097c478bd9Sstevel@tonic-gate 		off_t off = *offp;
9107c478bd9Sstevel@tonic-gate 		if (off >= mp->dmai_object.dmao_size)
9117c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
9127c478bd9Sstevel@tonic-gate 		off += mp->dmai_roffset;
9137c478bd9Sstevel@tonic-gate 		return (px_dma_win(dip, rdip, (ddi_dma_handle_t)mp,
9147c478bd9Sstevel@tonic-gate 		    off / mp->dmai_winsize, offp, lenp,
9157c478bd9Sstevel@tonic-gate 		    (ddi_dma_cookie_t *)objp, NULL));
9167c478bd9Sstevel@tonic-gate 		}
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 	case DDI_DMA_NEXTWIN: {
9197c478bd9Sstevel@tonic-gate 		px_window_t win = PX_DMA_CURWIN(mp);
9207c478bd9Sstevel@tonic-gate 		if (offp) {
9217c478bd9Sstevel@tonic-gate 			if (*(px_window_t *)offp != win) {
9227c478bd9Sstevel@tonic-gate 				/* window not active */
9237c478bd9Sstevel@tonic-gate 				*(px_window_t *)objp = win; /* return cur win */
9247c478bd9Sstevel@tonic-gate 				return (DDI_DMA_STALE);
9257c478bd9Sstevel@tonic-gate 			}
9267c478bd9Sstevel@tonic-gate 			win++;
9277c478bd9Sstevel@tonic-gate 		} else	/* map win 0 */
9287c478bd9Sstevel@tonic-gate 			win = 0;
9297c478bd9Sstevel@tonic-gate 		if (win >= mp->dmai_nwin) {
9307c478bd9Sstevel@tonic-gate 			*(px_window_t *)objp = win - 1;
9317c478bd9Sstevel@tonic-gate 			return (DDI_DMA_DONE);
9327c478bd9Sstevel@tonic-gate 		}
9337c478bd9Sstevel@tonic-gate 		if (px_dma_win(dip, rdip, (ddi_dma_handle_t)mp,
9347c478bd9Sstevel@tonic-gate 		    win, 0, 0, 0, 0)) {
9357c478bd9Sstevel@tonic-gate 			*(px_window_t *)objp = win - 1;
9367c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
9377c478bd9Sstevel@tonic-gate 		}
9387c478bd9Sstevel@tonic-gate 		*(px_window_t *)objp = win;
9397c478bd9Sstevel@tonic-gate 		}
9407c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 	case DDI_DMA_NEXTSEG:
9437c478bd9Sstevel@tonic-gate 		if (*(px_window_t *)offp != PX_DMA_CURWIN(mp))
9447c478bd9Sstevel@tonic-gate 			return (DDI_DMA_STALE);
9457c478bd9Sstevel@tonic-gate 		if (lenp)				/* only 1 seg allowed */
9467c478bd9Sstevel@tonic-gate 			return (DDI_DMA_DONE);
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 		/* return mp as seg 0 */
9497c478bd9Sstevel@tonic-gate 		*(ddi_dma_seg_t *)objp = (ddi_dma_seg_t)mp;
9507c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 	case DDI_DMA_SEGTOC:
9537c478bd9Sstevel@tonic-gate 		MAKE_DMA_COOKIE((ddi_dma_cookie_t *)objp, mp->dmai_mapping,
9547c478bd9Sstevel@tonic-gate 			mp->dmai_size);
9557c478bd9Sstevel@tonic-gate 		*offp = mp->dmai_offset;
9567c478bd9Sstevel@tonic-gate 		*lenp = mp->dmai_size;
9577c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 	case DDI_DMA_COFF: {
9607c478bd9Sstevel@tonic-gate 		ddi_dma_cookie_t *cp = (ddi_dma_cookie_t *)offp;
9617c478bd9Sstevel@tonic-gate 		if (cp->dmac_address < mp->dmai_mapping ||
9627c478bd9Sstevel@tonic-gate 			(cp->dmac_address + cp->dmac_size) >
9637c478bd9Sstevel@tonic-gate 			(mp->dmai_mapping + mp->dmai_size))
9647c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
9657c478bd9Sstevel@tonic-gate 		*objp = (caddr_t)(cp->dmac_address - mp->dmai_mapping +
9667c478bd9Sstevel@tonic-gate 			mp->dmai_offset);
9677c478bd9Sstevel@tonic-gate 		}
9687c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
9697c478bd9Sstevel@tonic-gate 	default:
9707c478bd9Sstevel@tonic-gate 		DBG(DBG_DMA_CTL, dip, "unknown command (%x): rdip=%s%d\n",
9717c478bd9Sstevel@tonic-gate 			cmd, ddi_driver_name(rdip), ddi_get_instance(rdip));
9727c478bd9Sstevel@tonic-gate 		break;
9737c478bd9Sstevel@tonic-gate 	}
9747c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
9757c478bd9Sstevel@tonic-gate }
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate void
9787c478bd9Sstevel@tonic-gate px_dma_freewin(ddi_dma_impl_t *mp)
9797c478bd9Sstevel@tonic-gate {
9807c478bd9Sstevel@tonic-gate 	px_dma_win_t *win_p = mp->dmai_winlst, *win2_p;
9817c478bd9Sstevel@tonic-gate 	for (win2_p = win_p; win_p; win2_p = win_p) {
9827c478bd9Sstevel@tonic-gate 		win_p = win2_p->win_next;
9837c478bd9Sstevel@tonic-gate 		kmem_free(win2_p, sizeof (px_dma_win_t) +
9847c478bd9Sstevel@tonic-gate 			sizeof (ddi_dma_cookie_t) * win2_p->win_ncookies);
9857c478bd9Sstevel@tonic-gate 	}
9867c478bd9Sstevel@tonic-gate 	mp->dmai_nwin = 0;
9877c478bd9Sstevel@tonic-gate 	mp->dmai_winlst = NULL;
9887c478bd9Sstevel@tonic-gate }
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate /*
9917c478bd9Sstevel@tonic-gate  * px_dma_newwin - create a dma window object and cookies
9927c478bd9Sstevel@tonic-gate  *
9937c478bd9Sstevel@tonic-gate  *	After the initial scan in px_dma_physwin(), which identifies
9947c478bd9Sstevel@tonic-gate  *	a portion of the pfn array that belongs to a dma window,
9957c478bd9Sstevel@tonic-gate  *	we are called to allocate and initialize representing memory
9967c478bd9Sstevel@tonic-gate  *	resources. We know from the 1st scan the number of cookies
9977c478bd9Sstevel@tonic-gate  *	or dma segment in this window so we can allocate a contiguous
9987c478bd9Sstevel@tonic-gate  *	memory array for the dma cookies (The implementation of
9997c478bd9Sstevel@tonic-gate  *	ddi_dma_nextcookie(9f) dictates dma cookies be contiguous).
10007c478bd9Sstevel@tonic-gate  *
10017c478bd9Sstevel@tonic-gate  *	A second round scan is done on the pfn array to identify
10027c478bd9Sstevel@tonic-gate  *	each dma segment and initialize its corresponding dma cookie.
10037c478bd9Sstevel@tonic-gate  *	We don't need to do all the safety checking and we know they
10047c478bd9Sstevel@tonic-gate  *	all belong to the same dma window.
10057c478bd9Sstevel@tonic-gate  *
10067c478bd9Sstevel@tonic-gate  *	Input:	cookie_no - # of cookies identified by the 1st scan
10077c478bd9Sstevel@tonic-gate  *		start_idx - subscript of the pfn array for the starting pfn
10087c478bd9Sstevel@tonic-gate  *		end_idx   - subscript of the last pfn in dma window
10097c478bd9Sstevel@tonic-gate  *		win_pp    - pointer to win_next member of previous window
10107c478bd9Sstevel@tonic-gate  *	Return:	DDI_SUCCESS - with **win_pp as newly created window object
10117c478bd9Sstevel@tonic-gate  *		DDI_DMA_NORESROUCE - caller frees all previous window objs
10127c478bd9Sstevel@tonic-gate  *	Note:	Each cookie and window size are all initialized on page
10137c478bd9Sstevel@tonic-gate  *		boundary. This is not true for the 1st cookie of the 1st
10147c478bd9Sstevel@tonic-gate  *		window and the last cookie of the last window.
10157c478bd9Sstevel@tonic-gate  *		We fix that later in upper layer which has access to size
10167c478bd9Sstevel@tonic-gate  *		and offset info.
10177c478bd9Sstevel@tonic-gate  *
10187c478bd9Sstevel@tonic-gate  */
10197c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10207c478bd9Sstevel@tonic-gate static int
10217c478bd9Sstevel@tonic-gate px_dma_newwin(dev_info_t *dip, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp,
10227c478bd9Sstevel@tonic-gate 	uint32_t cookie_no, uint32_t start_idx, uint32_t end_idx,
10237c478bd9Sstevel@tonic-gate 	px_dma_win_t **win_pp, uint64_t count_max, uint64_t bypass)
10247c478bd9Sstevel@tonic-gate {
10257c478bd9Sstevel@tonic-gate 	int (*waitfp)(caddr_t) = dmareq->dmar_fp;
10267c478bd9Sstevel@tonic-gate 	ddi_dma_cookie_t *cookie_p;
10277c478bd9Sstevel@tonic-gate 	uint32_t pfn_no = 1;
10287c478bd9Sstevel@tonic-gate 	px_iopfn_t pfn = PX_GET_MP_PFN(mp, start_idx);
10297c478bd9Sstevel@tonic-gate 	px_iopfn_t prev_pfn = pfn;
10307c478bd9Sstevel@tonic-gate 	uint64_t baddr, seg_pfn0 = pfn;
10317c478bd9Sstevel@tonic-gate 	size_t sz = cookie_no * sizeof (ddi_dma_cookie_t);
10327c478bd9Sstevel@tonic-gate 	px_dma_win_t *win_p = kmem_zalloc(sizeof (px_dma_win_t) + sz,
10337c478bd9Sstevel@tonic-gate 		waitfp == DDI_DMA_SLEEP ? KM_SLEEP : KM_NOSLEEP);
10347c478bd9Sstevel@tonic-gate 	io_attributes_t	attr = PX_GET_TTE_ATTR(mp->dmai_rflags);
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 	if (!win_p)
10377c478bd9Sstevel@tonic-gate 		goto noresource;
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	win_p->win_next = NULL;
10407c478bd9Sstevel@tonic-gate 	win_p->win_ncookies = cookie_no;
10417c478bd9Sstevel@tonic-gate 	win_p->win_curseg = 0;	/* start from segment 0 */
10427c478bd9Sstevel@tonic-gate 	win_p->win_size = MMU_PTOB(end_idx - start_idx + 1);
10437c478bd9Sstevel@tonic-gate 	/* win_p->win_offset is left uninitialized */
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 	cookie_p = (ddi_dma_cookie_t *)(win_p + 1);
10467c478bd9Sstevel@tonic-gate 	start_idx++;
10477c478bd9Sstevel@tonic-gate 	for (; start_idx <= end_idx; start_idx++, prev_pfn = pfn, pfn_no++) {
10487c478bd9Sstevel@tonic-gate 		pfn = PX_GET_MP_PFN1(mp, start_idx);
10497c478bd9Sstevel@tonic-gate 		if ((pfn == prev_pfn + 1) &&
10507c478bd9Sstevel@tonic-gate 			(MMU_PTOB(pfn_no + 1) - 1 <= count_max))
10517c478bd9Sstevel@tonic-gate 			continue;
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 		/* close up the cookie up to (including) prev_pfn */
10547c478bd9Sstevel@tonic-gate 		baddr = MMU_PTOB(seg_pfn0);
10557c478bd9Sstevel@tonic-gate 		if (bypass && (px_lib_iommu_getbypass(dip,
10567c478bd9Sstevel@tonic-gate 				baddr, attr, &baddr) != DDI_SUCCESS))
10577c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 		MAKE_DMA_COOKIE(cookie_p, baddr, MMU_PTOB(pfn_no));
10607c478bd9Sstevel@tonic-gate 		DBG(DBG_BYPASS, mp->dmai_rdip, "cookie %p (%x pages)\n",
10617c478bd9Sstevel@tonic-gate 			MMU_PTOB(seg_pfn0), pfn_no);
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 		cookie_p++;	/* advance to next available cookie cell */
10647c478bd9Sstevel@tonic-gate 		pfn_no = 0;
10657c478bd9Sstevel@tonic-gate 		seg_pfn0 = pfn;	/* start a new segment from current pfn */
10667c478bd9Sstevel@tonic-gate 	}
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	baddr = MMU_PTOB(seg_pfn0);
10697c478bd9Sstevel@tonic-gate 	if (bypass && (px_lib_iommu_getbypass(dip,
10707c478bd9Sstevel@tonic-gate 			baddr, attr, &baddr) != DDI_SUCCESS))
10717c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 	MAKE_DMA_COOKIE(cookie_p, baddr, MMU_PTOB(pfn_no));
10747c478bd9Sstevel@tonic-gate 	DBG(DBG_BYPASS, mp->dmai_rdip, "cookie %p (%x pages) of total %x\n",
10757c478bd9Sstevel@tonic-gate 		MMU_PTOB(seg_pfn0), pfn_no, cookie_no);
10767c478bd9Sstevel@tonic-gate #ifdef	DEBUG
10777c478bd9Sstevel@tonic-gate 	cookie_p++;
10787c478bd9Sstevel@tonic-gate 	ASSERT((cookie_p - (ddi_dma_cookie_t *)(win_p + 1)) == cookie_no);
10797c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
10807c478bd9Sstevel@tonic-gate 	*win_pp = win_p;
10817c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
10827c478bd9Sstevel@tonic-gate noresource:
10837c478bd9Sstevel@tonic-gate 	if (waitfp != DDI_DMA_DONTWAIT)
10847c478bd9Sstevel@tonic-gate 		ddi_set_callback(waitfp, dmareq->dmar_arg, &px_kmem_clid);
10857c478bd9Sstevel@tonic-gate 	return (DDI_DMA_NORESOURCES);
10867c478bd9Sstevel@tonic-gate }
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate /*
10897c478bd9Sstevel@tonic-gate  * px_dma_adjust - adjust 1st and last cookie and window sizes
10907c478bd9Sstevel@tonic-gate  *	remove initial dma page offset from 1st cookie and window size
10917c478bd9Sstevel@tonic-gate  *	remove last dma page remainder from last cookie and window size
10927c478bd9Sstevel@tonic-gate  *	fill win_offset of each dma window according to just fixed up
10937c478bd9Sstevel@tonic-gate  *		each window sizes
10947c478bd9Sstevel@tonic-gate  *	px_dma_win_t members modified:
10957c478bd9Sstevel@tonic-gate  *	win_p->win_offset - this window's offset within entire DMA object
10967c478bd9Sstevel@tonic-gate  *	win_p->win_size	  - xferrable size (in bytes) for this window
10977c478bd9Sstevel@tonic-gate  *
10987c478bd9Sstevel@tonic-gate  *	ddi_dma_impl_t members modified:
10997c478bd9Sstevel@tonic-gate  *	mp->dmai_size	  - 1st window xferrable size
11007c478bd9Sstevel@tonic-gate  *	mp->dmai_offset   - 0, which is the dma offset of the 1st window
11017c478bd9Sstevel@tonic-gate  *
11027c478bd9Sstevel@tonic-gate  *	ddi_dma_cookie_t members modified:
11037c478bd9Sstevel@tonic-gate  *	cookie_p->dmac_size - 1st and last cookie remove offset or remainder
11047c478bd9Sstevel@tonic-gate  *	cookie_p->dmac_laddress - 1st cookie add page offset
11057c478bd9Sstevel@tonic-gate  */
11067c478bd9Sstevel@tonic-gate static void
11077c478bd9Sstevel@tonic-gate px_dma_adjust(ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp, px_dma_win_t *win_p)
11087c478bd9Sstevel@tonic-gate {
11097c478bd9Sstevel@tonic-gate 	ddi_dma_cookie_t *cookie_p = (ddi_dma_cookie_t *)(win_p + 1);
11107c478bd9Sstevel@tonic-gate 	size_t pg_offset = mp->dmai_roffset;
11117c478bd9Sstevel@tonic-gate 	size_t win_offset = 0;
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 	cookie_p->dmac_size -= pg_offset;
11147c478bd9Sstevel@tonic-gate 	cookie_p->dmac_laddress |= pg_offset;
11157c478bd9Sstevel@tonic-gate 	win_p->win_size -= pg_offset;
11167c478bd9Sstevel@tonic-gate 	DBG(DBG_BYPASS, mp->dmai_rdip, "pg0 adjust %lx\n", pg_offset);
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 	mp->dmai_size = win_p->win_size;
11197c478bd9Sstevel@tonic-gate 	mp->dmai_offset = 0;
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 	pg_offset += mp->dmai_object.dmao_size;
11227c478bd9Sstevel@tonic-gate 	pg_offset &= MMU_PAGE_OFFSET;
11237c478bd9Sstevel@tonic-gate 	if (pg_offset)
11247c478bd9Sstevel@tonic-gate 		pg_offset = MMU_PAGE_SIZE - pg_offset;
11257c478bd9Sstevel@tonic-gate 	DBG(DBG_BYPASS, mp->dmai_rdip, "last pg adjust %lx\n", pg_offset);
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	for (; win_p->win_next; win_p = win_p->win_next) {
11287c478bd9Sstevel@tonic-gate 		DBG(DBG_BYPASS, mp->dmai_rdip, "win off %p\n", win_offset);
11297c478bd9Sstevel@tonic-gate 		win_p->win_offset = win_offset;
11307c478bd9Sstevel@tonic-gate 		win_offset += win_p->win_size;
11317c478bd9Sstevel@tonic-gate 	}
11327c478bd9Sstevel@tonic-gate 	/* last window */
11337c478bd9Sstevel@tonic-gate 	win_p->win_offset = win_offset;
11347c478bd9Sstevel@tonic-gate 	cookie_p = (ddi_dma_cookie_t *)(win_p + 1);
11357c478bd9Sstevel@tonic-gate 	cookie_p[win_p->win_ncookies - 1].dmac_size -= pg_offset;
11367c478bd9Sstevel@tonic-gate 	win_p->win_size -= pg_offset;
11377c478bd9Sstevel@tonic-gate 	ASSERT((win_offset + win_p->win_size) == mp->dmai_object.dmao_size);
11387c478bd9Sstevel@tonic-gate }
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate /*
11417c478bd9Sstevel@tonic-gate  * px_dma_physwin() - carve up dma windows using physical addresses.
11427c478bd9Sstevel@tonic-gate  *	Called to handle mmu bypass and pci peer-to-peer transfers.
11437c478bd9Sstevel@tonic-gate  *	Calls px_dma_newwin() to allocate window objects.
11447c478bd9Sstevel@tonic-gate  *
11457c478bd9Sstevel@tonic-gate  * Dependency: mp->dmai_pfnlst points to an array of pfns
11467c478bd9Sstevel@tonic-gate  *
11477c478bd9Sstevel@tonic-gate  * 1. Each dma window is represented by a px_dma_win_t object.
11487c478bd9Sstevel@tonic-gate  *	The object will be casted to ddi_dma_win_t and returned
11497c478bd9Sstevel@tonic-gate  *	to leaf driver through the DDI interface.
11507c478bd9Sstevel@tonic-gate  * 2. Each dma window can have several dma segments with each
11517c478bd9Sstevel@tonic-gate  *	segment representing a physically contiguous either memory
11527c478bd9Sstevel@tonic-gate  *	space (if we are doing an mmu bypass transfer) or pci address
11537c478bd9Sstevel@tonic-gate  *	space (if we are doing a peer-to-peer transfer).
11547c478bd9Sstevel@tonic-gate  * 3. Each segment has a DMA cookie to program the DMA engine.
11557c478bd9Sstevel@tonic-gate  *	The cookies within each DMA window must be located in a
11567c478bd9Sstevel@tonic-gate  *	contiguous array per ddi_dma_nextcookie(9f).
11577c478bd9Sstevel@tonic-gate  * 4. The number of DMA segments within each DMA window cannot exceed
11587c478bd9Sstevel@tonic-gate  *	mp->dmai_attr.dma_attr_sgllen. If the transfer size is
11597c478bd9Sstevel@tonic-gate  *	too large to fit in the sgllen, the rest needs to be
11607c478bd9Sstevel@tonic-gate  *	relocated to the next dma window.
11617c478bd9Sstevel@tonic-gate  * 5. Peer-to-peer DMA segment follows device hi, lo, count_max,
11627c478bd9Sstevel@tonic-gate  *	and nocross restrictions while bypass DMA follows the set of
11637c478bd9Sstevel@tonic-gate  *	restrictions with system limits factored in.
11647c478bd9Sstevel@tonic-gate  *
11657c478bd9Sstevel@tonic-gate  * Return:
11667c478bd9Sstevel@tonic-gate  *	mp->dmai_winlst	 - points to a link list of px_dma_win_t objects.
11677c478bd9Sstevel@tonic-gate  *		Each px_dma_win_t object on the link list contains
11687c478bd9Sstevel@tonic-gate  *		infomation such as its window size (# of pages),
11697c478bd9Sstevel@tonic-gate  *		starting offset (also see Restriction), an array of
11707c478bd9Sstevel@tonic-gate  *		DMA cookies, and # of cookies in the array.
11717c478bd9Sstevel@tonic-gate  *	mp->dmai_pfnlst	 - NULL, the pfn list is freed to conserve memory.
11727c478bd9Sstevel@tonic-gate  *	mp->dmai_nwin	 - # of total DMA windows on mp->dmai_winlst.
11737c478bd9Sstevel@tonic-gate  *	mp->dmai_mapping - starting cookie address
11747c478bd9Sstevel@tonic-gate  *	mp->dmai_rflags	 - consistent, nosync, no redzone
11757c478bd9Sstevel@tonic-gate  *	mp->dmai_cookie	 - start of cookie table of the 1st DMA window
11767c478bd9Sstevel@tonic-gate  *
11777c478bd9Sstevel@tonic-gate  * Restriction:
11787c478bd9Sstevel@tonic-gate  *	Each px_dma_win_t object can theoratically start from any offset
11797c478bd9Sstevel@tonic-gate  *	since the mmu is not involved. However, this implementation
11807c478bd9Sstevel@tonic-gate  *	always make windows start from page aligned offset (except
11817c478bd9Sstevel@tonic-gate  *	the 1st window, which follows the requested offset) due to the
11827c478bd9Sstevel@tonic-gate  *	fact that we are handed a pfn list. This does require device's
11837c478bd9Sstevel@tonic-gate  *	count_max and attr_seg to be at least MMU_PAGE_SIZE aligned.
11847c478bd9Sstevel@tonic-gate  */
11857c478bd9Sstevel@tonic-gate int
11867c478bd9Sstevel@tonic-gate px_dma_physwin(px_t *px_p, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp)
11877c478bd9Sstevel@tonic-gate {
11887c478bd9Sstevel@tonic-gate 	uint_t npages = mp->dmai_ndvmapages;
11897c478bd9Sstevel@tonic-gate 	int ret, sgllen = mp->dmai_attr.dma_attr_sgllen;
11907c478bd9Sstevel@tonic-gate 	px_iopfn_t pfn_lo, pfn_hi, prev_pfn;
11917c478bd9Sstevel@tonic-gate 	px_iopfn_t pfn = PX_GET_MP_PFN(mp, 0);
11927c478bd9Sstevel@tonic-gate 	uint32_t i, win_no = 0, pfn_no = 1, win_pfn0_index = 0, cookie_no = 0;
11937c478bd9Sstevel@tonic-gate 	uint64_t count_max, bypass_addr = 0;
11947c478bd9Sstevel@tonic-gate 	px_dma_win_t **win_pp = (px_dma_win_t **)&mp->dmai_winlst;
11957c478bd9Sstevel@tonic-gate 	ddi_dma_cookie_t *cookie0_p;
11967c478bd9Sstevel@tonic-gate 	io_attributes_t attr = PX_GET_TTE_ATTR(mp->dmai_rflags);
11977c478bd9Sstevel@tonic-gate 	dev_info_t *dip = px_p->px_dip;
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 	ASSERT(PX_DMA_ISPTP(mp) || PX_DMA_ISBYPASS(mp));
12007c478bd9Sstevel@tonic-gate 	if (PX_DMA_ISPTP(mp)) { /* ignore sys limits for peer-to-peer */
120136fe4a92Segillett 		ddi_dma_attr_t *dev_attr_p = PX_DEV_ATTR(mp);
12027c478bd9Sstevel@tonic-gate 		uint64_t nocross = dev_attr_p->dma_attr_seg;
12037c478bd9Sstevel@tonic-gate 		px_pec_t *pec_p = px_p->px_pec_p;
12047c478bd9Sstevel@tonic-gate 		px_iopfn_t pfn_last = PX_DMA_ISPTP32(mp) ?
12057c478bd9Sstevel@tonic-gate 				pec_p->pec_last32_pfn - pec_p->pec_base32_pfn :
12067c478bd9Sstevel@tonic-gate 				pec_p->pec_last64_pfn - pec_p->pec_base64_pfn;
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 		if (nocross && (nocross < UINT32_MAX))
12097c478bd9Sstevel@tonic-gate 			return (DDI_DMA_NOMAPPING);
12107c478bd9Sstevel@tonic-gate 		if (dev_attr_p->dma_attr_align > MMU_PAGE_SIZE)
12117c478bd9Sstevel@tonic-gate 			return (DDI_DMA_NOMAPPING);
12127c478bd9Sstevel@tonic-gate 		pfn_lo = MMU_BTOP(dev_attr_p->dma_attr_addr_lo);
12137c478bd9Sstevel@tonic-gate 		pfn_hi = MMU_BTOP(dev_attr_p->dma_attr_addr_hi);
12147c478bd9Sstevel@tonic-gate 		pfn_hi = MIN(pfn_hi, pfn_last);
12157c478bd9Sstevel@tonic-gate 		if ((pfn_lo > pfn_hi) || (pfn < pfn_lo))
12167c478bd9Sstevel@tonic-gate 			return (DDI_DMA_NOMAPPING);
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate 		count_max = dev_attr_p->dma_attr_count_max;
12197c478bd9Sstevel@tonic-gate 		count_max = MIN(count_max, nocross);
12207c478bd9Sstevel@tonic-gate 		/*
12217c478bd9Sstevel@tonic-gate 		 * the following count_max trim is not done because we are
12227c478bd9Sstevel@tonic-gate 		 * making sure pfn_lo <= pfn <= pfn_hi inside the loop
12237c478bd9Sstevel@tonic-gate 		 * count_max=MIN(count_max, MMU_PTOB(pfn_hi - pfn_lo + 1)-1);
12247c478bd9Sstevel@tonic-gate 		 */
12257c478bd9Sstevel@tonic-gate 	} else { /* bypass hi/lo/count_max have been processed by attr2hdl() */
12267c478bd9Sstevel@tonic-gate 		count_max = mp->dmai_attr.dma_attr_count_max;
12277c478bd9Sstevel@tonic-gate 		pfn_lo = MMU_BTOP(mp->dmai_attr.dma_attr_addr_lo);
12287c478bd9Sstevel@tonic-gate 		pfn_hi = MMU_BTOP(mp->dmai_attr.dma_attr_addr_hi);
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate 		if (px_lib_iommu_getbypass(dip, MMU_PTOB(pfn),
12317c478bd9Sstevel@tonic-gate 				attr, &bypass_addr) != DDI_SUCCESS) {
1232b40cec45Skrishnae 			cmn_err(CE_WARN, "bypass cookie failure %lx\n", pfn);
12337c478bd9Sstevel@tonic-gate 			return (DDI_DMA_NOMAPPING);
12347c478bd9Sstevel@tonic-gate 		}
12357c478bd9Sstevel@tonic-gate 		pfn = MMU_BTOP(bypass_addr);
12367c478bd9Sstevel@tonic-gate 	}
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 	/* pfn: absolute (bypass mode) or relative (p2p mode) */
12397c478bd9Sstevel@tonic-gate 	for (prev_pfn = pfn, i = 1; i < npages;
12407c478bd9Sstevel@tonic-gate 	    i++, prev_pfn = pfn, pfn_no++) {
12417c478bd9Sstevel@tonic-gate 		pfn = PX_GET_MP_PFN1(mp, i);
12427c478bd9Sstevel@tonic-gate 		if (bypass_addr) {
12437c478bd9Sstevel@tonic-gate 			if (px_lib_iommu_getbypass(dip, MMU_PTOB(pfn), attr,
12447c478bd9Sstevel@tonic-gate 					&bypass_addr) != DDI_SUCCESS) {
12457c478bd9Sstevel@tonic-gate 				ret = DDI_DMA_NOMAPPING;
12467c478bd9Sstevel@tonic-gate 				goto err;
12477c478bd9Sstevel@tonic-gate 			}
12487c478bd9Sstevel@tonic-gate 			pfn = MMU_BTOP(bypass_addr);
12497c478bd9Sstevel@tonic-gate 		}
12507c478bd9Sstevel@tonic-gate 		if ((pfn == prev_pfn + 1) &&
12517c478bd9Sstevel@tonic-gate 				(MMU_PTOB(pfn_no + 1) - 1 <= count_max))
12527c478bd9Sstevel@tonic-gate 			continue;
12537c478bd9Sstevel@tonic-gate 		if ((pfn < pfn_lo) || (prev_pfn > pfn_hi)) {
12547c478bd9Sstevel@tonic-gate 			ret = DDI_DMA_NOMAPPING;
12557c478bd9Sstevel@tonic-gate 			goto err;
12567c478bd9Sstevel@tonic-gate 		}
12577c478bd9Sstevel@tonic-gate 		cookie_no++;
12587c478bd9Sstevel@tonic-gate 		pfn_no = 0;
12597c478bd9Sstevel@tonic-gate 		if (cookie_no < sgllen)
12607c478bd9Sstevel@tonic-gate 			continue;
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate 		DBG(DBG_BYPASS, mp->dmai_rdip, "newwin pfn[%x-%x] %x cks\n",
12637c478bd9Sstevel@tonic-gate 			win_pfn0_index, i - 1, cookie_no);
12647c478bd9Sstevel@tonic-gate 		if (ret = px_dma_newwin(dip, dmareq, mp, cookie_no,
12657c478bd9Sstevel@tonic-gate 			win_pfn0_index, i - 1, win_pp, count_max, bypass_addr))
12667c478bd9Sstevel@tonic-gate 			goto err;
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate 		win_pp = &(*win_pp)->win_next;	/* win_pp = *(win_pp) */
12697c478bd9Sstevel@tonic-gate 		win_no++;
12707c478bd9Sstevel@tonic-gate 		win_pfn0_index = i;
12717c478bd9Sstevel@tonic-gate 		cookie_no = 0;
12727c478bd9Sstevel@tonic-gate 	}
12737c478bd9Sstevel@tonic-gate 	if (pfn > pfn_hi) {
12747c478bd9Sstevel@tonic-gate 		ret = DDI_DMA_NOMAPPING;
12757c478bd9Sstevel@tonic-gate 		goto err;
12767c478bd9Sstevel@tonic-gate 	}
12777c478bd9Sstevel@tonic-gate 	cookie_no++;
12787c478bd9Sstevel@tonic-gate 	DBG(DBG_BYPASS, mp->dmai_rdip, "newwin pfn[%x-%x] %x cks\n",
12797c478bd9Sstevel@tonic-gate 		win_pfn0_index, i - 1, cookie_no);
12807c478bd9Sstevel@tonic-gate 	if (ret = px_dma_newwin(dip, dmareq, mp, cookie_no, win_pfn0_index,
12817c478bd9Sstevel@tonic-gate 		i - 1, win_pp, count_max, bypass_addr))
12827c478bd9Sstevel@tonic-gate 		goto err;
12837c478bd9Sstevel@tonic-gate 	win_no++;
12847c478bd9Sstevel@tonic-gate 	px_dma_adjust(dmareq, mp, mp->dmai_winlst);
12857c478bd9Sstevel@tonic-gate 	mp->dmai_nwin = win_no;
12867c478bd9Sstevel@tonic-gate 	mp->dmai_rflags |= DDI_DMA_CONSISTENT | DMP_NOSYNC;
12877c478bd9Sstevel@tonic-gate 	mp->dmai_rflags &= ~DDI_DMA_REDZONE;
128836fe4a92Segillett 	mp->dmai_flags |= PX_DMAI_FLAGS_NOSYNC;
128936fe4a92Segillett 	cookie0_p = (ddi_dma_cookie_t *)(PX_WINLST(mp) + 1);
129036fe4a92Segillett 	mp->dmai_cookie = PX_WINLST(mp)->win_ncookies > 1 ? cookie0_p + 1 : 0;
12917c478bd9Sstevel@tonic-gate 	mp->dmai_mapping = cookie0_p->dmac_laddress;
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 	px_dma_freepfn(mp);
12947c478bd9Sstevel@tonic-gate 	return (DDI_DMA_MAPPED);
12957c478bd9Sstevel@tonic-gate err:
12967c478bd9Sstevel@tonic-gate 	px_dma_freewin(mp);
12977c478bd9Sstevel@tonic-gate 	return (ret);
12987c478bd9Sstevel@tonic-gate }
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate int
13017c478bd9Sstevel@tonic-gate px_dma_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_impl_t *mp,
13027c478bd9Sstevel@tonic-gate 	enum ddi_dma_ctlops cmd, off_t *offp, size_t *lenp, caddr_t *objp,
13037c478bd9Sstevel@tonic-gate 	uint_t cache_flags)
13047c478bd9Sstevel@tonic-gate {
13057c478bd9Sstevel@tonic-gate 	switch (cmd) {
13067c478bd9Sstevel@tonic-gate 	case DDI_DMA_SYNC:
13077c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate 	case DDI_DMA_HTOC: {
13107c478bd9Sstevel@tonic-gate 		off_t off = *offp;
13117c478bd9Sstevel@tonic-gate 		ddi_dma_cookie_t *loop_cp, *cp;
13127c478bd9Sstevel@tonic-gate 		px_dma_win_t *win_p = mp->dmai_winlst;
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate 		if (off >= mp->dmai_object.dmao_size)
13157c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
13167c478bd9Sstevel@tonic-gate 
13177c478bd9Sstevel@tonic-gate 		/* locate window */
13187c478bd9Sstevel@tonic-gate 		while (win_p->win_offset + win_p->win_size <= off)
13197c478bd9Sstevel@tonic-gate 			win_p = win_p->win_next;
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 		loop_cp = cp = (ddi_dma_cookie_t *)(win_p + 1);
13227c478bd9Sstevel@tonic-gate 		mp->dmai_offset = win_p->win_offset;
13237c478bd9Sstevel@tonic-gate 		mp->dmai_size   = win_p->win_size;
13247c478bd9Sstevel@tonic-gate 		mp->dmai_mapping = cp->dmac_laddress; /* cookie0 start addr */
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 		/* adjust cookie addr/len if we are not on cookie boundary */
13277c478bd9Sstevel@tonic-gate 		off -= win_p->win_offset;	   /* offset within window */
13287c478bd9Sstevel@tonic-gate 		for (; off >= loop_cp->dmac_size; loop_cp++)
13297c478bd9Sstevel@tonic-gate 			off -= loop_cp->dmac_size; /* offset within cookie */
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 		mp->dmai_cookie = loop_cp + 1;
13327c478bd9Sstevel@tonic-gate 		win_p->win_curseg = loop_cp - cp;
13337c478bd9Sstevel@tonic-gate 		cp = (ddi_dma_cookie_t *)objp;
13347c478bd9Sstevel@tonic-gate 		MAKE_DMA_COOKIE(cp, loop_cp->dmac_laddress + off,
13357c478bd9Sstevel@tonic-gate 			loop_cp->dmac_size - off);
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 		DBG(DBG_DMA_CTL, dip,
13387c478bd9Sstevel@tonic-gate 			"HTOC: cookie - dmac_laddress=%p dmac_size=%x\n",
13397c478bd9Sstevel@tonic-gate 			cp->dmac_laddress, cp->dmac_size);
13407c478bd9Sstevel@tonic-gate 		}
13417c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 	case DDI_DMA_REPWIN:
13447c478bd9Sstevel@tonic-gate 		*offp = mp->dmai_offset;
13457c478bd9Sstevel@tonic-gate 		*lenp = mp->dmai_size;
13467c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate 	case DDI_DMA_MOVWIN: {
13497c478bd9Sstevel@tonic-gate 		off_t off = *offp;
13507c478bd9Sstevel@tonic-gate 		ddi_dma_cookie_t *cp;
13517c478bd9Sstevel@tonic-gate 		px_dma_win_t *win_p = mp->dmai_winlst;
13527c478bd9Sstevel@tonic-gate 
13537c478bd9Sstevel@tonic-gate 		if (off >= mp->dmai_object.dmao_size)
13547c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate 		/* locate window */
13577c478bd9Sstevel@tonic-gate 		while (win_p->win_offset + win_p->win_size <= off)
13587c478bd9Sstevel@tonic-gate 			win_p = win_p->win_next;
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 		cp = (ddi_dma_cookie_t *)(win_p + 1);
13617c478bd9Sstevel@tonic-gate 		mp->dmai_offset = win_p->win_offset;
13627c478bd9Sstevel@tonic-gate 		mp->dmai_size   = win_p->win_size;
13637c478bd9Sstevel@tonic-gate 		mp->dmai_mapping = cp->dmac_laddress;	/* cookie0 star addr */
13647c478bd9Sstevel@tonic-gate 		mp->dmai_cookie = cp + 1;
13657c478bd9Sstevel@tonic-gate 		win_p->win_curseg = 0;
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate 		*(ddi_dma_cookie_t *)objp = *cp;
13687c478bd9Sstevel@tonic-gate 		*offp = win_p->win_offset;
13697c478bd9Sstevel@tonic-gate 		*lenp = win_p->win_size;
13707c478bd9Sstevel@tonic-gate 		DBG(DBG_DMA_CTL, dip,
13717c478bd9Sstevel@tonic-gate 			"HTOC: cookie - dmac_laddress=%p dmac_size=%x\n",
13727c478bd9Sstevel@tonic-gate 			cp->dmac_laddress, cp->dmac_size);
13737c478bd9Sstevel@tonic-gate 		}
13747c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate 	case DDI_DMA_NEXTWIN: {
13777c478bd9Sstevel@tonic-gate 		px_dma_win_t *win_p = *(px_dma_win_t **)offp;
13787c478bd9Sstevel@tonic-gate 		px_dma_win_t **nw_pp = (px_dma_win_t **)objp;
13797c478bd9Sstevel@tonic-gate 		ddi_dma_cookie_t *cp;
13807c478bd9Sstevel@tonic-gate 		if (!win_p) {
13817c478bd9Sstevel@tonic-gate 			*nw_pp = mp->dmai_winlst;
13827c478bd9Sstevel@tonic-gate 			return (DDI_SUCCESS);
13837c478bd9Sstevel@tonic-gate 		}
13847c478bd9Sstevel@tonic-gate 
13857c478bd9Sstevel@tonic-gate 		if (win_p->win_offset != mp->dmai_offset)
13867c478bd9Sstevel@tonic-gate 			return (DDI_DMA_STALE);
13877c478bd9Sstevel@tonic-gate 		if (!win_p->win_next)
13887c478bd9Sstevel@tonic-gate 			return (DDI_DMA_DONE);
13897c478bd9Sstevel@tonic-gate 		win_p = win_p->win_next;
13907c478bd9Sstevel@tonic-gate 		cp = (ddi_dma_cookie_t *)(win_p + 1);
13917c478bd9Sstevel@tonic-gate 		mp->dmai_offset = win_p->win_offset;
13927c478bd9Sstevel@tonic-gate 		mp->dmai_size   = win_p->win_size;
13937c478bd9Sstevel@tonic-gate 		mp->dmai_mapping = cp->dmac_laddress;   /* cookie0 star addr */
13947c478bd9Sstevel@tonic-gate 		mp->dmai_cookie = cp + 1;
13957c478bd9Sstevel@tonic-gate 		win_p->win_curseg = 0;
13967c478bd9Sstevel@tonic-gate 		*nw_pp = win_p;
13977c478bd9Sstevel@tonic-gate 		}
13987c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 	case DDI_DMA_NEXTSEG: {
14017c478bd9Sstevel@tonic-gate 		px_dma_win_t *w_p = *(px_dma_win_t **)offp;
14027c478bd9Sstevel@tonic-gate 		if (w_p->win_offset != mp->dmai_offset)
14037c478bd9Sstevel@tonic-gate 			return (DDI_DMA_STALE);
14047c478bd9Sstevel@tonic-gate 		if (w_p->win_curseg + 1 >= w_p->win_ncookies)
14057c478bd9Sstevel@tonic-gate 			return (DDI_DMA_DONE);
14067c478bd9Sstevel@tonic-gate 		w_p->win_curseg++;
14077c478bd9Sstevel@tonic-gate 		}
14087c478bd9Sstevel@tonic-gate 		*(ddi_dma_seg_t *)objp = (ddi_dma_seg_t)mp;
14097c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate 	case DDI_DMA_SEGTOC: {
14127c478bd9Sstevel@tonic-gate 		px_dma_win_t *win_p = mp->dmai_winlst;
14137c478bd9Sstevel@tonic-gate 		off_t off = mp->dmai_offset;
14147c478bd9Sstevel@tonic-gate 		ddi_dma_cookie_t *cp;
14157c478bd9Sstevel@tonic-gate 		int i;
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate 		/* locate active window */
14187c478bd9Sstevel@tonic-gate 		for (; win_p->win_offset != off; win_p = win_p->win_next);
14197c478bd9Sstevel@tonic-gate 		cp = (ddi_dma_cookie_t *)(win_p + 1);
14207c478bd9Sstevel@tonic-gate 		for (i = 0; i < win_p->win_curseg; i++, cp++)
14217c478bd9Sstevel@tonic-gate 			off += cp->dmac_size;
14227c478bd9Sstevel@tonic-gate 		*offp = off;
14237c478bd9Sstevel@tonic-gate 		*lenp = cp->dmac_size;
14247c478bd9Sstevel@tonic-gate 		*(ddi_dma_cookie_t *)objp = *cp;	/* copy cookie */
14257c478bd9Sstevel@tonic-gate 		}
14267c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate 	case DDI_DMA_COFF: {
14297c478bd9Sstevel@tonic-gate 		px_dma_win_t *win_p;
14307c478bd9Sstevel@tonic-gate 		ddi_dma_cookie_t *cp;
14317c478bd9Sstevel@tonic-gate 		uint64_t addr, key = ((ddi_dma_cookie_t *)offp)->dmac_laddress;
14327c478bd9Sstevel@tonic-gate 		size_t win_off;
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate 		for (win_p = mp->dmai_winlst; win_p; win_p = win_p->win_next) {
14357c478bd9Sstevel@tonic-gate 			int i;
14367c478bd9Sstevel@tonic-gate 			win_off = 0;
14377c478bd9Sstevel@tonic-gate 			cp = (ddi_dma_cookie_t *)(win_p + 1);
14387c478bd9Sstevel@tonic-gate 			for (i = 0; i < win_p->win_ncookies; i++, cp++) {
14397c478bd9Sstevel@tonic-gate 				size_t sz = cp->dmac_size;
14407c478bd9Sstevel@tonic-gate 
14417c478bd9Sstevel@tonic-gate 				addr = cp->dmac_laddress;
14427c478bd9Sstevel@tonic-gate 				if ((addr <= key) && (addr + sz >= key))
14437c478bd9Sstevel@tonic-gate 					goto found;
14447c478bd9Sstevel@tonic-gate 				win_off += sz;
14457c478bd9Sstevel@tonic-gate 			}
14467c478bd9Sstevel@tonic-gate 		}
14477c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
14487c478bd9Sstevel@tonic-gate found:
14497c478bd9Sstevel@tonic-gate 		*objp = (caddr_t)(win_p->win_offset + win_off + (key - addr));
14507c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
14517c478bd9Sstevel@tonic-gate 		}
14527c478bd9Sstevel@tonic-gate 	default:
14537c478bd9Sstevel@tonic-gate 		DBG(DBG_DMA_CTL, dip, "unknown command (%x): rdip=%s%d\n",
14547c478bd9Sstevel@tonic-gate 			cmd, ddi_driver_name(rdip), ddi_get_instance(rdip));
14557c478bd9Sstevel@tonic-gate 		break;
14567c478bd9Sstevel@tonic-gate 	}
14577c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
14587c478bd9Sstevel@tonic-gate }
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate static void
14617c478bd9Sstevel@tonic-gate px_dvma_debug_init(px_mmu_t *mmu_p)
14627c478bd9Sstevel@tonic-gate {
14637c478bd9Sstevel@tonic-gate 	size_t sz = sizeof (struct px_dvma_rec) * px_dvma_debug_rec;
14647c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&mmu_p->dvma_debug_lock));
14657c478bd9Sstevel@tonic-gate 	cmn_err(CE_NOTE, "PCI Express DVMA %p stat ON", mmu_p);
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate 	mmu_p->dvma_alloc_rec = kmem_alloc(sz, KM_SLEEP);
14687c478bd9Sstevel@tonic-gate 	mmu_p->dvma_free_rec = kmem_alloc(sz, KM_SLEEP);
14697c478bd9Sstevel@tonic-gate 
14707c478bd9Sstevel@tonic-gate 	mmu_p->dvma_active_list = NULL;
14717c478bd9Sstevel@tonic-gate 	mmu_p->dvma_alloc_rec_index = 0;
14727c478bd9Sstevel@tonic-gate 	mmu_p->dvma_free_rec_index = 0;
14737c478bd9Sstevel@tonic-gate 	mmu_p->dvma_active_count = 0;
14747c478bd9Sstevel@tonic-gate }
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate void
14777c478bd9Sstevel@tonic-gate px_dvma_debug_fini(px_mmu_t *mmu_p)
14787c478bd9Sstevel@tonic-gate {
14797c478bd9Sstevel@tonic-gate 	struct px_dvma_rec *prev, *ptr;
14807c478bd9Sstevel@tonic-gate 	size_t sz = sizeof (struct px_dvma_rec) * px_dvma_debug_rec;
14817c478bd9Sstevel@tonic-gate 	uint64_t mask = ~(1ull << mmu_p->mmu_inst);
14827c478bd9Sstevel@tonic-gate 	cmn_err(CE_NOTE, "PCI Express DVMA %p stat OFF", mmu_p);
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate 	kmem_free(mmu_p->dvma_alloc_rec, sz);
14857c478bd9Sstevel@tonic-gate 	kmem_free(mmu_p->dvma_free_rec, sz);
14867c478bd9Sstevel@tonic-gate 	mmu_p->dvma_alloc_rec = mmu_p->dvma_free_rec = NULL;
14877c478bd9Sstevel@tonic-gate 
14887c478bd9Sstevel@tonic-gate 	prev = mmu_p->dvma_active_list;
14897c478bd9Sstevel@tonic-gate 	if (!prev)
14907c478bd9Sstevel@tonic-gate 		return;
14917c478bd9Sstevel@tonic-gate 	for (ptr = prev->next; ptr; prev = ptr, ptr = ptr->next)
14927c478bd9Sstevel@tonic-gate 		kmem_free(prev, sizeof (struct px_dvma_rec));
14937c478bd9Sstevel@tonic-gate 	kmem_free(prev, sizeof (struct px_dvma_rec));
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate 	mmu_p->dvma_active_list = NULL;
14967c478bd9Sstevel@tonic-gate 	mmu_p->dvma_alloc_rec_index = 0;
14977c478bd9Sstevel@tonic-gate 	mmu_p->dvma_free_rec_index = 0;
14987c478bd9Sstevel@tonic-gate 	mmu_p->dvma_active_count = 0;
14997c478bd9Sstevel@tonic-gate 
15007c478bd9Sstevel@tonic-gate 	px_dvma_debug_off &= mask;
15017c478bd9Sstevel@tonic-gate 	px_dvma_debug_on &= mask;
15027c478bd9Sstevel@tonic-gate }
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate void
15057c478bd9Sstevel@tonic-gate px_dvma_alloc_debug(px_mmu_t *mmu_p, char *address, uint_t len,
15067c478bd9Sstevel@tonic-gate 	ddi_dma_impl_t *mp)
15077c478bd9Sstevel@tonic-gate {
15087c478bd9Sstevel@tonic-gate 	struct px_dvma_rec *ptr;
15097c478bd9Sstevel@tonic-gate 	mutex_enter(&mmu_p->dvma_debug_lock);
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 	if (!mmu_p->dvma_alloc_rec)
15127c478bd9Sstevel@tonic-gate 		px_dvma_debug_init(mmu_p);
151336fe4a92Segillett 	if (PX_DVMA_DBG_OFF(mmu_p)) {
15147c478bd9Sstevel@tonic-gate 		px_dvma_debug_fini(mmu_p);
15157c478bd9Sstevel@tonic-gate 		goto done;
15167c478bd9Sstevel@tonic-gate 	}
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate 	ptr = &mmu_p->dvma_alloc_rec[mmu_p->dvma_alloc_rec_index];
15197c478bd9Sstevel@tonic-gate 	ptr->dvma_addr = address;
15207c478bd9Sstevel@tonic-gate 	ptr->len = len;
15217c478bd9Sstevel@tonic-gate 	ptr->mp = mp;
15227c478bd9Sstevel@tonic-gate 	if (++mmu_p->dvma_alloc_rec_index == px_dvma_debug_rec)
15237c478bd9Sstevel@tonic-gate 		mmu_p->dvma_alloc_rec_index = 0;
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 	ptr = kmem_alloc(sizeof (struct px_dvma_rec), KM_SLEEP);
15267c478bd9Sstevel@tonic-gate 	ptr->dvma_addr = address;
15277c478bd9Sstevel@tonic-gate 	ptr->len = len;
15287c478bd9Sstevel@tonic-gate 	ptr->mp = mp;
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate 	ptr->next = mmu_p->dvma_active_list;
15317c478bd9Sstevel@tonic-gate 	mmu_p->dvma_active_list = ptr;
15327c478bd9Sstevel@tonic-gate 	mmu_p->dvma_active_count++;
15337c478bd9Sstevel@tonic-gate done:
15347c478bd9Sstevel@tonic-gate 	mutex_exit(&mmu_p->dvma_debug_lock);
15357c478bd9Sstevel@tonic-gate }
15367c478bd9Sstevel@tonic-gate 
15377c478bd9Sstevel@tonic-gate void
15387c478bd9Sstevel@tonic-gate px_dvma_free_debug(px_mmu_t *mmu_p, char *address, uint_t len,
15397c478bd9Sstevel@tonic-gate     ddi_dma_impl_t *mp)
15407c478bd9Sstevel@tonic-gate {
15417c478bd9Sstevel@tonic-gate 	struct px_dvma_rec *ptr, *ptr_save;
15427c478bd9Sstevel@tonic-gate 	mutex_enter(&mmu_p->dvma_debug_lock);
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate 	if (!mmu_p->dvma_alloc_rec)
15457c478bd9Sstevel@tonic-gate 		px_dvma_debug_init(mmu_p);
154636fe4a92Segillett 	if (PX_DVMA_DBG_OFF(mmu_p)) {
15477c478bd9Sstevel@tonic-gate 		px_dvma_debug_fini(mmu_p);
15487c478bd9Sstevel@tonic-gate 		goto done;
15497c478bd9Sstevel@tonic-gate 	}
15507c478bd9Sstevel@tonic-gate 
15517c478bd9Sstevel@tonic-gate 	ptr = &mmu_p->dvma_free_rec[mmu_p->dvma_free_rec_index];
15527c478bd9Sstevel@tonic-gate 	ptr->dvma_addr = address;
15537c478bd9Sstevel@tonic-gate 	ptr->len = len;
15547c478bd9Sstevel@tonic-gate 	ptr->mp = mp;
15557c478bd9Sstevel@tonic-gate 	if (++mmu_p->dvma_free_rec_index == px_dvma_debug_rec)
15567c478bd9Sstevel@tonic-gate 		mmu_p->dvma_free_rec_index = 0;
15577c478bd9Sstevel@tonic-gate 
15587c478bd9Sstevel@tonic-gate 	ptr_save = mmu_p->dvma_active_list;
15597c478bd9Sstevel@tonic-gate 	for (ptr = ptr_save; ptr; ptr = ptr->next) {
15607c478bd9Sstevel@tonic-gate 		if ((ptr->dvma_addr == address) && (ptr->len = len))
15617c478bd9Sstevel@tonic-gate 			break;
15627c478bd9Sstevel@tonic-gate 		ptr_save = ptr;
15637c478bd9Sstevel@tonic-gate 	}
15647c478bd9Sstevel@tonic-gate 	if (!ptr) {
15657c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "bad dvma free addr=%lx len=%x",
15667c478bd9Sstevel@tonic-gate 			(long)address, len);
15677c478bd9Sstevel@tonic-gate 		goto done;
15687c478bd9Sstevel@tonic-gate 	}
15697c478bd9Sstevel@tonic-gate 	if (ptr == mmu_p->dvma_active_list)
15707c478bd9Sstevel@tonic-gate 		mmu_p->dvma_active_list = ptr->next;
15717c478bd9Sstevel@tonic-gate 	else
15727c478bd9Sstevel@tonic-gate 		ptr_save->next = ptr->next;
15737c478bd9Sstevel@tonic-gate 	kmem_free(ptr, sizeof (struct px_dvma_rec));
15747c478bd9Sstevel@tonic-gate 	mmu_p->dvma_active_count--;
15757c478bd9Sstevel@tonic-gate done:
15767c478bd9Sstevel@tonic-gate 	mutex_exit(&mmu_p->dvma_debug_lock);
15777c478bd9Sstevel@tonic-gate }
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate #ifdef	DEBUG
15807c478bd9Sstevel@tonic-gate void
15817c478bd9Sstevel@tonic-gate px_dump_dma_handle(uint64_t flag, dev_info_t *dip, ddi_dma_impl_t *hp)
15827c478bd9Sstevel@tonic-gate {
15837c478bd9Sstevel@tonic-gate 	DBG(flag, dip, "mp(%p): flags=%x mapping=%lx xfer_size=%x\n",
15847c478bd9Sstevel@tonic-gate 		hp, hp->dmai_inuse, hp->dmai_mapping, hp->dmai_size);
15857c478bd9Sstevel@tonic-gate 	DBG(flag|DBG_CONT, dip, "\tnpages=%x roffset=%x rflags=%x nwin=%x\n",
15867c478bd9Sstevel@tonic-gate 		hp->dmai_ndvmapages, hp->dmai_roffset, hp->dmai_rflags,
15877c478bd9Sstevel@tonic-gate 		hp->dmai_nwin);
15887c478bd9Sstevel@tonic-gate 	DBG(flag|DBG_CONT, dip, "\twinsize=%x tte=%p pfnlst=%p pfn0=%p\n",
15897c478bd9Sstevel@tonic-gate 		hp->dmai_winsize, hp->dmai_tte, hp->dmai_pfnlst, hp->dmai_pfn0);
15907c478bd9Sstevel@tonic-gate 	DBG(flag|DBG_CONT, dip, "\twinlst=%x obj=%p attr=%p ckp=%p\n",
15917c478bd9Sstevel@tonic-gate 		hp->dmai_winlst, &hp->dmai_object, &hp->dmai_attr,
15927c478bd9Sstevel@tonic-gate 		hp->dmai_cookie);
15937c478bd9Sstevel@tonic-gate }
15947c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
1595