xref: /onnv-gate/usr/src/uts/sun4u/io/pci/pci_dma.c (revision 12027:6c3efe08f39d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51501Sgovinda  * Common Development and Distribution License (the "License").
61501Sgovinda  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*12027SStephen.Hanson@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * PCI nexus DVMA and DMA core routines:
280Sstevel@tonic-gate  *	dma_map/dma_bind_handle implementation
290Sstevel@tonic-gate  *	bypass and peer-to-peer support
300Sstevel@tonic-gate  *	fast track DVMA space allocation
310Sstevel@tonic-gate  *	runtime DVMA debug
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <sys/kmem.h>
350Sstevel@tonic-gate #include <sys/async.h>
360Sstevel@tonic-gate #include <sys/sysmacros.h>
370Sstevel@tonic-gate #include <sys/sunddi.h>
380Sstevel@tonic-gate #include <sys/machsystm.h>	/* lddphys() */
390Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
400Sstevel@tonic-gate #include <vm/hat.h>
410Sstevel@tonic-gate #include <sys/pci/pci_obj.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate /*LINTLIBRARY*/
440Sstevel@tonic-gate 
450Sstevel@tonic-gate static void
pci_sc_pg_inv(dev_info_t * dip,sc_t * sc_p,ddi_dma_impl_t * mp,off_t off,size_t len)460Sstevel@tonic-gate pci_sc_pg_inv(dev_info_t *dip, sc_t *sc_p, ddi_dma_impl_t *mp, off_t off,
470Sstevel@tonic-gate 	size_t len)
480Sstevel@tonic-gate {
490Sstevel@tonic-gate 	dvma_addr_t dvma_addr, pg_off;
500Sstevel@tonic-gate 	volatile uint64_t *invl_va = sc_p->sc_invl_reg;
510Sstevel@tonic-gate 
520Sstevel@tonic-gate 	if (!len)
530Sstevel@tonic-gate 		len = mp->dmai_size;
540Sstevel@tonic-gate 
550Sstevel@tonic-gate 	pg_off = mp->dmai_offset;			/* start min */
560Sstevel@tonic-gate 	dvma_addr = MAX(off, pg_off);			/* lo */
570Sstevel@tonic-gate 	pg_off += mp->dmai_size;			/* end max */
580Sstevel@tonic-gate 	pg_off = MIN(off + len, pg_off);		/* hi */
590Sstevel@tonic-gate 	if (dvma_addr >= pg_off) {			/* lo >= hi ? */
600Sstevel@tonic-gate 		DEBUG4(DBG_SC, dip, "%x+%x out of window [%x,%x)\n",
61*12027SStephen.Hanson@Sun.COM 		    off, len, mp->dmai_offset,
62*12027SStephen.Hanson@Sun.COM 		    mp->dmai_offset + mp->dmai_size);
630Sstevel@tonic-gate 		return;
640Sstevel@tonic-gate 	}
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	len = pg_off - dvma_addr;			/* sz = hi - lo */
670Sstevel@tonic-gate 	dvma_addr += mp->dmai_mapping;			/* start addr */
680Sstevel@tonic-gate 	pg_off = dvma_addr & IOMMU_PAGE_OFFSET;		/* offset in 1st pg */
690Sstevel@tonic-gate 	len = IOMMU_BTOPR(len + pg_off);		/* # of pages */
700Sstevel@tonic-gate 	dvma_addr ^= pg_off;
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	DEBUG2(DBG_SC, dip, "addr=%x+%x pages: \n", dvma_addr, len);
730Sstevel@tonic-gate 	for (; len; len--, dvma_addr += IOMMU_PAGE_SIZE) {
740Sstevel@tonic-gate 		DEBUG1(DBG_SC|DBG_CONT, dip, " %x", dvma_addr);
750Sstevel@tonic-gate 		*invl_va = (uint64_t)dvma_addr;
760Sstevel@tonic-gate 	}
770Sstevel@tonic-gate 	DEBUG0(DBG_SC|DBG_CONT, dip, "\n");
780Sstevel@tonic-gate }
790Sstevel@tonic-gate 
800Sstevel@tonic-gate static void
pci_dma_sync_flag_wait(ddi_dma_impl_t * mp,sc_t * sc_p,uint32_t onstack)810Sstevel@tonic-gate pci_dma_sync_flag_wait(ddi_dma_impl_t *mp, sc_t *sc_p, uint32_t onstack)
820Sstevel@tonic-gate {
830Sstevel@tonic-gate 	hrtime_t start_time;
840Sstevel@tonic-gate 	uint64_t loops = 0;
850Sstevel@tonic-gate 	uint64_t sync_flag_pa = SYNC_BUF_PA(mp);
860Sstevel@tonic-gate 	uint64_t sync_reg_pa = sc_p->sc_sync_reg_pa;
870Sstevel@tonic-gate 	uint8_t stack_buf[128];
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	stack_buf[0] = DDI_SUCCESS;
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	/* check for handle specific sync flag */
920Sstevel@tonic-gate 	if (sync_flag_pa)
930Sstevel@tonic-gate 		goto start;
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	sync_flag_pa = sc_p->sc_sync_flag_pa;
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	if (onstack) {
980Sstevel@tonic-gate 		sync_flag_pa = va_to_pa(stack_buf);
990Sstevel@tonic-gate 		sync_flag_pa += PCI_SYNC_FLAG_SIZE;
1000Sstevel@tonic-gate 		sync_flag_pa >>= PCI_SYNC_FLAG_SZSHIFT;
1010Sstevel@tonic-gate 		sync_flag_pa <<= PCI_SYNC_FLAG_SZSHIFT;
1020Sstevel@tonic-gate 		goto start;
1030Sstevel@tonic-gate 	}
1040Sstevel@tonic-gate 	stack_buf[0] |= PCI_SYNC_FLAG_LOCKED;
1050Sstevel@tonic-gate 	mutex_enter(&sc_p->sc_sync_mutex);
1060Sstevel@tonic-gate start:
1070Sstevel@tonic-gate 	ASSERT(!(sync_flag_pa & PCI_SYNC_FLAG_SIZE - 1));
1080Sstevel@tonic-gate 	stdphys(sync_flag_pa, 0);	/* reset sync flag to 0 */
1090Sstevel@tonic-gate 					/* membar  #LoadStore|#StoreStore */
1100Sstevel@tonic-gate 	stdphysio(sync_reg_pa, sync_flag_pa);
1110Sstevel@tonic-gate 	start_time = gethrtime();
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	for (; gethrtime() - start_time < pci_sync_buf_timeout; loops++)
1140Sstevel@tonic-gate 		if (lddphys(sync_flag_pa))
1150Sstevel@tonic-gate 			goto done;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	if (!lddphys(sync_flag_pa))
1180Sstevel@tonic-gate 		stack_buf[0] |= PCI_SYNC_FLAG_FAILED;
1190Sstevel@tonic-gate done:
1200Sstevel@tonic-gate 	DEBUG3(DBG_SC|DBG_CONT, 0, "flag wait loops=%lu ticks=%lu status=%x\n",
121*12027SStephen.Hanson@Sun.COM 	    loops, gethrtime() - start_time, stack_buf[0]);
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	if (stack_buf[0] & PCI_SYNC_FLAG_LOCKED)
1240Sstevel@tonic-gate 		mutex_exit(&sc_p->sc_sync_mutex);
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	if (stack_buf[0] & PCI_SYNC_FLAG_FAILED)
127946Smathue 		cmn_err(CE_PANIC, "%p pci dma sync %lx %lx timeout!",
1280Sstevel@tonic-gate 		    mp, sync_flag_pa, loops);
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate /*
1320Sstevel@tonic-gate  * Cache	RW	Before	During		After
1330Sstevel@tonic-gate  *
1340Sstevel@tonic-gate  * STREAMING	read	no/no	pg/no		ctx,pg/no
1350Sstevel@tonic-gate  * STREAMING	write	no/no	pg/yes		ctx,pg/yes
1360Sstevel@tonic-gate  * CONSISTENT	read	no/no	yes,no/no	yes,no/no
1370Sstevel@tonic-gate  * CONSISTENT	write	no/no	yes,yes/yes	yes,yes/yes
1380Sstevel@tonic-gate  *
1390Sstevel@tonic-gate  * STREAMING	read	ctx,pg/no
1400Sstevel@tonic-gate  * STREAMING	write	ctx,pg/yes
1410Sstevel@tonic-gate  * CONSISTENT	read	yes,no/no
1420Sstevel@tonic-gate  * CONSISTENT	write	yes,yes/yes
1430Sstevel@tonic-gate  */
1440Sstevel@tonic-gate int
pci_dma_sync(dev_info_t * dip,dev_info_t * rdip,ddi_dma_handle_t handle,off_t off,size_t len,uint32_t sync_flag)1450Sstevel@tonic-gate pci_dma_sync(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle,
1460Sstevel@tonic-gate 	off_t off, size_t len, uint32_t sync_flag)
1470Sstevel@tonic-gate {
1480Sstevel@tonic-gate 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
1490Sstevel@tonic-gate 	int ret = ddi_get_instance(dip);
1500Sstevel@tonic-gate 	pci_t *pci_p = get_pci_soft_state(ret);
1510Sstevel@tonic-gate 	pbm_t *pbm_p = pci_p->pci_pbm_p;
1520Sstevel@tonic-gate 	uint32_t dev_flag = mp->dmai_rflags;
1530Sstevel@tonic-gate 	sc_t *sc_p;
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	DEBUG4(DBG_DMA_SYNC, dip, "%s%d flags=%x,%x\n", ddi_driver_name(rdip),
156*12027SStephen.Hanson@Sun.COM 	    ddi_get_instance(rdip), dev_flag, sync_flag);
1570Sstevel@tonic-gate 	DEBUG4(DBG_SC, dip, "dmai_mapping=%x, dmai_sz=%x off=%x len=%x\n",
158*12027SStephen.Hanson@Sun.COM 	    mp->dmai_mapping, mp->dmai_size, off, len);
1590Sstevel@tonic-gate 	DEBUG2(DBG_SC, dip, "mp=%p, ctx=%x\n", mp, MP2CTX(mp));
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	if (!(mp->dmai_flags & DMAI_FLAGS_INUSE)) {
1620Sstevel@tonic-gate 		cmn_err(CE_WARN, "Unbound dma handle %p from %s%d", mp,
1630Sstevel@tonic-gate 		    ddi_driver_name(rdip), ddi_get_instance(rdip));
1640Sstevel@tonic-gate 		return (DDI_FAILURE);
1650Sstevel@tonic-gate 	}
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	if (mp->dmai_flags & DMAI_FLAGS_NOSYNC)
1680Sstevel@tonic-gate 		return (DDI_SUCCESS);
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	if (!(dev_flag & DDI_DMA_CONSISTENT))
1710Sstevel@tonic-gate 		goto streaming;
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	if (sync_flag & PCI_DMA_SYNC_EXT) {
1740Sstevel@tonic-gate 		if (sync_flag & (PCI_DMA_SYNC_BEFORE | PCI_DMA_SYNC_POST) ||
1750Sstevel@tonic-gate 		    !(sync_flag & PCI_DMA_SYNC_WRITE))
1760Sstevel@tonic-gate 			return (DDI_SUCCESS);
1770Sstevel@tonic-gate 	} else {
1780Sstevel@tonic-gate 		if (!(dev_flag & DDI_DMA_READ) ||
1790Sstevel@tonic-gate 		    ((sync_flag & PCI_DMA_SYNC_DDI_FLAGS) ==
1800Sstevel@tonic-gate 		    DDI_DMA_SYNC_FORDEV))
1810Sstevel@tonic-gate 			return (DDI_SUCCESS);
1820Sstevel@tonic-gate 	}
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	pci_pbm_dma_sync(pbm_p, pbm_p->pbm_sync_ino);
1850Sstevel@tonic-gate 	return (DDI_SUCCESS);
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate streaming:
1880Sstevel@tonic-gate 	ASSERT(pci_stream_buf_exists && (pci_stream_buf_enable & 1 << ret));
1890Sstevel@tonic-gate 	sc_p = pci_p->pci_sc_p;
1900Sstevel@tonic-gate 	ret = DDI_FAILURE;
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	if (sync_flag & PCI_DMA_SYNC_EXT)
1930Sstevel@tonic-gate 		goto ext;
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	if (mp->dmai_flags & DMAI_FLAGS_CONTEXT && pci_sc_use_contexts)
1960Sstevel@tonic-gate 		ret = pci_sc_ctx_inv(dip, sc_p, mp);
1970Sstevel@tonic-gate 	if (ret)
1980Sstevel@tonic-gate 		pci_sc_pg_inv(dip, sc_p, mp, off, len);
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	if ((dev_flag & DDI_DMA_READ) &&
2010Sstevel@tonic-gate 	    ((sync_flag & PCI_DMA_SYNC_DDI_FLAGS) != DDI_DMA_SYNC_FORDEV))
2020Sstevel@tonic-gate 		goto wait;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	return (DDI_SUCCESS);
2050Sstevel@tonic-gate ext:
2060Sstevel@tonic-gate 	if (sync_flag & PCI_DMA_SYNC_BEFORE)
2070Sstevel@tonic-gate 		return (DDI_SUCCESS);
2080Sstevel@tonic-gate 	if (sync_flag & PCI_DMA_SYNC_BAR)
2090Sstevel@tonic-gate 		goto wait_check;
2100Sstevel@tonic-gate 	if (sync_flag & PCI_DMA_SYNC_AFTER &&
211*12027SStephen.Hanson@Sun.COM 	    mp->dmai_flags & DMAI_FLAGS_CONTEXT && pci_sc_use_contexts)
2120Sstevel@tonic-gate 		ret = pci_sc_ctx_inv(dip, sc_p, mp);
2130Sstevel@tonic-gate 	if (ret)
2140Sstevel@tonic-gate 		pci_sc_pg_inv(dip, sc_p, mp, off, len);
2150Sstevel@tonic-gate wait_check:
2160Sstevel@tonic-gate 	if (sync_flag & PCI_DMA_SYNC_POST || !(sync_flag & PCI_DMA_SYNC_WRITE))
2170Sstevel@tonic-gate 		return (DDI_SUCCESS);
2180Sstevel@tonic-gate wait:
2190Sstevel@tonic-gate 	pci_dma_sync_flag_wait(mp, sc_p, sync_flag & PCI_DMA_SYNC_PRIVATE);
2200Sstevel@tonic-gate 	return (DDI_SUCCESS);
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate int
pci_dma_handle_clean(dev_info_t * rdip,ddi_dma_handle_t h)2240Sstevel@tonic-gate pci_dma_handle_clean(dev_info_t *rdip, ddi_dma_handle_t h)
2250Sstevel@tonic-gate {
2260Sstevel@tonic-gate 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)h;
2270Sstevel@tonic-gate 	if ((mp->dmai_flags & DMAI_FLAGS_INUSE) == 0)
2280Sstevel@tonic-gate 		return (DDI_FAILURE);
2290Sstevel@tonic-gate 	mp->dmai_rflags |= DMP_NOSYNC;
2300Sstevel@tonic-gate 	mp->dmai_flags |= DMAI_FLAGS_NOSYNC;
2310Sstevel@tonic-gate 	return (DDI_SUCCESS);
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate /*
2350Sstevel@tonic-gate  * pci_dma_allocmp - Allocate a pci dma implementation structure
2360Sstevel@tonic-gate  *
2370Sstevel@tonic-gate  * An extra ddi_dma_attr structure is bundled with the usual ddi_dma_impl
2380Sstevel@tonic-gate  * to hold unmodified device limits. The ddi_dma_attr inside the
2390Sstevel@tonic-gate  * ddi_dma_impl structure is augumented with system limits to enhance
2400Sstevel@tonic-gate  * DVMA performance at runtime. The unaugumented device limits saved
2410Sstevel@tonic-gate  * right after (accessed through the DEV_ATTR macro) is used
2420Sstevel@tonic-gate  * strictly for peer-to-peer transfers which do not obey system limits.
2430Sstevel@tonic-gate  *
2440Sstevel@tonic-gate  * return: DDI_SUCCESS DDI_DMA_NORESOURCES
2450Sstevel@tonic-gate  */
2460Sstevel@tonic-gate ddi_dma_impl_t *
pci_dma_allocmp(dev_info_t * dip,dev_info_t * rdip,int (* waitfp)(caddr_t),caddr_t arg)2470Sstevel@tonic-gate pci_dma_allocmp(dev_info_t *dip, dev_info_t *rdip, int (*waitfp)(caddr_t),
2480Sstevel@tonic-gate 	caddr_t arg)
2490Sstevel@tonic-gate {
2500Sstevel@tonic-gate 	ddi_dma_impl_t *mp;
2510Sstevel@tonic-gate 	int sleep = (waitfp == DDI_DMA_SLEEP) ? KM_SLEEP : KM_NOSLEEP;
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	/* Caution: we don't use zalloc to enhance performance! */
2540Sstevel@tonic-gate 	if ((mp = kmem_alloc(sizeof (pci_dma_hdl_t), sleep)) == 0) {
2550Sstevel@tonic-gate 		DEBUG0(DBG_DMA_MAP, dip, "can't alloc dma_handle\n");
2560Sstevel@tonic-gate 		if (waitfp != DDI_DMA_DONTWAIT) {
2570Sstevel@tonic-gate 			DEBUG0(DBG_DMA_MAP, dip, "alloc_mp kmem cb\n");
2580Sstevel@tonic-gate 			ddi_set_callback(waitfp, arg, &pci_kmem_clid);
2590Sstevel@tonic-gate 		}
2600Sstevel@tonic-gate 		return (mp);
2610Sstevel@tonic-gate 	}
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	mp->dmai_rdip = rdip;
2640Sstevel@tonic-gate 	mp->dmai_flags = 0;
2650Sstevel@tonic-gate 	mp->dmai_pfnlst = NULL;
2660Sstevel@tonic-gate 	mp->dmai_winlst = NULL;
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	/*
2690Sstevel@tonic-gate 	 * kmem_alloc debug: the following fields are not zero-ed
2700Sstevel@tonic-gate 	 * mp->dmai_mapping = 0;
2710Sstevel@tonic-gate 	 * mp->dmai_size = 0;
2720Sstevel@tonic-gate 	 * mp->dmai_offset = 0;
2730Sstevel@tonic-gate 	 * mp->dmai_minxfer = 0;
2740Sstevel@tonic-gate 	 * mp->dmai_burstsizes = 0;
2750Sstevel@tonic-gate 	 * mp->dmai_ndvmapages = 0;
2760Sstevel@tonic-gate 	 * mp->dmai_pool/roffset = 0;
2770Sstevel@tonic-gate 	 * mp->dmai_rflags = 0;
2780Sstevel@tonic-gate 	 * mp->dmai_inuse/flags
2790Sstevel@tonic-gate 	 * mp->dmai_nwin = 0;
2800Sstevel@tonic-gate 	 * mp->dmai_winsize = 0;
2810Sstevel@tonic-gate 	 * mp->dmai_nexus_private/tte = 0;
2820Sstevel@tonic-gate 	 * mp->dmai_iopte/pfnlst
2830Sstevel@tonic-gate 	 * mp->dmai_sbi/pfn0 = 0;
2840Sstevel@tonic-gate 	 * mp->dmai_minfo/winlst/fdvma
2850Sstevel@tonic-gate 	 * mp->dmai_rdip
2860Sstevel@tonic-gate 	 * bzero(&mp->dmai_object, sizeof (ddi_dma_obj_t));
2870Sstevel@tonic-gate 	 * mp->dmai_cookie = 0;
2880Sstevel@tonic-gate 	 */
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	mp->dmai_attr.dma_attr_version = (uint_t)DMA_ATTR_VERSION;
2910Sstevel@tonic-gate 	mp->dmai_attr.dma_attr_flags = (uint_t)0;
2920Sstevel@tonic-gate 	mp->dmai_fault = 0;
2930Sstevel@tonic-gate 	mp->dmai_fault_check = NULL;
2940Sstevel@tonic-gate 	mp->dmai_fault_notify = NULL;
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	mp->dmai_error.err_ena = 0;
2970Sstevel@tonic-gate 	mp->dmai_error.err_status = DDI_FM_OK;
2980Sstevel@tonic-gate 	mp->dmai_error.err_expected = DDI_FM_ERR_UNEXPECTED;
2990Sstevel@tonic-gate 	mp->dmai_error.err_ontrap = NULL;
3000Sstevel@tonic-gate 	mp->dmai_error.err_fep = NULL;
3011865Sdilpreet 	mp->dmai_error.err_cf = NULL;
302*12027SStephen.Hanson@Sun.COM 	ndi_fmc_insert(rdip, DMA_HANDLE, mp, NULL);
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	SYNC_BUF_PA(mp) = 0ull;
3050Sstevel@tonic-gate 	return (mp);
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate void
pci_dma_freemp(ddi_dma_impl_t * mp)3090Sstevel@tonic-gate pci_dma_freemp(ddi_dma_impl_t *mp)
3100Sstevel@tonic-gate {
311*12027SStephen.Hanson@Sun.COM 	ndi_fmc_remove(mp->dmai_rdip, DMA_HANDLE, mp);
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
pci_dma_freepfn(ddi_dma_impl_t * mp)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 *
pci_dma_lmts2hdl(dev_info_t * dip,dev_info_t * rdip,iommu_t * iommu_p,ddi_dma_req_t * dmareq)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,
374*12027SStephen.Hanson@Sun.COM 	    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 |
390*12027SStephen.Hanson@Sun.COM 		    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
pci_dma_attr2hdl(pci_t * pci_p,ddi_dma_impl_t * mp)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",
434*12027SStephen.Hanson@Sun.COM 	    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",
436*12027SStephen.Hanson@Sun.COM 	    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",
438*12027SStephen.Hanson@Sun.COM 	    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",
463*12027SStephen.Hanson@Sun.COM 			    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",
481*12027SStephen.Hanson@Sun.COM 	    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",
487*12027SStephen.Hanson@Sun.COM 			    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 |
496*12027SStephen.Hanson@Sun.COM 		    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
pci_dma_consist_check(uint32_t req_flags,pbm_t * pbm_p)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
pci_dma_type(pci_t * pci_p,ddi_dma_req_t * dmareq,ddi_dma_impl_t * mp)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 &&
578*12027SStephen.Hanson@Sun.COM 		    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)?
581*12027SStephen.Hanson@Sun.COM 			    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,
5852251Selowe 			    IOMMU_PAGE_SIZE - offset, flags, mp, &pfn0,
5862251Selowe 			    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",
613*12027SStephen.Hanson@Sun.COM 		    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",
618*12027SStephen.Hanson@Sun.COM 		    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) ?
631*12027SStephen.Hanson@Sun.COM 	    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
pci_dma_pgpfn(pci_t * pci_p,ddi_dma_impl_t * mp,uint_t npages)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=",
658*12027SStephen.Hanson@Sun.COM 		    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
pci_dma_vapfn(pci_t * pci_p,ddi_dma_req_t * dmareq,ddi_dma_impl_t * mp,uint_t npages)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 &&
710*12027SStephen.Hanson@Sun.COM 	    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,
7232251Selowe 			    IOMMU_PAGE_SIZE, flags, mp, &pfn,
7242251Selowe 			    MP_HAT_CB_COOKIE_PTR(mp, i));
7252251Selowe 
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",
739*12027SStephen.Hanson@Sun.COM 		    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
pci_dma_pfn(pci_t * pci_p,ddi_dma_req_t * dmareq,ddi_dma_impl_t * mp)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",
770*12027SStephen.Hanson@Sun.COM 	    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),
778*12027SStephen.Hanson@Sun.COM 	    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,
781*12027SStephen.Hanson@Sun.COM 			    &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) :
787*12027SStephen.Hanson@Sun.COM 	    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",
795*12027SStephen.Hanson@Sun.COM 			    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,
800*12027SStephen.Hanson@Sun.COM 		    "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
pci_dvma_win(pci_t * pci_p,ddi_dma_req_t * dmareq,ddi_dma_impl_t * mp)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 
853*12027SStephen.Hanson@Sun.COM 	/* include redzone in nocross check */
854*12027SStephen.Hanson@Sun.COM 	{
8550Sstevel@tonic-gate 		uint64_t nocross = mp->dmai_attr.dma_attr_seg;
8560Sstevel@tonic-gate 		if (xfer_sz + pg_off - 1 > nocross)
8570Sstevel@tonic-gate 			xfer_sz = nocross - pg_off + 1;
8580Sstevel@tonic-gate 		if (redzone_sz && (xfer_sz <= redzone_sz)) {
8590Sstevel@tonic-gate 			DEBUG5(DBG_DMA_MAP, pci_p->pci_dip,
8600Sstevel@tonic-gate 			    "nocross too small %lx(%lx)+%lx+%x < %" PRIx64 "\n",
8610Sstevel@tonic-gate 			    xfer_sz, obj_sz, pg_off, redzone_sz, nocross);
8620Sstevel@tonic-gate 			return (DDI_DMA_TOOBIG);
8630Sstevel@tonic-gate 		}
8640Sstevel@tonic-gate 	}
865*12027SStephen.Hanson@Sun.COM 	xfer_sz -= redzone_sz;	/* restore transfer size  */
866*12027SStephen.Hanson@Sun.COM 	/* check counter max */
867*12027SStephen.Hanson@Sun.COM 	{
8680Sstevel@tonic-gate 		uint32_t count_max = mp->dmai_attr.dma_attr_count_max;
8690Sstevel@tonic-gate 		if (xfer_sz - 1 > count_max)
8700Sstevel@tonic-gate 			xfer_sz = count_max + 1;
8710Sstevel@tonic-gate 	}
8720Sstevel@tonic-gate 	if (xfer_sz >= obj_sz) {
8730Sstevel@tonic-gate 		mp->dmai_rflags &= ~DDI_DMA_PARTIAL;
8740Sstevel@tonic-gate 		mp->dmai_size = xfer_sz;
8750Sstevel@tonic-gate 		mp->dmai_winsize = P2ROUNDUP(xfer_sz + pg_off, IOMMU_PAGE_SIZE);
8760Sstevel@tonic-gate 		mp->dmai_nwin = 1;
8770Sstevel@tonic-gate 		goto done;
8780Sstevel@tonic-gate 	}
8790Sstevel@tonic-gate 	if (!(dmareq->dmar_flags & DDI_DMA_PARTIAL)) {
8800Sstevel@tonic-gate 		DEBUG4(DBG_DMA_MAP, pci_p->pci_dip,
8810Sstevel@tonic-gate 		    "too big: %lx+%lx+%x > %lx\n",
8820Sstevel@tonic-gate 		    obj_sz, pg_off, redzone_sz, xfer_sz);
8830Sstevel@tonic-gate 		return (DDI_DMA_TOOBIG);
8840Sstevel@tonic-gate 	}
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 	xfer_sz = IOMMU_PTOB(IOMMU_BTOP(xfer_sz + pg_off)); /* page align */
8870Sstevel@tonic-gate 	mp->dmai_size = xfer_sz - pg_off;	/* 1st window xferrable size */
8880Sstevel@tonic-gate 	mp->dmai_winsize = xfer_sz;		/* redzone not in winsize */
8890Sstevel@tonic-gate 	mp->dmai_nwin = (obj_sz + pg_off + xfer_sz - 1) / xfer_sz;
8900Sstevel@tonic-gate done:
8910Sstevel@tonic-gate 	mp->dmai_winlst = NULL;
8920Sstevel@tonic-gate 	dump_dma_handle(DBG_DMA_MAP, pci_p->pci_dip, mp);
8930Sstevel@tonic-gate 	return (DDI_SUCCESS);
8940Sstevel@tonic-gate }
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate /*
8970Sstevel@tonic-gate  * fast track cache entry to iommu context, inserts 3 0 bits between
8980Sstevel@tonic-gate  * upper 6-bits and lower 3-bits of the 9-bit cache entry
8990Sstevel@tonic-gate  */
9000Sstevel@tonic-gate #define	IOMMU_FCE_TO_CTX(i)	(((i) << 3) | ((i) & 0x7) | 0x38)
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate /*
9030Sstevel@tonic-gate  * pci_dvma_map_fast - attempts to map fast trackable DVMA
9040Sstevel@tonic-gate  */
9050Sstevel@tonic-gate int
pci_dvma_map_fast(iommu_t * iommu_p,ddi_dma_impl_t * mp)9060Sstevel@tonic-gate pci_dvma_map_fast(iommu_t *iommu_p, ddi_dma_impl_t *mp)
9070Sstevel@tonic-gate {
9080Sstevel@tonic-gate 	uint_t clustsz = pci_dvma_page_cache_clustsz;
9090Sstevel@tonic-gate 	uint_t entries = pci_dvma_page_cache_entries;
9100Sstevel@tonic-gate 	uint64_t *tte_addr;
9110Sstevel@tonic-gate 	uint64_t tte = GET_TTE_TEMPLATE(mp);
9120Sstevel@tonic-gate 	int i = iommu_p->iommu_dvma_addr_scan_start;
9130Sstevel@tonic-gate 	uint8_t *lock_addr = iommu_p->iommu_dvma_cache_locks + i;
9140Sstevel@tonic-gate 	iopfn_t *pfn_addr;
9150Sstevel@tonic-gate 	dvma_addr_t dvma_pg;
9160Sstevel@tonic-gate 	size_t npages = IOMMU_BTOP(mp->dmai_winsize);
9170Sstevel@tonic-gate #ifdef DEBUG
9180Sstevel@tonic-gate 	dev_info_t *dip = mp->dmai_rdip;
9190Sstevel@tonic-gate #endif
9200Sstevel@tonic-gate 	extern uint8_t ldstub(uint8_t *);
9210Sstevel@tonic-gate 	ASSERT(IOMMU_PTOB(npages) == mp->dmai_winsize);
9220Sstevel@tonic-gate 	ASSERT(npages + HAS_REDZONE(mp) <= clustsz);
9230Sstevel@tonic-gate 
924*12027SStephen.Hanson@Sun.COM 	for (; i < entries && ldstub(lock_addr); i++, lock_addr++)
925*12027SStephen.Hanson@Sun.COM 		;
9260Sstevel@tonic-gate 	if (i >= entries) {
9270Sstevel@tonic-gate 		lock_addr = iommu_p->iommu_dvma_cache_locks;
9280Sstevel@tonic-gate 		i = 0;
929*12027SStephen.Hanson@Sun.COM 		for (; i < entries && ldstub(lock_addr); i++, lock_addr++)
930*12027SStephen.Hanson@Sun.COM 			;
9310Sstevel@tonic-gate 		if (i >= entries) {
9320Sstevel@tonic-gate #ifdef PCI_DMA_PROF
9330Sstevel@tonic-gate 			pci_dvmaft_exhaust++;
9340Sstevel@tonic-gate #endif
9350Sstevel@tonic-gate 			return (DDI_DMA_NORESOURCES);
9360Sstevel@tonic-gate 		}
9370Sstevel@tonic-gate 	}
9380Sstevel@tonic-gate 	iommu_p->iommu_dvma_addr_scan_start = (i + 1) & (entries - 1);
9390Sstevel@tonic-gate 	if (PCI_DMA_USECTX(mp)) {
9400Sstevel@tonic-gate 		dvma_context_t ctx = IOMMU_FCE_TO_CTX(i);
9410Sstevel@tonic-gate 		tte |= IOMMU_CTX2TTE(ctx);
9420Sstevel@tonic-gate 		mp->dmai_flags |= DMAI_FLAGS_CONTEXT;
9430Sstevel@tonic-gate 		DEBUG1(DBG_DMA_MAP, dip, "fast: ctx=0x%x\n", ctx);
9440Sstevel@tonic-gate 	}
9450Sstevel@tonic-gate 	i *= clustsz;
9460Sstevel@tonic-gate 	tte_addr = iommu_p->iommu_tsb_vaddr + i;
9470Sstevel@tonic-gate 	dvma_pg = iommu_p->dvma_base_pg + i;
9480Sstevel@tonic-gate #ifdef DEBUG
9490Sstevel@tonic-gate 	for (i = 0; i < clustsz; i++)
9500Sstevel@tonic-gate 		ASSERT(TTE_IS_INVALID(tte_addr[i]));
9510Sstevel@tonic-gate #endif
9520Sstevel@tonic-gate 	*tte_addr = tte | IOMMU_PTOB(MP_PFN0(mp)); /* map page 0 */
9530Sstevel@tonic-gate 	DEBUG5(DBG_DMA_MAP, dip, "fast %p:dvma_pg=%x tte0(%p)=%08x.%08x\n", mp,
954*12027SStephen.Hanson@Sun.COM 	    dvma_pg, tte_addr, HI32(*tte_addr), LO32(*tte_addr));
9550Sstevel@tonic-gate 	if (npages == 1)
9560Sstevel@tonic-gate 		goto tte_done;
9570Sstevel@tonic-gate 	pfn_addr = PCI_GET_MP_PFN1_ADDR(mp); /* short iommu_map_pages() */
9580Sstevel@tonic-gate 	for (tte_addr++, i = 1; i < npages; i++, tte_addr++, pfn_addr++) {
9590Sstevel@tonic-gate 		*tte_addr = tte | IOMMU_PTOB(*pfn_addr);
9600Sstevel@tonic-gate 		DEBUG5(DBG_DMA_MAP, dip, "fast %p:tte(%p, %p)=%08x.%08x\n", mp,
961*12027SStephen.Hanson@Sun.COM 		    tte_addr, pfn_addr, HI32(*tte_addr), LO32(*tte_addr));
9620Sstevel@tonic-gate 	}
9630Sstevel@tonic-gate tte_done:
9640Sstevel@tonic-gate #ifdef PCI_DMA_PROF
9650Sstevel@tonic-gate 	pci_dvmaft_success++;
9660Sstevel@tonic-gate #endif
9670Sstevel@tonic-gate 	mp->dmai_mapping = mp->dmai_roffset | IOMMU_PTOB(dvma_pg);
9680Sstevel@tonic-gate 	mp->dmai_offset = 0;
9690Sstevel@tonic-gate 	mp->dmai_flags |= DMAI_FLAGS_FASTTRACK;
9700Sstevel@tonic-gate 	PCI_SAVE_MP_TTE(mp, tte);	/* save TTE template for unmapping */
9710Sstevel@tonic-gate 	if (DVMA_DBG_ON(iommu_p))
9720Sstevel@tonic-gate 		pci_dvma_alloc_debug(iommu_p, (char *)mp->dmai_mapping,
973*12027SStephen.Hanson@Sun.COM 		    mp->dmai_size, mp);
9740Sstevel@tonic-gate 	return (DDI_SUCCESS);
9750Sstevel@tonic-gate }
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate /*
9780Sstevel@tonic-gate  * pci_dvma_map: map non-fasttrack DMA
9790Sstevel@tonic-gate  *		Use quantum cache if single page DMA.
9800Sstevel@tonic-gate  */
9810Sstevel@tonic-gate int
pci_dvma_map(ddi_dma_impl_t * mp,ddi_dma_req_t * dmareq,iommu_t * iommu_p)9820Sstevel@tonic-gate pci_dvma_map(ddi_dma_impl_t *mp, ddi_dma_req_t *dmareq, iommu_t *iommu_p)
9830Sstevel@tonic-gate {
9840Sstevel@tonic-gate 	uint_t npages = PCI_DMA_WINNPGS(mp);
9850Sstevel@tonic-gate 	dvma_addr_t dvma_pg, dvma_pg_index;
9860Sstevel@tonic-gate 	void *dvma_addr;
9870Sstevel@tonic-gate 	uint64_t tte = GET_TTE_TEMPLATE(mp);
9880Sstevel@tonic-gate 	int sleep = dmareq->dmar_fp == DDI_DMA_SLEEP ? VM_SLEEP : VM_NOSLEEP;
9890Sstevel@tonic-gate #ifdef DEBUG
9900Sstevel@tonic-gate 	dev_info_t *dip = mp->dmai_rdip;
9910Sstevel@tonic-gate #endif
9920Sstevel@tonic-gate 	/*
9930Sstevel@tonic-gate 	 * allocate dvma space resource and map in the first window.
9940Sstevel@tonic-gate 	 * (vmem_t *vmp, size_t size,
9950Sstevel@tonic-gate 	 *	size_t align, size_t phase, size_t nocross,
9960Sstevel@tonic-gate 	 *	void *minaddr, void *maxaddr, int vmflag)
9970Sstevel@tonic-gate 	 */
9981501Sgovinda 	if ((npages == 1) && !HAS_REDZONE(mp) && HAS_NOSYSLIMIT(mp)) {
9990Sstevel@tonic-gate 		dvma_addr = vmem_alloc(iommu_p->iommu_dvma_map,
1000*12027SStephen.Hanson@Sun.COM 		    IOMMU_PAGE_SIZE, sleep);
10010Sstevel@tonic-gate 		mp->dmai_flags |= DMAI_FLAGS_VMEMCACHE;
10020Sstevel@tonic-gate #ifdef PCI_DMA_PROF
10030Sstevel@tonic-gate 		pci_dvma_vmem_alloc++;
10040Sstevel@tonic-gate #endif
10050Sstevel@tonic-gate 	} else {
10060Sstevel@tonic-gate 		dvma_addr = vmem_xalloc(iommu_p->iommu_dvma_map,
1007*12027SStephen.Hanson@Sun.COM 		    IOMMU_PTOB(npages + HAS_REDZONE(mp)),
1008*12027SStephen.Hanson@Sun.COM 		    MAX(mp->dmai_attr.dma_attr_align, IOMMU_PAGE_SIZE),
1009*12027SStephen.Hanson@Sun.COM 		    0,
1010*12027SStephen.Hanson@Sun.COM 		    mp->dmai_attr.dma_attr_seg + 1,
1011*12027SStephen.Hanson@Sun.COM 		    (void *)mp->dmai_attr.dma_attr_addr_lo,
1012*12027SStephen.Hanson@Sun.COM 		    (void *)(mp->dmai_attr.dma_attr_addr_hi + 1),
1013*12027SStephen.Hanson@Sun.COM 		    sleep);
10140Sstevel@tonic-gate #ifdef PCI_DMA_PROF
10150Sstevel@tonic-gate 		pci_dvma_vmem_xalloc++;
10160Sstevel@tonic-gate #endif
10170Sstevel@tonic-gate 	}
10180Sstevel@tonic-gate 	dvma_pg = IOMMU_BTOP((ulong_t)dvma_addr);
10190Sstevel@tonic-gate 	dvma_pg_index = dvma_pg - iommu_p->dvma_base_pg;
10200Sstevel@tonic-gate 	DEBUG2(DBG_DMA_MAP, dip, "fallback dvma_pages: dvma_pg=%x index=%x\n",
1021*12027SStephen.Hanson@Sun.COM 	    dvma_pg, dvma_pg_index);
10220Sstevel@tonic-gate 	if (dvma_pg == 0)
10230Sstevel@tonic-gate 		goto noresource;
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 	/* allocate DVMA context */
10260Sstevel@tonic-gate 	if ((npages >= pci_context_minpages) && PCI_DMA_USECTX(mp)) {
10270Sstevel@tonic-gate 		dvma_context_t ctx;
10280Sstevel@tonic-gate 		if (ctx = pci_iommu_get_dvma_context(iommu_p, dvma_pg_index)) {
10290Sstevel@tonic-gate 			tte |= IOMMU_CTX2TTE(ctx);
10300Sstevel@tonic-gate 			mp->dmai_flags |= DMAI_FLAGS_CONTEXT;
10310Sstevel@tonic-gate 		}
10320Sstevel@tonic-gate 	}
10330Sstevel@tonic-gate 	mp->dmai_mapping = mp->dmai_roffset | IOMMU_PTOB(dvma_pg);
10340Sstevel@tonic-gate 	mp->dmai_offset = 0;
10350Sstevel@tonic-gate 	PCI_SAVE_MP_TTE(mp, tte);	/* mp->dmai_tte = tte */
10360Sstevel@tonic-gate 	iommu_map_pages(iommu_p, mp, dvma_pg, npages, 0);
10370Sstevel@tonic-gate 	return (DDI_SUCCESS);
10380Sstevel@tonic-gate noresource:
10390Sstevel@tonic-gate 	if (dmareq->dmar_fp != DDI_DMA_DONTWAIT) {
10400Sstevel@tonic-gate 		DEBUG0(DBG_DMA_MAP, dip, "dvma_pg 0 - set callback\n");
10410Sstevel@tonic-gate 		ddi_set_callback(dmareq->dmar_fp, dmareq->dmar_arg,
1042*12027SStephen.Hanson@Sun.COM 		    &iommu_p->iommu_dvma_clid);
10430Sstevel@tonic-gate 	}
10440Sstevel@tonic-gate 	DEBUG0(DBG_DMA_MAP, dip, "vmem_xalloc - DDI_DMA_NORESOURCES\n");
10450Sstevel@tonic-gate 	return (DDI_DMA_NORESOURCES);
10460Sstevel@tonic-gate }
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate void
pci_dvma_unmap(iommu_t * iommu_p,ddi_dma_impl_t * mp)10490Sstevel@tonic-gate pci_dvma_unmap(iommu_t *iommu_p, ddi_dma_impl_t *mp)
10500Sstevel@tonic-gate {
10510Sstevel@tonic-gate 	size_t npages;
10520Sstevel@tonic-gate 	dvma_addr_t dvma_addr = (dvma_addr_t)mp->dmai_mapping;
10530Sstevel@tonic-gate 	dvma_addr_t dvma_pg = IOMMU_BTOP(dvma_addr);
10540Sstevel@tonic-gate 	dvma_addr = IOMMU_PTOB(dvma_pg);
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 	if (mp->dmai_flags & DMAI_FLAGS_FASTTRACK) {
10570Sstevel@tonic-gate 		iopfn_t index = dvma_pg - iommu_p->dvma_base_pg;
10580Sstevel@tonic-gate 		ASSERT(index % pci_dvma_page_cache_clustsz == 0);
10590Sstevel@tonic-gate 		index /= pci_dvma_page_cache_clustsz;
10600Sstevel@tonic-gate 		ASSERT(index < pci_dvma_page_cache_entries);
10610Sstevel@tonic-gate 		iommu_p->iommu_dvma_cache_locks[index] = 0;
10620Sstevel@tonic-gate #ifdef PCI_DMA_PROF
10630Sstevel@tonic-gate 		pci_dvmaft_free++;
10640Sstevel@tonic-gate #endif
10650Sstevel@tonic-gate 		return;
10660Sstevel@tonic-gate 	}
10670Sstevel@tonic-gate 	npages = IOMMU_BTOP(mp->dmai_winsize) + HAS_REDZONE(mp);
10680Sstevel@tonic-gate 	pci_vmem_free(iommu_p, mp, (void *)dvma_addr, npages);
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate 	if (mp->dmai_flags & DMAI_FLAGS_CONTEXT)
10710Sstevel@tonic-gate 		pci_iommu_free_dvma_context(iommu_p, MP2CTX(mp));
10720Sstevel@tonic-gate }
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate void
pci_dma_sync_unmap(dev_info_t * dip,dev_info_t * rdip,ddi_dma_impl_t * mp)10750Sstevel@tonic-gate pci_dma_sync_unmap(dev_info_t *dip, dev_info_t *rdip, ddi_dma_impl_t *mp)
10760Sstevel@tonic-gate {
10770Sstevel@tonic-gate 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
10780Sstevel@tonic-gate 	iommu_t *iommu_p = pci_p->pci_iommu_p;
10790Sstevel@tonic-gate 	uint64_t sync_buf_save = SYNC_BUF_PA(mp);
10800Sstevel@tonic-gate 	uint32_t fast_track = mp->dmai_flags & DMAI_FLAGS_FASTTRACK;
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 	if (fast_track) {
10830Sstevel@tonic-gate 		dvma_addr_t dvma_pg = IOMMU_BTOP(mp->dmai_mapping);
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 		SYNC_BUF_PA(mp) = IOMMU_PAGE_TTEPA(iommu_p, dvma_pg);
10860Sstevel@tonic-gate 		ASSERT(!(SYNC_BUF_PA(mp) & PCI_SYNC_FLAG_SIZE - 1));
10870Sstevel@tonic-gate 	}
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate 	if (pci_dvma_sync_before_unmap) {
10903141Ssuha 		pci_dma_sync(dip, rdip, (ddi_dma_handle_t)mp, 0, 0,
10913141Ssuha 		    DDI_DMA_SYNC_FORCPU);
10920Sstevel@tonic-gate 		iommu_unmap_window(iommu_p, mp);
10930Sstevel@tonic-gate 	} else {
10940Sstevel@tonic-gate 		iommu_unmap_window(iommu_p, mp);
10953141Ssuha 		pci_dma_sync(dip, rdip, (ddi_dma_handle_t)mp, 0, 0,
10963141Ssuha 		    DDI_DMA_SYNC_FORCPU);
10970Sstevel@tonic-gate 	}
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 	if (fast_track)
11000Sstevel@tonic-gate 		SYNC_BUF_PA(mp) = sync_buf_save;
11010Sstevel@tonic-gate }
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate /*
11040Sstevel@tonic-gate  * DVMA mappings may have multiple windows, but each window always have
11050Sstevel@tonic-gate  * one segment.
11060Sstevel@tonic-gate  */
11070Sstevel@tonic-gate int
pci_dvma_ctl(dev_info_t * dip,dev_info_t * rdip,ddi_dma_impl_t * mp,enum ddi_dma_ctlops cmd,off_t * offp,size_t * lenp,caddr_t * objp,uint_t cache_flags)11080Sstevel@tonic-gate pci_dvma_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_impl_t *mp,
11090Sstevel@tonic-gate 	enum ddi_dma_ctlops cmd, off_t *offp, size_t *lenp, caddr_t *objp,
11100Sstevel@tonic-gate 	uint_t cache_flags)
11110Sstevel@tonic-gate {
11120Sstevel@tonic-gate 	switch (cmd) {
11130Sstevel@tonic-gate 	case DDI_DMA_SYNC:
11140Sstevel@tonic-gate 		return (pci_dma_sync(dip, rdip, (ddi_dma_handle_t)mp,
11150Sstevel@tonic-gate 		    *offp, *lenp, cache_flags));
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 	case DDI_DMA_HTOC: {
11180Sstevel@tonic-gate 		int ret;
11190Sstevel@tonic-gate 		off_t wo_off, off = *offp;	/* wo_off: wnd's obj offset */
11200Sstevel@tonic-gate 		uint_t win_size = mp->dmai_winsize;
11210Sstevel@tonic-gate 		ddi_dma_cookie_t *cp = (ddi_dma_cookie_t *)objp;
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate 		if (off >= mp->dmai_object.dmao_size) {
11240Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d invalid dma_htoc offset %lx",
1125*12027SStephen.Hanson@Sun.COM 			    NAMEINST(mp->dmai_rdip), off);
11260Sstevel@tonic-gate 			return (DDI_FAILURE);
11270Sstevel@tonic-gate 		}
11280Sstevel@tonic-gate 		off += mp->dmai_roffset;
11290Sstevel@tonic-gate 		ret = pci_dma_win(dip, rdip, (ddi_dma_handle_t)mp,
11300Sstevel@tonic-gate 		    off / win_size, &wo_off, NULL, cp, NULL); /* lenp == NULL */
11310Sstevel@tonic-gate 		if (ret)
11320Sstevel@tonic-gate 			return (ret);
11330Sstevel@tonic-gate 		DEBUG4(DBG_DMA_CTL, dip, "HTOC:cookie=%x+%lx off=%lx,%lx\n",
1134*12027SStephen.Hanson@Sun.COM 		    cp->dmac_address, cp->dmac_size, off, *offp);
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate 		/* adjust cookie addr/len if we are not on window boundary */
11370Sstevel@tonic-gate 		ASSERT((off % win_size) == (off -
1138*12027SStephen.Hanson@Sun.COM 		    (PCI_DMA_CURWIN(mp) ? mp->dmai_roffset : 0) - wo_off));
11390Sstevel@tonic-gate 		off = PCI_DMA_CURWIN(mp) ? off % win_size : *offp;
11400Sstevel@tonic-gate 		ASSERT(cp->dmac_size > off);
11410Sstevel@tonic-gate 		cp->dmac_laddress += off;
11420Sstevel@tonic-gate 		cp->dmac_size -= off;
11430Sstevel@tonic-gate 		DEBUG5(DBG_DMA_CTL, dip,
1144*12027SStephen.Hanson@Sun.COM 		    "HTOC:mp=%p cookie=%x+%lx off=%lx,%lx\n",
1145*12027SStephen.Hanson@Sun.COM 		    mp, cp->dmac_address, cp->dmac_size, off, wo_off);
11460Sstevel@tonic-gate 		}
11470Sstevel@tonic-gate 		return (DDI_SUCCESS);
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate 	case DDI_DMA_REPWIN:
11500Sstevel@tonic-gate 		*offp = mp->dmai_offset;
11510Sstevel@tonic-gate 		*lenp = mp->dmai_size;
11520Sstevel@tonic-gate 		return (DDI_SUCCESS);
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 	case DDI_DMA_MOVWIN: {
11550Sstevel@tonic-gate 		off_t off = *offp;
11560Sstevel@tonic-gate 		if (off >= mp->dmai_object.dmao_size)
11570Sstevel@tonic-gate 			return (DDI_FAILURE);
11580Sstevel@tonic-gate 		off += mp->dmai_roffset;
11590Sstevel@tonic-gate 		return (pci_dma_win(dip, rdip, (ddi_dma_handle_t)mp,
11600Sstevel@tonic-gate 		    off / mp->dmai_winsize, offp, lenp,
11610Sstevel@tonic-gate 		    (ddi_dma_cookie_t *)objp, NULL));
11620Sstevel@tonic-gate 		}
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate 	case DDI_DMA_NEXTWIN: {
11650Sstevel@tonic-gate 		window_t win = PCI_DMA_CURWIN(mp);
11660Sstevel@tonic-gate 		if (offp) {
11670Sstevel@tonic-gate 			if (*(window_t *)offp != win) {  /* window not active */
11680Sstevel@tonic-gate 				*(window_t *)objp = win; /* return cur win */
11690Sstevel@tonic-gate 				return (DDI_DMA_STALE);
11700Sstevel@tonic-gate 			}
11710Sstevel@tonic-gate 			win++;
11720Sstevel@tonic-gate 		} else	/* map win 0 */
11730Sstevel@tonic-gate 			win = 0;
11740Sstevel@tonic-gate 		if (win >= mp->dmai_nwin) {
11750Sstevel@tonic-gate 			*(window_t *)objp = win - 1;
11760Sstevel@tonic-gate 			return (DDI_DMA_DONE);
11770Sstevel@tonic-gate 		}
11780Sstevel@tonic-gate 		if (pci_dma_win(dip, rdip, (ddi_dma_handle_t)mp,
11790Sstevel@tonic-gate 		    win, 0, 0, 0, 0)) {
11800Sstevel@tonic-gate 			*(window_t *)objp = win - 1;
11810Sstevel@tonic-gate 			return (DDI_FAILURE);
11820Sstevel@tonic-gate 		}
11830Sstevel@tonic-gate 		*(window_t *)objp = win;
11840Sstevel@tonic-gate 		}
11850Sstevel@tonic-gate 		return (DDI_SUCCESS);
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 	case DDI_DMA_NEXTSEG:
11880Sstevel@tonic-gate 		if (*(window_t *)offp != PCI_DMA_CURWIN(mp))
11890Sstevel@tonic-gate 			return (DDI_DMA_STALE);
11900Sstevel@tonic-gate 		if (lenp)				/* only 1 seg allowed */
11910Sstevel@tonic-gate 			return (DDI_DMA_DONE);
11920Sstevel@tonic-gate 							/* return mp as seg 0 */
11930Sstevel@tonic-gate 		*(ddi_dma_seg_t *)objp = (ddi_dma_seg_t)mp;
11940Sstevel@tonic-gate 		return (DDI_SUCCESS);
11950Sstevel@tonic-gate 
11960Sstevel@tonic-gate 	case DDI_DMA_SEGTOC:
11970Sstevel@tonic-gate 		MAKE_DMA_COOKIE((ddi_dma_cookie_t *)objp, mp->dmai_mapping,
1198*12027SStephen.Hanson@Sun.COM 		    mp->dmai_size);
11990Sstevel@tonic-gate 		*offp = mp->dmai_offset;
12000Sstevel@tonic-gate 		*lenp = mp->dmai_size;
12010Sstevel@tonic-gate 		return (DDI_SUCCESS);
12020Sstevel@tonic-gate 
12030Sstevel@tonic-gate 	case DDI_DMA_COFF: {
12040Sstevel@tonic-gate 		ddi_dma_cookie_t *cp = (ddi_dma_cookie_t *)offp;
12050Sstevel@tonic-gate 		if (cp->dmac_address < mp->dmai_mapping ||
12060Sstevel@tonic-gate 		    (cp->dmac_address + cp->dmac_size) >
12070Sstevel@tonic-gate 		    (mp->dmai_mapping + mp->dmai_size))
12080Sstevel@tonic-gate 			return (DDI_FAILURE);
12090Sstevel@tonic-gate 		*objp = (caddr_t)(cp->dmac_address - mp->dmai_mapping +
1210*12027SStephen.Hanson@Sun.COM 		    mp->dmai_offset);
12110Sstevel@tonic-gate 		}
12120Sstevel@tonic-gate 		return (DDI_SUCCESS);
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate 	case DDI_DMA_REMAP:
12150Sstevel@tonic-gate 		if (pci_dvma_remap_enabled)
12160Sstevel@tonic-gate 			return (pci_dvma_remap(dip, rdip, mp, *offp, *lenp));
12170Sstevel@tonic-gate 		return (DDI_FAILURE);
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate 	default:
12200Sstevel@tonic-gate 		DEBUG3(DBG_DMA_CTL, dip, "unknown command (%x): rdip=%s%d\n",
1221*12027SStephen.Hanson@Sun.COM 		    cmd, ddi_driver_name(rdip), ddi_get_instance(rdip));
12220Sstevel@tonic-gate 		break;
12230Sstevel@tonic-gate 	}
12240Sstevel@tonic-gate 	return (DDI_FAILURE);
12250Sstevel@tonic-gate }
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate void
pci_dma_freewin(ddi_dma_impl_t * mp)12280Sstevel@tonic-gate pci_dma_freewin(ddi_dma_impl_t *mp)
12290Sstevel@tonic-gate {
12300Sstevel@tonic-gate 	pci_dma_win_t *win_p = mp->dmai_winlst, *win2_p;
12310Sstevel@tonic-gate 	for (win2_p = win_p; win_p; win2_p = win_p) {
12320Sstevel@tonic-gate 		win_p = win2_p->win_next;
12330Sstevel@tonic-gate 		kmem_free(win2_p, sizeof (pci_dma_win_t) +
1234*12027SStephen.Hanson@Sun.COM 		    sizeof (ddi_dma_cookie_t) * win2_p->win_ncookies);
12350Sstevel@tonic-gate 	}
12360Sstevel@tonic-gate 	mp->dmai_nwin = 0;
12370Sstevel@tonic-gate 	mp->dmai_winlst = NULL;
12380Sstevel@tonic-gate }
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate /*
12410Sstevel@tonic-gate  * pci_dma_newwin - create a dma window object and cookies
12420Sstevel@tonic-gate  *
12430Sstevel@tonic-gate  *	After the initial scan in pci_dma_physwin(), which identifies
12440Sstevel@tonic-gate  *	a portion of the pfn array that belongs to a dma window,
12450Sstevel@tonic-gate  *	we are called to allocate and initialize representing memory
12460Sstevel@tonic-gate  *	resources. We know from the 1st scan the number of cookies
12470Sstevel@tonic-gate  *	or dma segment in this window so we can allocate a contiguous
12480Sstevel@tonic-gate  *	memory array for the dma cookies (The implementation of
12490Sstevel@tonic-gate  *	ddi_dma_nextcookie(9f) dictates dma cookies be contiguous).
12500Sstevel@tonic-gate  *
12510Sstevel@tonic-gate  *	A second round scan is done on the pfn array to identify
12520Sstevel@tonic-gate  *	each dma segment and initialize its corresponding dma cookie.
12530Sstevel@tonic-gate  *	We don't need to do all the safety checking and we know they
12540Sstevel@tonic-gate  *	all belong to the same dma window.
12550Sstevel@tonic-gate  *
12560Sstevel@tonic-gate  *	Input:	cookie_no - # of cookies identified by the 1st scan
12570Sstevel@tonic-gate  *		start_idx - subscript of the pfn array for the starting pfn
12580Sstevel@tonic-gate  *		end_idx   - subscript of the last pfn in dma window
12590Sstevel@tonic-gate  *		win_pp    - pointer to win_next member of previous window
12600Sstevel@tonic-gate  *	Return:	DDI_SUCCESS - with **win_pp as newly created window object
12610Sstevel@tonic-gate  *		DDI_DMA_NORESROUCE - caller frees all previous window objs
12620Sstevel@tonic-gate  *	Note:	Each cookie and window size are all initialized on page
12630Sstevel@tonic-gate  *		boundary. This is not true for the 1st cookie of the 1st
12640Sstevel@tonic-gate  *		window and the last cookie of the last window.
12650Sstevel@tonic-gate  *		We fix that later in upper layer which has access to size
12660Sstevel@tonic-gate  *		and offset info.
12670Sstevel@tonic-gate  *
12680Sstevel@tonic-gate  */
12690Sstevel@tonic-gate static int
pci_dma_newwin(ddi_dma_req_t * dmareq,ddi_dma_impl_t * mp,uint32_t cookie_no,uint32_t start_idx,uint32_t end_idx,pci_dma_win_t ** win_pp,uint64_t count_max,uint64_t bypass_prefix)12700Sstevel@tonic-gate pci_dma_newwin(ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp, uint32_t cookie_no,
12710Sstevel@tonic-gate 	uint32_t start_idx, uint32_t end_idx, pci_dma_win_t **win_pp,
12720Sstevel@tonic-gate 	uint64_t count_max, uint64_t bypass_prefix)
12730Sstevel@tonic-gate {
12740Sstevel@tonic-gate 	int (*waitfp)(caddr_t) = dmareq->dmar_fp;
12750Sstevel@tonic-gate 	ddi_dma_cookie_t *cookie_p;
12760Sstevel@tonic-gate 	uint32_t pfn_no = 1;
12770Sstevel@tonic-gate 	iopfn_t pfn = PCI_GET_MP_PFN(mp, start_idx);
12780Sstevel@tonic-gate 	iopfn_t prev_pfn = pfn;
12790Sstevel@tonic-gate 	uint64_t seg_pfn0 = pfn;
12800Sstevel@tonic-gate 	size_t sz = cookie_no * sizeof (ddi_dma_cookie_t);
12810Sstevel@tonic-gate 	pci_dma_win_t *win_p = kmem_alloc(sizeof (pci_dma_win_t) + sz,
1282*12027SStephen.Hanson@Sun.COM 	    waitfp == DDI_DMA_SLEEP ? KM_SLEEP : KM_NOSLEEP);
12830Sstevel@tonic-gate 	if (!win_p)
12840Sstevel@tonic-gate 		goto noresource;
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate 	win_p->win_next = NULL;
12870Sstevel@tonic-gate 	win_p->win_ncookies = cookie_no;
12880Sstevel@tonic-gate 	win_p->win_curseg = 0;	/* start from segment 0 */
12890Sstevel@tonic-gate 	win_p->win_size = IOMMU_PTOB(end_idx - start_idx + 1);
12900Sstevel@tonic-gate 	/* win_p->win_offset is left uninitialized */
12910Sstevel@tonic-gate 
12920Sstevel@tonic-gate 	cookie_p = (ddi_dma_cookie_t *)(win_p + 1);
12930Sstevel@tonic-gate 	start_idx++;
12940Sstevel@tonic-gate 	for (; start_idx <= end_idx; start_idx++, prev_pfn = pfn, pfn_no++) {
12950Sstevel@tonic-gate 		pfn = PCI_GET_MP_PFN1(mp, start_idx);
12960Sstevel@tonic-gate 		if ((pfn == prev_pfn + 1) &&
1297*12027SStephen.Hanson@Sun.COM 		    (IOMMU_PTOB(pfn_no + 1) - 1 <= count_max))
12980Sstevel@tonic-gate 			continue;
12990Sstevel@tonic-gate 
13000Sstevel@tonic-gate 		/* close up the cookie up to (including) prev_pfn */
13010Sstevel@tonic-gate 		MAKE_DMA_COOKIE(cookie_p, IOMMU_PTOB(seg_pfn0) | bypass_prefix,
1302*12027SStephen.Hanson@Sun.COM 		    IOMMU_PTOB(pfn_no));
13030Sstevel@tonic-gate 		DEBUG2(DBG_BYPASS, mp->dmai_rdip, "cookie %p (%x pages)\n",
1304*12027SStephen.Hanson@Sun.COM 		    IOMMU_PTOB(seg_pfn0) | bypass_prefix, pfn_no);
13050Sstevel@tonic-gate 
13060Sstevel@tonic-gate 		cookie_p++;	/* advance to next available cookie cell */
13070Sstevel@tonic-gate 		pfn_no = 0;
13080Sstevel@tonic-gate 		seg_pfn0 = pfn;	/* start a new segment from current pfn */
13090Sstevel@tonic-gate 	}
13100Sstevel@tonic-gate 	MAKE_DMA_COOKIE(cookie_p, IOMMU_PTOB(seg_pfn0) | bypass_prefix,
1311*12027SStephen.Hanson@Sun.COM 	    IOMMU_PTOB(pfn_no));
13120Sstevel@tonic-gate 	DEBUG3(DBG_BYPASS, mp->dmai_rdip, "cookie %p (%x pages) of total %x\n",
1313*12027SStephen.Hanson@Sun.COM 	    IOMMU_PTOB(seg_pfn0) | bypass_prefix, pfn_no, cookie_no);
13140Sstevel@tonic-gate #ifdef DEBUG
13150Sstevel@tonic-gate 	cookie_p++;
13160Sstevel@tonic-gate 	ASSERT((cookie_p - (ddi_dma_cookie_t *)(win_p + 1)) == cookie_no);
13170Sstevel@tonic-gate #endif
13180Sstevel@tonic-gate 	*win_pp = win_p;
13190Sstevel@tonic-gate 	return (DDI_SUCCESS);
13200Sstevel@tonic-gate noresource:
13210Sstevel@tonic-gate 	if (waitfp != DDI_DMA_DONTWAIT)
13220Sstevel@tonic-gate 		ddi_set_callback(waitfp, dmareq->dmar_arg, &pci_kmem_clid);
13230Sstevel@tonic-gate 	return (DDI_DMA_NORESOURCES);
13240Sstevel@tonic-gate }
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate /*
13270Sstevel@tonic-gate  * pci_dma_adjust - adjust 1st and last cookie and window sizes
13280Sstevel@tonic-gate  *	remove initial dma page offset from 1st cookie and window size
13290Sstevel@tonic-gate  *	remove last dma page remainder from last cookie and window size
13300Sstevel@tonic-gate  *	fill win_offset of each dma window according to just fixed up
13310Sstevel@tonic-gate  *		each window sizes
13320Sstevel@tonic-gate  *	pci_dma_win_t members modified:
13330Sstevel@tonic-gate  *	win_p->win_offset - this window's offset within entire DMA object
13340Sstevel@tonic-gate  *	win_p->win_size	  - xferrable size (in bytes) for this window
13350Sstevel@tonic-gate  *
13360Sstevel@tonic-gate  *	ddi_dma_impl_t members modified:
13370Sstevel@tonic-gate  *	mp->dmai_size	  - 1st window xferrable size
13380Sstevel@tonic-gate  *	mp->dmai_offset   - 0, which is the dma offset of the 1st window
13390Sstevel@tonic-gate  *
13400Sstevel@tonic-gate  *	ddi_dma_cookie_t members modified:
13410Sstevel@tonic-gate  *	cookie_p->dmac_size - 1st and last cookie remove offset or remainder
13420Sstevel@tonic-gate  *	cookie_p->dmac_laddress - 1st cookie add page offset
13430Sstevel@tonic-gate  */
13440Sstevel@tonic-gate static void
pci_dma_adjust(ddi_dma_req_t * dmareq,ddi_dma_impl_t * mp,pci_dma_win_t * win_p)13450Sstevel@tonic-gate pci_dma_adjust(ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp, pci_dma_win_t *win_p)
13460Sstevel@tonic-gate {
13470Sstevel@tonic-gate 	ddi_dma_cookie_t *cookie_p = (ddi_dma_cookie_t *)(win_p + 1);
13480Sstevel@tonic-gate 	size_t pg_offset = mp->dmai_roffset;
13490Sstevel@tonic-gate 	size_t win_offset = 0;
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate 	cookie_p->dmac_size -= pg_offset;
13520Sstevel@tonic-gate 	cookie_p->dmac_laddress |= pg_offset;
13530Sstevel@tonic-gate 	win_p->win_size -= pg_offset;
13540Sstevel@tonic-gate 	DEBUG1(DBG_BYPASS, mp->dmai_rdip, "pg0 adjust %lx\n", pg_offset);
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate 	mp->dmai_size = win_p->win_size;
13570Sstevel@tonic-gate 	mp->dmai_offset = 0;
13580Sstevel@tonic-gate 
13590Sstevel@tonic-gate 	pg_offset += mp->dmai_object.dmao_size;
13600Sstevel@tonic-gate 	pg_offset &= IOMMU_PAGE_OFFSET;
13610Sstevel@tonic-gate 	if (pg_offset)
13620Sstevel@tonic-gate 		pg_offset = IOMMU_PAGE_SIZE - pg_offset;
13630Sstevel@tonic-gate 	DEBUG1(DBG_BYPASS, mp->dmai_rdip, "last pg adjust %lx\n", pg_offset);
13640Sstevel@tonic-gate 
13650Sstevel@tonic-gate 	for (; win_p->win_next; win_p = win_p->win_next) {
13660Sstevel@tonic-gate 		DEBUG1(DBG_BYPASS, mp->dmai_rdip, "win off %p\n", win_offset);
13670Sstevel@tonic-gate 		win_p->win_offset = win_offset;
13680Sstevel@tonic-gate 		win_offset += win_p->win_size;
13690Sstevel@tonic-gate 	}
13700Sstevel@tonic-gate 	/* last window */
13710Sstevel@tonic-gate 	win_p->win_offset = win_offset;
13720Sstevel@tonic-gate 	cookie_p = (ddi_dma_cookie_t *)(win_p + 1);
13730Sstevel@tonic-gate 	cookie_p[win_p->win_ncookies - 1].dmac_size -= pg_offset;
13740Sstevel@tonic-gate 	win_p->win_size -= pg_offset;
13750Sstevel@tonic-gate 	ASSERT((win_offset + win_p->win_size) == mp->dmai_object.dmao_size);
13760Sstevel@tonic-gate }
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate /*
13790Sstevel@tonic-gate  * pci_dma_physwin() - carve up dma windows using physical addresses.
13800Sstevel@tonic-gate  *	Called to handle iommu bypass and pci peer-to-peer transfers.
13810Sstevel@tonic-gate  *	Calls pci_dma_newwin() to allocate window objects.
13820Sstevel@tonic-gate  *
13830Sstevel@tonic-gate  * Dependency: mp->dmai_pfnlst points to an array of pfns
13840Sstevel@tonic-gate  *
13850Sstevel@tonic-gate  * 1. Each dma window is represented by a pci_dma_win_t object.
13860Sstevel@tonic-gate  *	The object will be casted to ddi_dma_win_t and returned
13870Sstevel@tonic-gate  *	to leaf driver through the DDI interface.
13880Sstevel@tonic-gate  * 2. Each dma window can have several dma segments with each
13890Sstevel@tonic-gate  *	segment representing a physically contiguous either memory
13900Sstevel@tonic-gate  *	space (if we are doing an iommu bypass transfer) or pci address
13910Sstevel@tonic-gate  *	space (if we are doing a peer-to-peer transfer).
13920Sstevel@tonic-gate  * 3. Each segment has a DMA cookie to program the DMA engine.
13930Sstevel@tonic-gate  *	The cookies within each DMA window must be located in a
13940Sstevel@tonic-gate  *	contiguous array per ddi_dma_nextcookie(9f).
13950Sstevel@tonic-gate  * 4. The number of DMA segments within each DMA window cannot exceed
13960Sstevel@tonic-gate  *	mp->dmai_attr.dma_attr_sgllen. If the transfer size is
13970Sstevel@tonic-gate  *	too large to fit in the sgllen, the rest needs to be
13980Sstevel@tonic-gate  *	relocated to the next dma window.
13990Sstevel@tonic-gate  * 5. Peer-to-peer DMA segment follows device hi, lo, count_max,
14000Sstevel@tonic-gate  *	and nocross restrictions while bypass DMA follows the set of
14010Sstevel@tonic-gate  *	restrictions with system limits factored in.
14020Sstevel@tonic-gate  *
14030Sstevel@tonic-gate  * Return:
14040Sstevel@tonic-gate  *	mp->dmai_winlst	 - points to a link list of pci_dma_win_t objects.
14050Sstevel@tonic-gate  *		Each pci_dma_win_t object on the link list contains
14060Sstevel@tonic-gate  *		infomation such as its window size (# of pages),
14070Sstevel@tonic-gate  *		starting offset (also see Restriction), an array of
14080Sstevel@tonic-gate  *		DMA cookies, and # of cookies in the array.
14090Sstevel@tonic-gate  *	mp->dmai_pfnlst	 - NULL, the pfn list is freed to conserve memory.
14100Sstevel@tonic-gate  *	mp->dmai_nwin	 - # of total DMA windows on mp->dmai_winlst.
14110Sstevel@tonic-gate  *	mp->dmai_mapping - starting cookie address
14120Sstevel@tonic-gate  *	mp->dmai_rflags	 - consistent, nosync, no redzone
14130Sstevel@tonic-gate  *	mp->dmai_cookie	 - start of cookie table of the 1st DMA window
14140Sstevel@tonic-gate  *
14150Sstevel@tonic-gate  * Restriction:
14160Sstevel@tonic-gate  *	Each pci_dma_win_t object can theoratically start from any offset
14170Sstevel@tonic-gate  *	since the iommu is not involved. However, this implementation
14180Sstevel@tonic-gate  *	always make windows start from page aligned offset (except
14190Sstevel@tonic-gate  *	the 1st window, which follows the requested offset) due to the
14200Sstevel@tonic-gate  *	fact that we are handed a pfn list. This does require device's
14210Sstevel@tonic-gate  *	count_max and attr_seg to be at least IOMMU_PAGE_SIZE aligned.
14220Sstevel@tonic-gate  */
14230Sstevel@tonic-gate int
pci_dma_physwin(pci_t * pci_p,ddi_dma_req_t * dmareq,ddi_dma_impl_t * mp)14240Sstevel@tonic-gate pci_dma_physwin(pci_t *pci_p, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp)
14250Sstevel@tonic-gate {
14260Sstevel@tonic-gate 	uint_t npages = mp->dmai_ndvmapages;
14270Sstevel@tonic-gate 	int ret, sgllen = mp->dmai_attr.dma_attr_sgllen;
14280Sstevel@tonic-gate 	iopfn_t pfn_lo, pfn_hi, prev_pfn, bypass_pfn;
14290Sstevel@tonic-gate 	iopfn_t pfn = PCI_GET_MP_PFN(mp, 0);
14300Sstevel@tonic-gate 	uint32_t i, win_no = 0, pfn_no = 1, win_pfn0_index = 0, cookie_no = 0;
14310Sstevel@tonic-gate 	uint64_t count_max, bypass = PCI_DMA_BYPASS_PREFIX(mp, pfn);
14320Sstevel@tonic-gate 	pci_dma_win_t **win_pp = (pci_dma_win_t **)&mp->dmai_winlst;
14330Sstevel@tonic-gate 	ddi_dma_cookie_t *cookie0_p;
14340Sstevel@tonic-gate 
14350Sstevel@tonic-gate 	if (PCI_DMA_ISPTP(mp)) { /* ignore sys limits for peer-to-peer */
14360Sstevel@tonic-gate 		ddi_dma_attr_t *dev_attr_p = DEV_ATTR(mp);
14370Sstevel@tonic-gate 		iopfn_t pfn_base = pci_p->pci_pbm_p->pbm_base_pfn;
14380Sstevel@tonic-gate 		iopfn_t pfn_last = pci_p->pci_pbm_p->pbm_last_pfn - pfn_base;
14390Sstevel@tonic-gate 		uint64_t nocross = dev_attr_p->dma_attr_seg;
14400Sstevel@tonic-gate 		if (nocross && (nocross < UINT32_MAX))
14410Sstevel@tonic-gate 			return (DDI_DMA_NOMAPPING);
14420Sstevel@tonic-gate 		if (dev_attr_p->dma_attr_align > IOMMU_PAGE_SIZE)
14430Sstevel@tonic-gate 			return (DDI_DMA_NOMAPPING);
14440Sstevel@tonic-gate 		pfn_lo = IOMMU_BTOP(dev_attr_p->dma_attr_addr_lo);
14450Sstevel@tonic-gate 		pfn_hi = IOMMU_BTOP(dev_attr_p->dma_attr_addr_hi);
14460Sstevel@tonic-gate 		pfn_hi = MIN(pfn_hi, pfn_last);
14470Sstevel@tonic-gate 		if ((pfn_lo > pfn_hi) || (pfn < pfn_lo))
14480Sstevel@tonic-gate 			return (DDI_DMA_NOMAPPING);
14490Sstevel@tonic-gate 		count_max = dev_attr_p->dma_attr_count_max;
14500Sstevel@tonic-gate 		count_max = MIN(count_max, nocross);
14510Sstevel@tonic-gate 		/*
14520Sstevel@tonic-gate 		 * the following count_max trim is not done because we are
14530Sstevel@tonic-gate 		 * making sure pfn_lo <= pfn <= pfn_hi inside the loop
14540Sstevel@tonic-gate 		 * count_max=MIN(count_max, IOMMU_PTOB(pfn_hi - pfn_lo + 1)-1);
14550Sstevel@tonic-gate 		 */
14560Sstevel@tonic-gate 	} else { /* bypass hi/lo/count_max have been processed by attr2hdl() */
14570Sstevel@tonic-gate 		count_max = mp->dmai_attr.dma_attr_count_max;
14580Sstevel@tonic-gate 		pfn_lo = IOMMU_BTOP(mp->dmai_attr.dma_attr_addr_lo);
14590Sstevel@tonic-gate 		pfn_hi = IOMMU_BTOP(mp->dmai_attr.dma_attr_addr_hi);
14600Sstevel@tonic-gate 	}
14610Sstevel@tonic-gate 
14620Sstevel@tonic-gate 	bypass_pfn = IOMMU_BTOP(bypass);
14630Sstevel@tonic-gate 
14640Sstevel@tonic-gate 	for (prev_pfn = (bypass_pfn | pfn), i = 1; i < npages;
14650Sstevel@tonic-gate 	    i++, prev_pfn = pfn, pfn_no++) {
14660Sstevel@tonic-gate 		pfn = bypass_pfn | PCI_GET_MP_PFN1(mp, i);
14670Sstevel@tonic-gate 		if ((pfn == prev_pfn + 1) &&
1468*12027SStephen.Hanson@Sun.COM 		    (IOMMU_PTOB(pfn_no + 1) - 1 <= count_max))
14690Sstevel@tonic-gate 			continue;
14700Sstevel@tonic-gate 		if ((pfn < pfn_lo) || (prev_pfn > pfn_hi)) {
14710Sstevel@tonic-gate 			ret = DDI_DMA_NOMAPPING;
14720Sstevel@tonic-gate 			goto err;
14730Sstevel@tonic-gate 		}
14740Sstevel@tonic-gate 		cookie_no++;
14750Sstevel@tonic-gate 		pfn_no = 0;
14760Sstevel@tonic-gate 		if (cookie_no < sgllen)
14770Sstevel@tonic-gate 			continue;
14780Sstevel@tonic-gate 
14790Sstevel@tonic-gate 		DEBUG3(DBG_BYPASS, mp->dmai_rdip, "newwin pfn[%x-%x] %x cks\n",
1480*12027SStephen.Hanson@Sun.COM 		    win_pfn0_index, i - 1, cookie_no);
14810Sstevel@tonic-gate 		if (ret = pci_dma_newwin(dmareq, mp, cookie_no,
1482*12027SStephen.Hanson@Sun.COM 		    win_pfn0_index, i - 1, win_pp, count_max, bypass))
14830Sstevel@tonic-gate 			goto err;
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate 		win_pp = &(*win_pp)->win_next;	/* win_pp = *(win_pp) */
14860Sstevel@tonic-gate 		win_no++;
14870Sstevel@tonic-gate 		win_pfn0_index = i;
14880Sstevel@tonic-gate 		cookie_no = 0;
14890Sstevel@tonic-gate 	}
14900Sstevel@tonic-gate 	if (pfn > pfn_hi) {
14910Sstevel@tonic-gate 		ret = DDI_DMA_NOMAPPING;
14920Sstevel@tonic-gate 		goto err;
14930Sstevel@tonic-gate 	}
14940Sstevel@tonic-gate 	cookie_no++;
14950Sstevel@tonic-gate 	DEBUG3(DBG_BYPASS, mp->dmai_rdip, "newwin pfn[%x-%x] %x cks\n",
1496*12027SStephen.Hanson@Sun.COM 	    win_pfn0_index, i - 1, cookie_no);
14970Sstevel@tonic-gate 	if (ret = pci_dma_newwin(dmareq, mp, cookie_no, win_pfn0_index,
1498*12027SStephen.Hanson@Sun.COM 	    i - 1, win_pp, count_max, bypass))
14990Sstevel@tonic-gate 		goto err;
15000Sstevel@tonic-gate 	win_no++;
15010Sstevel@tonic-gate 	pci_dma_adjust(dmareq, mp, mp->dmai_winlst);
15020Sstevel@tonic-gate 	mp->dmai_nwin = win_no;
15030Sstevel@tonic-gate 	mp->dmai_rflags |= DDI_DMA_CONSISTENT;
15040Sstevel@tonic-gate 	if (!pci_p->pci_pbm_p->pbm_sync_reg_pa) {
15050Sstevel@tonic-gate 		mp->dmai_rflags |= DMP_NOSYNC;
15060Sstevel@tonic-gate 		mp->dmai_flags |= DMAI_FLAGS_NOSYNC;
15070Sstevel@tonic-gate 	}
15080Sstevel@tonic-gate 	mp->dmai_rflags &= ~DDI_DMA_REDZONE;
15090Sstevel@tonic-gate 	cookie0_p = (ddi_dma_cookie_t *)(WINLST(mp) + 1);
15100Sstevel@tonic-gate 	mp->dmai_cookie = WINLST(mp)->win_ncookies > 1 ? cookie0_p + 1 : 0;
15110Sstevel@tonic-gate 	mp->dmai_mapping = cookie0_p->dmac_laddress;
15120Sstevel@tonic-gate 
15130Sstevel@tonic-gate 	pci_dma_freepfn(mp);
15140Sstevel@tonic-gate 	return (DDI_DMA_MAPPED);
15150Sstevel@tonic-gate err:
15160Sstevel@tonic-gate 	pci_dma_freewin(mp);
15170Sstevel@tonic-gate 	return (ret);
15180Sstevel@tonic-gate }
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate /*ARGSUSED*/
15210Sstevel@tonic-gate int
pci_dma_ctl(dev_info_t * dip,dev_info_t * rdip,ddi_dma_impl_t * mp,enum ddi_dma_ctlops cmd,off_t * offp,size_t * lenp,caddr_t * objp,uint_t cache_flags)15220Sstevel@tonic-gate pci_dma_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_impl_t *mp,
15230Sstevel@tonic-gate 	enum ddi_dma_ctlops cmd, off_t *offp, size_t *lenp, caddr_t *objp,
15240Sstevel@tonic-gate 	uint_t cache_flags)
15250Sstevel@tonic-gate {
15260Sstevel@tonic-gate 	switch (cmd) {
15270Sstevel@tonic-gate 	case DDI_DMA_SYNC: /* XXX */
15280Sstevel@tonic-gate 		return (DDI_SUCCESS);
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate 	case DDI_DMA_HTOC: {
15310Sstevel@tonic-gate 		off_t off = *offp;
15320Sstevel@tonic-gate 		ddi_dma_cookie_t *loop_cp, *cp;
15330Sstevel@tonic-gate 		pci_dma_win_t *win_p = mp->dmai_winlst;
15340Sstevel@tonic-gate 
15350Sstevel@tonic-gate 		if (off >= mp->dmai_object.dmao_size)
15360Sstevel@tonic-gate 			return (DDI_FAILURE);
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate 		/* locate window */
15390Sstevel@tonic-gate 		while (win_p->win_offset + win_p->win_size <= off)
15400Sstevel@tonic-gate 			win_p = win_p->win_next;
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate 		loop_cp = cp = (ddi_dma_cookie_t *)(win_p + 1);
15430Sstevel@tonic-gate 		mp->dmai_offset = win_p->win_offset;
15440Sstevel@tonic-gate 		mp->dmai_size   = win_p->win_size;
15450Sstevel@tonic-gate 		mp->dmai_mapping = cp->dmac_laddress; /* cookie0 start addr */
15460Sstevel@tonic-gate 
15470Sstevel@tonic-gate 		/* adjust cookie addr/len if we are not on cookie boundary */
15480Sstevel@tonic-gate 		off -= win_p->win_offset;	   /* offset within window */
15490Sstevel@tonic-gate 		for (; off >= loop_cp->dmac_size; loop_cp++)
15500Sstevel@tonic-gate 			off -= loop_cp->dmac_size; /* offset within cookie */
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate 		mp->dmai_cookie = loop_cp + 1;
15530Sstevel@tonic-gate 		win_p->win_curseg = loop_cp - cp;
15540Sstevel@tonic-gate 		cp = (ddi_dma_cookie_t *)objp;
15550Sstevel@tonic-gate 		MAKE_DMA_COOKIE(cp, loop_cp->dmac_laddress + off,
1556*12027SStephen.Hanson@Sun.COM 		    loop_cp->dmac_size - off);
15570Sstevel@tonic-gate 
15580Sstevel@tonic-gate 		DEBUG2(DBG_DMA_CTL, dip,
1559*12027SStephen.Hanson@Sun.COM 		    "HTOC: cookie - dmac_laddress=%p dmac_size=%x\n",
1560*12027SStephen.Hanson@Sun.COM 		    cp->dmac_laddress, cp->dmac_size);
15610Sstevel@tonic-gate 		}
15620Sstevel@tonic-gate 		return (DDI_SUCCESS);
15630Sstevel@tonic-gate 
15640Sstevel@tonic-gate 	case DDI_DMA_REPWIN:
15650Sstevel@tonic-gate 		*offp = mp->dmai_offset;
15660Sstevel@tonic-gate 		*lenp = mp->dmai_size;
15670Sstevel@tonic-gate 		return (DDI_SUCCESS);
15680Sstevel@tonic-gate 
15690Sstevel@tonic-gate 	case DDI_DMA_MOVWIN: {
15700Sstevel@tonic-gate 		off_t off = *offp;
15710Sstevel@tonic-gate 		ddi_dma_cookie_t *cp;
15720Sstevel@tonic-gate 		pci_dma_win_t *win_p = mp->dmai_winlst;
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate 		if (off >= mp->dmai_object.dmao_size)
15750Sstevel@tonic-gate 			return (DDI_FAILURE);
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate 		/* locate window */
15780Sstevel@tonic-gate 		while (win_p->win_offset + win_p->win_size <= off)
15790Sstevel@tonic-gate 			win_p = win_p->win_next;
15800Sstevel@tonic-gate 
15810Sstevel@tonic-gate 		cp = (ddi_dma_cookie_t *)(win_p + 1);
15820Sstevel@tonic-gate 		mp->dmai_offset = win_p->win_offset;
15830Sstevel@tonic-gate 		mp->dmai_size   = win_p->win_size;
15840Sstevel@tonic-gate 		mp->dmai_mapping = cp->dmac_laddress;	/* cookie0 star addr */
15850Sstevel@tonic-gate 		mp->dmai_cookie = cp + 1;
15860Sstevel@tonic-gate 		win_p->win_curseg = 0;
15870Sstevel@tonic-gate 
15880Sstevel@tonic-gate 		*(ddi_dma_cookie_t *)objp = *cp;
15890Sstevel@tonic-gate 		*offp = win_p->win_offset;
15900Sstevel@tonic-gate 		*lenp = win_p->win_size;
15910Sstevel@tonic-gate 		DEBUG2(DBG_DMA_CTL, dip,
1592*12027SStephen.Hanson@Sun.COM 		    "HTOC: cookie - dmac_laddress=%p dmac_size=%x\n",
1593*12027SStephen.Hanson@Sun.COM 		    cp->dmac_laddress, cp->dmac_size);
15940Sstevel@tonic-gate 		}
15950Sstevel@tonic-gate 		return (DDI_SUCCESS);
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate 	case DDI_DMA_NEXTWIN: {
15980Sstevel@tonic-gate 		pci_dma_win_t *win_p = *(pci_dma_win_t **)offp;
15990Sstevel@tonic-gate 		pci_dma_win_t **nw_pp = (pci_dma_win_t **)objp;
16000Sstevel@tonic-gate 		ddi_dma_cookie_t *cp;
16010Sstevel@tonic-gate 		if (!win_p) {
16020Sstevel@tonic-gate 			*nw_pp = mp->dmai_winlst;
16030Sstevel@tonic-gate 			return (DDI_SUCCESS);
16040Sstevel@tonic-gate 		}
16050Sstevel@tonic-gate 
16060Sstevel@tonic-gate 		if (win_p->win_offset != mp->dmai_offset)
16070Sstevel@tonic-gate 			return (DDI_DMA_STALE);
16080Sstevel@tonic-gate 		if (!win_p->win_next)
16090Sstevel@tonic-gate 			return (DDI_DMA_DONE);
16100Sstevel@tonic-gate 		win_p = win_p->win_next;
16110Sstevel@tonic-gate 		cp = (ddi_dma_cookie_t *)(win_p + 1);
16120Sstevel@tonic-gate 		mp->dmai_offset = win_p->win_offset;
16130Sstevel@tonic-gate 		mp->dmai_size   = win_p->win_size;
16140Sstevel@tonic-gate 		mp->dmai_mapping = cp->dmac_laddress;   /* cookie0 star addr */
16150Sstevel@tonic-gate 		mp->dmai_cookie = cp + 1;
16160Sstevel@tonic-gate 		win_p->win_curseg = 0;
16170Sstevel@tonic-gate 		*nw_pp = win_p;
16180Sstevel@tonic-gate 		}
16190Sstevel@tonic-gate 		return (DDI_SUCCESS);
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate 	case DDI_DMA_NEXTSEG: {
16220Sstevel@tonic-gate 		pci_dma_win_t *w_p = *(pci_dma_win_t **)offp;
16230Sstevel@tonic-gate 		if (w_p->win_offset != mp->dmai_offset)
16240Sstevel@tonic-gate 			return (DDI_DMA_STALE);
16250Sstevel@tonic-gate 		if (w_p->win_curseg + 1 >= w_p->win_ncookies)
16260Sstevel@tonic-gate 			return (DDI_DMA_DONE);
16270Sstevel@tonic-gate 		w_p->win_curseg++;
16280Sstevel@tonic-gate 		}
16290Sstevel@tonic-gate 		*(ddi_dma_seg_t *)objp = (ddi_dma_seg_t)mp;
16300Sstevel@tonic-gate 		return (DDI_SUCCESS);
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate 	case DDI_DMA_SEGTOC: {
16330Sstevel@tonic-gate 		pci_dma_win_t *win_p = mp->dmai_winlst;
16340Sstevel@tonic-gate 		off_t off = mp->dmai_offset;
16350Sstevel@tonic-gate 		ddi_dma_cookie_t *cp;
16360Sstevel@tonic-gate 		int i;
16370Sstevel@tonic-gate 
16380Sstevel@tonic-gate 		/* locate active window */
1639*12027SStephen.Hanson@Sun.COM 		for (; win_p->win_offset != off; win_p = win_p->win_next)
1640*12027SStephen.Hanson@Sun.COM 			;
16410Sstevel@tonic-gate 		cp = (ddi_dma_cookie_t *)(win_p + 1);
16420Sstevel@tonic-gate 		for (i = 0; i < win_p->win_curseg; i++, cp++)
16430Sstevel@tonic-gate 			off += cp->dmac_size;
16440Sstevel@tonic-gate 		*offp = off;
16450Sstevel@tonic-gate 		*lenp = cp->dmac_size;
16460Sstevel@tonic-gate 		*(ddi_dma_cookie_t *)objp = *cp;	/* copy cookie */
16470Sstevel@tonic-gate 		}
16480Sstevel@tonic-gate 		return (DDI_SUCCESS);
16490Sstevel@tonic-gate 
16500Sstevel@tonic-gate 	case DDI_DMA_COFF: {
16510Sstevel@tonic-gate 		pci_dma_win_t *win_p;
16520Sstevel@tonic-gate 		ddi_dma_cookie_t *cp;
16530Sstevel@tonic-gate 		uint64_t addr, key = ((ddi_dma_cookie_t *)offp)->dmac_laddress;
16540Sstevel@tonic-gate 		size_t win_off;
16550Sstevel@tonic-gate 
16560Sstevel@tonic-gate 		for (win_p = mp->dmai_winlst; win_p; win_p = win_p->win_next) {
16570Sstevel@tonic-gate 			int i;
16580Sstevel@tonic-gate 			win_off = 0;
16590Sstevel@tonic-gate 			cp = (ddi_dma_cookie_t *)(win_p + 1);
16600Sstevel@tonic-gate 			for (i = 0; i < win_p->win_ncookies; i++, cp++) {
16610Sstevel@tonic-gate 				size_t sz = cp->dmac_size;
16620Sstevel@tonic-gate 
16630Sstevel@tonic-gate 				addr = cp->dmac_laddress;
16640Sstevel@tonic-gate 				if ((addr <= key) && (addr + sz >= key))
16650Sstevel@tonic-gate 					goto found;
16660Sstevel@tonic-gate 				win_off += sz;
16670Sstevel@tonic-gate 			}
16680Sstevel@tonic-gate 		}
16690Sstevel@tonic-gate 		return (DDI_FAILURE);
16700Sstevel@tonic-gate found:
16710Sstevel@tonic-gate 		*objp = (caddr_t)(win_p->win_offset + win_off + (key - addr));
16720Sstevel@tonic-gate 		return (DDI_SUCCESS);
16730Sstevel@tonic-gate 		}
16740Sstevel@tonic-gate 
16750Sstevel@tonic-gate 	case DDI_DMA_REMAP:
16760Sstevel@tonic-gate 		return (DDI_FAILURE);
16770Sstevel@tonic-gate 
16780Sstevel@tonic-gate 	default:
16790Sstevel@tonic-gate 		DEBUG3(DBG_DMA_CTL, dip, "unknown command (%x): rdip=%s%d\n",
1680*12027SStephen.Hanson@Sun.COM 		    cmd, ddi_driver_name(rdip), ddi_get_instance(rdip));
16810Sstevel@tonic-gate 		break;
16820Sstevel@tonic-gate 	}
16830Sstevel@tonic-gate 	return (DDI_FAILURE);
16840Sstevel@tonic-gate }
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate static void
pci_dvma_debug_init(iommu_t * iommu_p)16870Sstevel@tonic-gate pci_dvma_debug_init(iommu_t *iommu_p)
16880Sstevel@tonic-gate {
16890Sstevel@tonic-gate 	size_t sz = sizeof (struct dvma_rec) * pci_dvma_debug_rec;
16900Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&iommu_p->dvma_debug_lock));
16910Sstevel@tonic-gate 	cmn_err(CE_NOTE, "PCI DVMA %p stat ON", iommu_p);
16920Sstevel@tonic-gate 
16930Sstevel@tonic-gate 	iommu_p->dvma_alloc_rec = kmem_zalloc(sz, KM_SLEEP);
16940Sstevel@tonic-gate 	iommu_p->dvma_free_rec = kmem_zalloc(sz, KM_SLEEP);
16950Sstevel@tonic-gate 
16960Sstevel@tonic-gate 	iommu_p->dvma_active_list = NULL;
16970Sstevel@tonic-gate 	iommu_p->dvma_alloc_rec_index = 0;
16980Sstevel@tonic-gate 	iommu_p->dvma_free_rec_index = 0;
16990Sstevel@tonic-gate 	iommu_p->dvma_active_count = 0;
17000Sstevel@tonic-gate }
17010Sstevel@tonic-gate 
17020Sstevel@tonic-gate void
pci_dvma_debug_fini(iommu_t * iommu_p)17030Sstevel@tonic-gate pci_dvma_debug_fini(iommu_t *iommu_p)
17040Sstevel@tonic-gate {
17050Sstevel@tonic-gate 	struct dvma_rec *prev, *ptr;
17060Sstevel@tonic-gate 	size_t sz = sizeof (struct dvma_rec) * pci_dvma_debug_rec;
17070Sstevel@tonic-gate 	uint64_t mask = ~(1ull << iommu_p->iommu_inst);
17080Sstevel@tonic-gate 	cmn_err(CE_NOTE, "PCI DVMA %p stat OFF", iommu_p);
17090Sstevel@tonic-gate 
17100Sstevel@tonic-gate 	kmem_free(iommu_p->dvma_alloc_rec, sz);
17110Sstevel@tonic-gate 	kmem_free(iommu_p->dvma_free_rec, sz);
17120Sstevel@tonic-gate 	iommu_p->dvma_alloc_rec = iommu_p->dvma_free_rec = NULL;
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate 	prev = iommu_p->dvma_active_list;
17150Sstevel@tonic-gate 	if (!prev)
17160Sstevel@tonic-gate 		return;
17170Sstevel@tonic-gate 	for (ptr = prev->next; ptr; prev = ptr, ptr = ptr->next)
17180Sstevel@tonic-gate 		kmem_free(prev, sizeof (struct dvma_rec));
17190Sstevel@tonic-gate 	kmem_free(prev, sizeof (struct dvma_rec));
17200Sstevel@tonic-gate 
17210Sstevel@tonic-gate 	iommu_p->dvma_active_list = NULL;
17220Sstevel@tonic-gate 	iommu_p->dvma_alloc_rec_index = 0;
17230Sstevel@tonic-gate 	iommu_p->dvma_free_rec_index = 0;
17240Sstevel@tonic-gate 	iommu_p->dvma_active_count = 0;
17250Sstevel@tonic-gate 
17260Sstevel@tonic-gate 	pci_dvma_debug_on  &= mask;
17270Sstevel@tonic-gate 	pci_dvma_debug_off &= mask;
17280Sstevel@tonic-gate }
17290Sstevel@tonic-gate 
17300Sstevel@tonic-gate void
pci_dvma_alloc_debug(iommu_t * iommu_p,char * address,uint_t len,ddi_dma_impl_t * mp)17310Sstevel@tonic-gate pci_dvma_alloc_debug(iommu_t *iommu_p, char *address, uint_t len,
17320Sstevel@tonic-gate 	ddi_dma_impl_t *mp)
17330Sstevel@tonic-gate {
17340Sstevel@tonic-gate 	struct dvma_rec *ptr;
17350Sstevel@tonic-gate 	mutex_enter(&iommu_p->dvma_debug_lock);
17360Sstevel@tonic-gate 
17370Sstevel@tonic-gate 	if (!iommu_p->dvma_alloc_rec)
17380Sstevel@tonic-gate 		pci_dvma_debug_init(iommu_p);
17390Sstevel@tonic-gate 	if (DVMA_DBG_OFF(iommu_p)) {
17400Sstevel@tonic-gate 		pci_dvma_debug_fini(iommu_p);
17410Sstevel@tonic-gate 		goto done;
17420Sstevel@tonic-gate 	}
17430Sstevel@tonic-gate 
17440Sstevel@tonic-gate 	ptr = &iommu_p->dvma_alloc_rec[iommu_p->dvma_alloc_rec_index];
17450Sstevel@tonic-gate 	ptr->dvma_addr = address;
17460Sstevel@tonic-gate 	ptr->len = len;
17470Sstevel@tonic-gate 	ptr->mp = mp;
17480Sstevel@tonic-gate 	if (++iommu_p->dvma_alloc_rec_index == pci_dvma_debug_rec)
17490Sstevel@tonic-gate 		iommu_p->dvma_alloc_rec_index = 0;
17500Sstevel@tonic-gate 
17510Sstevel@tonic-gate 	ptr = kmem_alloc(sizeof (struct dvma_rec), KM_SLEEP);
17520Sstevel@tonic-gate 	ptr->dvma_addr = address;
17530Sstevel@tonic-gate 	ptr->len = len;
17540Sstevel@tonic-gate 	ptr->mp = mp;
17550Sstevel@tonic-gate 
17560Sstevel@tonic-gate 	ptr->next = iommu_p->dvma_active_list;
17570Sstevel@tonic-gate 	iommu_p->dvma_active_list = ptr;
17580Sstevel@tonic-gate 	iommu_p->dvma_active_count++;
17590Sstevel@tonic-gate done:
17600Sstevel@tonic-gate 	mutex_exit(&iommu_p->dvma_debug_lock);
17610Sstevel@tonic-gate }
17620Sstevel@tonic-gate 
17630Sstevel@tonic-gate void
pci_dvma_free_debug(iommu_t * iommu_p,char * address,uint_t len,ddi_dma_impl_t * mp)17640Sstevel@tonic-gate pci_dvma_free_debug(iommu_t *iommu_p, char *address, uint_t len,
17650Sstevel@tonic-gate 	ddi_dma_impl_t *mp)
17660Sstevel@tonic-gate {
17670Sstevel@tonic-gate 	struct dvma_rec *ptr, *ptr_save;
17680Sstevel@tonic-gate 	mutex_enter(&iommu_p->dvma_debug_lock);
17690Sstevel@tonic-gate 
17700Sstevel@tonic-gate 	if (!iommu_p->dvma_alloc_rec)
17710Sstevel@tonic-gate 		pci_dvma_debug_init(iommu_p);
17720Sstevel@tonic-gate 	if (DVMA_DBG_OFF(iommu_p)) {
17730Sstevel@tonic-gate 		pci_dvma_debug_fini(iommu_p);
17740Sstevel@tonic-gate 		goto done;
17750Sstevel@tonic-gate 	}
17760Sstevel@tonic-gate 
17770Sstevel@tonic-gate 	ptr = &iommu_p->dvma_free_rec[iommu_p->dvma_free_rec_index];
17780Sstevel@tonic-gate 	ptr->dvma_addr = address;
17790Sstevel@tonic-gate 	ptr->len = len;
17800Sstevel@tonic-gate 	ptr->mp = mp;
17810Sstevel@tonic-gate 	if (++iommu_p->dvma_free_rec_index == pci_dvma_debug_rec)
17820Sstevel@tonic-gate 		iommu_p->dvma_free_rec_index = 0;
17830Sstevel@tonic-gate 
17840Sstevel@tonic-gate 	ptr_save = iommu_p->dvma_active_list;
17850Sstevel@tonic-gate 	for (ptr = ptr_save; ptr; ptr = ptr->next) {
17860Sstevel@tonic-gate 		if ((ptr->dvma_addr == address) && (ptr->len = len))
17870Sstevel@tonic-gate 			break;
17880Sstevel@tonic-gate 		ptr_save = ptr;
17890Sstevel@tonic-gate 	}
17900Sstevel@tonic-gate 	if (!ptr) {
17910Sstevel@tonic-gate 		cmn_err(CE_WARN, "bad dvma free addr=%lx len=%x",
1792*12027SStephen.Hanson@Sun.COM 		    (long)address, len);
17930Sstevel@tonic-gate 		goto done;
17940Sstevel@tonic-gate 	}
17950Sstevel@tonic-gate 	if (ptr == iommu_p->dvma_active_list)
17960Sstevel@tonic-gate 		iommu_p->dvma_active_list = ptr->next;
17970Sstevel@tonic-gate 	else
17980Sstevel@tonic-gate 		ptr_save->next = ptr->next;
17990Sstevel@tonic-gate 	kmem_free(ptr, sizeof (struct dvma_rec));
18000Sstevel@tonic-gate 	iommu_p->dvma_active_count--;
18010Sstevel@tonic-gate done:
18020Sstevel@tonic-gate 	mutex_exit(&iommu_p->dvma_debug_lock);
18030Sstevel@tonic-gate }
18040Sstevel@tonic-gate 
18050Sstevel@tonic-gate #ifdef DEBUG
18060Sstevel@tonic-gate void
dump_dma_handle(uint64_t flag,dev_info_t * dip,ddi_dma_impl_t * hp)18070Sstevel@tonic-gate dump_dma_handle(uint64_t flag, dev_info_t *dip, ddi_dma_impl_t *hp)
18080Sstevel@tonic-gate {
18090Sstevel@tonic-gate 	DEBUG4(flag, dip, "mp(%p): flags=%x mapping=%lx xfer_size=%x\n",
1810*12027SStephen.Hanson@Sun.COM 	    hp, hp->dmai_inuse, hp->dmai_mapping, hp->dmai_size);
18110Sstevel@tonic-gate 	DEBUG4(flag|DBG_CONT, dip, "\tnpages=%x roffset=%x rflags=%x nwin=%x\n",
1812*12027SStephen.Hanson@Sun.COM 	    hp->dmai_ndvmapages, hp->dmai_roffset, hp->dmai_rflags,
1813*12027SStephen.Hanson@Sun.COM 	    hp->dmai_nwin);
18140Sstevel@tonic-gate 	DEBUG4(flag|DBG_CONT, dip, "\twinsize=%x tte=%p pfnlst=%p pfn0=%p\n",
1815*12027SStephen.Hanson@Sun.COM 	    hp->dmai_winsize, hp->dmai_tte, hp->dmai_pfnlst, hp->dmai_pfn0);
18160Sstevel@tonic-gate 	DEBUG4(flag|DBG_CONT, dip, "\twinlst=%x obj=%p attr=%p ckp=%p\n",
1817*12027SStephen.Hanson@Sun.COM 	    hp->dmai_winlst, &hp->dmai_object, &hp->dmai_attr,
1818*12027SStephen.Hanson@Sun.COM 	    hp->dmai_cookie);
18190Sstevel@tonic-gate }
18200Sstevel@tonic-gate #endif
18210Sstevel@tonic-gate 
18220Sstevel@tonic-gate void
pci_vmem_do_free(iommu_t * iommu_p,void * base_addr,size_t npages,int vmemcache)18230Sstevel@tonic-gate pci_vmem_do_free(iommu_t *iommu_p, void *base_addr, size_t npages,
18240Sstevel@tonic-gate     int vmemcache)
18250Sstevel@tonic-gate {
18260Sstevel@tonic-gate 	vmem_t *map_p = iommu_p->iommu_dvma_map;
18270Sstevel@tonic-gate 
18280Sstevel@tonic-gate 	if (vmemcache) {
18290Sstevel@tonic-gate 		vmem_free(map_p, base_addr, IOMMU_PAGE_SIZE);
18300Sstevel@tonic-gate #ifdef PCI_DMA_PROF
18310Sstevel@tonic-gate 		pci_dvma_vmem_free++;
18320Sstevel@tonic-gate #endif
18330Sstevel@tonic-gate 		return;
18340Sstevel@tonic-gate 	}
18350Sstevel@tonic-gate 
18360Sstevel@tonic-gate 	vmem_xfree(map_p, base_addr, IOMMU_PTOB(npages));
18370Sstevel@tonic-gate #ifdef PCI_DMA_PROF
18380Sstevel@tonic-gate 		pci_dvma_vmem_xfree++;
18390Sstevel@tonic-gate #endif
18400Sstevel@tonic-gate }
1841