xref: /onnv-gate/usr/src/uts/sun4u/io/pci/pci_dma.c (revision 2251:99824a0e7def)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51501Sgovinda  * Common Development and Distribution License (the "License").
61501Sgovinda  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
221501Sgovinda  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * PCI nexus DVMA and DMA core routines:
300Sstevel@tonic-gate  *	dma_map/dma_bind_handle implementation
310Sstevel@tonic-gate  *	bypass and peer-to-peer support
320Sstevel@tonic-gate  *	fast track DVMA space allocation
330Sstevel@tonic-gate  *	runtime DVMA debug
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate #include <sys/types.h>
360Sstevel@tonic-gate #include <sys/kmem.h>
370Sstevel@tonic-gate #include <sys/async.h>
380Sstevel@tonic-gate #include <sys/sysmacros.h>
390Sstevel@tonic-gate #include <sys/sunddi.h>
400Sstevel@tonic-gate #include <sys/machsystm.h>	/* lddphys() */
410Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
420Sstevel@tonic-gate #include <vm/hat.h>
430Sstevel@tonic-gate #include <sys/pci/pci_obj.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate /*LINTLIBRARY*/
460Sstevel@tonic-gate 
470Sstevel@tonic-gate static void
480Sstevel@tonic-gate pci_sc_pg_inv(dev_info_t *dip, sc_t *sc_p, ddi_dma_impl_t *mp, off_t off,
490Sstevel@tonic-gate 	size_t len)
500Sstevel@tonic-gate {
510Sstevel@tonic-gate 	dvma_addr_t dvma_addr, pg_off;
520Sstevel@tonic-gate 	volatile uint64_t *invl_va = sc_p->sc_invl_reg;
530Sstevel@tonic-gate 
540Sstevel@tonic-gate 	if (!len)
550Sstevel@tonic-gate 		len = mp->dmai_size;
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 	pg_off = mp->dmai_offset;			/* start min */
580Sstevel@tonic-gate 	dvma_addr = MAX(off, pg_off);			/* lo */
590Sstevel@tonic-gate 	pg_off += mp->dmai_size;			/* end max */
600Sstevel@tonic-gate 	pg_off = MIN(off + len, pg_off);		/* hi */
610Sstevel@tonic-gate 	if (dvma_addr >= pg_off) {			/* lo >= hi ? */
620Sstevel@tonic-gate 		DEBUG4(DBG_SC, dip, "%x+%x out of window [%x,%x)\n",
630Sstevel@tonic-gate 			off, len, mp->dmai_offset,
640Sstevel@tonic-gate 			mp->dmai_offset + mp->dmai_size);
650Sstevel@tonic-gate 		return;
660Sstevel@tonic-gate 	}
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	len = pg_off - dvma_addr;			/* sz = hi - lo */
690Sstevel@tonic-gate 	dvma_addr += mp->dmai_mapping;			/* start addr */
700Sstevel@tonic-gate 	pg_off = dvma_addr & IOMMU_PAGE_OFFSET;		/* offset in 1st pg */
710Sstevel@tonic-gate 	len = IOMMU_BTOPR(len + pg_off);		/* # of pages */
720Sstevel@tonic-gate 	dvma_addr ^= pg_off;
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 	DEBUG2(DBG_SC, dip, "addr=%x+%x pages: \n", dvma_addr, len);
750Sstevel@tonic-gate 	for (; len; len--, dvma_addr += IOMMU_PAGE_SIZE) {
760Sstevel@tonic-gate 		DEBUG1(DBG_SC|DBG_CONT, dip, " %x", dvma_addr);
770Sstevel@tonic-gate 		*invl_va = (uint64_t)dvma_addr;
780Sstevel@tonic-gate 	}
790Sstevel@tonic-gate 	DEBUG0(DBG_SC|DBG_CONT, dip, "\n");
800Sstevel@tonic-gate }
810Sstevel@tonic-gate 
820Sstevel@tonic-gate static void
830Sstevel@tonic-gate pci_dma_sync_flag_wait(ddi_dma_impl_t *mp, sc_t *sc_p, uint32_t onstack)
840Sstevel@tonic-gate {
850Sstevel@tonic-gate 	hrtime_t start_time;
860Sstevel@tonic-gate 	uint64_t loops = 0;
870Sstevel@tonic-gate 	uint64_t sync_flag_pa = SYNC_BUF_PA(mp);
880Sstevel@tonic-gate 	uint64_t sync_reg_pa = sc_p->sc_sync_reg_pa;
890Sstevel@tonic-gate 	uint8_t stack_buf[128];
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	stack_buf[0] = DDI_SUCCESS;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	/* check for handle specific sync flag */
940Sstevel@tonic-gate 	if (sync_flag_pa)
950Sstevel@tonic-gate 		goto start;
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	sync_flag_pa = sc_p->sc_sync_flag_pa;
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	if (onstack) {
1000Sstevel@tonic-gate 		sync_flag_pa = va_to_pa(stack_buf);
1010Sstevel@tonic-gate 		sync_flag_pa += PCI_SYNC_FLAG_SIZE;
1020Sstevel@tonic-gate 		sync_flag_pa >>= PCI_SYNC_FLAG_SZSHIFT;
1030Sstevel@tonic-gate 		sync_flag_pa <<= PCI_SYNC_FLAG_SZSHIFT;
1040Sstevel@tonic-gate 		goto start;
1050Sstevel@tonic-gate 	}
1060Sstevel@tonic-gate 	stack_buf[0] |= PCI_SYNC_FLAG_LOCKED;
1070Sstevel@tonic-gate 	mutex_enter(&sc_p->sc_sync_mutex);
1080Sstevel@tonic-gate start:
1090Sstevel@tonic-gate 	ASSERT(!(sync_flag_pa & PCI_SYNC_FLAG_SIZE - 1));
1100Sstevel@tonic-gate 	stdphys(sync_flag_pa, 0);	/* reset sync flag to 0 */
1110Sstevel@tonic-gate 					/* membar  #LoadStore|#StoreStore */
1120Sstevel@tonic-gate 	stdphysio(sync_reg_pa, sync_flag_pa);
1130Sstevel@tonic-gate 	start_time = gethrtime();
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	for (; gethrtime() - start_time < pci_sync_buf_timeout; loops++)
1160Sstevel@tonic-gate 		if (lddphys(sync_flag_pa))
1170Sstevel@tonic-gate 			goto done;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	if (!lddphys(sync_flag_pa))
1200Sstevel@tonic-gate 		stack_buf[0] |= PCI_SYNC_FLAG_FAILED;
1210Sstevel@tonic-gate done:
1220Sstevel@tonic-gate 	DEBUG3(DBG_SC|DBG_CONT, 0, "flag wait loops=%lu ticks=%lu status=%x\n",
1230Sstevel@tonic-gate 		loops, gethrtime() - start_time, stack_buf[0]);
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	if (stack_buf[0] & PCI_SYNC_FLAG_LOCKED)
1260Sstevel@tonic-gate 		mutex_exit(&sc_p->sc_sync_mutex);
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	if (stack_buf[0] & PCI_SYNC_FLAG_FAILED)
129946Smathue 		cmn_err(CE_PANIC, "%p pci dma sync %lx %lx timeout!",
1300Sstevel@tonic-gate 		    mp, sync_flag_pa, loops);
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate /*
1340Sstevel@tonic-gate  * Cache	RW	Before	During		After
1350Sstevel@tonic-gate  *
1360Sstevel@tonic-gate  * STREAMING	read	no/no	pg/no		ctx,pg/no
1370Sstevel@tonic-gate  * STREAMING	write	no/no	pg/yes		ctx,pg/yes
1380Sstevel@tonic-gate  * CONSISTENT	read	no/no	yes,no/no	yes,no/no
1390Sstevel@tonic-gate  * CONSISTENT	write	no/no	yes,yes/yes	yes,yes/yes
1400Sstevel@tonic-gate  *
1410Sstevel@tonic-gate  * STREAMING	read	ctx,pg/no
1420Sstevel@tonic-gate  * STREAMING	write	ctx,pg/yes
1430Sstevel@tonic-gate  * CONSISTENT	read	yes,no/no
1440Sstevel@tonic-gate  * CONSISTENT	write	yes,yes/yes
1450Sstevel@tonic-gate  */
1460Sstevel@tonic-gate int
1470Sstevel@tonic-gate pci_dma_sync(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle,
1480Sstevel@tonic-gate 	off_t off, size_t len, uint32_t sync_flag)
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
1510Sstevel@tonic-gate 	int ret = ddi_get_instance(dip);
1520Sstevel@tonic-gate 	pci_t *pci_p = get_pci_soft_state(ret);
1530Sstevel@tonic-gate 	pbm_t *pbm_p = pci_p->pci_pbm_p;
1540Sstevel@tonic-gate 	uint32_t dev_flag = mp->dmai_rflags;
1550Sstevel@tonic-gate 	sc_t *sc_p;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	DEBUG4(DBG_DMA_SYNC, dip, "%s%d flags=%x,%x\n", ddi_driver_name(rdip),
1580Sstevel@tonic-gate 		ddi_get_instance(rdip), dev_flag, sync_flag);
1590Sstevel@tonic-gate 	DEBUG4(DBG_SC, dip, "dmai_mapping=%x, dmai_sz=%x off=%x len=%x\n",
1600Sstevel@tonic-gate 		mp->dmai_mapping, mp->dmai_size, off, len);
1610Sstevel@tonic-gate 	DEBUG2(DBG_SC, dip, "mp=%p, ctx=%x\n", mp, MP2CTX(mp));
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	if (!(mp->dmai_flags & DMAI_FLAGS_INUSE)) {
1640Sstevel@tonic-gate 		cmn_err(CE_WARN, "Unbound dma handle %p from %s%d", mp,
1650Sstevel@tonic-gate 		    ddi_driver_name(rdip), ddi_get_instance(rdip));
1660Sstevel@tonic-gate 		return (DDI_FAILURE);
1670Sstevel@tonic-gate 	}
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	if (mp->dmai_flags & DMAI_FLAGS_NOSYNC)
1700Sstevel@tonic-gate 		return (DDI_SUCCESS);
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	if (!(dev_flag & DDI_DMA_CONSISTENT))
1730Sstevel@tonic-gate 		goto streaming;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	if (sync_flag & PCI_DMA_SYNC_EXT) {
1760Sstevel@tonic-gate 		if (sync_flag & (PCI_DMA_SYNC_BEFORE | PCI_DMA_SYNC_POST) ||
1770Sstevel@tonic-gate 		    !(sync_flag & PCI_DMA_SYNC_WRITE))
1780Sstevel@tonic-gate 			return (DDI_SUCCESS);
1790Sstevel@tonic-gate 	} else {
1800Sstevel@tonic-gate 		if (!(dev_flag & DDI_DMA_READ) ||
1810Sstevel@tonic-gate 		    ((sync_flag & PCI_DMA_SYNC_DDI_FLAGS) ==
1820Sstevel@tonic-gate 		    DDI_DMA_SYNC_FORDEV))
1830Sstevel@tonic-gate 			return (DDI_SUCCESS);
1840Sstevel@tonic-gate 	}
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	pci_pbm_dma_sync(pbm_p, pbm_p->pbm_sync_ino);
1870Sstevel@tonic-gate 	return (DDI_SUCCESS);
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate streaming:
1900Sstevel@tonic-gate 	ASSERT(pci_stream_buf_exists && (pci_stream_buf_enable & 1 << ret));
1910Sstevel@tonic-gate 	sc_p = pci_p->pci_sc_p;
1920Sstevel@tonic-gate 	ret = DDI_FAILURE;
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	if (sync_flag & PCI_DMA_SYNC_EXT)
1950Sstevel@tonic-gate 		goto ext;
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	if (mp->dmai_flags & DMAI_FLAGS_CONTEXT && pci_sc_use_contexts)
1980Sstevel@tonic-gate 		ret = pci_sc_ctx_inv(dip, sc_p, mp);
1990Sstevel@tonic-gate 	if (ret)
2000Sstevel@tonic-gate 		pci_sc_pg_inv(dip, sc_p, mp, off, len);
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	if ((dev_flag & DDI_DMA_READ) &&
2030Sstevel@tonic-gate 	    ((sync_flag & PCI_DMA_SYNC_DDI_FLAGS) != DDI_DMA_SYNC_FORDEV))
2040Sstevel@tonic-gate 		goto wait;
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	return (DDI_SUCCESS);
2070Sstevel@tonic-gate ext:
2080Sstevel@tonic-gate 	if (sync_flag & PCI_DMA_SYNC_BEFORE)
2090Sstevel@tonic-gate 		return (DDI_SUCCESS);
2100Sstevel@tonic-gate 	if (sync_flag & PCI_DMA_SYNC_BAR)
2110Sstevel@tonic-gate 		goto wait_check;
2120Sstevel@tonic-gate 	if (sync_flag & PCI_DMA_SYNC_AFTER &&
2130Sstevel@tonic-gate 		mp->dmai_flags & DMAI_FLAGS_CONTEXT && pci_sc_use_contexts)
2140Sstevel@tonic-gate 		ret = pci_sc_ctx_inv(dip, sc_p, mp);
2150Sstevel@tonic-gate 	if (ret)
2160Sstevel@tonic-gate 		pci_sc_pg_inv(dip, sc_p, mp, off, len);
2170Sstevel@tonic-gate wait_check:
2180Sstevel@tonic-gate 	if (sync_flag & PCI_DMA_SYNC_POST || !(sync_flag & PCI_DMA_SYNC_WRITE))
2190Sstevel@tonic-gate 		return (DDI_SUCCESS);
2200Sstevel@tonic-gate wait:
2210Sstevel@tonic-gate 	pci_dma_sync_flag_wait(mp, sc_p, sync_flag & PCI_DMA_SYNC_PRIVATE);
2220Sstevel@tonic-gate 	return (DDI_SUCCESS);
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate int
2260Sstevel@tonic-gate pci_dma_handle_clean(dev_info_t *rdip, ddi_dma_handle_t h)
2270Sstevel@tonic-gate {
2280Sstevel@tonic-gate 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)h;
2290Sstevel@tonic-gate 	if ((mp->dmai_flags & DMAI_FLAGS_INUSE) == 0)
2300Sstevel@tonic-gate 		return (DDI_FAILURE);
2310Sstevel@tonic-gate 	mp->dmai_rflags |= DMP_NOSYNC;
2320Sstevel@tonic-gate 	mp->dmai_flags |= DMAI_FLAGS_NOSYNC;
2330Sstevel@tonic-gate 	return (DDI_SUCCESS);
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate /*
2370Sstevel@tonic-gate  * pci_dma_allocmp - Allocate a pci dma implementation structure
2380Sstevel@tonic-gate  *
2390Sstevel@tonic-gate  * An extra ddi_dma_attr structure is bundled with the usual ddi_dma_impl
2400Sstevel@tonic-gate  * to hold unmodified device limits. The ddi_dma_attr inside the
2410Sstevel@tonic-gate  * ddi_dma_impl structure is augumented with system limits to enhance
2420Sstevel@tonic-gate  * DVMA performance at runtime. The unaugumented device limits saved
2430Sstevel@tonic-gate  * right after (accessed through the DEV_ATTR macro) is used
2440Sstevel@tonic-gate  * strictly for peer-to-peer transfers which do not obey system limits.
2450Sstevel@tonic-gate  *
2460Sstevel@tonic-gate  * return: DDI_SUCCESS DDI_DMA_NORESOURCES
2470Sstevel@tonic-gate  */
2480Sstevel@tonic-gate ddi_dma_impl_t *
2490Sstevel@tonic-gate pci_dma_allocmp(dev_info_t *dip, dev_info_t *rdip, int (*waitfp)(caddr_t),
2500Sstevel@tonic-gate 	caddr_t arg)
2510Sstevel@tonic-gate {
2520Sstevel@tonic-gate 	ddi_dma_impl_t *mp;
2530Sstevel@tonic-gate 	int sleep = (waitfp == DDI_DMA_SLEEP) ? KM_SLEEP : KM_NOSLEEP;
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	/* Caution: we don't use zalloc to enhance performance! */
2560Sstevel@tonic-gate 	if ((mp = kmem_alloc(sizeof (pci_dma_hdl_t), sleep)) == 0) {
2570Sstevel@tonic-gate 		DEBUG0(DBG_DMA_MAP, dip, "can't alloc dma_handle\n");
2580Sstevel@tonic-gate 		if (waitfp != DDI_DMA_DONTWAIT) {
2590Sstevel@tonic-gate 			DEBUG0(DBG_DMA_MAP, dip, "alloc_mp kmem cb\n");
2600Sstevel@tonic-gate 			ddi_set_callback(waitfp, arg, &pci_kmem_clid);
2610Sstevel@tonic-gate 		}
2620Sstevel@tonic-gate 		return (mp);
2630Sstevel@tonic-gate 	}
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	mp->dmai_rdip = rdip;
2660Sstevel@tonic-gate 	mp->dmai_flags = 0;
2670Sstevel@tonic-gate 	mp->dmai_pfnlst = NULL;
2680Sstevel@tonic-gate 	mp->dmai_winlst = NULL;
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	/*
2710Sstevel@tonic-gate 	 * kmem_alloc debug: the following fields are not zero-ed
2720Sstevel@tonic-gate 	 * mp->dmai_mapping = 0;
2730Sstevel@tonic-gate 	 * mp->dmai_size = 0;
2740Sstevel@tonic-gate 	 * mp->dmai_offset = 0;
2750Sstevel@tonic-gate 	 * mp->dmai_minxfer = 0;
2760Sstevel@tonic-gate 	 * mp->dmai_burstsizes = 0;
2770Sstevel@tonic-gate 	 * mp->dmai_ndvmapages = 0;
2780Sstevel@tonic-gate 	 * mp->dmai_pool/roffset = 0;
2790Sstevel@tonic-gate 	 * mp->dmai_rflags = 0;
2800Sstevel@tonic-gate 	 * mp->dmai_inuse/flags
2810Sstevel@tonic-gate 	 * mp->dmai_nwin = 0;
2820Sstevel@tonic-gate 	 * mp->dmai_winsize = 0;
2830Sstevel@tonic-gate 	 * mp->dmai_nexus_private/tte = 0;
2840Sstevel@tonic-gate 	 * mp->dmai_iopte/pfnlst
2850Sstevel@tonic-gate 	 * mp->dmai_sbi/pfn0 = 0;
2860Sstevel@tonic-gate 	 * mp->dmai_minfo/winlst/fdvma
2870Sstevel@tonic-gate 	 * mp->dmai_rdip
2880Sstevel@tonic-gate 	 * bzero(&mp->dmai_object, sizeof (ddi_dma_obj_t));
2890Sstevel@tonic-gate 	 * mp->dmai_cookie = 0;
2900Sstevel@tonic-gate 	 */
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	mp->dmai_attr.dma_attr_version = (uint_t)DMA_ATTR_VERSION;
2930Sstevel@tonic-gate 	mp->dmai_attr.dma_attr_flags = (uint_t)0;
2940Sstevel@tonic-gate 	mp->dmai_fault = 0;
2950Sstevel@tonic-gate 	mp->dmai_fault_check = NULL;
2960Sstevel@tonic-gate 	mp->dmai_fault_notify = NULL;
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	mp->dmai_error.err_ena = 0;
2990Sstevel@tonic-gate 	mp->dmai_error.err_status = DDI_FM_OK;
3000Sstevel@tonic-gate 	mp->dmai_error.err_expected = DDI_FM_ERR_UNEXPECTED;
3010Sstevel@tonic-gate 	mp->dmai_error.err_ontrap = NULL;
3020Sstevel@tonic-gate 	mp->dmai_error.err_fep = NULL;
3031865Sdilpreet 	mp->dmai_error.err_cf = NULL;
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 	SYNC_BUF_PA(mp) = 0ull;
3060Sstevel@tonic-gate 	return (mp);
3070Sstevel@tonic-gate }
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate void
3100Sstevel@tonic-gate pci_dma_freemp(ddi_dma_impl_t *mp)
3110Sstevel@tonic-gate {
3120Sstevel@tonic-gate 	if (mp->dmai_ndvmapages > 1)
3130Sstevel@tonic-gate 		pci_dma_freepfn(mp);
3140Sstevel@tonic-gate 	if (mp->dmai_winlst)
3150Sstevel@tonic-gate 		pci_dma_freewin(mp);
3160Sstevel@tonic-gate 	kmem_free(mp, sizeof (pci_dma_hdl_t));
3170Sstevel@tonic-gate }
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate void
3200Sstevel@tonic-gate pci_dma_freepfn(ddi_dma_impl_t *mp)
3210Sstevel@tonic-gate {
3220Sstevel@tonic-gate 	void *addr = mp->dmai_pfnlst;
3230Sstevel@tonic-gate 	ASSERT(!PCI_DMA_CANRELOC(mp));
3240Sstevel@tonic-gate 	if (addr) {
3250Sstevel@tonic-gate 		size_t npages = mp->dmai_ndvmapages;
3260Sstevel@tonic-gate 		if (npages > 1)
3270Sstevel@tonic-gate 			kmem_free(addr, npages * sizeof (iopfn_t));
3280Sstevel@tonic-gate 		mp->dmai_pfnlst = NULL;
3290Sstevel@tonic-gate 	}
3300Sstevel@tonic-gate 	mp->dmai_ndvmapages = 0;
3310Sstevel@tonic-gate }
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate /*
3340Sstevel@tonic-gate  * pci_dma_lmts2hdl - alloate a ddi_dma_impl_t, validate practical limits
3350Sstevel@tonic-gate  *			and convert dmareq->dmar_limits to mp->dmai_attr
3360Sstevel@tonic-gate  *
3370Sstevel@tonic-gate  * ddi_dma_impl_t member modified     input
3380Sstevel@tonic-gate  * ------------------------------------------------------------------------
3390Sstevel@tonic-gate  * mp->dmai_minxfer		    - dev
3400Sstevel@tonic-gate  * mp->dmai_burstsizes		    - dev
3410Sstevel@tonic-gate  * mp->dmai_flags		    - no limit? peer-to-peer only?
3420Sstevel@tonic-gate  *
3430Sstevel@tonic-gate  * ddi_dma_attr member modified       input
3440Sstevel@tonic-gate  * ------------------------------------------------------------------------
3450Sstevel@tonic-gate  * mp->dmai_attr.dma_attr_addr_lo   - dev lo, sys lo
3460Sstevel@tonic-gate  * mp->dmai_attr.dma_attr_addr_hi   - dev hi, sys hi
3470Sstevel@tonic-gate  * mp->dmai_attr.dma_attr_count_max - dev count max, dev/sys lo/hi delta
3480Sstevel@tonic-gate  * mp->dmai_attr.dma_attr_seg       - 0         (no nocross   restriction)
3490Sstevel@tonic-gate  * mp->dmai_attr.dma_attr_align     - 1		(no alignment restriction)
3500Sstevel@tonic-gate  *
3510Sstevel@tonic-gate  * The dlim_dmaspeed member of dmareq->dmar_limits is ignored.
3520Sstevel@tonic-gate  */
3530Sstevel@tonic-gate ddi_dma_impl_t *
3540Sstevel@tonic-gate pci_dma_lmts2hdl(dev_info_t *dip, dev_info_t *rdip, iommu_t *iommu_p,
3550Sstevel@tonic-gate 	ddi_dma_req_t *dmareq)
3560Sstevel@tonic-gate {
3570Sstevel@tonic-gate 	ddi_dma_impl_t *mp;
3580Sstevel@tonic-gate 	ddi_dma_attr_t *attr_p;
3590Sstevel@tonic-gate 	uint64_t syslo		= iommu_p->iommu_dvma_base;
3600Sstevel@tonic-gate 	uint64_t syshi		= iommu_p->iommu_dvma_end;
3610Sstevel@tonic-gate 	uint64_t fasthi		= iommu_p->iommu_dvma_fast_end;
3620Sstevel@tonic-gate 	ddi_dma_lim_t *lim_p	= dmareq->dmar_limits;
3630Sstevel@tonic-gate 	uint32_t count_max	= lim_p->dlim_cntr_max;
3640Sstevel@tonic-gate 	uint64_t lo		= lim_p->dlim_addr_lo;
3650Sstevel@tonic-gate 	uint64_t hi		= lim_p->dlim_addr_hi;
3660Sstevel@tonic-gate 	if (hi <= lo) {
3670Sstevel@tonic-gate 		DEBUG0(DBG_DMA_MAP, dip, "Bad limits\n");
3680Sstevel@tonic-gate 		return ((ddi_dma_impl_t *)DDI_DMA_NOMAPPING);
3690Sstevel@tonic-gate 	}
3700Sstevel@tonic-gate 	if (!count_max)
3710Sstevel@tonic-gate 		count_max--;
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	if (!(mp = pci_dma_allocmp(dip, rdip, dmareq->dmar_fp,
3740Sstevel@tonic-gate 		dmareq->dmar_arg)))
3750Sstevel@tonic-gate 		return (NULL);
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	/* store original dev input at the 2nd ddi_dma_attr */
3780Sstevel@tonic-gate 	attr_p = DEV_ATTR(mp);
3790Sstevel@tonic-gate 	SET_DMAATTR(attr_p, lo, hi, -1, count_max);
3800Sstevel@tonic-gate 	SET_DMAALIGN(attr_p, 1);
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	lo = MAX(lo, syslo);
3830Sstevel@tonic-gate 	hi = MIN(hi, syshi);
3840Sstevel@tonic-gate 	if (hi <= lo)
3850Sstevel@tonic-gate 		mp->dmai_flags |= DMAI_FLAGS_PEER_ONLY;
3860Sstevel@tonic-gate 	count_max = MIN(count_max, hi - lo);
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	if (DEV_NOSYSLIMIT(lo, hi, syslo, fasthi, 1))
3890Sstevel@tonic-gate 		mp->dmai_flags |= DMAI_FLAGS_NOFASTLIMIT |
3900Sstevel@tonic-gate 			DMAI_FLAGS_NOSYSLIMIT;
3910Sstevel@tonic-gate 	else {
3920Sstevel@tonic-gate 		if (DEV_NOFASTLIMIT(lo, hi, syslo, syshi, 1))
3930Sstevel@tonic-gate 			mp->dmai_flags |= DMAI_FLAGS_NOFASTLIMIT;
3940Sstevel@tonic-gate 	}
3950Sstevel@tonic-gate 	if (PCI_DMA_NOCTX(rdip))
3960Sstevel@tonic-gate 		mp->dmai_flags |= DMAI_FLAGS_NOCTX;
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	/* store augumented dev input to mp->dmai_attr */
3990Sstevel@tonic-gate 	mp->dmai_minxfer	= lim_p->dlim_minxfer;
4000Sstevel@tonic-gate 	mp->dmai_burstsizes	= lim_p->dlim_burstsizes;
4010Sstevel@tonic-gate 	attr_p = &mp->dmai_attr;
4020Sstevel@tonic-gate 	SET_DMAATTR(attr_p, lo, hi, -1, count_max);
4030Sstevel@tonic-gate 	SET_DMAALIGN(attr_p, 1);
4040Sstevel@tonic-gate 	return (mp);
4050Sstevel@tonic-gate }
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate /*
4080Sstevel@tonic-gate  * pci_dma_attr2hdl
4090Sstevel@tonic-gate  *
4100Sstevel@tonic-gate  * This routine is called from the alloc handle entry point to sanity check the
4110Sstevel@tonic-gate  * dma attribute structure.
4120Sstevel@tonic-gate  *
4130Sstevel@tonic-gate  * use by: pci_dma_allochdl()
4140Sstevel@tonic-gate  *
4150Sstevel@tonic-gate  * return value:
4160Sstevel@tonic-gate  *
4170Sstevel@tonic-gate  *	DDI_SUCCESS		- on success
4180Sstevel@tonic-gate  *	DDI_DMA_BADATTR		- attribute has invalid version number
4190Sstevel@tonic-gate  *				  or address limits exclude dvma space
4200Sstevel@tonic-gate  */
4210Sstevel@tonic-gate int
4220Sstevel@tonic-gate pci_dma_attr2hdl(pci_t *pci_p, ddi_dma_impl_t *mp)
4230Sstevel@tonic-gate {
4240Sstevel@tonic-gate 	iommu_t *iommu_p = pci_p->pci_iommu_p;
4250Sstevel@tonic-gate 	uint64_t syslo, syshi;
4260Sstevel@tonic-gate 	ddi_dma_attr_t *attrp		= DEV_ATTR(mp);
4270Sstevel@tonic-gate 	uint64_t hi		= attrp->dma_attr_addr_hi;
4280Sstevel@tonic-gate 	uint64_t lo		= attrp->dma_attr_addr_lo;
4290Sstevel@tonic-gate 	uint64_t align		= attrp->dma_attr_align;
4300Sstevel@tonic-gate 	uint64_t nocross	= attrp->dma_attr_seg;
4310Sstevel@tonic-gate 	uint64_t count_max	= attrp->dma_attr_count_max;
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	DEBUG3(DBG_DMA_ALLOCH, pci_p->pci_dip, "attrp=%p cntr_max=%x.%08x\n",
4340Sstevel@tonic-gate 		attrp, HI32(count_max), LO32(count_max));
4350Sstevel@tonic-gate 	DEBUG4(DBG_DMA_ALLOCH, pci_p->pci_dip, "hi=%x.%08x lo=%x.%08x\n",
4360Sstevel@tonic-gate 		HI32(hi), LO32(hi), HI32(lo), LO32(lo));
4370Sstevel@tonic-gate 	DEBUG4(DBG_DMA_ALLOCH, pci_p->pci_dip, "seg=%x.%08x align=%x.%08x\n",
4380Sstevel@tonic-gate 		HI32(nocross), LO32(nocross), HI32(align), LO32(align));
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 	if (!nocross)
4410Sstevel@tonic-gate 		nocross--;
4420Sstevel@tonic-gate 	if (attrp->dma_attr_flags & DDI_DMA_FORCE_PHYSICAL) { /* BYPASS */
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 		DEBUG0(DBG_DMA_ALLOCH, pci_p->pci_dip, "bypass mode\n");
4450Sstevel@tonic-gate 		/* if tomatillo ver <= 2.3 don't allow bypass */
4460Sstevel@tonic-gate 		if (tomatillo_disallow_bypass)
4470Sstevel@tonic-gate 			return (DDI_DMA_BADATTR);
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 		mp->dmai_flags |= DMAI_FLAGS_BYPASSREQ;
4500Sstevel@tonic-gate 		if (nocross != UINT64_MAX)
4510Sstevel@tonic-gate 			return (DDI_DMA_BADATTR);
4520Sstevel@tonic-gate 		if (align && (align > IOMMU_PAGE_SIZE))
4530Sstevel@tonic-gate 			return (DDI_DMA_BADATTR);
4540Sstevel@tonic-gate 		align = 1; /* align on 1 page boundary */
4550Sstevel@tonic-gate 		syslo = iommu_p->iommu_dma_bypass_base;
4560Sstevel@tonic-gate 		syshi = iommu_p->iommu_dma_bypass_end;
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	} else { /* IOMMU_XLATE or PEER_TO_PEER */
4590Sstevel@tonic-gate 		align = MAX(align, IOMMU_PAGE_SIZE) - 1;
4600Sstevel@tonic-gate 		if ((align & nocross) != align) {
4610Sstevel@tonic-gate 			dev_info_t *rdip = mp->dmai_rdip;
4620Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d dma_attr_seg not aligned",
4630Sstevel@tonic-gate 				NAMEINST(rdip));
4640Sstevel@tonic-gate 			return (DDI_DMA_BADATTR);
4650Sstevel@tonic-gate 		}
4660Sstevel@tonic-gate 		align = IOMMU_BTOP(align + 1);
4670Sstevel@tonic-gate 		syslo = iommu_p->iommu_dvma_base;
4680Sstevel@tonic-gate 		syshi = iommu_p->iommu_dvma_end;
4690Sstevel@tonic-gate 	}
4700Sstevel@tonic-gate 	if (hi <= lo) {
4710Sstevel@tonic-gate 		dev_info_t *rdip = mp->dmai_rdip;
4720Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d limits out of range", NAMEINST(rdip));
4730Sstevel@tonic-gate 		return (DDI_DMA_BADATTR);
4740Sstevel@tonic-gate 	}
4750Sstevel@tonic-gate 	lo = MAX(lo, syslo);
4760Sstevel@tonic-gate 	hi = MIN(hi, syshi);
4770Sstevel@tonic-gate 	if (!count_max)
4780Sstevel@tonic-gate 		count_max--;
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	DEBUG4(DBG_DMA_ALLOCH, pci_p->pci_dip, "hi=%x.%08x, lo=%x.%08x\n",
4810Sstevel@tonic-gate 		HI32(hi), LO32(hi), HI32(lo), LO32(lo));
4820Sstevel@tonic-gate 	if (hi <= lo) { /* peer transfers cannot have alignment & nocross */
4830Sstevel@tonic-gate 		dev_info_t *rdip = mp->dmai_rdip;
4840Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d peer only dev %p", NAMEINST(rdip), mp);
4850Sstevel@tonic-gate 		if ((nocross < UINT32_MAX) || (align > 1)) {
4860Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d peer only device bad attr",
4870Sstevel@tonic-gate 				NAMEINST(rdip));
4880Sstevel@tonic-gate 			return (DDI_DMA_BADATTR);
4890Sstevel@tonic-gate 		}
4900Sstevel@tonic-gate 		mp->dmai_flags |= DMAI_FLAGS_PEER_ONLY;
4910Sstevel@tonic-gate 	} else /* set practical counter_max value */
4920Sstevel@tonic-gate 		count_max = MIN(count_max, hi - lo);
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	if (DEV_NOSYSLIMIT(lo, hi, syslo, syshi, align))
4950Sstevel@tonic-gate 		mp->dmai_flags |= DMAI_FLAGS_NOSYSLIMIT |
4960Sstevel@tonic-gate 			DMAI_FLAGS_NOFASTLIMIT;
4970Sstevel@tonic-gate 	else {
4980Sstevel@tonic-gate 		syshi = iommu_p->iommu_dvma_fast_end;
4990Sstevel@tonic-gate 		if (DEV_NOFASTLIMIT(lo, hi, syslo, syshi, align))
5000Sstevel@tonic-gate 			mp->dmai_flags |= DMAI_FLAGS_NOFASTLIMIT;
5010Sstevel@tonic-gate 	}
5020Sstevel@tonic-gate 	if (PCI_DMA_NOCTX(mp->dmai_rdip))
5030Sstevel@tonic-gate 		mp->dmai_flags |= DMAI_FLAGS_NOCTX;
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 	mp->dmai_minxfer	= attrp->dma_attr_minxfer;
5060Sstevel@tonic-gate 	mp->dmai_burstsizes	= attrp->dma_attr_burstsizes;
5070Sstevel@tonic-gate 	attrp = &mp->dmai_attr;
5080Sstevel@tonic-gate 	SET_DMAATTR(attrp, lo, hi, nocross, count_max);
5090Sstevel@tonic-gate 	return (DDI_SUCCESS);
5100Sstevel@tonic-gate }
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate /*
5130Sstevel@tonic-gate  * set up consistent dma flags according to hardware capability
5140Sstevel@tonic-gate  */
5150Sstevel@tonic-gate uint32_t
5160Sstevel@tonic-gate pci_dma_consist_check(uint32_t req_flags, pbm_t *pbm_p)
5170Sstevel@tonic-gate {
5180Sstevel@tonic-gate 	if (!pci_stream_buf_enable || !pci_stream_buf_exists)
5190Sstevel@tonic-gate 		req_flags |= DDI_DMA_CONSISTENT;
5200Sstevel@tonic-gate 	if (req_flags & DDI_DMA_CONSISTENT && !pbm_p->pbm_sync_reg_pa)
5210Sstevel@tonic-gate 		req_flags |= DMP_NOSYNC;
5220Sstevel@tonic-gate 	return (req_flags);
5230Sstevel@tonic-gate }
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate #define	TGT_PFN_INBETWEEN(pfn, bgn, end) ((pfn >= bgn) && (pfn <= end))
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate /*
5280Sstevel@tonic-gate  * pci_dma_type - determine which of the three types DMA (peer-to-peer,
5290Sstevel@tonic-gate  *		iommu bypass, or iommu translate) we are asked to do.
5300Sstevel@tonic-gate  *		Also checks pfn0 and rejects any non-peer-to-peer
5310Sstevel@tonic-gate  *		requests for peer-only devices.
5320Sstevel@tonic-gate  *
5330Sstevel@tonic-gate  *	return values:
5340Sstevel@tonic-gate  *		DDI_DMA_NOMAPPING - can't get valid pfn0, or bad dma type
5350Sstevel@tonic-gate  *		DDI_SUCCESS
5360Sstevel@tonic-gate  *
5370Sstevel@tonic-gate  *	dma handle members affected (set on exit):
5380Sstevel@tonic-gate  *	mp->dmai_object		- dmareq->dmar_object
5390Sstevel@tonic-gate  *	mp->dmai_rflags		- consistent?, nosync?, dmareq->dmar_flags
5400Sstevel@tonic-gate  *	mp->dmai_flags   	- DMA type
5410Sstevel@tonic-gate  *	mp->dmai_pfn0   	- 1st page pfn (if va/size pair and not shadow)
5420Sstevel@tonic-gate  *	mp->dmai_roffset 	- initialized to starting IOMMU page offset
5430Sstevel@tonic-gate  *	mp->dmai_ndvmapages	- # of total IOMMU pages of entire object
5440Sstevel@tonic-gate  *	mp->pdh_sync_buf_pa	- dma sync buffer PA is DMA flow is supported
5450Sstevel@tonic-gate  */
5460Sstevel@tonic-gate int
5470Sstevel@tonic-gate pci_dma_type(pci_t *pci_p, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp)
5480Sstevel@tonic-gate {
5490Sstevel@tonic-gate 	dev_info_t *dip = pci_p->pci_dip;
5500Sstevel@tonic-gate 	ddi_dma_obj_t *dobj_p = &dmareq->dmar_object;
5510Sstevel@tonic-gate 	pbm_t *pbm_p = pci_p->pci_pbm_p;
5520Sstevel@tonic-gate 	page_t **pplist;
5530Sstevel@tonic-gate 	struct as *as_p;
5540Sstevel@tonic-gate 	uint32_t offset;
5550Sstevel@tonic-gate 	caddr_t vaddr;
5560Sstevel@tonic-gate 	pfn_t pfn0;
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	mp->dmai_rflags = pci_dma_consist_check(dmareq->dmar_flags, pbm_p);
5590Sstevel@tonic-gate 	mp->dmai_flags |= mp->dmai_rflags & DMP_NOSYNC ? DMAI_FLAGS_NOSYNC : 0;
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 	switch (dobj_p->dmao_type) {
5620Sstevel@tonic-gate 	case DMA_OTYP_BUFVADDR:
5630Sstevel@tonic-gate 	case DMA_OTYP_VADDR: {
5640Sstevel@tonic-gate 		vaddr = dobj_p->dmao_obj.virt_obj.v_addr;
5650Sstevel@tonic-gate 		pplist = dobj_p->dmao_obj.virt_obj.v_priv;
5660Sstevel@tonic-gate 		as_p = dobj_p->dmao_obj.virt_obj.v_as;
5670Sstevel@tonic-gate 		if (as_p == NULL)
5680Sstevel@tonic-gate 			as_p = &kas;
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 		DEBUG2(DBG_DMA_MAP, dip, "vaddr=%p pplist=%p\n", vaddr, pplist);
5710Sstevel@tonic-gate 		offset = (ulong_t)vaddr & IOMMU_PAGE_OFFSET;
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 		if (pplist) {				/* shadow list */
5740Sstevel@tonic-gate 			mp->dmai_flags |= DMAI_FLAGS_PGPFN;
5750Sstevel@tonic-gate 			ASSERT(PAGE_LOCKED(*pplist));
5760Sstevel@tonic-gate 			pfn0 = page_pptonum(*pplist);
5770Sstevel@tonic-gate 		} else if (pci_dvma_remap_enabled && as_p == &kas &&
5780Sstevel@tonic-gate 			dobj_p->dmao_type != DMA_OTYP_BUFVADDR) {
5790Sstevel@tonic-gate 			int (*waitfp)(caddr_t) = dmareq->dmar_fp;
5800Sstevel@tonic-gate 			uint_t flags = ((waitfp == DDI_DMA_SLEEP)?
5810Sstevel@tonic-gate 				    HAC_SLEEP : HAC_NOSLEEP) | HAC_PAGELOCK;
5820Sstevel@tonic-gate 			int ret;
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 			ret = hat_add_callback(pci_dvma_cbid, vaddr,
585*2251Selowe 			    IOMMU_PAGE_SIZE - offset, flags, mp, &pfn0,
586*2251Selowe 			    MP_HAT_CB_COOKIE_PTR(mp, 0));
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 			if (pfn0 == PFN_INVALID && ret == ENOMEM) {
5890Sstevel@tonic-gate 				ASSERT(waitfp != DDI_DMA_SLEEP);
5900Sstevel@tonic-gate 				if (waitfp != DDI_DMA_DONTWAIT) {
5910Sstevel@tonic-gate 					ddi_set_callback(waitfp,
5920Sstevel@tonic-gate 					    dmareq->dmar_arg,
5930Sstevel@tonic-gate 					    &pci_kmem_clid);
5940Sstevel@tonic-gate 					return (DDI_DMA_NORESOURCES);
5950Sstevel@tonic-gate 					}
5960Sstevel@tonic-gate 			}
5970Sstevel@tonic-gate 			mp->dmai_flags |= DMAI_FLAGS_RELOC;
5980Sstevel@tonic-gate 		} else
5990Sstevel@tonic-gate 			pfn0 = hat_getpfnum(as_p->a_hat, vaddr);
6000Sstevel@tonic-gate 		}
6010Sstevel@tonic-gate 		break;
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	case DMA_OTYP_PAGES:
6040Sstevel@tonic-gate 		offset = dobj_p->dmao_obj.pp_obj.pp_offset;
6050Sstevel@tonic-gate 		mp->dmai_flags |= DMAI_FLAGS_PGPFN;
6060Sstevel@tonic-gate 		pfn0 = page_pptonum(dobj_p->dmao_obj.pp_obj.pp_pp);
6070Sstevel@tonic-gate 		ASSERT(PAGE_LOCKED(dobj_p->dmao_obj.pp_obj.pp_pp));
6080Sstevel@tonic-gate 		break;
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	case DMA_OTYP_PADDR:
6110Sstevel@tonic-gate 	default:
6120Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d requested unsupported dma type %x",
6130Sstevel@tonic-gate 			NAMEINST(mp->dmai_rdip), dobj_p->dmao_type);
6140Sstevel@tonic-gate 		return (DDI_DMA_NOMAPPING);
6150Sstevel@tonic-gate 	}
6160Sstevel@tonic-gate 	if (pfn0 == PFN_INVALID) {
6170Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: invalid pfn0 for DMA object %p",
6180Sstevel@tonic-gate 			NAMEINST(dip), dobj_p);
6190Sstevel@tonic-gate 		return (DDI_DMA_NOMAPPING);
6200Sstevel@tonic-gate 	}
6210Sstevel@tonic-gate 	if (TGT_PFN_INBETWEEN(pfn0, pbm_p->pbm_base_pfn, pbm_p->pbm_last_pfn)) {
6220Sstevel@tonic-gate 		mp->dmai_flags |= DMAI_FLAGS_PEER_TO_PEER;
6230Sstevel@tonic-gate 		goto done;	/* leave bypass and dvma flag as 0 */
6240Sstevel@tonic-gate 	}
6250Sstevel@tonic-gate 	if (PCI_DMA_ISPEERONLY(mp)) {
6260Sstevel@tonic-gate 		dev_info_t *rdip = mp->dmai_rdip;
6270Sstevel@tonic-gate 		cmn_err(CE_WARN, "Bad peer-to-peer req %s%d", NAMEINST(rdip));
6280Sstevel@tonic-gate 		return (DDI_DMA_NOMAPPING);
6290Sstevel@tonic-gate 	}
6300Sstevel@tonic-gate 	mp->dmai_flags |= (mp->dmai_flags & DMAI_FLAGS_BYPASSREQ) ?
6310Sstevel@tonic-gate 		DMAI_FLAGS_BYPASS : DMAI_FLAGS_DVMA;
6320Sstevel@tonic-gate done:
6330Sstevel@tonic-gate 	mp->dmai_object	 = *dobj_p;			/* whole object    */
6340Sstevel@tonic-gate 	mp->dmai_pfn0	 = (void *)pfn0;		/* cache pfn0	   */
6350Sstevel@tonic-gate 	mp->dmai_roffset = offset;			/* win0 pg0 offset */
6360Sstevel@tonic-gate 	mp->dmai_ndvmapages = IOMMU_BTOPR(offset + mp->dmai_object.dmao_size);
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	return (DDI_SUCCESS);
6390Sstevel@tonic-gate }
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate /*
6420Sstevel@tonic-gate  * pci_dma_pgpfn - set up pfnlst array according to pages
6430Sstevel@tonic-gate  *	VA/size pair: <shadow IO, bypass, peer-to-peer>, or OTYP_PAGES
6440Sstevel@tonic-gate  */
6450Sstevel@tonic-gate /*ARGSUSED*/
6460Sstevel@tonic-gate static int
6470Sstevel@tonic-gate pci_dma_pgpfn(pci_t *pci_p, ddi_dma_impl_t *mp, uint_t npages)
6480Sstevel@tonic-gate {
6490Sstevel@tonic-gate 	int i;
6500Sstevel@tonic-gate #ifdef DEBUG
6510Sstevel@tonic-gate 	dev_info_t *dip = pci_p->pci_dip;
6520Sstevel@tonic-gate #endif
6530Sstevel@tonic-gate 	switch (mp->dmai_object.dmao_type) {
6540Sstevel@tonic-gate 	case DMA_OTYP_BUFVADDR:
6550Sstevel@tonic-gate 	case DMA_OTYP_VADDR: {
6560Sstevel@tonic-gate 		page_t **pplist = mp->dmai_object.dmao_obj.virt_obj.v_priv;
6570Sstevel@tonic-gate 		DEBUG2(DBG_DMA_MAP, dip, "shadow pplist=%p, %x pages, pfns=",
6580Sstevel@tonic-gate 			pplist, npages);
6590Sstevel@tonic-gate 		for (i = 1; i < npages; i++) {
6600Sstevel@tonic-gate 			iopfn_t pfn = page_pptonum(pplist[i]);
6610Sstevel@tonic-gate 			ASSERT(PAGE_LOCKED(pplist[i]));
6620Sstevel@tonic-gate 			PCI_SET_MP_PFN1(mp, i, pfn);
6630Sstevel@tonic-gate 			DEBUG1(DBG_DMA_MAP|DBG_CONT, dip, "%x ", pfn);
6640Sstevel@tonic-gate 		}
6650Sstevel@tonic-gate 		DEBUG0(DBG_DMA_MAP|DBG_CONT, dip, "\n");
6660Sstevel@tonic-gate 		}
6670Sstevel@tonic-gate 		break;
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	case DMA_OTYP_PAGES: {
6700Sstevel@tonic-gate 		page_t *pp = mp->dmai_object.dmao_obj.pp_obj.pp_pp->p_next;
6710Sstevel@tonic-gate 		DEBUG1(DBG_DMA_MAP, dip, "pp=%p pfns=", pp);
6720Sstevel@tonic-gate 		for (i = 1; i < npages; i++, pp = pp->p_next) {
6730Sstevel@tonic-gate 			iopfn_t pfn = page_pptonum(pp);
6740Sstevel@tonic-gate 			ASSERT(PAGE_LOCKED(pp));
6750Sstevel@tonic-gate 			PCI_SET_MP_PFN1(mp, i, pfn);
6760Sstevel@tonic-gate 			DEBUG1(DBG_DMA_MAP|DBG_CONT, dip, "%x ", pfn);
6770Sstevel@tonic-gate 		}
6780Sstevel@tonic-gate 		DEBUG0(DBG_DMA_MAP|DBG_CONT, dip, "\n");
6790Sstevel@tonic-gate 		}
6800Sstevel@tonic-gate 		break;
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 	default:	/* check is already done by pci_dma_type */
6830Sstevel@tonic-gate 		ASSERT(0);
6840Sstevel@tonic-gate 		break;
6850Sstevel@tonic-gate 	}
6860Sstevel@tonic-gate 	return (DDI_SUCCESS);
6870Sstevel@tonic-gate }
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate /*
6900Sstevel@tonic-gate  * pci_dma_vapfn - set up pfnlst array according to VA
6910Sstevel@tonic-gate  *	VA/size pair: <normal, bypass, peer-to-peer>
6920Sstevel@tonic-gate  *	pfn0 is skipped as it is already done.
6930Sstevel@tonic-gate  *	In this case, the cached pfn0 is used to fill pfnlst[0]
6940Sstevel@tonic-gate  */
6950Sstevel@tonic-gate static int
6960Sstevel@tonic-gate pci_dma_vapfn(pci_t *pci_p, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp,
6970Sstevel@tonic-gate 	uint_t npages)
6980Sstevel@tonic-gate {
6990Sstevel@tonic-gate 	dev_info_t *dip = pci_p->pci_dip;
7000Sstevel@tonic-gate 	int i;
7010Sstevel@tonic-gate 	caddr_t vaddr = (caddr_t)mp->dmai_object.dmao_obj.virt_obj.v_as;
7020Sstevel@tonic-gate 	struct hat *hat_p = vaddr ? ((struct as *)vaddr)->a_hat : kas.a_hat;
7030Sstevel@tonic-gate 	caddr_t sva;
7040Sstevel@tonic-gate 	int needcb = 0;
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	sva = (caddr_t)(((uintptr_t)mp->dmai_object.dmao_obj.virt_obj.v_addr +
7070Sstevel@tonic-gate 	    IOMMU_PAGE_SIZE) & IOMMU_PAGE_MASK);
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 	if (pci_dvma_remap_enabled && hat_p == kas.a_hat &&
7100Sstevel@tonic-gate 		mp->dmai_object.dmao_type != DMA_OTYP_BUFVADDR)
7110Sstevel@tonic-gate 		needcb = 1;
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	for (vaddr = sva, i = 1; i < npages; i++, vaddr += IOMMU_PAGE_SIZE) {
7140Sstevel@tonic-gate 		pfn_t pfn;
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 		if (needcb) {
7170Sstevel@tonic-gate 			int (*waitfp)(caddr_t) = dmareq->dmar_fp;
7180Sstevel@tonic-gate 			uint_t flags = ((waitfp == DDI_DMA_SLEEP)?
7190Sstevel@tonic-gate 			    HAC_SLEEP : HAC_NOSLEEP) | HAC_PAGELOCK;
7200Sstevel@tonic-gate 			int ret;
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 			ret = hat_add_callback(pci_dvma_cbid, vaddr,
723*2251Selowe 			    IOMMU_PAGE_SIZE, flags, mp, &pfn,
724*2251Selowe 			    MP_HAT_CB_COOKIE_PTR(mp, i));
725*2251Selowe 
7260Sstevel@tonic-gate 			if (pfn == PFN_INVALID && ret == ENOMEM) {
7270Sstevel@tonic-gate 				ASSERT(waitfp != DDI_DMA_SLEEP);
7280Sstevel@tonic-gate 				if (waitfp != DDI_DMA_DONTWAIT)
7290Sstevel@tonic-gate 					ddi_set_callback(waitfp,
7300Sstevel@tonic-gate 					    dmareq->dmar_arg, &pci_kmem_clid);
7310Sstevel@tonic-gate 				return (DDI_DMA_NORESOURCES);
7320Sstevel@tonic-gate 			}
7330Sstevel@tonic-gate 		} else
7340Sstevel@tonic-gate 			pfn = hat_getpfnum(hat_p, vaddr);
7350Sstevel@tonic-gate 		if (pfn == PFN_INVALID)
7360Sstevel@tonic-gate 			goto err_badpfn;
7370Sstevel@tonic-gate 		PCI_SET_MP_PFN1(mp, i, (iopfn_t)pfn);
7380Sstevel@tonic-gate 		DEBUG3(DBG_DMA_MAP, dip, "pci_dma_vapfn: mp=%p pfnlst[%x]=%x\n",
7390Sstevel@tonic-gate 			mp, i, (iopfn_t)pfn);
7400Sstevel@tonic-gate 	}
7410Sstevel@tonic-gate 	return (DDI_SUCCESS);
7420Sstevel@tonic-gate err_badpfn:
7430Sstevel@tonic-gate 	cmn_err(CE_WARN, "%s%d: bad page frame vaddr=%p", NAMEINST(dip), vaddr);
7440Sstevel@tonic-gate 	return (DDI_DMA_NOMAPPING);
7450Sstevel@tonic-gate }
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate /*
7480Sstevel@tonic-gate  * pci_dma_pfn - Fills pfn list for all pages being DMA-ed.
7490Sstevel@tonic-gate  *
7500Sstevel@tonic-gate  * dependencies:
7510Sstevel@tonic-gate  *	mp->dmai_ndvmapages	- set to total # of dma pages
7520Sstevel@tonic-gate  *
7530Sstevel@tonic-gate  * return value:
7540Sstevel@tonic-gate  *	DDI_SUCCESS
7550Sstevel@tonic-gate  *	DDI_DMA_NOMAPPING
7560Sstevel@tonic-gate  */
7570Sstevel@tonic-gate int
7580Sstevel@tonic-gate pci_dma_pfn(pci_t *pci_p, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp)
7590Sstevel@tonic-gate {
7600Sstevel@tonic-gate 	uint32_t npages = mp->dmai_ndvmapages;
7610Sstevel@tonic-gate 	int (*waitfp)(caddr_t) = dmareq->dmar_fp;
7620Sstevel@tonic-gate 	int i, ret, peer = PCI_DMA_ISPTP(mp);
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	pbm_t *pbm_p = pci_p->pci_pbm_p;
7650Sstevel@tonic-gate 	iopfn_t pfn_base = pbm_p->pbm_base_pfn;
7660Sstevel@tonic-gate 	iopfn_t pfn_last = pbm_p->pbm_last_pfn;
7670Sstevel@tonic-gate 	iopfn_t pfn_adj = peer ? pfn_base : 0;
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 	DEBUG2(DBG_DMA_MAP, pci_p->pci_dip, "pci_dma_pfn: mp=%p pfn0=%x\n",
7700Sstevel@tonic-gate 		mp, MP_PFN0(mp) - pfn_adj);
7710Sstevel@tonic-gate 	/* 1 page: no array alloc/fill, no mixed mode check */
7720Sstevel@tonic-gate 	if (npages == 1) {
7730Sstevel@tonic-gate 		PCI_SET_MP_PFN(mp, 0, MP_PFN0(mp) - pfn_adj);
7740Sstevel@tonic-gate 		return (DDI_SUCCESS);
7750Sstevel@tonic-gate 	}
7760Sstevel@tonic-gate 	/* allocate pfn array */
7770Sstevel@tonic-gate 	if (!(mp->dmai_pfnlst = kmem_alloc(npages * sizeof (iopfn_t),
7780Sstevel@tonic-gate 		waitfp == DDI_DMA_SLEEP ? KM_SLEEP : KM_NOSLEEP))) {
7790Sstevel@tonic-gate 		if (waitfp != DDI_DMA_DONTWAIT)
7800Sstevel@tonic-gate 			ddi_set_callback(waitfp, dmareq->dmar_arg,
7810Sstevel@tonic-gate 				&pci_kmem_clid);
7820Sstevel@tonic-gate 		return (DDI_DMA_NORESOURCES);
7830Sstevel@tonic-gate 	}
7840Sstevel@tonic-gate 	/* fill pfn array */
7850Sstevel@tonic-gate 	PCI_SET_MP_PFN(mp, 0, MP_PFN0(mp) - pfn_adj);	/* pfnlst[0] */
7860Sstevel@tonic-gate 	if ((ret = PCI_DMA_ISPGPFN(mp) ? pci_dma_pgpfn(pci_p, mp, npages) :
7870Sstevel@tonic-gate 		pci_dma_vapfn(pci_p, dmareq, mp, npages)) != DDI_SUCCESS)
7880Sstevel@tonic-gate 		goto err;
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	/* skip pfn0, check mixed mode and adjust peer to peer pfn */
7910Sstevel@tonic-gate 	for (i = 1; i < npages; i++) {
7920Sstevel@tonic-gate 		iopfn_t pfn = PCI_GET_MP_PFN1(mp, i);
7930Sstevel@tonic-gate 		if (peer ^ TGT_PFN_INBETWEEN(pfn, pfn_base, pfn_last)) {
794946Smathue 			cmn_err(CE_WARN, "%s%d mixed mode DMA %lx %lx",
7950Sstevel@tonic-gate 				NAMEINST(mp->dmai_rdip), MP_PFN0(mp), pfn);
7960Sstevel@tonic-gate 			ret = DDI_DMA_NOMAPPING;	/* mixed mode */
7970Sstevel@tonic-gate 			goto err;
7980Sstevel@tonic-gate 		}
7990Sstevel@tonic-gate 		DEBUG3(DBG_DMA_MAP, pci_p->pci_dip,
8000Sstevel@tonic-gate 			"pci_dma_pfn: pfnlst[%x]=%x-%x\n", i, pfn, pfn_adj);
8010Sstevel@tonic-gate 		if (pfn_adj)
8020Sstevel@tonic-gate 			PCI_SET_MP_PFN1(mp, i, pfn - pfn_adj);
8030Sstevel@tonic-gate 	}
8040Sstevel@tonic-gate 	return (DDI_SUCCESS);
8050Sstevel@tonic-gate err:
8060Sstevel@tonic-gate 	pci_dvma_unregister_callbacks(pci_p, mp);
8070Sstevel@tonic-gate 	pci_dma_freepfn(mp);
8080Sstevel@tonic-gate 	return (ret);
8090Sstevel@tonic-gate }
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate /*
8120Sstevel@tonic-gate  * pci_dvma_win() - trim requested DVMA size down to window size
8130Sstevel@tonic-gate  *	The 1st window starts from offset and ends at page-aligned boundary.
8140Sstevel@tonic-gate  *	From the 2nd window on, each window starts and ends at page-aligned
8150Sstevel@tonic-gate  *	boundary except the last window ends at wherever requested.
8160Sstevel@tonic-gate  *
8170Sstevel@tonic-gate  *	accesses the following mp-> members:
8180Sstevel@tonic-gate  *	mp->dmai_attr.dma_attr_count_max
8190Sstevel@tonic-gate  *	mp->dmai_attr.dma_attr_seg
8200Sstevel@tonic-gate  *	mp->dmai_roffset   - start offset of 1st window
8210Sstevel@tonic-gate  *	mp->dmai_rflags (redzone)
8220Sstevel@tonic-gate  *	mp->dmai_ndvmapages (for 1 page fast path)
8230Sstevel@tonic-gate  *
8240Sstevel@tonic-gate  *	sets the following mp-> members:
8250Sstevel@tonic-gate  *	mp->dmai_size	   - xfer size, != winsize if 1st/last win  (not fixed)
8260Sstevel@tonic-gate  *	mp->dmai_winsize   - window size (no redzone), n * page size    (fixed)
8270Sstevel@tonic-gate  *	mp->dmai_nwin	   - # of DMA windows of entire object		(fixed)
8280Sstevel@tonic-gate  *	mp->dmai_rflags	   - remove partial flag if nwin == 1		(fixed)
8290Sstevel@tonic-gate  *	mp->dmai_winlst	   - NULL, window objects not used for DVMA	(fixed)
8300Sstevel@tonic-gate  *
8310Sstevel@tonic-gate  *	fixed - not changed across different DMA windows
8320Sstevel@tonic-gate  */
8330Sstevel@tonic-gate /*ARGSUSED*/
8340Sstevel@tonic-gate int
8350Sstevel@tonic-gate pci_dvma_win(pci_t *pci_p, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp)
8360Sstevel@tonic-gate {
8370Sstevel@tonic-gate 	uint32_t redzone_sz	= HAS_REDZONE(mp) ? IOMMU_PAGE_SIZE : 0;
8380Sstevel@tonic-gate 	size_t obj_sz	= mp->dmai_object.dmao_size;
8390Sstevel@tonic-gate 	size_t xfer_sz;
8400Sstevel@tonic-gate 	ulong_t pg_off;
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate 	if ((mp->dmai_ndvmapages == 1) && !redzone_sz) {
8430Sstevel@tonic-gate 		mp->dmai_rflags &= ~DDI_DMA_PARTIAL;
8440Sstevel@tonic-gate 		mp->dmai_size = obj_sz;
8450Sstevel@tonic-gate 		mp->dmai_winsize = IOMMU_PAGE_SIZE;
8460Sstevel@tonic-gate 		mp->dmai_nwin = 1;
8470Sstevel@tonic-gate 		goto done;
8480Sstevel@tonic-gate 	}
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 	pg_off	= mp->dmai_roffset;
8510Sstevel@tonic-gate 	xfer_sz	= obj_sz + redzone_sz;
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 	/* include redzone in nocross check */ {
8540Sstevel@tonic-gate 		uint64_t nocross = mp->dmai_attr.dma_attr_seg;
8550Sstevel@tonic-gate 		if (xfer_sz + pg_off - 1 > nocross)
8560Sstevel@tonic-gate 			xfer_sz = nocross - pg_off + 1;
8570Sstevel@tonic-gate 		if (redzone_sz && (xfer_sz <= redzone_sz)) {
8580Sstevel@tonic-gate 			DEBUG5(DBG_DMA_MAP, pci_p->pci_dip,
8590Sstevel@tonic-gate 			    "nocross too small %lx(%lx)+%lx+%x < %" PRIx64 "\n",
8600Sstevel@tonic-gate 			    xfer_sz, obj_sz, pg_off, redzone_sz, nocross);
8610Sstevel@tonic-gate 			return (DDI_DMA_TOOBIG);
8620Sstevel@tonic-gate 		}
8630Sstevel@tonic-gate 	}
8640Sstevel@tonic-gate 	xfer_sz -= redzone_sz;		/* restore transfer size  */
8650Sstevel@tonic-gate 	/* check counter max */ {
8660Sstevel@tonic-gate 		uint32_t count_max = mp->dmai_attr.dma_attr_count_max;
8670Sstevel@tonic-gate 		if (xfer_sz - 1 > count_max)
8680Sstevel@tonic-gate 			xfer_sz = count_max + 1;
8690Sstevel@tonic-gate 	}
8700Sstevel@tonic-gate 	if (xfer_sz >= obj_sz) {
8710Sstevel@tonic-gate 		mp->dmai_rflags &= ~DDI_DMA_PARTIAL;
8720Sstevel@tonic-gate 		mp->dmai_size = xfer_sz;
8730Sstevel@tonic-gate 		mp->dmai_winsize = P2ROUNDUP(xfer_sz + pg_off, IOMMU_PAGE_SIZE);
8740Sstevel@tonic-gate 		mp->dmai_nwin = 1;
8750Sstevel@tonic-gate 		goto done;
8760Sstevel@tonic-gate 	}
8770Sstevel@tonic-gate 	if (!(dmareq->dmar_flags & DDI_DMA_PARTIAL)) {
8780Sstevel@tonic-gate 		DEBUG4(DBG_DMA_MAP, pci_p->pci_dip,
8790Sstevel@tonic-gate 		    "too big: %lx+%lx+%x > %lx\n",
8800Sstevel@tonic-gate 		    obj_sz, pg_off, redzone_sz, xfer_sz);
8810Sstevel@tonic-gate 		return (DDI_DMA_TOOBIG);
8820Sstevel@tonic-gate 	}
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 	xfer_sz = IOMMU_PTOB(IOMMU_BTOP(xfer_sz + pg_off)); /* page align */
8850Sstevel@tonic-gate 	mp->dmai_size = xfer_sz - pg_off;	/* 1st window xferrable size */
8860Sstevel@tonic-gate 	mp->dmai_winsize = xfer_sz;		/* redzone not in winsize */
8870Sstevel@tonic-gate 	mp->dmai_nwin = (obj_sz + pg_off + xfer_sz - 1) / xfer_sz;
8880Sstevel@tonic-gate done:
8890Sstevel@tonic-gate 	mp->dmai_winlst = NULL;
8900Sstevel@tonic-gate 	dump_dma_handle(DBG_DMA_MAP, pci_p->pci_dip, mp);
8910Sstevel@tonic-gate 	return (DDI_SUCCESS);
8920Sstevel@tonic-gate }
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate /*
8950Sstevel@tonic-gate  * fast track cache entry to iommu context, inserts 3 0 bits between
8960Sstevel@tonic-gate  * upper 6-bits and lower 3-bits of the 9-bit cache entry
8970Sstevel@tonic-gate  */
8980Sstevel@tonic-gate #define	IOMMU_FCE_TO_CTX(i)	(((i) << 3) | ((i) & 0x7) | 0x38)
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate /*
9010Sstevel@tonic-gate  * pci_dvma_map_fast - attempts to map fast trackable DVMA
9020Sstevel@tonic-gate  */
9030Sstevel@tonic-gate int
9040Sstevel@tonic-gate pci_dvma_map_fast(iommu_t *iommu_p, ddi_dma_impl_t *mp)
9050Sstevel@tonic-gate {
9060Sstevel@tonic-gate 	uint_t clustsz = pci_dvma_page_cache_clustsz;
9070Sstevel@tonic-gate 	uint_t entries = pci_dvma_page_cache_entries;
9080Sstevel@tonic-gate 	uint64_t *tte_addr;
9090Sstevel@tonic-gate 	uint64_t tte = GET_TTE_TEMPLATE(mp);
9100Sstevel@tonic-gate 	int i = iommu_p->iommu_dvma_addr_scan_start;
9110Sstevel@tonic-gate 	uint8_t *lock_addr = iommu_p->iommu_dvma_cache_locks + i;
9120Sstevel@tonic-gate 	iopfn_t *pfn_addr;
9130Sstevel@tonic-gate 	dvma_addr_t dvma_pg;
9140Sstevel@tonic-gate 	size_t npages = IOMMU_BTOP(mp->dmai_winsize);
9150Sstevel@tonic-gate #ifdef DEBUG
9160Sstevel@tonic-gate 	dev_info_t *dip = mp->dmai_rdip;
9170Sstevel@tonic-gate #endif
9180Sstevel@tonic-gate 	extern uint8_t ldstub(uint8_t *);
9190Sstevel@tonic-gate 	ASSERT(IOMMU_PTOB(npages) == mp->dmai_winsize);
9200Sstevel@tonic-gate 	ASSERT(npages + HAS_REDZONE(mp) <= clustsz);
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 	for (; i < entries && ldstub(lock_addr); i++, lock_addr++);
9230Sstevel@tonic-gate 	if (i >= entries) {
9240Sstevel@tonic-gate 		lock_addr = iommu_p->iommu_dvma_cache_locks;
9250Sstevel@tonic-gate 		i = 0;
9260Sstevel@tonic-gate 		for (; i < entries && ldstub(lock_addr); i++, lock_addr++);
9270Sstevel@tonic-gate 		if (i >= entries) {
9280Sstevel@tonic-gate #ifdef PCI_DMA_PROF
9290Sstevel@tonic-gate 			pci_dvmaft_exhaust++;
9300Sstevel@tonic-gate #endif
9310Sstevel@tonic-gate 			return (DDI_DMA_NORESOURCES);
9320Sstevel@tonic-gate 		}
9330Sstevel@tonic-gate 	}
9340Sstevel@tonic-gate 	iommu_p->iommu_dvma_addr_scan_start = (i + 1) & (entries - 1);
9350Sstevel@tonic-gate 	if (PCI_DMA_USECTX(mp)) {
9360Sstevel@tonic-gate 		dvma_context_t ctx = IOMMU_FCE_TO_CTX(i);
9370Sstevel@tonic-gate 		tte |= IOMMU_CTX2TTE(ctx);
9380Sstevel@tonic-gate 		mp->dmai_flags |= DMAI_FLAGS_CONTEXT;
9390Sstevel@tonic-gate 		DEBUG1(DBG_DMA_MAP, dip, "fast: ctx=0x%x\n", ctx);
9400Sstevel@tonic-gate 	}
9410Sstevel@tonic-gate 	i *= clustsz;
9420Sstevel@tonic-gate 	tte_addr = iommu_p->iommu_tsb_vaddr + i;
9430Sstevel@tonic-gate 	dvma_pg = iommu_p->dvma_base_pg + i;
9440Sstevel@tonic-gate #ifdef DEBUG
9450Sstevel@tonic-gate 	for (i = 0; i < clustsz; i++)
9460Sstevel@tonic-gate 		ASSERT(TTE_IS_INVALID(tte_addr[i]));
9470Sstevel@tonic-gate #endif
9480Sstevel@tonic-gate 	*tte_addr = tte | IOMMU_PTOB(MP_PFN0(mp)); /* map page 0 */
9490Sstevel@tonic-gate 	DEBUG5(DBG_DMA_MAP, dip, "fast %p:dvma_pg=%x tte0(%p)=%08x.%08x\n", mp,
9500Sstevel@tonic-gate 		dvma_pg, tte_addr, HI32(*tte_addr), LO32(*tte_addr));
9510Sstevel@tonic-gate 	if (npages == 1)
9520Sstevel@tonic-gate 		goto tte_done;
9530Sstevel@tonic-gate 	pfn_addr = PCI_GET_MP_PFN1_ADDR(mp); /* short iommu_map_pages() */
9540Sstevel@tonic-gate 	for (tte_addr++, i = 1; i < npages; i++, tte_addr++, pfn_addr++) {
9550Sstevel@tonic-gate 		*tte_addr = tte | IOMMU_PTOB(*pfn_addr);
9560Sstevel@tonic-gate 		DEBUG5(DBG_DMA_MAP, dip, "fast %p:tte(%p, %p)=%08x.%08x\n", mp,
9570Sstevel@tonic-gate 			tte_addr, pfn_addr, HI32(*tte_addr), LO32(*tte_addr));
9580Sstevel@tonic-gate 	}
9590Sstevel@tonic-gate tte_done:
9600Sstevel@tonic-gate #ifdef PCI_DMA_PROF
9610Sstevel@tonic-gate 	pci_dvmaft_success++;
9620Sstevel@tonic-gate #endif
9630Sstevel@tonic-gate 	mp->dmai_mapping = mp->dmai_roffset | IOMMU_PTOB(dvma_pg);
9640Sstevel@tonic-gate 	mp->dmai_offset = 0;
9650Sstevel@tonic-gate 	mp->dmai_flags |= DMAI_FLAGS_FASTTRACK;
9660Sstevel@tonic-gate 	PCI_SAVE_MP_TTE(mp, tte);	/* save TTE template for unmapping */
9670Sstevel@tonic-gate 	if (DVMA_DBG_ON(iommu_p))
9680Sstevel@tonic-gate 		pci_dvma_alloc_debug(iommu_p, (char *)mp->dmai_mapping,
9690Sstevel@tonic-gate 			mp->dmai_size, mp);
9700Sstevel@tonic-gate 	return (DDI_SUCCESS);
9710Sstevel@tonic-gate }
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate /*
9740Sstevel@tonic-gate  * pci_dvma_map: map non-fasttrack DMA
9750Sstevel@tonic-gate  *		Use quantum cache if single page DMA.
9760Sstevel@tonic-gate  */
9770Sstevel@tonic-gate int
9780Sstevel@tonic-gate pci_dvma_map(ddi_dma_impl_t *mp, ddi_dma_req_t *dmareq, iommu_t *iommu_p)
9790Sstevel@tonic-gate {
9800Sstevel@tonic-gate 	uint_t npages = PCI_DMA_WINNPGS(mp);
9810Sstevel@tonic-gate 	dvma_addr_t dvma_pg, dvma_pg_index;
9820Sstevel@tonic-gate 	void *dvma_addr;
9830Sstevel@tonic-gate 	uint64_t tte = GET_TTE_TEMPLATE(mp);
9840Sstevel@tonic-gate 	int sleep = dmareq->dmar_fp == DDI_DMA_SLEEP ? VM_SLEEP : VM_NOSLEEP;
9850Sstevel@tonic-gate #ifdef DEBUG
9860Sstevel@tonic-gate 	dev_info_t *dip = mp->dmai_rdip;
9870Sstevel@tonic-gate #endif
9880Sstevel@tonic-gate 	/*
9890Sstevel@tonic-gate 	 * allocate dvma space resource and map in the first window.
9900Sstevel@tonic-gate 	 * (vmem_t *vmp, size_t size,
9910Sstevel@tonic-gate 	 *	size_t align, size_t phase, size_t nocross,
9920Sstevel@tonic-gate 	 *	void *minaddr, void *maxaddr, int vmflag)
9930Sstevel@tonic-gate 	 */
9941501Sgovinda 	if ((npages == 1) && !HAS_REDZONE(mp) && HAS_NOSYSLIMIT(mp)) {
9950Sstevel@tonic-gate 		dvma_addr = vmem_alloc(iommu_p->iommu_dvma_map,
9960Sstevel@tonic-gate 			IOMMU_PAGE_SIZE, sleep);
9970Sstevel@tonic-gate 		mp->dmai_flags |= DMAI_FLAGS_VMEMCACHE;
9980Sstevel@tonic-gate #ifdef PCI_DMA_PROF
9990Sstevel@tonic-gate 		pci_dvma_vmem_alloc++;
10000Sstevel@tonic-gate #endif
10010Sstevel@tonic-gate 	} else {
10020Sstevel@tonic-gate 		dvma_addr = vmem_xalloc(iommu_p->iommu_dvma_map,
10030Sstevel@tonic-gate 			IOMMU_PTOB(npages + HAS_REDZONE(mp)),
10040Sstevel@tonic-gate 			MAX(mp->dmai_attr.dma_attr_align, IOMMU_PAGE_SIZE),
10050Sstevel@tonic-gate 			0,
10060Sstevel@tonic-gate 			mp->dmai_attr.dma_attr_seg + 1,
10070Sstevel@tonic-gate 			(void *)mp->dmai_attr.dma_attr_addr_lo,
10080Sstevel@tonic-gate 			(void *)(mp->dmai_attr.dma_attr_addr_hi + 1),
10090Sstevel@tonic-gate 			sleep);
10100Sstevel@tonic-gate #ifdef PCI_DMA_PROF
10110Sstevel@tonic-gate 		pci_dvma_vmem_xalloc++;
10120Sstevel@tonic-gate #endif
10130Sstevel@tonic-gate 	}
10140Sstevel@tonic-gate 	dvma_pg = IOMMU_BTOP((ulong_t)dvma_addr);
10150Sstevel@tonic-gate 	dvma_pg_index = dvma_pg - iommu_p->dvma_base_pg;
10160Sstevel@tonic-gate 	DEBUG2(DBG_DMA_MAP, dip, "fallback dvma_pages: dvma_pg=%x index=%x\n",
10170Sstevel@tonic-gate 		dvma_pg, dvma_pg_index);
10180Sstevel@tonic-gate 	if (dvma_pg == 0)
10190Sstevel@tonic-gate 		goto noresource;
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 	/* allocate DVMA context */
10220Sstevel@tonic-gate 	if ((npages >= pci_context_minpages) && PCI_DMA_USECTX(mp)) {
10230Sstevel@tonic-gate 		dvma_context_t ctx;
10240Sstevel@tonic-gate 		if (ctx = pci_iommu_get_dvma_context(iommu_p, dvma_pg_index)) {
10250Sstevel@tonic-gate 			tte |= IOMMU_CTX2TTE(ctx);
10260Sstevel@tonic-gate 			mp->dmai_flags |= DMAI_FLAGS_CONTEXT;
10270Sstevel@tonic-gate 		}
10280Sstevel@tonic-gate 	}
10290Sstevel@tonic-gate 	mp->dmai_mapping = mp->dmai_roffset | IOMMU_PTOB(dvma_pg);
10300Sstevel@tonic-gate 	mp->dmai_offset = 0;
10310Sstevel@tonic-gate 	PCI_SAVE_MP_TTE(mp, tte);	/* mp->dmai_tte = tte */
10320Sstevel@tonic-gate 	iommu_map_pages(iommu_p, mp, dvma_pg, npages, 0);
10330Sstevel@tonic-gate 	return (DDI_SUCCESS);
10340Sstevel@tonic-gate noresource:
10350Sstevel@tonic-gate 	if (dmareq->dmar_fp != DDI_DMA_DONTWAIT) {
10360Sstevel@tonic-gate 		DEBUG0(DBG_DMA_MAP, dip, "dvma_pg 0 - set callback\n");
10370Sstevel@tonic-gate 		ddi_set_callback(dmareq->dmar_fp, dmareq->dmar_arg,
10380Sstevel@tonic-gate 			&iommu_p->iommu_dvma_clid);
10390Sstevel@tonic-gate 	}
10400Sstevel@tonic-gate 	DEBUG0(DBG_DMA_MAP, dip, "vmem_xalloc - DDI_DMA_NORESOURCES\n");
10410Sstevel@tonic-gate 	return (DDI_DMA_NORESOURCES);
10420Sstevel@tonic-gate }
10430Sstevel@tonic-gate 
10440Sstevel@tonic-gate void
10450Sstevel@tonic-gate pci_dvma_unmap(iommu_t *iommu_p, ddi_dma_impl_t *mp)
10460Sstevel@tonic-gate {
10470Sstevel@tonic-gate 	size_t npages;
10480Sstevel@tonic-gate 	dvma_addr_t dvma_addr = (dvma_addr_t)mp->dmai_mapping;
10490Sstevel@tonic-gate 	dvma_addr_t dvma_pg = IOMMU_BTOP(dvma_addr);
10500Sstevel@tonic-gate 	dvma_addr = IOMMU_PTOB(dvma_pg);
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	if (mp->dmai_flags & DMAI_FLAGS_FASTTRACK) {
10530Sstevel@tonic-gate 		iopfn_t index = dvma_pg - iommu_p->dvma_base_pg;
10540Sstevel@tonic-gate 		ASSERT(index % pci_dvma_page_cache_clustsz == 0);
10550Sstevel@tonic-gate 		index /= pci_dvma_page_cache_clustsz;
10560Sstevel@tonic-gate 		ASSERT(index < pci_dvma_page_cache_entries);
10570Sstevel@tonic-gate 		iommu_p->iommu_dvma_cache_locks[index] = 0;
10580Sstevel@tonic-gate #ifdef PCI_DMA_PROF
10590Sstevel@tonic-gate 		pci_dvmaft_free++;
10600Sstevel@tonic-gate #endif
10610Sstevel@tonic-gate 		return;
10620Sstevel@tonic-gate 	}
10630Sstevel@tonic-gate 	npages = IOMMU_BTOP(mp->dmai_winsize) + HAS_REDZONE(mp);
10640Sstevel@tonic-gate 	pci_vmem_free(iommu_p, mp, (void *)dvma_addr, npages);
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 	if (mp->dmai_flags & DMAI_FLAGS_CONTEXT)
10670Sstevel@tonic-gate 		pci_iommu_free_dvma_context(iommu_p, MP2CTX(mp));
10680Sstevel@tonic-gate }
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate void
10710Sstevel@tonic-gate pci_dma_sync_unmap(dev_info_t *dip, dev_info_t *rdip, ddi_dma_impl_t *mp)
10720Sstevel@tonic-gate {
10730Sstevel@tonic-gate 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
10740Sstevel@tonic-gate 	iommu_t *iommu_p = pci_p->pci_iommu_p;
10750Sstevel@tonic-gate 	uint64_t sync_buf_save = SYNC_BUF_PA(mp);
10760Sstevel@tonic-gate 	uint32_t fast_track = mp->dmai_flags & DMAI_FLAGS_FASTTRACK;
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate 	if (fast_track) {
10790Sstevel@tonic-gate 		dvma_addr_t dvma_pg = IOMMU_BTOP(mp->dmai_mapping);
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 		SYNC_BUF_PA(mp) = IOMMU_PAGE_TTEPA(iommu_p, dvma_pg);
10820Sstevel@tonic-gate 		ASSERT(!(SYNC_BUF_PA(mp) & PCI_SYNC_FLAG_SIZE - 1));
10830Sstevel@tonic-gate 	}
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 	if (pci_dvma_sync_before_unmap) {
10860Sstevel@tonic-gate 		pci_dma_sync(dip, rdip, (ddi_dma_handle_t)mp, 0, 0, 0);
10870Sstevel@tonic-gate 		iommu_unmap_window(iommu_p, mp);
10880Sstevel@tonic-gate 	} else {
10890Sstevel@tonic-gate 		iommu_unmap_window(iommu_p, mp);
10900Sstevel@tonic-gate 		pci_dma_sync(dip, rdip, (ddi_dma_handle_t)mp, 0, 0, 0);
10910Sstevel@tonic-gate 	}
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 	if (fast_track)
10940Sstevel@tonic-gate 		SYNC_BUF_PA(mp) = sync_buf_save;
10950Sstevel@tonic-gate }
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate /*
10980Sstevel@tonic-gate  * DVMA mappings may have multiple windows, but each window always have
10990Sstevel@tonic-gate  * one segment.
11000Sstevel@tonic-gate  */
11010Sstevel@tonic-gate int
11020Sstevel@tonic-gate pci_dvma_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_impl_t *mp,
11030Sstevel@tonic-gate 	enum ddi_dma_ctlops cmd, off_t *offp, size_t *lenp, caddr_t *objp,
11040Sstevel@tonic-gate 	uint_t cache_flags)
11050Sstevel@tonic-gate {
11060Sstevel@tonic-gate 	switch (cmd) {
11070Sstevel@tonic-gate 	case DDI_DMA_SYNC:
11080Sstevel@tonic-gate 		return (pci_dma_sync(dip, rdip, (ddi_dma_handle_t)mp,
11090Sstevel@tonic-gate 		    *offp, *lenp, cache_flags));
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 	case DDI_DMA_HTOC: {
11120Sstevel@tonic-gate 		int ret;
11130Sstevel@tonic-gate 		off_t wo_off, off = *offp;	/* wo_off: wnd's obj offset */
11140Sstevel@tonic-gate 		uint_t win_size = mp->dmai_winsize;
11150Sstevel@tonic-gate 		ddi_dma_cookie_t *cp = (ddi_dma_cookie_t *)objp;
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 		if (off >= mp->dmai_object.dmao_size) {
11180Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d invalid dma_htoc offset %lx",
11190Sstevel@tonic-gate 				NAMEINST(mp->dmai_rdip), off);
11200Sstevel@tonic-gate 			return (DDI_FAILURE);
11210Sstevel@tonic-gate 		}
11220Sstevel@tonic-gate 		off += mp->dmai_roffset;
11230Sstevel@tonic-gate 		ret = pci_dma_win(dip, rdip, (ddi_dma_handle_t)mp,
11240Sstevel@tonic-gate 		    off / win_size, &wo_off, NULL, cp, NULL); /* lenp == NULL */
11250Sstevel@tonic-gate 		if (ret)
11260Sstevel@tonic-gate 			return (ret);
11270Sstevel@tonic-gate 		DEBUG4(DBG_DMA_CTL, dip, "HTOC:cookie=%x+%lx off=%lx,%lx\n",
11280Sstevel@tonic-gate 			cp->dmac_address, cp->dmac_size, off, *offp);
11290Sstevel@tonic-gate 
11300Sstevel@tonic-gate 		/* adjust cookie addr/len if we are not on window boundary */
11310Sstevel@tonic-gate 		ASSERT((off % win_size) == (off -
11320Sstevel@tonic-gate 			(PCI_DMA_CURWIN(mp) ? mp->dmai_roffset : 0) - wo_off));
11330Sstevel@tonic-gate 		off = PCI_DMA_CURWIN(mp) ? off % win_size : *offp;
11340Sstevel@tonic-gate 		ASSERT(cp->dmac_size > off);
11350Sstevel@tonic-gate 		cp->dmac_laddress += off;
11360Sstevel@tonic-gate 		cp->dmac_size -= off;
11370Sstevel@tonic-gate 		DEBUG5(DBG_DMA_CTL, dip,
11380Sstevel@tonic-gate 			"HTOC:mp=%p cookie=%x+%lx off=%lx,%lx\n",
11390Sstevel@tonic-gate 			mp, cp->dmac_address, cp->dmac_size, off, wo_off);
11400Sstevel@tonic-gate 		}
11410Sstevel@tonic-gate 		return (DDI_SUCCESS);
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate 	case DDI_DMA_REPWIN:
11440Sstevel@tonic-gate 		*offp = mp->dmai_offset;
11450Sstevel@tonic-gate 		*lenp = mp->dmai_size;
11460Sstevel@tonic-gate 		return (DDI_SUCCESS);
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate 	case DDI_DMA_MOVWIN: {
11490Sstevel@tonic-gate 		off_t off = *offp;
11500Sstevel@tonic-gate 		if (off >= mp->dmai_object.dmao_size)
11510Sstevel@tonic-gate 			return (DDI_FAILURE);
11520Sstevel@tonic-gate 		off += mp->dmai_roffset;
11530Sstevel@tonic-gate 		return (pci_dma_win(dip, rdip, (ddi_dma_handle_t)mp,
11540Sstevel@tonic-gate 		    off / mp->dmai_winsize, offp, lenp,
11550Sstevel@tonic-gate 		    (ddi_dma_cookie_t *)objp, NULL));
11560Sstevel@tonic-gate 		}
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 	case DDI_DMA_NEXTWIN: {
11590Sstevel@tonic-gate 		window_t win = PCI_DMA_CURWIN(mp);
11600Sstevel@tonic-gate 		if (offp) {
11610Sstevel@tonic-gate 			if (*(window_t *)offp != win) {  /* window not active */
11620Sstevel@tonic-gate 				*(window_t *)objp = win; /* return cur win */
11630Sstevel@tonic-gate 				return (DDI_DMA_STALE);
11640Sstevel@tonic-gate 			}
11650Sstevel@tonic-gate 			win++;
11660Sstevel@tonic-gate 		} else	/* map win 0 */
11670Sstevel@tonic-gate 			win = 0;
11680Sstevel@tonic-gate 		if (win >= mp->dmai_nwin) {
11690Sstevel@tonic-gate 			*(window_t *)objp = win - 1;
11700Sstevel@tonic-gate 			return (DDI_DMA_DONE);
11710Sstevel@tonic-gate 		}
11720Sstevel@tonic-gate 		if (pci_dma_win(dip, rdip, (ddi_dma_handle_t)mp,
11730Sstevel@tonic-gate 		    win, 0, 0, 0, 0)) {
11740Sstevel@tonic-gate 			*(window_t *)objp = win - 1;
11750Sstevel@tonic-gate 			return (DDI_FAILURE);
11760Sstevel@tonic-gate 		}
11770Sstevel@tonic-gate 		*(window_t *)objp = win;
11780Sstevel@tonic-gate 		}
11790Sstevel@tonic-gate 		return (DDI_SUCCESS);
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 	case DDI_DMA_NEXTSEG:
11820Sstevel@tonic-gate 		if (*(window_t *)offp != PCI_DMA_CURWIN(mp))
11830Sstevel@tonic-gate 			return (DDI_DMA_STALE);
11840Sstevel@tonic-gate 		if (lenp)				/* only 1 seg allowed */
11850Sstevel@tonic-gate 			return (DDI_DMA_DONE);
11860Sstevel@tonic-gate 							/* return mp as seg 0 */
11870Sstevel@tonic-gate 		*(ddi_dma_seg_t *)objp = (ddi_dma_seg_t)mp;
11880Sstevel@tonic-gate 		return (DDI_SUCCESS);
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 	case DDI_DMA_SEGTOC:
11910Sstevel@tonic-gate 		MAKE_DMA_COOKIE((ddi_dma_cookie_t *)objp, mp->dmai_mapping,
11920Sstevel@tonic-gate 			mp->dmai_size);
11930Sstevel@tonic-gate 		*offp = mp->dmai_offset;
11940Sstevel@tonic-gate 		*lenp = mp->dmai_size;
11950Sstevel@tonic-gate 		return (DDI_SUCCESS);
11960Sstevel@tonic-gate 
11970Sstevel@tonic-gate 	case DDI_DMA_COFF: {
11980Sstevel@tonic-gate 		ddi_dma_cookie_t *cp = (ddi_dma_cookie_t *)offp;
11990Sstevel@tonic-gate 		if (cp->dmac_address < mp->dmai_mapping ||
12000Sstevel@tonic-gate 		    (cp->dmac_address + cp->dmac_size) >
12010Sstevel@tonic-gate 		    (mp->dmai_mapping + mp->dmai_size))
12020Sstevel@tonic-gate 			return (DDI_FAILURE);
12030Sstevel@tonic-gate 		*objp = (caddr_t)(cp->dmac_address - mp->dmai_mapping +
12040Sstevel@tonic-gate 			mp->dmai_offset);
12050Sstevel@tonic-gate 		}
12060Sstevel@tonic-gate 		return (DDI_SUCCESS);
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate 	case DDI_DMA_REMAP:
12090Sstevel@tonic-gate 		if (pci_dvma_remap_enabled)
12100Sstevel@tonic-gate 			return (pci_dvma_remap(dip, rdip, mp, *offp, *lenp));
12110Sstevel@tonic-gate 		return (DDI_FAILURE);
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 	default:
12140Sstevel@tonic-gate 		DEBUG3(DBG_DMA_CTL, dip, "unknown command (%x): rdip=%s%d\n",
12150Sstevel@tonic-gate 			cmd, ddi_driver_name(rdip), ddi_get_instance(rdip));
12160Sstevel@tonic-gate 		break;
12170Sstevel@tonic-gate 	}
12180Sstevel@tonic-gate 	return (DDI_FAILURE);
12190Sstevel@tonic-gate }
12200Sstevel@tonic-gate 
12210Sstevel@tonic-gate void
12220Sstevel@tonic-gate pci_dma_freewin(ddi_dma_impl_t *mp)
12230Sstevel@tonic-gate {
12240Sstevel@tonic-gate 	pci_dma_win_t *win_p = mp->dmai_winlst, *win2_p;
12250Sstevel@tonic-gate 	for (win2_p = win_p; win_p; win2_p = win_p) {
12260Sstevel@tonic-gate 		win_p = win2_p->win_next;
12270Sstevel@tonic-gate 		kmem_free(win2_p, sizeof (pci_dma_win_t) +
12280Sstevel@tonic-gate 			sizeof (ddi_dma_cookie_t) * win2_p->win_ncookies);
12290Sstevel@tonic-gate 	}
12300Sstevel@tonic-gate 	mp->dmai_nwin = 0;
12310Sstevel@tonic-gate 	mp->dmai_winlst = NULL;
12320Sstevel@tonic-gate }
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate /*
12350Sstevel@tonic-gate  * pci_dma_newwin - create a dma window object and cookies
12360Sstevel@tonic-gate  *
12370Sstevel@tonic-gate  *	After the initial scan in pci_dma_physwin(), which identifies
12380Sstevel@tonic-gate  *	a portion of the pfn array that belongs to a dma window,
12390Sstevel@tonic-gate  *	we are called to allocate and initialize representing memory
12400Sstevel@tonic-gate  *	resources. We know from the 1st scan the number of cookies
12410Sstevel@tonic-gate  *	or dma segment in this window so we can allocate a contiguous
12420Sstevel@tonic-gate  *	memory array for the dma cookies (The implementation of
12430Sstevel@tonic-gate  *	ddi_dma_nextcookie(9f) dictates dma cookies be contiguous).
12440Sstevel@tonic-gate  *
12450Sstevel@tonic-gate  *	A second round scan is done on the pfn array to identify
12460Sstevel@tonic-gate  *	each dma segment and initialize its corresponding dma cookie.
12470Sstevel@tonic-gate  *	We don't need to do all the safety checking and we know they
12480Sstevel@tonic-gate  *	all belong to the same dma window.
12490Sstevel@tonic-gate  *
12500Sstevel@tonic-gate  *	Input:	cookie_no - # of cookies identified by the 1st scan
12510Sstevel@tonic-gate  *		start_idx - subscript of the pfn array for the starting pfn
12520Sstevel@tonic-gate  *		end_idx   - subscript of the last pfn in dma window
12530Sstevel@tonic-gate  *		win_pp    - pointer to win_next member of previous window
12540Sstevel@tonic-gate  *	Return:	DDI_SUCCESS - with **win_pp as newly created window object
12550Sstevel@tonic-gate  *		DDI_DMA_NORESROUCE - caller frees all previous window objs
12560Sstevel@tonic-gate  *	Note:	Each cookie and window size are all initialized on page
12570Sstevel@tonic-gate  *		boundary. This is not true for the 1st cookie of the 1st
12580Sstevel@tonic-gate  *		window and the last cookie of the last window.
12590Sstevel@tonic-gate  *		We fix that later in upper layer which has access to size
12600Sstevel@tonic-gate  *		and offset info.
12610Sstevel@tonic-gate  *
12620Sstevel@tonic-gate  */
12630Sstevel@tonic-gate static int
12640Sstevel@tonic-gate pci_dma_newwin(ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp, uint32_t cookie_no,
12650Sstevel@tonic-gate 	uint32_t start_idx, uint32_t end_idx, pci_dma_win_t **win_pp,
12660Sstevel@tonic-gate 	uint64_t count_max, uint64_t bypass_prefix)
12670Sstevel@tonic-gate {
12680Sstevel@tonic-gate 	int (*waitfp)(caddr_t) = dmareq->dmar_fp;
12690Sstevel@tonic-gate 	ddi_dma_cookie_t *cookie_p;
12700Sstevel@tonic-gate 	uint32_t pfn_no = 1;
12710Sstevel@tonic-gate 	iopfn_t pfn = PCI_GET_MP_PFN(mp, start_idx);
12720Sstevel@tonic-gate 	iopfn_t prev_pfn = pfn;
12730Sstevel@tonic-gate 	uint64_t seg_pfn0 = pfn;
12740Sstevel@tonic-gate 	size_t sz = cookie_no * sizeof (ddi_dma_cookie_t);
12750Sstevel@tonic-gate 	pci_dma_win_t *win_p = kmem_alloc(sizeof (pci_dma_win_t) + sz,
12760Sstevel@tonic-gate 		waitfp == DDI_DMA_SLEEP ? KM_SLEEP : KM_NOSLEEP);
12770Sstevel@tonic-gate 	if (!win_p)
12780Sstevel@tonic-gate 		goto noresource;
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 	win_p->win_next = NULL;
12810Sstevel@tonic-gate 	win_p->win_ncookies = cookie_no;
12820Sstevel@tonic-gate 	win_p->win_curseg = 0;	/* start from segment 0 */
12830Sstevel@tonic-gate 	win_p->win_size = IOMMU_PTOB(end_idx - start_idx + 1);
12840Sstevel@tonic-gate 	/* win_p->win_offset is left uninitialized */
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate 	cookie_p = (ddi_dma_cookie_t *)(win_p + 1);
12870Sstevel@tonic-gate 	start_idx++;
12880Sstevel@tonic-gate 	for (; start_idx <= end_idx; start_idx++, prev_pfn = pfn, pfn_no++) {
12890Sstevel@tonic-gate 		pfn = PCI_GET_MP_PFN1(mp, start_idx);
12900Sstevel@tonic-gate 		if ((pfn == prev_pfn + 1) &&
12910Sstevel@tonic-gate 			(IOMMU_PTOB(pfn_no + 1) - 1 <= count_max))
12920Sstevel@tonic-gate 			continue;
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate 		/* close up the cookie up to (including) prev_pfn */
12950Sstevel@tonic-gate 		MAKE_DMA_COOKIE(cookie_p, IOMMU_PTOB(seg_pfn0) | bypass_prefix,
12960Sstevel@tonic-gate 			IOMMU_PTOB(pfn_no));
12970Sstevel@tonic-gate 		DEBUG2(DBG_BYPASS, mp->dmai_rdip, "cookie %p (%x pages)\n",
12980Sstevel@tonic-gate 			IOMMU_PTOB(seg_pfn0) | bypass_prefix, pfn_no);
12990Sstevel@tonic-gate 
13000Sstevel@tonic-gate 		cookie_p++;	/* advance to next available cookie cell */
13010Sstevel@tonic-gate 		pfn_no = 0;
13020Sstevel@tonic-gate 		seg_pfn0 = pfn;	/* start a new segment from current pfn */
13030Sstevel@tonic-gate 	}
13040Sstevel@tonic-gate 	MAKE_DMA_COOKIE(cookie_p, IOMMU_PTOB(seg_pfn0) | bypass_prefix,
13050Sstevel@tonic-gate 		IOMMU_PTOB(pfn_no));
13060Sstevel@tonic-gate 	DEBUG3(DBG_BYPASS, mp->dmai_rdip, "cookie %p (%x pages) of total %x\n",
13070Sstevel@tonic-gate 		IOMMU_PTOB(seg_pfn0) | bypass_prefix, pfn_no, cookie_no);
13080Sstevel@tonic-gate #ifdef DEBUG
13090Sstevel@tonic-gate 	cookie_p++;
13100Sstevel@tonic-gate 	ASSERT((cookie_p - (ddi_dma_cookie_t *)(win_p + 1)) == cookie_no);
13110Sstevel@tonic-gate #endif
13120Sstevel@tonic-gate 	*win_pp = win_p;
13130Sstevel@tonic-gate 	return (DDI_SUCCESS);
13140Sstevel@tonic-gate noresource:
13150Sstevel@tonic-gate 	if (waitfp != DDI_DMA_DONTWAIT)
13160Sstevel@tonic-gate 		ddi_set_callback(waitfp, dmareq->dmar_arg, &pci_kmem_clid);
13170Sstevel@tonic-gate 	return (DDI_DMA_NORESOURCES);
13180Sstevel@tonic-gate }
13190Sstevel@tonic-gate 
13200Sstevel@tonic-gate /*
13210Sstevel@tonic-gate  * pci_dma_adjust - adjust 1st and last cookie and window sizes
13220Sstevel@tonic-gate  *	remove initial dma page offset from 1st cookie and window size
13230Sstevel@tonic-gate  *	remove last dma page remainder from last cookie and window size
13240Sstevel@tonic-gate  *	fill win_offset of each dma window according to just fixed up
13250Sstevel@tonic-gate  *		each window sizes
13260Sstevel@tonic-gate  *	pci_dma_win_t members modified:
13270Sstevel@tonic-gate  *	win_p->win_offset - this window's offset within entire DMA object
13280Sstevel@tonic-gate  *	win_p->win_size	  - xferrable size (in bytes) for this window
13290Sstevel@tonic-gate  *
13300Sstevel@tonic-gate  *	ddi_dma_impl_t members modified:
13310Sstevel@tonic-gate  *	mp->dmai_size	  - 1st window xferrable size
13320Sstevel@tonic-gate  *	mp->dmai_offset   - 0, which is the dma offset of the 1st window
13330Sstevel@tonic-gate  *
13340Sstevel@tonic-gate  *	ddi_dma_cookie_t members modified:
13350Sstevel@tonic-gate  *	cookie_p->dmac_size - 1st and last cookie remove offset or remainder
13360Sstevel@tonic-gate  *	cookie_p->dmac_laddress - 1st cookie add page offset
13370Sstevel@tonic-gate  */
13380Sstevel@tonic-gate static void
13390Sstevel@tonic-gate pci_dma_adjust(ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp, pci_dma_win_t *win_p)
13400Sstevel@tonic-gate {
13410Sstevel@tonic-gate 	ddi_dma_cookie_t *cookie_p = (ddi_dma_cookie_t *)(win_p + 1);
13420Sstevel@tonic-gate 	size_t pg_offset = mp->dmai_roffset;
13430Sstevel@tonic-gate 	size_t win_offset = 0;
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 	cookie_p->dmac_size -= pg_offset;
13460Sstevel@tonic-gate 	cookie_p->dmac_laddress |= pg_offset;
13470Sstevel@tonic-gate 	win_p->win_size -= pg_offset;
13480Sstevel@tonic-gate 	DEBUG1(DBG_BYPASS, mp->dmai_rdip, "pg0 adjust %lx\n", pg_offset);
13490Sstevel@tonic-gate 
13500Sstevel@tonic-gate 	mp->dmai_size = win_p->win_size;
13510Sstevel@tonic-gate 	mp->dmai_offset = 0;
13520Sstevel@tonic-gate 
13530Sstevel@tonic-gate 	pg_offset += mp->dmai_object.dmao_size;
13540Sstevel@tonic-gate 	pg_offset &= IOMMU_PAGE_OFFSET;
13550Sstevel@tonic-gate 	if (pg_offset)
13560Sstevel@tonic-gate 		pg_offset = IOMMU_PAGE_SIZE - pg_offset;
13570Sstevel@tonic-gate 	DEBUG1(DBG_BYPASS, mp->dmai_rdip, "last pg adjust %lx\n", pg_offset);
13580Sstevel@tonic-gate 
13590Sstevel@tonic-gate 	for (; win_p->win_next; win_p = win_p->win_next) {
13600Sstevel@tonic-gate 		DEBUG1(DBG_BYPASS, mp->dmai_rdip, "win off %p\n", win_offset);
13610Sstevel@tonic-gate 		win_p->win_offset = win_offset;
13620Sstevel@tonic-gate 		win_offset += win_p->win_size;
13630Sstevel@tonic-gate 	}
13640Sstevel@tonic-gate 	/* last window */
13650Sstevel@tonic-gate 	win_p->win_offset = win_offset;
13660Sstevel@tonic-gate 	cookie_p = (ddi_dma_cookie_t *)(win_p + 1);
13670Sstevel@tonic-gate 	cookie_p[win_p->win_ncookies - 1].dmac_size -= pg_offset;
13680Sstevel@tonic-gate 	win_p->win_size -= pg_offset;
13690Sstevel@tonic-gate 	ASSERT((win_offset + win_p->win_size) == mp->dmai_object.dmao_size);
13700Sstevel@tonic-gate }
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate /*
13730Sstevel@tonic-gate  * pci_dma_physwin() - carve up dma windows using physical addresses.
13740Sstevel@tonic-gate  *	Called to handle iommu bypass and pci peer-to-peer transfers.
13750Sstevel@tonic-gate  *	Calls pci_dma_newwin() to allocate window objects.
13760Sstevel@tonic-gate  *
13770Sstevel@tonic-gate  * Dependency: mp->dmai_pfnlst points to an array of pfns
13780Sstevel@tonic-gate  *
13790Sstevel@tonic-gate  * 1. Each dma window is represented by a pci_dma_win_t object.
13800Sstevel@tonic-gate  *	The object will be casted to ddi_dma_win_t and returned
13810Sstevel@tonic-gate  *	to leaf driver through the DDI interface.
13820Sstevel@tonic-gate  * 2. Each dma window can have several dma segments with each
13830Sstevel@tonic-gate  *	segment representing a physically contiguous either memory
13840Sstevel@tonic-gate  *	space (if we are doing an iommu bypass transfer) or pci address
13850Sstevel@tonic-gate  *	space (if we are doing a peer-to-peer transfer).
13860Sstevel@tonic-gate  * 3. Each segment has a DMA cookie to program the DMA engine.
13870Sstevel@tonic-gate  *	The cookies within each DMA window must be located in a
13880Sstevel@tonic-gate  *	contiguous array per ddi_dma_nextcookie(9f).
13890Sstevel@tonic-gate  * 4. The number of DMA segments within each DMA window cannot exceed
13900Sstevel@tonic-gate  *	mp->dmai_attr.dma_attr_sgllen. If the transfer size is
13910Sstevel@tonic-gate  *	too large to fit in the sgllen, the rest needs to be
13920Sstevel@tonic-gate  *	relocated to the next dma window.
13930Sstevel@tonic-gate  * 5. Peer-to-peer DMA segment follows device hi, lo, count_max,
13940Sstevel@tonic-gate  *	and nocross restrictions while bypass DMA follows the set of
13950Sstevel@tonic-gate  *	restrictions with system limits factored in.
13960Sstevel@tonic-gate  *
13970Sstevel@tonic-gate  * Return:
13980Sstevel@tonic-gate  *	mp->dmai_winlst	 - points to a link list of pci_dma_win_t objects.
13990Sstevel@tonic-gate  *		Each pci_dma_win_t object on the link list contains
14000Sstevel@tonic-gate  *		infomation such as its window size (# of pages),
14010Sstevel@tonic-gate  *		starting offset (also see Restriction), an array of
14020Sstevel@tonic-gate  *		DMA cookies, and # of cookies in the array.
14030Sstevel@tonic-gate  *	mp->dmai_pfnlst	 - NULL, the pfn list is freed to conserve memory.
14040Sstevel@tonic-gate  *	mp->dmai_nwin	 - # of total DMA windows on mp->dmai_winlst.
14050Sstevel@tonic-gate  *	mp->dmai_mapping - starting cookie address
14060Sstevel@tonic-gate  *	mp->dmai_rflags	 - consistent, nosync, no redzone
14070Sstevel@tonic-gate  *	mp->dmai_cookie	 - start of cookie table of the 1st DMA window
14080Sstevel@tonic-gate  *
14090Sstevel@tonic-gate  * Restriction:
14100Sstevel@tonic-gate  *	Each pci_dma_win_t object can theoratically start from any offset
14110Sstevel@tonic-gate  *	since the iommu is not involved. However, this implementation
14120Sstevel@tonic-gate  *	always make windows start from page aligned offset (except
14130Sstevel@tonic-gate  *	the 1st window, which follows the requested offset) due to the
14140Sstevel@tonic-gate  *	fact that we are handed a pfn list. This does require device's
14150Sstevel@tonic-gate  *	count_max and attr_seg to be at least IOMMU_PAGE_SIZE aligned.
14160Sstevel@tonic-gate  */
14170Sstevel@tonic-gate int
14180Sstevel@tonic-gate pci_dma_physwin(pci_t *pci_p, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp)
14190Sstevel@tonic-gate {
14200Sstevel@tonic-gate 	uint_t npages = mp->dmai_ndvmapages;
14210Sstevel@tonic-gate 	int ret, sgllen = mp->dmai_attr.dma_attr_sgllen;
14220Sstevel@tonic-gate 	iopfn_t pfn_lo, pfn_hi, prev_pfn, bypass_pfn;
14230Sstevel@tonic-gate 	iopfn_t pfn = PCI_GET_MP_PFN(mp, 0);
14240Sstevel@tonic-gate 	uint32_t i, win_no = 0, pfn_no = 1, win_pfn0_index = 0, cookie_no = 0;
14250Sstevel@tonic-gate 	uint64_t count_max, bypass = PCI_DMA_BYPASS_PREFIX(mp, pfn);
14260Sstevel@tonic-gate 	pci_dma_win_t **win_pp = (pci_dma_win_t **)&mp->dmai_winlst;
14270Sstevel@tonic-gate 	ddi_dma_cookie_t *cookie0_p;
14280Sstevel@tonic-gate 
14290Sstevel@tonic-gate 	if (PCI_DMA_ISPTP(mp)) { /* ignore sys limits for peer-to-peer */
14300Sstevel@tonic-gate 		ddi_dma_attr_t *dev_attr_p = DEV_ATTR(mp);
14310Sstevel@tonic-gate 		iopfn_t pfn_base = pci_p->pci_pbm_p->pbm_base_pfn;
14320Sstevel@tonic-gate 		iopfn_t pfn_last = pci_p->pci_pbm_p->pbm_last_pfn - pfn_base;
14330Sstevel@tonic-gate 		uint64_t nocross = dev_attr_p->dma_attr_seg;
14340Sstevel@tonic-gate 		if (nocross && (nocross < UINT32_MAX))
14350Sstevel@tonic-gate 			return (DDI_DMA_NOMAPPING);
14360Sstevel@tonic-gate 		if (dev_attr_p->dma_attr_align > IOMMU_PAGE_SIZE)
14370Sstevel@tonic-gate 			return (DDI_DMA_NOMAPPING);
14380Sstevel@tonic-gate 		pfn_lo = IOMMU_BTOP(dev_attr_p->dma_attr_addr_lo);
14390Sstevel@tonic-gate 		pfn_hi = IOMMU_BTOP(dev_attr_p->dma_attr_addr_hi);
14400Sstevel@tonic-gate 		pfn_hi = MIN(pfn_hi, pfn_last);
14410Sstevel@tonic-gate 		if ((pfn_lo > pfn_hi) || (pfn < pfn_lo))
14420Sstevel@tonic-gate 			return (DDI_DMA_NOMAPPING);
14430Sstevel@tonic-gate 		count_max = dev_attr_p->dma_attr_count_max;
14440Sstevel@tonic-gate 		count_max = MIN(count_max, nocross);
14450Sstevel@tonic-gate 		/*
14460Sstevel@tonic-gate 		 * the following count_max trim is not done because we are
14470Sstevel@tonic-gate 		 * making sure pfn_lo <= pfn <= pfn_hi inside the loop
14480Sstevel@tonic-gate 		 * count_max=MIN(count_max, IOMMU_PTOB(pfn_hi - pfn_lo + 1)-1);
14490Sstevel@tonic-gate 		 */
14500Sstevel@tonic-gate 	} else { /* bypass hi/lo/count_max have been processed by attr2hdl() */
14510Sstevel@tonic-gate 		count_max = mp->dmai_attr.dma_attr_count_max;
14520Sstevel@tonic-gate 		pfn_lo = IOMMU_BTOP(mp->dmai_attr.dma_attr_addr_lo);
14530Sstevel@tonic-gate 		pfn_hi = IOMMU_BTOP(mp->dmai_attr.dma_attr_addr_hi);
14540Sstevel@tonic-gate 	}
14550Sstevel@tonic-gate 
14560Sstevel@tonic-gate 	bypass_pfn = IOMMU_BTOP(bypass);
14570Sstevel@tonic-gate 
14580Sstevel@tonic-gate 	for (prev_pfn = (bypass_pfn | pfn), i = 1; i < npages;
14590Sstevel@tonic-gate 	    i++, prev_pfn = pfn, pfn_no++) {
14600Sstevel@tonic-gate 		pfn = bypass_pfn | PCI_GET_MP_PFN1(mp, i);
14610Sstevel@tonic-gate 		if ((pfn == prev_pfn + 1) &&
14620Sstevel@tonic-gate 			(IOMMU_PTOB(pfn_no + 1) - 1 <= count_max))
14630Sstevel@tonic-gate 			continue;
14640Sstevel@tonic-gate 		if ((pfn < pfn_lo) || (prev_pfn > pfn_hi)) {
14650Sstevel@tonic-gate 			ret = DDI_DMA_NOMAPPING;
14660Sstevel@tonic-gate 			goto err;
14670Sstevel@tonic-gate 		}
14680Sstevel@tonic-gate 		cookie_no++;
14690Sstevel@tonic-gate 		pfn_no = 0;
14700Sstevel@tonic-gate 		if (cookie_no < sgllen)
14710Sstevel@tonic-gate 			continue;
14720Sstevel@tonic-gate 
14730Sstevel@tonic-gate 		DEBUG3(DBG_BYPASS, mp->dmai_rdip, "newwin pfn[%x-%x] %x cks\n",
14740Sstevel@tonic-gate 			win_pfn0_index, i - 1, cookie_no);
14750Sstevel@tonic-gate 		if (ret = pci_dma_newwin(dmareq, mp, cookie_no,
14760Sstevel@tonic-gate 			win_pfn0_index, i - 1, win_pp, count_max, bypass))
14770Sstevel@tonic-gate 			goto err;
14780Sstevel@tonic-gate 
14790Sstevel@tonic-gate 		win_pp = &(*win_pp)->win_next;	/* win_pp = *(win_pp) */
14800Sstevel@tonic-gate 		win_no++;
14810Sstevel@tonic-gate 		win_pfn0_index = i;
14820Sstevel@tonic-gate 		cookie_no = 0;
14830Sstevel@tonic-gate 	}
14840Sstevel@tonic-gate 	if (pfn > pfn_hi) {
14850Sstevel@tonic-gate 		ret = DDI_DMA_NOMAPPING;
14860Sstevel@tonic-gate 		goto err;
14870Sstevel@tonic-gate 	}
14880Sstevel@tonic-gate 	cookie_no++;
14890Sstevel@tonic-gate 	DEBUG3(DBG_BYPASS, mp->dmai_rdip, "newwin pfn[%x-%x] %x cks\n",
14900Sstevel@tonic-gate 		win_pfn0_index, i - 1, cookie_no);
14910Sstevel@tonic-gate 	if (ret = pci_dma_newwin(dmareq, mp, cookie_no, win_pfn0_index,
14920Sstevel@tonic-gate 		i - 1, win_pp, count_max, bypass))
14930Sstevel@tonic-gate 		goto err;
14940Sstevel@tonic-gate 	win_no++;
14950Sstevel@tonic-gate 	pci_dma_adjust(dmareq, mp, mp->dmai_winlst);
14960Sstevel@tonic-gate 	mp->dmai_nwin = win_no;
14970Sstevel@tonic-gate 	mp->dmai_rflags |= DDI_DMA_CONSISTENT;
14980Sstevel@tonic-gate 	if (!pci_p->pci_pbm_p->pbm_sync_reg_pa) {
14990Sstevel@tonic-gate 		mp->dmai_rflags |= DMP_NOSYNC;
15000Sstevel@tonic-gate 		mp->dmai_flags |= DMAI_FLAGS_NOSYNC;
15010Sstevel@tonic-gate 	}
15020Sstevel@tonic-gate 	mp->dmai_rflags &= ~DDI_DMA_REDZONE;
15030Sstevel@tonic-gate 	cookie0_p = (ddi_dma_cookie_t *)(WINLST(mp) + 1);
15040Sstevel@tonic-gate 	mp->dmai_cookie = WINLST(mp)->win_ncookies > 1 ? cookie0_p + 1 : 0;
15050Sstevel@tonic-gate 	mp->dmai_mapping = cookie0_p->dmac_laddress;
15060Sstevel@tonic-gate 
15070Sstevel@tonic-gate 	pci_dma_freepfn(mp);
15080Sstevel@tonic-gate 	return (DDI_DMA_MAPPED);
15090Sstevel@tonic-gate err:
15100Sstevel@tonic-gate 	pci_dma_freewin(mp);
15110Sstevel@tonic-gate 	return (ret);
15120Sstevel@tonic-gate }
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate /*ARGSUSED*/
15150Sstevel@tonic-gate int
15160Sstevel@tonic-gate pci_dma_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_impl_t *mp,
15170Sstevel@tonic-gate 	enum ddi_dma_ctlops cmd, off_t *offp, size_t *lenp, caddr_t *objp,
15180Sstevel@tonic-gate 	uint_t cache_flags)
15190Sstevel@tonic-gate {
15200Sstevel@tonic-gate 	switch (cmd) {
15210Sstevel@tonic-gate 	case DDI_DMA_SYNC: /* XXX */
15220Sstevel@tonic-gate 		return (DDI_SUCCESS);
15230Sstevel@tonic-gate 
15240Sstevel@tonic-gate 	case DDI_DMA_HTOC: {
15250Sstevel@tonic-gate 		off_t off = *offp;
15260Sstevel@tonic-gate 		ddi_dma_cookie_t *loop_cp, *cp;
15270Sstevel@tonic-gate 		pci_dma_win_t *win_p = mp->dmai_winlst;
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate 		if (off >= mp->dmai_object.dmao_size)
15300Sstevel@tonic-gate 			return (DDI_FAILURE);
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate 		/* locate window */
15330Sstevel@tonic-gate 		while (win_p->win_offset + win_p->win_size <= off)
15340Sstevel@tonic-gate 			win_p = win_p->win_next;
15350Sstevel@tonic-gate 
15360Sstevel@tonic-gate 		loop_cp = cp = (ddi_dma_cookie_t *)(win_p + 1);
15370Sstevel@tonic-gate 		mp->dmai_offset = win_p->win_offset;
15380Sstevel@tonic-gate 		mp->dmai_size   = win_p->win_size;
15390Sstevel@tonic-gate 		mp->dmai_mapping = cp->dmac_laddress; /* cookie0 start addr */
15400Sstevel@tonic-gate 
15410Sstevel@tonic-gate 		/* adjust cookie addr/len if we are not on cookie boundary */
15420Sstevel@tonic-gate 		off -= win_p->win_offset;	   /* offset within window */
15430Sstevel@tonic-gate 		for (; off >= loop_cp->dmac_size; loop_cp++)
15440Sstevel@tonic-gate 			off -= loop_cp->dmac_size; /* offset within cookie */
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate 		mp->dmai_cookie = loop_cp + 1;
15470Sstevel@tonic-gate 		win_p->win_curseg = loop_cp - cp;
15480Sstevel@tonic-gate 		cp = (ddi_dma_cookie_t *)objp;
15490Sstevel@tonic-gate 		MAKE_DMA_COOKIE(cp, loop_cp->dmac_laddress + off,
15500Sstevel@tonic-gate 			loop_cp->dmac_size - off);
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate 		DEBUG2(DBG_DMA_CTL, dip,
15530Sstevel@tonic-gate 			"HTOC: cookie - dmac_laddress=%p dmac_size=%x\n",
15540Sstevel@tonic-gate 			cp->dmac_laddress, cp->dmac_size);
15550Sstevel@tonic-gate 		}
15560Sstevel@tonic-gate 		return (DDI_SUCCESS);
15570Sstevel@tonic-gate 
15580Sstevel@tonic-gate 	case DDI_DMA_REPWIN:
15590Sstevel@tonic-gate 		*offp = mp->dmai_offset;
15600Sstevel@tonic-gate 		*lenp = mp->dmai_size;
15610Sstevel@tonic-gate 		return (DDI_SUCCESS);
15620Sstevel@tonic-gate 
15630Sstevel@tonic-gate 	case DDI_DMA_MOVWIN: {
15640Sstevel@tonic-gate 		off_t off = *offp;
15650Sstevel@tonic-gate 		ddi_dma_cookie_t *cp;
15660Sstevel@tonic-gate 		pci_dma_win_t *win_p = mp->dmai_winlst;
15670Sstevel@tonic-gate 
15680Sstevel@tonic-gate 		if (off >= mp->dmai_object.dmao_size)
15690Sstevel@tonic-gate 			return (DDI_FAILURE);
15700Sstevel@tonic-gate 
15710Sstevel@tonic-gate 		/* locate window */
15720Sstevel@tonic-gate 		while (win_p->win_offset + win_p->win_size <= off)
15730Sstevel@tonic-gate 			win_p = win_p->win_next;
15740Sstevel@tonic-gate 
15750Sstevel@tonic-gate 		cp = (ddi_dma_cookie_t *)(win_p + 1);
15760Sstevel@tonic-gate 		mp->dmai_offset = win_p->win_offset;
15770Sstevel@tonic-gate 		mp->dmai_size   = win_p->win_size;
15780Sstevel@tonic-gate 		mp->dmai_mapping = cp->dmac_laddress;	/* cookie0 star addr */
15790Sstevel@tonic-gate 		mp->dmai_cookie = cp + 1;
15800Sstevel@tonic-gate 		win_p->win_curseg = 0;
15810Sstevel@tonic-gate 
15820Sstevel@tonic-gate 		*(ddi_dma_cookie_t *)objp = *cp;
15830Sstevel@tonic-gate 		*offp = win_p->win_offset;
15840Sstevel@tonic-gate 		*lenp = win_p->win_size;
15850Sstevel@tonic-gate 		DEBUG2(DBG_DMA_CTL, dip,
15860Sstevel@tonic-gate 			"HTOC: cookie - dmac_laddress=%p dmac_size=%x\n",
15870Sstevel@tonic-gate 			cp->dmac_laddress, cp->dmac_size);
15880Sstevel@tonic-gate 		}
15890Sstevel@tonic-gate 		return (DDI_SUCCESS);
15900Sstevel@tonic-gate 
15910Sstevel@tonic-gate 	case DDI_DMA_NEXTWIN: {
15920Sstevel@tonic-gate 		pci_dma_win_t *win_p = *(pci_dma_win_t **)offp;
15930Sstevel@tonic-gate 		pci_dma_win_t **nw_pp = (pci_dma_win_t **)objp;
15940Sstevel@tonic-gate 		ddi_dma_cookie_t *cp;
15950Sstevel@tonic-gate 		if (!win_p) {
15960Sstevel@tonic-gate 			*nw_pp = mp->dmai_winlst;
15970Sstevel@tonic-gate 			return (DDI_SUCCESS);
15980Sstevel@tonic-gate 		}
15990Sstevel@tonic-gate 
16000Sstevel@tonic-gate 		if (win_p->win_offset != mp->dmai_offset)
16010Sstevel@tonic-gate 			return (DDI_DMA_STALE);
16020Sstevel@tonic-gate 		if (!win_p->win_next)
16030Sstevel@tonic-gate 			return (DDI_DMA_DONE);
16040Sstevel@tonic-gate 		win_p = win_p->win_next;
16050Sstevel@tonic-gate 		cp = (ddi_dma_cookie_t *)(win_p + 1);
16060Sstevel@tonic-gate 		mp->dmai_offset = win_p->win_offset;
16070Sstevel@tonic-gate 		mp->dmai_size   = win_p->win_size;
16080Sstevel@tonic-gate 		mp->dmai_mapping = cp->dmac_laddress;   /* cookie0 star addr */
16090Sstevel@tonic-gate 		mp->dmai_cookie = cp + 1;
16100Sstevel@tonic-gate 		win_p->win_curseg = 0;
16110Sstevel@tonic-gate 		*nw_pp = win_p;
16120Sstevel@tonic-gate 		}
16130Sstevel@tonic-gate 		return (DDI_SUCCESS);
16140Sstevel@tonic-gate 
16150Sstevel@tonic-gate 	case DDI_DMA_NEXTSEG: {
16160Sstevel@tonic-gate 		pci_dma_win_t *w_p = *(pci_dma_win_t **)offp;
16170Sstevel@tonic-gate 		if (w_p->win_offset != mp->dmai_offset)
16180Sstevel@tonic-gate 			return (DDI_DMA_STALE);
16190Sstevel@tonic-gate 		if (w_p->win_curseg + 1 >= w_p->win_ncookies)
16200Sstevel@tonic-gate 			return (DDI_DMA_DONE);
16210Sstevel@tonic-gate 		w_p->win_curseg++;
16220Sstevel@tonic-gate 		}
16230Sstevel@tonic-gate 		*(ddi_dma_seg_t *)objp = (ddi_dma_seg_t)mp;
16240Sstevel@tonic-gate 		return (DDI_SUCCESS);
16250Sstevel@tonic-gate 
16260Sstevel@tonic-gate 	case DDI_DMA_SEGTOC: {
16270Sstevel@tonic-gate 		pci_dma_win_t *win_p = mp->dmai_winlst;
16280Sstevel@tonic-gate 		off_t off = mp->dmai_offset;
16290Sstevel@tonic-gate 		ddi_dma_cookie_t *cp;
16300Sstevel@tonic-gate 		int i;
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate 		/* locate active window */
16330Sstevel@tonic-gate 		for (; win_p->win_offset != off; win_p = win_p->win_next);
16340Sstevel@tonic-gate 		cp = (ddi_dma_cookie_t *)(win_p + 1);
16350Sstevel@tonic-gate 		for (i = 0; i < win_p->win_curseg; i++, cp++)
16360Sstevel@tonic-gate 			off += cp->dmac_size;
16370Sstevel@tonic-gate 		*offp = off;
16380Sstevel@tonic-gate 		*lenp = cp->dmac_size;
16390Sstevel@tonic-gate 		*(ddi_dma_cookie_t *)objp = *cp;	/* copy cookie */
16400Sstevel@tonic-gate 		}
16410Sstevel@tonic-gate 		return (DDI_SUCCESS);
16420Sstevel@tonic-gate 
16430Sstevel@tonic-gate 	case DDI_DMA_COFF: {
16440Sstevel@tonic-gate 		pci_dma_win_t *win_p;
16450Sstevel@tonic-gate 		ddi_dma_cookie_t *cp;
16460Sstevel@tonic-gate 		uint64_t addr, key = ((ddi_dma_cookie_t *)offp)->dmac_laddress;
16470Sstevel@tonic-gate 		size_t win_off;
16480Sstevel@tonic-gate 
16490Sstevel@tonic-gate 		for (win_p = mp->dmai_winlst; win_p; win_p = win_p->win_next) {
16500Sstevel@tonic-gate 			int i;
16510Sstevel@tonic-gate 			win_off = 0;
16520Sstevel@tonic-gate 			cp = (ddi_dma_cookie_t *)(win_p + 1);
16530Sstevel@tonic-gate 			for (i = 0; i < win_p->win_ncookies; i++, cp++) {
16540Sstevel@tonic-gate 				size_t sz = cp->dmac_size;
16550Sstevel@tonic-gate 
16560Sstevel@tonic-gate 				addr = cp->dmac_laddress;
16570Sstevel@tonic-gate 				if ((addr <= key) && (addr + sz >= key))
16580Sstevel@tonic-gate 					goto found;
16590Sstevel@tonic-gate 				win_off += sz;
16600Sstevel@tonic-gate 			}
16610Sstevel@tonic-gate 		}
16620Sstevel@tonic-gate 		return (DDI_FAILURE);
16630Sstevel@tonic-gate found:
16640Sstevel@tonic-gate 		*objp = (caddr_t)(win_p->win_offset + win_off + (key - addr));
16650Sstevel@tonic-gate 		return (DDI_SUCCESS);
16660Sstevel@tonic-gate 		}
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate 	case DDI_DMA_REMAP:
16690Sstevel@tonic-gate 		return (DDI_FAILURE);
16700Sstevel@tonic-gate 
16710Sstevel@tonic-gate 	default:
16720Sstevel@tonic-gate 		DEBUG3(DBG_DMA_CTL, dip, "unknown command (%x): rdip=%s%d\n",
16730Sstevel@tonic-gate 			cmd, ddi_driver_name(rdip), ddi_get_instance(rdip));
16740Sstevel@tonic-gate 		break;
16750Sstevel@tonic-gate 	}
16760Sstevel@tonic-gate 	return (DDI_FAILURE);
16770Sstevel@tonic-gate }
16780Sstevel@tonic-gate 
16790Sstevel@tonic-gate static void
16800Sstevel@tonic-gate pci_dvma_debug_init(iommu_t *iommu_p)
16810Sstevel@tonic-gate {
16820Sstevel@tonic-gate 	size_t sz = sizeof (struct dvma_rec) * pci_dvma_debug_rec;
16830Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&iommu_p->dvma_debug_lock));
16840Sstevel@tonic-gate 	cmn_err(CE_NOTE, "PCI DVMA %p stat ON", iommu_p);
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate 	iommu_p->dvma_alloc_rec = kmem_zalloc(sz, KM_SLEEP);
16870Sstevel@tonic-gate 	iommu_p->dvma_free_rec = kmem_zalloc(sz, KM_SLEEP);
16880Sstevel@tonic-gate 
16890Sstevel@tonic-gate 	iommu_p->dvma_active_list = NULL;
16900Sstevel@tonic-gate 	iommu_p->dvma_alloc_rec_index = 0;
16910Sstevel@tonic-gate 	iommu_p->dvma_free_rec_index = 0;
16920Sstevel@tonic-gate 	iommu_p->dvma_active_count = 0;
16930Sstevel@tonic-gate }
16940Sstevel@tonic-gate 
16950Sstevel@tonic-gate void
16960Sstevel@tonic-gate pci_dvma_debug_fini(iommu_t *iommu_p)
16970Sstevel@tonic-gate {
16980Sstevel@tonic-gate 	struct dvma_rec *prev, *ptr;
16990Sstevel@tonic-gate 	size_t sz = sizeof (struct dvma_rec) * pci_dvma_debug_rec;
17000Sstevel@tonic-gate 	uint64_t mask = ~(1ull << iommu_p->iommu_inst);
17010Sstevel@tonic-gate 	cmn_err(CE_NOTE, "PCI DVMA %p stat OFF", iommu_p);
17020Sstevel@tonic-gate 
17030Sstevel@tonic-gate 	kmem_free(iommu_p->dvma_alloc_rec, sz);
17040Sstevel@tonic-gate 	kmem_free(iommu_p->dvma_free_rec, sz);
17050Sstevel@tonic-gate 	iommu_p->dvma_alloc_rec = iommu_p->dvma_free_rec = NULL;
17060Sstevel@tonic-gate 
17070Sstevel@tonic-gate 	prev = iommu_p->dvma_active_list;
17080Sstevel@tonic-gate 	if (!prev)
17090Sstevel@tonic-gate 		return;
17100Sstevel@tonic-gate 	for (ptr = prev->next; ptr; prev = ptr, ptr = ptr->next)
17110Sstevel@tonic-gate 		kmem_free(prev, sizeof (struct dvma_rec));
17120Sstevel@tonic-gate 	kmem_free(prev, sizeof (struct dvma_rec));
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate 	iommu_p->dvma_active_list = NULL;
17150Sstevel@tonic-gate 	iommu_p->dvma_alloc_rec_index = 0;
17160Sstevel@tonic-gate 	iommu_p->dvma_free_rec_index = 0;
17170Sstevel@tonic-gate 	iommu_p->dvma_active_count = 0;
17180Sstevel@tonic-gate 
17190Sstevel@tonic-gate 	pci_dvma_debug_on  &= mask;
17200Sstevel@tonic-gate 	pci_dvma_debug_off &= mask;
17210Sstevel@tonic-gate }
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate void
17240Sstevel@tonic-gate pci_dvma_alloc_debug(iommu_t *iommu_p, char *address, uint_t len,
17250Sstevel@tonic-gate 	ddi_dma_impl_t *mp)
17260Sstevel@tonic-gate {
17270Sstevel@tonic-gate 	struct dvma_rec *ptr;
17280Sstevel@tonic-gate 	mutex_enter(&iommu_p->dvma_debug_lock);
17290Sstevel@tonic-gate 
17300Sstevel@tonic-gate 	if (!iommu_p->dvma_alloc_rec)
17310Sstevel@tonic-gate 		pci_dvma_debug_init(iommu_p);
17320Sstevel@tonic-gate 	if (DVMA_DBG_OFF(iommu_p)) {
17330Sstevel@tonic-gate 		pci_dvma_debug_fini(iommu_p);
17340Sstevel@tonic-gate 		goto done;
17350Sstevel@tonic-gate 	}
17360Sstevel@tonic-gate 
17370Sstevel@tonic-gate 	ptr = &iommu_p->dvma_alloc_rec[iommu_p->dvma_alloc_rec_index];
17380Sstevel@tonic-gate 	ptr->dvma_addr = address;
17390Sstevel@tonic-gate 	ptr->len = len;
17400Sstevel@tonic-gate 	ptr->mp = mp;
17410Sstevel@tonic-gate 	if (++iommu_p->dvma_alloc_rec_index == pci_dvma_debug_rec)
17420Sstevel@tonic-gate 		iommu_p->dvma_alloc_rec_index = 0;
17430Sstevel@tonic-gate 
17440Sstevel@tonic-gate 	ptr = kmem_alloc(sizeof (struct dvma_rec), KM_SLEEP);
17450Sstevel@tonic-gate 	ptr->dvma_addr = address;
17460Sstevel@tonic-gate 	ptr->len = len;
17470Sstevel@tonic-gate 	ptr->mp = mp;
17480Sstevel@tonic-gate 
17490Sstevel@tonic-gate 	ptr->next = iommu_p->dvma_active_list;
17500Sstevel@tonic-gate 	iommu_p->dvma_active_list = ptr;
17510Sstevel@tonic-gate 	iommu_p->dvma_active_count++;
17520Sstevel@tonic-gate done:
17530Sstevel@tonic-gate 	mutex_exit(&iommu_p->dvma_debug_lock);
17540Sstevel@tonic-gate }
17550Sstevel@tonic-gate 
17560Sstevel@tonic-gate void
17570Sstevel@tonic-gate pci_dvma_free_debug(iommu_t *iommu_p, char *address, uint_t len,
17580Sstevel@tonic-gate 	ddi_dma_impl_t *mp)
17590Sstevel@tonic-gate {
17600Sstevel@tonic-gate 	struct dvma_rec *ptr, *ptr_save;
17610Sstevel@tonic-gate 	mutex_enter(&iommu_p->dvma_debug_lock);
17620Sstevel@tonic-gate 
17630Sstevel@tonic-gate 	if (!iommu_p->dvma_alloc_rec)
17640Sstevel@tonic-gate 		pci_dvma_debug_init(iommu_p);
17650Sstevel@tonic-gate 	if (DVMA_DBG_OFF(iommu_p)) {
17660Sstevel@tonic-gate 		pci_dvma_debug_fini(iommu_p);
17670Sstevel@tonic-gate 		goto done;
17680Sstevel@tonic-gate 	}
17690Sstevel@tonic-gate 
17700Sstevel@tonic-gate 	ptr = &iommu_p->dvma_free_rec[iommu_p->dvma_free_rec_index];
17710Sstevel@tonic-gate 	ptr->dvma_addr = address;
17720Sstevel@tonic-gate 	ptr->len = len;
17730Sstevel@tonic-gate 	ptr->mp = mp;
17740Sstevel@tonic-gate 	if (++iommu_p->dvma_free_rec_index == pci_dvma_debug_rec)
17750Sstevel@tonic-gate 		iommu_p->dvma_free_rec_index = 0;
17760Sstevel@tonic-gate 
17770Sstevel@tonic-gate 	ptr_save = iommu_p->dvma_active_list;
17780Sstevel@tonic-gate 	for (ptr = ptr_save; ptr; ptr = ptr->next) {
17790Sstevel@tonic-gate 		if ((ptr->dvma_addr == address) && (ptr->len = len))
17800Sstevel@tonic-gate 			break;
17810Sstevel@tonic-gate 		ptr_save = ptr;
17820Sstevel@tonic-gate 	}
17830Sstevel@tonic-gate 	if (!ptr) {
17840Sstevel@tonic-gate 		cmn_err(CE_WARN, "bad dvma free addr=%lx len=%x",
17850Sstevel@tonic-gate 			(long)address, len);
17860Sstevel@tonic-gate 		goto done;
17870Sstevel@tonic-gate 	}
17880Sstevel@tonic-gate 	if (ptr == iommu_p->dvma_active_list)
17890Sstevel@tonic-gate 		iommu_p->dvma_active_list = ptr->next;
17900Sstevel@tonic-gate 	else
17910Sstevel@tonic-gate 		ptr_save->next = ptr->next;
17920Sstevel@tonic-gate 	kmem_free(ptr, sizeof (struct dvma_rec));
17930Sstevel@tonic-gate 	iommu_p->dvma_active_count--;
17940Sstevel@tonic-gate done:
17950Sstevel@tonic-gate 	mutex_exit(&iommu_p->dvma_debug_lock);
17960Sstevel@tonic-gate }
17970Sstevel@tonic-gate 
17980Sstevel@tonic-gate #ifdef DEBUG
17990Sstevel@tonic-gate void
18000Sstevel@tonic-gate dump_dma_handle(uint64_t flag, dev_info_t *dip, ddi_dma_impl_t *hp)
18010Sstevel@tonic-gate {
18020Sstevel@tonic-gate 	DEBUG4(flag, dip, "mp(%p): flags=%x mapping=%lx xfer_size=%x\n",
18030Sstevel@tonic-gate 		hp, hp->dmai_inuse, hp->dmai_mapping, hp->dmai_size);
18040Sstevel@tonic-gate 	DEBUG4(flag|DBG_CONT, dip, "\tnpages=%x roffset=%x rflags=%x nwin=%x\n",
18050Sstevel@tonic-gate 		hp->dmai_ndvmapages, hp->dmai_roffset, hp->dmai_rflags,
18060Sstevel@tonic-gate 		hp->dmai_nwin);
18070Sstevel@tonic-gate 	DEBUG4(flag|DBG_CONT, dip, "\twinsize=%x tte=%p pfnlst=%p pfn0=%p\n",
18080Sstevel@tonic-gate 		hp->dmai_winsize, hp->dmai_tte, hp->dmai_pfnlst, hp->dmai_pfn0);
18090Sstevel@tonic-gate 	DEBUG4(flag|DBG_CONT, dip, "\twinlst=%x obj=%p attr=%p ckp=%p\n",
18100Sstevel@tonic-gate 		hp->dmai_winlst, &hp->dmai_object, &hp->dmai_attr,
18110Sstevel@tonic-gate 		hp->dmai_cookie);
18120Sstevel@tonic-gate }
18130Sstevel@tonic-gate #endif
18140Sstevel@tonic-gate 
18150Sstevel@tonic-gate void
18160Sstevel@tonic-gate pci_vmem_do_free(iommu_t *iommu_p, void *base_addr, size_t npages,
18170Sstevel@tonic-gate     int vmemcache)
18180Sstevel@tonic-gate {
18190Sstevel@tonic-gate 	vmem_t *map_p = iommu_p->iommu_dvma_map;
18200Sstevel@tonic-gate 
18210Sstevel@tonic-gate 	if (vmemcache) {
18220Sstevel@tonic-gate 		vmem_free(map_p, base_addr, IOMMU_PAGE_SIZE);
18230Sstevel@tonic-gate #ifdef PCI_DMA_PROF
18240Sstevel@tonic-gate 		pci_dvma_vmem_free++;
18250Sstevel@tonic-gate #endif
18260Sstevel@tonic-gate 		return;
18270Sstevel@tonic-gate 	}
18280Sstevel@tonic-gate 
18290Sstevel@tonic-gate 	vmem_xfree(map_p, base_addr, IOMMU_PTOB(npages));
18300Sstevel@tonic-gate #ifdef PCI_DMA_PROF
18310Sstevel@tonic-gate 		pci_dvma_vmem_xfree++;
18320Sstevel@tonic-gate #endif
18330Sstevel@tonic-gate }
1834