xref: /onnv-gate/usr/src/uts/sun4u/io/pci/pci_dma.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * PCI nexus DVMA and DMA core routines:
31*0Sstevel@tonic-gate  *	dma_map/dma_bind_handle implementation
32*0Sstevel@tonic-gate  *	bypass and peer-to-peer support
33*0Sstevel@tonic-gate  *	fast track DVMA space allocation
34*0Sstevel@tonic-gate  *	runtime DVMA debug
35*0Sstevel@tonic-gate  */
36*0Sstevel@tonic-gate #include <sys/types.h>
37*0Sstevel@tonic-gate #include <sys/kmem.h>
38*0Sstevel@tonic-gate #include <sys/async.h>
39*0Sstevel@tonic-gate #include <sys/sysmacros.h>
40*0Sstevel@tonic-gate #include <sys/sunddi.h>
41*0Sstevel@tonic-gate #include <sys/machsystm.h>	/* lddphys() */
42*0Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
43*0Sstevel@tonic-gate #include <vm/hat.h>
44*0Sstevel@tonic-gate #include <sys/pci/pci_obj.h>
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate /*LINTLIBRARY*/
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate static void
49*0Sstevel@tonic-gate pci_sc_pg_inv(dev_info_t *dip, sc_t *sc_p, ddi_dma_impl_t *mp, off_t off,
50*0Sstevel@tonic-gate 	size_t len)
51*0Sstevel@tonic-gate {
52*0Sstevel@tonic-gate 	dvma_addr_t dvma_addr, pg_off;
53*0Sstevel@tonic-gate 	volatile uint64_t *invl_va = sc_p->sc_invl_reg;
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate 	if (!len)
56*0Sstevel@tonic-gate 		len = mp->dmai_size;
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate 	pg_off = mp->dmai_offset;			/* start min */
59*0Sstevel@tonic-gate 	dvma_addr = MAX(off, pg_off);			/* lo */
60*0Sstevel@tonic-gate 	pg_off += mp->dmai_size;			/* end max */
61*0Sstevel@tonic-gate 	pg_off = MIN(off + len, pg_off);		/* hi */
62*0Sstevel@tonic-gate 	if (dvma_addr >= pg_off) {			/* lo >= hi ? */
63*0Sstevel@tonic-gate 		DEBUG4(DBG_SC, dip, "%x+%x out of window [%x,%x)\n",
64*0Sstevel@tonic-gate 			off, len, mp->dmai_offset,
65*0Sstevel@tonic-gate 			mp->dmai_offset + mp->dmai_size);
66*0Sstevel@tonic-gate 		return;
67*0Sstevel@tonic-gate 	}
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	len = pg_off - dvma_addr;			/* sz = hi - lo */
70*0Sstevel@tonic-gate 	dvma_addr += mp->dmai_mapping;			/* start addr */
71*0Sstevel@tonic-gate 	pg_off = dvma_addr & IOMMU_PAGE_OFFSET;		/* offset in 1st pg */
72*0Sstevel@tonic-gate 	len = IOMMU_BTOPR(len + pg_off);		/* # of pages */
73*0Sstevel@tonic-gate 	dvma_addr ^= pg_off;
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate 	DEBUG2(DBG_SC, dip, "addr=%x+%x pages: \n", dvma_addr, len);
76*0Sstevel@tonic-gate 	for (; len; len--, dvma_addr += IOMMU_PAGE_SIZE) {
77*0Sstevel@tonic-gate 		DEBUG1(DBG_SC|DBG_CONT, dip, " %x", dvma_addr);
78*0Sstevel@tonic-gate 		*invl_va = (uint64_t)dvma_addr;
79*0Sstevel@tonic-gate 	}
80*0Sstevel@tonic-gate 	DEBUG0(DBG_SC|DBG_CONT, dip, "\n");
81*0Sstevel@tonic-gate }
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate static void
84*0Sstevel@tonic-gate pci_dma_sync_flag_wait(ddi_dma_impl_t *mp, sc_t *sc_p, uint32_t onstack)
85*0Sstevel@tonic-gate {
86*0Sstevel@tonic-gate 	hrtime_t start_time;
87*0Sstevel@tonic-gate 	uint64_t loops = 0;
88*0Sstevel@tonic-gate 	uint64_t sync_flag_pa = SYNC_BUF_PA(mp);
89*0Sstevel@tonic-gate 	uint64_t sync_reg_pa = sc_p->sc_sync_reg_pa;
90*0Sstevel@tonic-gate 	uint8_t stack_buf[128];
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 	stack_buf[0] = DDI_SUCCESS;
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate 	/* check for handle specific sync flag */
95*0Sstevel@tonic-gate 	if (sync_flag_pa)
96*0Sstevel@tonic-gate 		goto start;
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 	sync_flag_pa = sc_p->sc_sync_flag_pa;
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 	if (onstack) {
101*0Sstevel@tonic-gate 		sync_flag_pa = va_to_pa(stack_buf);
102*0Sstevel@tonic-gate 		sync_flag_pa += PCI_SYNC_FLAG_SIZE;
103*0Sstevel@tonic-gate 		sync_flag_pa >>= PCI_SYNC_FLAG_SZSHIFT;
104*0Sstevel@tonic-gate 		sync_flag_pa <<= PCI_SYNC_FLAG_SZSHIFT;
105*0Sstevel@tonic-gate 		goto start;
106*0Sstevel@tonic-gate 	}
107*0Sstevel@tonic-gate 	stack_buf[0] |= PCI_SYNC_FLAG_LOCKED;
108*0Sstevel@tonic-gate 	mutex_enter(&sc_p->sc_sync_mutex);
109*0Sstevel@tonic-gate start:
110*0Sstevel@tonic-gate 	ASSERT(!(sync_flag_pa & PCI_SYNC_FLAG_SIZE - 1));
111*0Sstevel@tonic-gate 	stdphys(sync_flag_pa, 0);	/* reset sync flag to 0 */
112*0Sstevel@tonic-gate 					/* membar  #LoadStore|#StoreStore */
113*0Sstevel@tonic-gate 	stdphysio(sync_reg_pa, sync_flag_pa);
114*0Sstevel@tonic-gate 	start_time = gethrtime();
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	for (; gethrtime() - start_time < pci_sync_buf_timeout; loops++)
117*0Sstevel@tonic-gate 		if (lddphys(sync_flag_pa))
118*0Sstevel@tonic-gate 			goto done;
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 	if (!lddphys(sync_flag_pa))
121*0Sstevel@tonic-gate 		stack_buf[0] |= PCI_SYNC_FLAG_FAILED;
122*0Sstevel@tonic-gate done:
123*0Sstevel@tonic-gate 	DEBUG3(DBG_SC|DBG_CONT, 0, "flag wait loops=%lu ticks=%lu status=%x\n",
124*0Sstevel@tonic-gate 		loops, gethrtime() - start_time, stack_buf[0]);
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	if (stack_buf[0] & PCI_SYNC_FLAG_LOCKED)
127*0Sstevel@tonic-gate 		mutex_exit(&sc_p->sc_sync_mutex);
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 	if (stack_buf[0] & PCI_SYNC_FLAG_FAILED)
130*0Sstevel@tonic-gate 		cmn_err(CE_PANIC, "%p pci dma sync %llx %llx timeout!",
131*0Sstevel@tonic-gate 		    mp, sync_flag_pa, loops);
132*0Sstevel@tonic-gate }
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate /*
135*0Sstevel@tonic-gate  * Cache	RW	Before	During		After
136*0Sstevel@tonic-gate  *
137*0Sstevel@tonic-gate  * STREAMING	read	no/no	pg/no		ctx,pg/no
138*0Sstevel@tonic-gate  * STREAMING	write	no/no	pg/yes		ctx,pg/yes
139*0Sstevel@tonic-gate  * CONSISTENT	read	no/no	yes,no/no	yes,no/no
140*0Sstevel@tonic-gate  * CONSISTENT	write	no/no	yes,yes/yes	yes,yes/yes
141*0Sstevel@tonic-gate  *
142*0Sstevel@tonic-gate  * STREAMING	read	ctx,pg/no
143*0Sstevel@tonic-gate  * STREAMING	write	ctx,pg/yes
144*0Sstevel@tonic-gate  * CONSISTENT	read	yes,no/no
145*0Sstevel@tonic-gate  * CONSISTENT	write	yes,yes/yes
146*0Sstevel@tonic-gate  */
147*0Sstevel@tonic-gate int
148*0Sstevel@tonic-gate pci_dma_sync(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle,
149*0Sstevel@tonic-gate 	off_t off, size_t len, uint32_t sync_flag)
150*0Sstevel@tonic-gate {
151*0Sstevel@tonic-gate 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
152*0Sstevel@tonic-gate 	int ret = ddi_get_instance(dip);
153*0Sstevel@tonic-gate 	pci_t *pci_p = get_pci_soft_state(ret);
154*0Sstevel@tonic-gate 	pbm_t *pbm_p = pci_p->pci_pbm_p;
155*0Sstevel@tonic-gate 	uint32_t dev_flag = mp->dmai_rflags;
156*0Sstevel@tonic-gate 	sc_t *sc_p;
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 	DEBUG4(DBG_DMA_SYNC, dip, "%s%d flags=%x,%x\n", ddi_driver_name(rdip),
159*0Sstevel@tonic-gate 		ddi_get_instance(rdip), dev_flag, sync_flag);
160*0Sstevel@tonic-gate 	DEBUG4(DBG_SC, dip, "dmai_mapping=%x, dmai_sz=%x off=%x len=%x\n",
161*0Sstevel@tonic-gate 		mp->dmai_mapping, mp->dmai_size, off, len);
162*0Sstevel@tonic-gate 	DEBUG2(DBG_SC, dip, "mp=%p, ctx=%x\n", mp, MP2CTX(mp));
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 	if (!(mp->dmai_flags & DMAI_FLAGS_INUSE)) {
165*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "Unbound dma handle %p from %s%d", mp,
166*0Sstevel@tonic-gate 		    ddi_driver_name(rdip), ddi_get_instance(rdip));
167*0Sstevel@tonic-gate 		return (DDI_FAILURE);
168*0Sstevel@tonic-gate 	}
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 	if (mp->dmai_flags & DMAI_FLAGS_NOSYNC)
171*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 	if (!(dev_flag & DDI_DMA_CONSISTENT))
174*0Sstevel@tonic-gate 		goto streaming;
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 	if (sync_flag & PCI_DMA_SYNC_EXT) {
177*0Sstevel@tonic-gate 		if (sync_flag & (PCI_DMA_SYNC_BEFORE | PCI_DMA_SYNC_POST) ||
178*0Sstevel@tonic-gate 		    !(sync_flag & PCI_DMA_SYNC_WRITE))
179*0Sstevel@tonic-gate 			return (DDI_SUCCESS);
180*0Sstevel@tonic-gate 	} else {
181*0Sstevel@tonic-gate 		if (!(dev_flag & DDI_DMA_READ) ||
182*0Sstevel@tonic-gate 		    ((sync_flag & PCI_DMA_SYNC_DDI_FLAGS) ==
183*0Sstevel@tonic-gate 		    DDI_DMA_SYNC_FORDEV))
184*0Sstevel@tonic-gate 			return (DDI_SUCCESS);
185*0Sstevel@tonic-gate 	}
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 	pci_pbm_dma_sync(pbm_p, pbm_p->pbm_sync_ino);
188*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate streaming:
191*0Sstevel@tonic-gate 	ASSERT(pci_stream_buf_exists && (pci_stream_buf_enable & 1 << ret));
192*0Sstevel@tonic-gate 	sc_p = pci_p->pci_sc_p;
193*0Sstevel@tonic-gate 	ret = DDI_FAILURE;
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 	if (sync_flag & PCI_DMA_SYNC_EXT)
196*0Sstevel@tonic-gate 		goto ext;
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 	if (mp->dmai_flags & DMAI_FLAGS_CONTEXT && pci_sc_use_contexts)
199*0Sstevel@tonic-gate 		ret = pci_sc_ctx_inv(dip, sc_p, mp);
200*0Sstevel@tonic-gate 	if (ret)
201*0Sstevel@tonic-gate 		pci_sc_pg_inv(dip, sc_p, mp, off, len);
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate 	if ((dev_flag & DDI_DMA_READ) &&
204*0Sstevel@tonic-gate 	    ((sync_flag & PCI_DMA_SYNC_DDI_FLAGS) != DDI_DMA_SYNC_FORDEV))
205*0Sstevel@tonic-gate 		goto wait;
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
208*0Sstevel@tonic-gate ext:
209*0Sstevel@tonic-gate 	if (sync_flag & PCI_DMA_SYNC_BEFORE)
210*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
211*0Sstevel@tonic-gate 	if (sync_flag & PCI_DMA_SYNC_BAR)
212*0Sstevel@tonic-gate 		goto wait_check;
213*0Sstevel@tonic-gate 	if (sync_flag & PCI_DMA_SYNC_AFTER &&
214*0Sstevel@tonic-gate 		mp->dmai_flags & DMAI_FLAGS_CONTEXT && pci_sc_use_contexts)
215*0Sstevel@tonic-gate 		ret = pci_sc_ctx_inv(dip, sc_p, mp);
216*0Sstevel@tonic-gate 	if (ret)
217*0Sstevel@tonic-gate 		pci_sc_pg_inv(dip, sc_p, mp, off, len);
218*0Sstevel@tonic-gate wait_check:
219*0Sstevel@tonic-gate 	if (sync_flag & PCI_DMA_SYNC_POST || !(sync_flag & PCI_DMA_SYNC_WRITE))
220*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
221*0Sstevel@tonic-gate wait:
222*0Sstevel@tonic-gate 	pci_dma_sync_flag_wait(mp, sc_p, sync_flag & PCI_DMA_SYNC_PRIVATE);
223*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
224*0Sstevel@tonic-gate }
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate int
227*0Sstevel@tonic-gate pci_dma_handle_clean(dev_info_t *rdip, ddi_dma_handle_t h)
228*0Sstevel@tonic-gate {
229*0Sstevel@tonic-gate 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)h;
230*0Sstevel@tonic-gate 	if ((mp->dmai_flags & DMAI_FLAGS_INUSE) == 0)
231*0Sstevel@tonic-gate 		return (DDI_FAILURE);
232*0Sstevel@tonic-gate 	mp->dmai_rflags |= DMP_NOSYNC;
233*0Sstevel@tonic-gate 	mp->dmai_flags |= DMAI_FLAGS_NOSYNC;
234*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
235*0Sstevel@tonic-gate }
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate /*
238*0Sstevel@tonic-gate  * pci_dma_allocmp - Allocate a pci dma implementation structure
239*0Sstevel@tonic-gate  *
240*0Sstevel@tonic-gate  * An extra ddi_dma_attr structure is bundled with the usual ddi_dma_impl
241*0Sstevel@tonic-gate  * to hold unmodified device limits. The ddi_dma_attr inside the
242*0Sstevel@tonic-gate  * ddi_dma_impl structure is augumented with system limits to enhance
243*0Sstevel@tonic-gate  * DVMA performance at runtime. The unaugumented device limits saved
244*0Sstevel@tonic-gate  * right after (accessed through the DEV_ATTR macro) is used
245*0Sstevel@tonic-gate  * strictly for peer-to-peer transfers which do not obey system limits.
246*0Sstevel@tonic-gate  *
247*0Sstevel@tonic-gate  * return: DDI_SUCCESS DDI_DMA_NORESOURCES
248*0Sstevel@tonic-gate  */
249*0Sstevel@tonic-gate ddi_dma_impl_t *
250*0Sstevel@tonic-gate pci_dma_allocmp(dev_info_t *dip, dev_info_t *rdip, int (*waitfp)(caddr_t),
251*0Sstevel@tonic-gate 	caddr_t arg)
252*0Sstevel@tonic-gate {
253*0Sstevel@tonic-gate 	ddi_dma_impl_t *mp;
254*0Sstevel@tonic-gate 	int sleep = (waitfp == DDI_DMA_SLEEP) ? KM_SLEEP : KM_NOSLEEP;
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 	/* Caution: we don't use zalloc to enhance performance! */
257*0Sstevel@tonic-gate 	if ((mp = kmem_alloc(sizeof (pci_dma_hdl_t), sleep)) == 0) {
258*0Sstevel@tonic-gate 		DEBUG0(DBG_DMA_MAP, dip, "can't alloc dma_handle\n");
259*0Sstevel@tonic-gate 		if (waitfp != DDI_DMA_DONTWAIT) {
260*0Sstevel@tonic-gate 			DEBUG0(DBG_DMA_MAP, dip, "alloc_mp kmem cb\n");
261*0Sstevel@tonic-gate 			ddi_set_callback(waitfp, arg, &pci_kmem_clid);
262*0Sstevel@tonic-gate 		}
263*0Sstevel@tonic-gate 		return (mp);
264*0Sstevel@tonic-gate 	}
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate 	mp->dmai_rdip = rdip;
267*0Sstevel@tonic-gate 	mp->dmai_flags = 0;
268*0Sstevel@tonic-gate 	mp->dmai_pfnlst = NULL;
269*0Sstevel@tonic-gate 	mp->dmai_winlst = NULL;
270*0Sstevel@tonic-gate 
271*0Sstevel@tonic-gate 	/*
272*0Sstevel@tonic-gate 	 * kmem_alloc debug: the following fields are not zero-ed
273*0Sstevel@tonic-gate 	 * mp->dmai_mapping = 0;
274*0Sstevel@tonic-gate 	 * mp->dmai_size = 0;
275*0Sstevel@tonic-gate 	 * mp->dmai_offset = 0;
276*0Sstevel@tonic-gate 	 * mp->dmai_minxfer = 0;
277*0Sstevel@tonic-gate 	 * mp->dmai_burstsizes = 0;
278*0Sstevel@tonic-gate 	 * mp->dmai_ndvmapages = 0;
279*0Sstevel@tonic-gate 	 * mp->dmai_pool/roffset = 0;
280*0Sstevel@tonic-gate 	 * mp->dmai_rflags = 0;
281*0Sstevel@tonic-gate 	 * mp->dmai_inuse/flags
282*0Sstevel@tonic-gate 	 * mp->dmai_nwin = 0;
283*0Sstevel@tonic-gate 	 * mp->dmai_winsize = 0;
284*0Sstevel@tonic-gate 	 * mp->dmai_nexus_private/tte = 0;
285*0Sstevel@tonic-gate 	 * mp->dmai_iopte/pfnlst
286*0Sstevel@tonic-gate 	 * mp->dmai_sbi/pfn0 = 0;
287*0Sstevel@tonic-gate 	 * mp->dmai_minfo/winlst/fdvma
288*0Sstevel@tonic-gate 	 * mp->dmai_rdip
289*0Sstevel@tonic-gate 	 * bzero(&mp->dmai_object, sizeof (ddi_dma_obj_t));
290*0Sstevel@tonic-gate 	 * mp->dmai_cookie = 0;
291*0Sstevel@tonic-gate 	 */
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate 	mp->dmai_attr.dma_attr_version = (uint_t)DMA_ATTR_VERSION;
294*0Sstevel@tonic-gate 	mp->dmai_attr.dma_attr_flags = (uint_t)0;
295*0Sstevel@tonic-gate 	mp->dmai_fault = 0;
296*0Sstevel@tonic-gate 	mp->dmai_fault_check = NULL;
297*0Sstevel@tonic-gate 	mp->dmai_fault_notify = NULL;
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate 	mp->dmai_error.err_ena = 0;
300*0Sstevel@tonic-gate 	mp->dmai_error.err_status = DDI_FM_OK;
301*0Sstevel@tonic-gate 	mp->dmai_error.err_expected = DDI_FM_ERR_UNEXPECTED;
302*0Sstevel@tonic-gate 	mp->dmai_error.err_ontrap = NULL;
303*0Sstevel@tonic-gate 	mp->dmai_error.err_fep = NULL;
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate 	SYNC_BUF_PA(mp) = 0ull;
306*0Sstevel@tonic-gate 	return (mp);
307*0Sstevel@tonic-gate }
308*0Sstevel@tonic-gate 
309*0Sstevel@tonic-gate void
310*0Sstevel@tonic-gate pci_dma_freemp(ddi_dma_impl_t *mp)
311*0Sstevel@tonic-gate {
312*0Sstevel@tonic-gate 	if (mp->dmai_ndvmapages > 1)
313*0Sstevel@tonic-gate 		pci_dma_freepfn(mp);
314*0Sstevel@tonic-gate 	if (mp->dmai_winlst)
315*0Sstevel@tonic-gate 		pci_dma_freewin(mp);
316*0Sstevel@tonic-gate 	kmem_free(mp, sizeof (pci_dma_hdl_t));
317*0Sstevel@tonic-gate }
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate void
320*0Sstevel@tonic-gate pci_dma_freepfn(ddi_dma_impl_t *mp)
321*0Sstevel@tonic-gate {
322*0Sstevel@tonic-gate 	void *addr = mp->dmai_pfnlst;
323*0Sstevel@tonic-gate 	ASSERT(!PCI_DMA_CANRELOC(mp));
324*0Sstevel@tonic-gate 	if (addr) {
325*0Sstevel@tonic-gate 		size_t npages = mp->dmai_ndvmapages;
326*0Sstevel@tonic-gate 		if (npages > 1)
327*0Sstevel@tonic-gate 			kmem_free(addr, npages * sizeof (iopfn_t));
328*0Sstevel@tonic-gate 		mp->dmai_pfnlst = NULL;
329*0Sstevel@tonic-gate 	}
330*0Sstevel@tonic-gate 	mp->dmai_ndvmapages = 0;
331*0Sstevel@tonic-gate }
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate /*
334*0Sstevel@tonic-gate  * pci_dma_lmts2hdl - alloate a ddi_dma_impl_t, validate practical limits
335*0Sstevel@tonic-gate  *			and convert dmareq->dmar_limits to mp->dmai_attr
336*0Sstevel@tonic-gate  *
337*0Sstevel@tonic-gate  * ddi_dma_impl_t member modified     input
338*0Sstevel@tonic-gate  * ------------------------------------------------------------------------
339*0Sstevel@tonic-gate  * mp->dmai_minxfer		    - dev
340*0Sstevel@tonic-gate  * mp->dmai_burstsizes		    - dev
341*0Sstevel@tonic-gate  * mp->dmai_flags		    - no limit? peer-to-peer only?
342*0Sstevel@tonic-gate  *
343*0Sstevel@tonic-gate  * ddi_dma_attr member modified       input
344*0Sstevel@tonic-gate  * ------------------------------------------------------------------------
345*0Sstevel@tonic-gate  * mp->dmai_attr.dma_attr_addr_lo   - dev lo, sys lo
346*0Sstevel@tonic-gate  * mp->dmai_attr.dma_attr_addr_hi   - dev hi, sys hi
347*0Sstevel@tonic-gate  * mp->dmai_attr.dma_attr_count_max - dev count max, dev/sys lo/hi delta
348*0Sstevel@tonic-gate  * mp->dmai_attr.dma_attr_seg       - 0         (no nocross   restriction)
349*0Sstevel@tonic-gate  * mp->dmai_attr.dma_attr_align     - 1		(no alignment restriction)
350*0Sstevel@tonic-gate  *
351*0Sstevel@tonic-gate  * The dlim_dmaspeed member of dmareq->dmar_limits is ignored.
352*0Sstevel@tonic-gate  */
353*0Sstevel@tonic-gate ddi_dma_impl_t *
354*0Sstevel@tonic-gate pci_dma_lmts2hdl(dev_info_t *dip, dev_info_t *rdip, iommu_t *iommu_p,
355*0Sstevel@tonic-gate 	ddi_dma_req_t *dmareq)
356*0Sstevel@tonic-gate {
357*0Sstevel@tonic-gate 	ddi_dma_impl_t *mp;
358*0Sstevel@tonic-gate 	ddi_dma_attr_t *attr_p;
359*0Sstevel@tonic-gate 	uint64_t syslo		= iommu_p->iommu_dvma_base;
360*0Sstevel@tonic-gate 	uint64_t syshi		= iommu_p->iommu_dvma_end;
361*0Sstevel@tonic-gate 	uint64_t fasthi		= iommu_p->iommu_dvma_fast_end;
362*0Sstevel@tonic-gate 	ddi_dma_lim_t *lim_p	= dmareq->dmar_limits;
363*0Sstevel@tonic-gate 	uint32_t count_max	= lim_p->dlim_cntr_max;
364*0Sstevel@tonic-gate 	uint64_t lo		= lim_p->dlim_addr_lo;
365*0Sstevel@tonic-gate 	uint64_t hi		= lim_p->dlim_addr_hi;
366*0Sstevel@tonic-gate 	if (hi <= lo) {
367*0Sstevel@tonic-gate 		DEBUG0(DBG_DMA_MAP, dip, "Bad limits\n");
368*0Sstevel@tonic-gate 		return ((ddi_dma_impl_t *)DDI_DMA_NOMAPPING);
369*0Sstevel@tonic-gate 	}
370*0Sstevel@tonic-gate 	if (!count_max)
371*0Sstevel@tonic-gate 		count_max--;
372*0Sstevel@tonic-gate 
373*0Sstevel@tonic-gate 	if (!(mp = pci_dma_allocmp(dip, rdip, dmareq->dmar_fp,
374*0Sstevel@tonic-gate 		dmareq->dmar_arg)))
375*0Sstevel@tonic-gate 		return (NULL);
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate 	/* store original dev input at the 2nd ddi_dma_attr */
378*0Sstevel@tonic-gate 	attr_p = DEV_ATTR(mp);
379*0Sstevel@tonic-gate 	SET_DMAATTR(attr_p, lo, hi, -1, count_max);
380*0Sstevel@tonic-gate 	SET_DMAALIGN(attr_p, 1);
381*0Sstevel@tonic-gate 
382*0Sstevel@tonic-gate 	lo = MAX(lo, syslo);
383*0Sstevel@tonic-gate 	hi = MIN(hi, syshi);
384*0Sstevel@tonic-gate 	if (hi <= lo)
385*0Sstevel@tonic-gate 		mp->dmai_flags |= DMAI_FLAGS_PEER_ONLY;
386*0Sstevel@tonic-gate 	count_max = MIN(count_max, hi - lo);
387*0Sstevel@tonic-gate 
388*0Sstevel@tonic-gate 	if (DEV_NOSYSLIMIT(lo, hi, syslo, fasthi, 1))
389*0Sstevel@tonic-gate 		mp->dmai_flags |= DMAI_FLAGS_NOFASTLIMIT |
390*0Sstevel@tonic-gate 			DMAI_FLAGS_NOSYSLIMIT;
391*0Sstevel@tonic-gate 	else {
392*0Sstevel@tonic-gate 		if (DEV_NOFASTLIMIT(lo, hi, syslo, syshi, 1))
393*0Sstevel@tonic-gate 			mp->dmai_flags |= DMAI_FLAGS_NOFASTLIMIT;
394*0Sstevel@tonic-gate 	}
395*0Sstevel@tonic-gate 	if (PCI_DMA_NOCTX(rdip))
396*0Sstevel@tonic-gate 		mp->dmai_flags |= DMAI_FLAGS_NOCTX;
397*0Sstevel@tonic-gate 
398*0Sstevel@tonic-gate 	/* store augumented dev input to mp->dmai_attr */
399*0Sstevel@tonic-gate 	mp->dmai_minxfer	= lim_p->dlim_minxfer;
400*0Sstevel@tonic-gate 	mp->dmai_burstsizes	= lim_p->dlim_burstsizes;
401*0Sstevel@tonic-gate 	attr_p = &mp->dmai_attr;
402*0Sstevel@tonic-gate 	SET_DMAATTR(attr_p, lo, hi, -1, count_max);
403*0Sstevel@tonic-gate 	SET_DMAALIGN(attr_p, 1);
404*0Sstevel@tonic-gate 	return (mp);
405*0Sstevel@tonic-gate }
406*0Sstevel@tonic-gate 
407*0Sstevel@tonic-gate /*
408*0Sstevel@tonic-gate  * pci_dma_attr2hdl
409*0Sstevel@tonic-gate  *
410*0Sstevel@tonic-gate  * This routine is called from the alloc handle entry point to sanity check the
411*0Sstevel@tonic-gate  * dma attribute structure.
412*0Sstevel@tonic-gate  *
413*0Sstevel@tonic-gate  * use by: pci_dma_allochdl()
414*0Sstevel@tonic-gate  *
415*0Sstevel@tonic-gate  * return value:
416*0Sstevel@tonic-gate  *
417*0Sstevel@tonic-gate  *	DDI_SUCCESS		- on success
418*0Sstevel@tonic-gate  *	DDI_DMA_BADATTR		- attribute has invalid version number
419*0Sstevel@tonic-gate  *				  or address limits exclude dvma space
420*0Sstevel@tonic-gate  */
421*0Sstevel@tonic-gate int
422*0Sstevel@tonic-gate pci_dma_attr2hdl(pci_t *pci_p, ddi_dma_impl_t *mp)
423*0Sstevel@tonic-gate {
424*0Sstevel@tonic-gate 	iommu_t *iommu_p = pci_p->pci_iommu_p;
425*0Sstevel@tonic-gate 	uint64_t syslo, syshi;
426*0Sstevel@tonic-gate 	ddi_dma_attr_t *attrp		= DEV_ATTR(mp);
427*0Sstevel@tonic-gate 	uint64_t hi		= attrp->dma_attr_addr_hi;
428*0Sstevel@tonic-gate 	uint64_t lo		= attrp->dma_attr_addr_lo;
429*0Sstevel@tonic-gate 	uint64_t align		= attrp->dma_attr_align;
430*0Sstevel@tonic-gate 	uint64_t nocross	= attrp->dma_attr_seg;
431*0Sstevel@tonic-gate 	uint64_t count_max	= attrp->dma_attr_count_max;
432*0Sstevel@tonic-gate 
433*0Sstevel@tonic-gate 	DEBUG3(DBG_DMA_ALLOCH, pci_p->pci_dip, "attrp=%p cntr_max=%x.%08x\n",
434*0Sstevel@tonic-gate 		attrp, HI32(count_max), LO32(count_max));
435*0Sstevel@tonic-gate 	DEBUG4(DBG_DMA_ALLOCH, pci_p->pci_dip, "hi=%x.%08x lo=%x.%08x\n",
436*0Sstevel@tonic-gate 		HI32(hi), LO32(hi), HI32(lo), LO32(lo));
437*0Sstevel@tonic-gate 	DEBUG4(DBG_DMA_ALLOCH, pci_p->pci_dip, "seg=%x.%08x align=%x.%08x\n",
438*0Sstevel@tonic-gate 		HI32(nocross), LO32(nocross), HI32(align), LO32(align));
439*0Sstevel@tonic-gate 
440*0Sstevel@tonic-gate 	if (!nocross)
441*0Sstevel@tonic-gate 		nocross--;
442*0Sstevel@tonic-gate 	if (attrp->dma_attr_flags & DDI_DMA_FORCE_PHYSICAL) { /* BYPASS */
443*0Sstevel@tonic-gate 
444*0Sstevel@tonic-gate 		DEBUG0(DBG_DMA_ALLOCH, pci_p->pci_dip, "bypass mode\n");
445*0Sstevel@tonic-gate 		/* if tomatillo ver <= 2.3 don't allow bypass */
446*0Sstevel@tonic-gate 		if (tomatillo_disallow_bypass)
447*0Sstevel@tonic-gate 			return (DDI_DMA_BADATTR);
448*0Sstevel@tonic-gate 
449*0Sstevel@tonic-gate 		mp->dmai_flags |= DMAI_FLAGS_BYPASSREQ;
450*0Sstevel@tonic-gate 		if (nocross != UINT64_MAX)
451*0Sstevel@tonic-gate 			return (DDI_DMA_BADATTR);
452*0Sstevel@tonic-gate 		if (align && (align > IOMMU_PAGE_SIZE))
453*0Sstevel@tonic-gate 			return (DDI_DMA_BADATTR);
454*0Sstevel@tonic-gate 		align = 1; /* align on 1 page boundary */
455*0Sstevel@tonic-gate 		syslo = iommu_p->iommu_dma_bypass_base;
456*0Sstevel@tonic-gate 		syshi = iommu_p->iommu_dma_bypass_end;
457*0Sstevel@tonic-gate 
458*0Sstevel@tonic-gate 	} else { /* IOMMU_XLATE or PEER_TO_PEER */
459*0Sstevel@tonic-gate 		align = MAX(align, IOMMU_PAGE_SIZE) - 1;
460*0Sstevel@tonic-gate 		if ((align & nocross) != align) {
461*0Sstevel@tonic-gate 			dev_info_t *rdip = mp->dmai_rdip;
462*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d dma_attr_seg not aligned",
463*0Sstevel@tonic-gate 				NAMEINST(rdip));
464*0Sstevel@tonic-gate 			return (DDI_DMA_BADATTR);
465*0Sstevel@tonic-gate 		}
466*0Sstevel@tonic-gate 		align = IOMMU_BTOP(align + 1);
467*0Sstevel@tonic-gate 		syslo = iommu_p->iommu_dvma_base;
468*0Sstevel@tonic-gate 		syshi = iommu_p->iommu_dvma_end;
469*0Sstevel@tonic-gate 	}
470*0Sstevel@tonic-gate 	if (hi <= lo) {
471*0Sstevel@tonic-gate 		dev_info_t *rdip = mp->dmai_rdip;
472*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d limits out of range", NAMEINST(rdip));
473*0Sstevel@tonic-gate 		return (DDI_DMA_BADATTR);
474*0Sstevel@tonic-gate 	}
475*0Sstevel@tonic-gate 	lo = MAX(lo, syslo);
476*0Sstevel@tonic-gate 	hi = MIN(hi, syshi);
477*0Sstevel@tonic-gate 	if (!count_max)
478*0Sstevel@tonic-gate 		count_max--;
479*0Sstevel@tonic-gate 
480*0Sstevel@tonic-gate 	DEBUG4(DBG_DMA_ALLOCH, pci_p->pci_dip, "hi=%x.%08x, lo=%x.%08x\n",
481*0Sstevel@tonic-gate 		HI32(hi), LO32(hi), HI32(lo), LO32(lo));
482*0Sstevel@tonic-gate 	if (hi <= lo) { /* peer transfers cannot have alignment & nocross */
483*0Sstevel@tonic-gate 		dev_info_t *rdip = mp->dmai_rdip;
484*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d peer only dev %p", NAMEINST(rdip), mp);
485*0Sstevel@tonic-gate 		if ((nocross < UINT32_MAX) || (align > 1)) {
486*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d peer only device bad attr",
487*0Sstevel@tonic-gate 				NAMEINST(rdip));
488*0Sstevel@tonic-gate 			return (DDI_DMA_BADATTR);
489*0Sstevel@tonic-gate 		}
490*0Sstevel@tonic-gate 		mp->dmai_flags |= DMAI_FLAGS_PEER_ONLY;
491*0Sstevel@tonic-gate 	} else /* set practical counter_max value */
492*0Sstevel@tonic-gate 		count_max = MIN(count_max, hi - lo);
493*0Sstevel@tonic-gate 
494*0Sstevel@tonic-gate 	if (DEV_NOSYSLIMIT(lo, hi, syslo, syshi, align))
495*0Sstevel@tonic-gate 		mp->dmai_flags |= DMAI_FLAGS_NOSYSLIMIT |
496*0Sstevel@tonic-gate 			DMAI_FLAGS_NOFASTLIMIT;
497*0Sstevel@tonic-gate 	else {
498*0Sstevel@tonic-gate 		syshi = iommu_p->iommu_dvma_fast_end;
499*0Sstevel@tonic-gate 		if (DEV_NOFASTLIMIT(lo, hi, syslo, syshi, align))
500*0Sstevel@tonic-gate 			mp->dmai_flags |= DMAI_FLAGS_NOFASTLIMIT;
501*0Sstevel@tonic-gate 	}
502*0Sstevel@tonic-gate 	if (PCI_DMA_NOCTX(mp->dmai_rdip))
503*0Sstevel@tonic-gate 		mp->dmai_flags |= DMAI_FLAGS_NOCTX;
504*0Sstevel@tonic-gate 
505*0Sstevel@tonic-gate 	mp->dmai_minxfer	= attrp->dma_attr_minxfer;
506*0Sstevel@tonic-gate 	mp->dmai_burstsizes	= attrp->dma_attr_burstsizes;
507*0Sstevel@tonic-gate 	attrp = &mp->dmai_attr;
508*0Sstevel@tonic-gate 	SET_DMAATTR(attrp, lo, hi, nocross, count_max);
509*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
510*0Sstevel@tonic-gate }
511*0Sstevel@tonic-gate 
512*0Sstevel@tonic-gate /*
513*0Sstevel@tonic-gate  * set up consistent dma flags according to hardware capability
514*0Sstevel@tonic-gate  */
515*0Sstevel@tonic-gate uint32_t
516*0Sstevel@tonic-gate pci_dma_consist_check(uint32_t req_flags, pbm_t *pbm_p)
517*0Sstevel@tonic-gate {
518*0Sstevel@tonic-gate 	if (!pci_stream_buf_enable || !pci_stream_buf_exists)
519*0Sstevel@tonic-gate 		req_flags |= DDI_DMA_CONSISTENT;
520*0Sstevel@tonic-gate 	if (req_flags & DDI_DMA_CONSISTENT && !pbm_p->pbm_sync_reg_pa)
521*0Sstevel@tonic-gate 		req_flags |= DMP_NOSYNC;
522*0Sstevel@tonic-gate 	return (req_flags);
523*0Sstevel@tonic-gate }
524*0Sstevel@tonic-gate 
525*0Sstevel@tonic-gate #define	TGT_PFN_INBETWEEN(pfn, bgn, end) ((pfn >= bgn) && (pfn <= end))
526*0Sstevel@tonic-gate 
527*0Sstevel@tonic-gate /*
528*0Sstevel@tonic-gate  * pci_dma_type - determine which of the three types DMA (peer-to-peer,
529*0Sstevel@tonic-gate  *		iommu bypass, or iommu translate) we are asked to do.
530*0Sstevel@tonic-gate  *		Also checks pfn0 and rejects any non-peer-to-peer
531*0Sstevel@tonic-gate  *		requests for peer-only devices.
532*0Sstevel@tonic-gate  *
533*0Sstevel@tonic-gate  *	return values:
534*0Sstevel@tonic-gate  *		DDI_DMA_NOMAPPING - can't get valid pfn0, or bad dma type
535*0Sstevel@tonic-gate  *		DDI_SUCCESS
536*0Sstevel@tonic-gate  *
537*0Sstevel@tonic-gate  *	dma handle members affected (set on exit):
538*0Sstevel@tonic-gate  *	mp->dmai_object		- dmareq->dmar_object
539*0Sstevel@tonic-gate  *	mp->dmai_rflags		- consistent?, nosync?, dmareq->dmar_flags
540*0Sstevel@tonic-gate  *	mp->dmai_flags   	- DMA type
541*0Sstevel@tonic-gate  *	mp->dmai_pfn0   	- 1st page pfn (if va/size pair and not shadow)
542*0Sstevel@tonic-gate  *	mp->dmai_roffset 	- initialized to starting IOMMU page offset
543*0Sstevel@tonic-gate  *	mp->dmai_ndvmapages	- # of total IOMMU pages of entire object
544*0Sstevel@tonic-gate  *	mp->pdh_sync_buf_pa	- dma sync buffer PA is DMA flow is supported
545*0Sstevel@tonic-gate  */
546*0Sstevel@tonic-gate int
547*0Sstevel@tonic-gate pci_dma_type(pci_t *pci_p, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp)
548*0Sstevel@tonic-gate {
549*0Sstevel@tonic-gate 	dev_info_t *dip = pci_p->pci_dip;
550*0Sstevel@tonic-gate 	ddi_dma_obj_t *dobj_p = &dmareq->dmar_object;
551*0Sstevel@tonic-gate 	pbm_t *pbm_p = pci_p->pci_pbm_p;
552*0Sstevel@tonic-gate 	page_t **pplist;
553*0Sstevel@tonic-gate 	struct as *as_p;
554*0Sstevel@tonic-gate 	uint32_t offset;
555*0Sstevel@tonic-gate 	caddr_t vaddr;
556*0Sstevel@tonic-gate 	pfn_t pfn0;
557*0Sstevel@tonic-gate 
558*0Sstevel@tonic-gate 	mp->dmai_rflags = pci_dma_consist_check(dmareq->dmar_flags, pbm_p);
559*0Sstevel@tonic-gate 	mp->dmai_flags |= mp->dmai_rflags & DMP_NOSYNC ? DMAI_FLAGS_NOSYNC : 0;
560*0Sstevel@tonic-gate 
561*0Sstevel@tonic-gate 	switch (dobj_p->dmao_type) {
562*0Sstevel@tonic-gate 	case DMA_OTYP_BUFVADDR:
563*0Sstevel@tonic-gate 	case DMA_OTYP_VADDR: {
564*0Sstevel@tonic-gate 		vaddr = dobj_p->dmao_obj.virt_obj.v_addr;
565*0Sstevel@tonic-gate 		pplist = dobj_p->dmao_obj.virt_obj.v_priv;
566*0Sstevel@tonic-gate 		as_p = dobj_p->dmao_obj.virt_obj.v_as;
567*0Sstevel@tonic-gate 		if (as_p == NULL)
568*0Sstevel@tonic-gate 			as_p = &kas;
569*0Sstevel@tonic-gate 
570*0Sstevel@tonic-gate 		DEBUG2(DBG_DMA_MAP, dip, "vaddr=%p pplist=%p\n", vaddr, pplist);
571*0Sstevel@tonic-gate 		offset = (ulong_t)vaddr & IOMMU_PAGE_OFFSET;
572*0Sstevel@tonic-gate 
573*0Sstevel@tonic-gate 		if (pplist) {				/* shadow list */
574*0Sstevel@tonic-gate 			mp->dmai_flags |= DMAI_FLAGS_PGPFN;
575*0Sstevel@tonic-gate 			ASSERT(PAGE_LOCKED(*pplist));
576*0Sstevel@tonic-gate 			pfn0 = page_pptonum(*pplist);
577*0Sstevel@tonic-gate 		} else if (pci_dvma_remap_enabled && as_p == &kas &&
578*0Sstevel@tonic-gate 			dobj_p->dmao_type != DMA_OTYP_BUFVADDR) {
579*0Sstevel@tonic-gate 			int (*waitfp)(caddr_t) = dmareq->dmar_fp;
580*0Sstevel@tonic-gate 			uint_t flags = ((waitfp == DDI_DMA_SLEEP)?
581*0Sstevel@tonic-gate 				    HAC_SLEEP : HAC_NOSLEEP) | HAC_PAGELOCK;
582*0Sstevel@tonic-gate 			int ret;
583*0Sstevel@tonic-gate 
584*0Sstevel@tonic-gate 			ret = hat_add_callback(pci_dvma_cbid, vaddr,
585*0Sstevel@tonic-gate 			    IOMMU_PAGE_SIZE - offset, flags, mp, &pfn0);
586*0Sstevel@tonic-gate 
587*0Sstevel@tonic-gate 			if (pfn0 == PFN_INVALID && ret == ENOMEM) {
588*0Sstevel@tonic-gate 				ASSERT(waitfp != DDI_DMA_SLEEP);
589*0Sstevel@tonic-gate 				if (waitfp != DDI_DMA_DONTWAIT) {
590*0Sstevel@tonic-gate 					ddi_set_callback(waitfp,
591*0Sstevel@tonic-gate 					    dmareq->dmar_arg,
592*0Sstevel@tonic-gate 					    &pci_kmem_clid);
593*0Sstevel@tonic-gate 					return (DDI_DMA_NORESOURCES);
594*0Sstevel@tonic-gate 					}
595*0Sstevel@tonic-gate 			}
596*0Sstevel@tonic-gate 			mp->dmai_flags |= DMAI_FLAGS_RELOC;
597*0Sstevel@tonic-gate 		} else
598*0Sstevel@tonic-gate 			pfn0 = hat_getpfnum(as_p->a_hat, vaddr);
599*0Sstevel@tonic-gate 		}
600*0Sstevel@tonic-gate 		break;
601*0Sstevel@tonic-gate 
602*0Sstevel@tonic-gate 	case DMA_OTYP_PAGES:
603*0Sstevel@tonic-gate 		offset = dobj_p->dmao_obj.pp_obj.pp_offset;
604*0Sstevel@tonic-gate 		mp->dmai_flags |= DMAI_FLAGS_PGPFN;
605*0Sstevel@tonic-gate 		pfn0 = page_pptonum(dobj_p->dmao_obj.pp_obj.pp_pp);
606*0Sstevel@tonic-gate 		ASSERT(PAGE_LOCKED(dobj_p->dmao_obj.pp_obj.pp_pp));
607*0Sstevel@tonic-gate 		break;
608*0Sstevel@tonic-gate 
609*0Sstevel@tonic-gate 	case DMA_OTYP_PADDR:
610*0Sstevel@tonic-gate 	default:
611*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d requested unsupported dma type %x",
612*0Sstevel@tonic-gate 			NAMEINST(mp->dmai_rdip), dobj_p->dmao_type);
613*0Sstevel@tonic-gate 		return (DDI_DMA_NOMAPPING);
614*0Sstevel@tonic-gate 	}
615*0Sstevel@tonic-gate 	if (pfn0 == PFN_INVALID) {
616*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: invalid pfn0 for DMA object %p",
617*0Sstevel@tonic-gate 			NAMEINST(dip), dobj_p);
618*0Sstevel@tonic-gate 		return (DDI_DMA_NOMAPPING);
619*0Sstevel@tonic-gate 	}
620*0Sstevel@tonic-gate 	if (TGT_PFN_INBETWEEN(pfn0, pbm_p->pbm_base_pfn, pbm_p->pbm_last_pfn)) {
621*0Sstevel@tonic-gate 		mp->dmai_flags |= DMAI_FLAGS_PEER_TO_PEER;
622*0Sstevel@tonic-gate 		goto done;	/* leave bypass and dvma flag as 0 */
623*0Sstevel@tonic-gate 	}
624*0Sstevel@tonic-gate 	if (PCI_DMA_ISPEERONLY(mp)) {
625*0Sstevel@tonic-gate 		dev_info_t *rdip = mp->dmai_rdip;
626*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "Bad peer-to-peer req %s%d", NAMEINST(rdip));
627*0Sstevel@tonic-gate 		return (DDI_DMA_NOMAPPING);
628*0Sstevel@tonic-gate 	}
629*0Sstevel@tonic-gate 	mp->dmai_flags |= (mp->dmai_flags & DMAI_FLAGS_BYPASSREQ) ?
630*0Sstevel@tonic-gate 		DMAI_FLAGS_BYPASS : DMAI_FLAGS_DVMA;
631*0Sstevel@tonic-gate done:
632*0Sstevel@tonic-gate 	mp->dmai_object	 = *dobj_p;			/* whole object    */
633*0Sstevel@tonic-gate 	mp->dmai_pfn0	 = (void *)pfn0;		/* cache pfn0	   */
634*0Sstevel@tonic-gate 	mp->dmai_roffset = offset;			/* win0 pg0 offset */
635*0Sstevel@tonic-gate 	mp->dmai_ndvmapages = IOMMU_BTOPR(offset + mp->dmai_object.dmao_size);
636*0Sstevel@tonic-gate 
637*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
638*0Sstevel@tonic-gate }
639*0Sstevel@tonic-gate 
640*0Sstevel@tonic-gate /*
641*0Sstevel@tonic-gate  * pci_dma_pgpfn - set up pfnlst array according to pages
642*0Sstevel@tonic-gate  *	VA/size pair: <shadow IO, bypass, peer-to-peer>, or OTYP_PAGES
643*0Sstevel@tonic-gate  */
644*0Sstevel@tonic-gate /*ARGSUSED*/
645*0Sstevel@tonic-gate static int
646*0Sstevel@tonic-gate pci_dma_pgpfn(pci_t *pci_p, ddi_dma_impl_t *mp, uint_t npages)
647*0Sstevel@tonic-gate {
648*0Sstevel@tonic-gate 	int i;
649*0Sstevel@tonic-gate #ifdef DEBUG
650*0Sstevel@tonic-gate 	dev_info_t *dip = pci_p->pci_dip;
651*0Sstevel@tonic-gate #endif
652*0Sstevel@tonic-gate 	switch (mp->dmai_object.dmao_type) {
653*0Sstevel@tonic-gate 	case DMA_OTYP_BUFVADDR:
654*0Sstevel@tonic-gate 	case DMA_OTYP_VADDR: {
655*0Sstevel@tonic-gate 		page_t **pplist = mp->dmai_object.dmao_obj.virt_obj.v_priv;
656*0Sstevel@tonic-gate 		DEBUG2(DBG_DMA_MAP, dip, "shadow pplist=%p, %x pages, pfns=",
657*0Sstevel@tonic-gate 			pplist, npages);
658*0Sstevel@tonic-gate 		for (i = 1; i < npages; i++) {
659*0Sstevel@tonic-gate 			iopfn_t pfn = page_pptonum(pplist[i]);
660*0Sstevel@tonic-gate 			ASSERT(PAGE_LOCKED(pplist[i]));
661*0Sstevel@tonic-gate 			PCI_SET_MP_PFN1(mp, i, pfn);
662*0Sstevel@tonic-gate 			DEBUG1(DBG_DMA_MAP|DBG_CONT, dip, "%x ", pfn);
663*0Sstevel@tonic-gate 		}
664*0Sstevel@tonic-gate 		DEBUG0(DBG_DMA_MAP|DBG_CONT, dip, "\n");
665*0Sstevel@tonic-gate 		}
666*0Sstevel@tonic-gate 		break;
667*0Sstevel@tonic-gate 
668*0Sstevel@tonic-gate 	case DMA_OTYP_PAGES: {
669*0Sstevel@tonic-gate 		page_t *pp = mp->dmai_object.dmao_obj.pp_obj.pp_pp->p_next;
670*0Sstevel@tonic-gate 		DEBUG1(DBG_DMA_MAP, dip, "pp=%p pfns=", pp);
671*0Sstevel@tonic-gate 		for (i = 1; i < npages; i++, pp = pp->p_next) {
672*0Sstevel@tonic-gate 			iopfn_t pfn = page_pptonum(pp);
673*0Sstevel@tonic-gate 			ASSERT(PAGE_LOCKED(pp));
674*0Sstevel@tonic-gate 			PCI_SET_MP_PFN1(mp, i, pfn);
675*0Sstevel@tonic-gate 			DEBUG1(DBG_DMA_MAP|DBG_CONT, dip, "%x ", pfn);
676*0Sstevel@tonic-gate 		}
677*0Sstevel@tonic-gate 		DEBUG0(DBG_DMA_MAP|DBG_CONT, dip, "\n");
678*0Sstevel@tonic-gate 		}
679*0Sstevel@tonic-gate 		break;
680*0Sstevel@tonic-gate 
681*0Sstevel@tonic-gate 	default:	/* check is already done by pci_dma_type */
682*0Sstevel@tonic-gate 		ASSERT(0);
683*0Sstevel@tonic-gate 		break;
684*0Sstevel@tonic-gate 	}
685*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
686*0Sstevel@tonic-gate }
687*0Sstevel@tonic-gate 
688*0Sstevel@tonic-gate /*
689*0Sstevel@tonic-gate  * pci_dma_vapfn - set up pfnlst array according to VA
690*0Sstevel@tonic-gate  *	VA/size pair: <normal, bypass, peer-to-peer>
691*0Sstevel@tonic-gate  *	pfn0 is skipped as it is already done.
692*0Sstevel@tonic-gate  *	In this case, the cached pfn0 is used to fill pfnlst[0]
693*0Sstevel@tonic-gate  */
694*0Sstevel@tonic-gate static int
695*0Sstevel@tonic-gate pci_dma_vapfn(pci_t *pci_p, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp,
696*0Sstevel@tonic-gate 	uint_t npages)
697*0Sstevel@tonic-gate {
698*0Sstevel@tonic-gate 	dev_info_t *dip = pci_p->pci_dip;
699*0Sstevel@tonic-gate 	int i;
700*0Sstevel@tonic-gate 	caddr_t vaddr = (caddr_t)mp->dmai_object.dmao_obj.virt_obj.v_as;
701*0Sstevel@tonic-gate 	struct hat *hat_p = vaddr ? ((struct as *)vaddr)->a_hat : kas.a_hat;
702*0Sstevel@tonic-gate 	caddr_t sva;
703*0Sstevel@tonic-gate 	int needcb = 0;
704*0Sstevel@tonic-gate 
705*0Sstevel@tonic-gate 	sva = (caddr_t)(((uintptr_t)mp->dmai_object.dmao_obj.virt_obj.v_addr +
706*0Sstevel@tonic-gate 	    IOMMU_PAGE_SIZE) & IOMMU_PAGE_MASK);
707*0Sstevel@tonic-gate 
708*0Sstevel@tonic-gate 	if (pci_dvma_remap_enabled && hat_p == kas.a_hat &&
709*0Sstevel@tonic-gate 		mp->dmai_object.dmao_type != DMA_OTYP_BUFVADDR)
710*0Sstevel@tonic-gate 		needcb = 1;
711*0Sstevel@tonic-gate 
712*0Sstevel@tonic-gate 	for (vaddr = sva, i = 1; i < npages; i++, vaddr += IOMMU_PAGE_SIZE) {
713*0Sstevel@tonic-gate 		pfn_t pfn;
714*0Sstevel@tonic-gate 
715*0Sstevel@tonic-gate 		if (needcb) {
716*0Sstevel@tonic-gate 			int (*waitfp)(caddr_t) = dmareq->dmar_fp;
717*0Sstevel@tonic-gate 			uint_t flags = ((waitfp == DDI_DMA_SLEEP)?
718*0Sstevel@tonic-gate 			    HAC_SLEEP : HAC_NOSLEEP) | HAC_PAGELOCK;
719*0Sstevel@tonic-gate 			int ret;
720*0Sstevel@tonic-gate 
721*0Sstevel@tonic-gate 			ret = hat_add_callback(pci_dvma_cbid, vaddr,
722*0Sstevel@tonic-gate 			    IOMMU_PAGE_SIZE, flags, mp, &pfn);
723*0Sstevel@tonic-gate 			if (pfn == PFN_INVALID && ret == ENOMEM) {
724*0Sstevel@tonic-gate 				ASSERT(waitfp != DDI_DMA_SLEEP);
725*0Sstevel@tonic-gate 				if (waitfp != DDI_DMA_DONTWAIT)
726*0Sstevel@tonic-gate 					ddi_set_callback(waitfp,
727*0Sstevel@tonic-gate 					    dmareq->dmar_arg, &pci_kmem_clid);
728*0Sstevel@tonic-gate 				return (DDI_DMA_NORESOURCES);
729*0Sstevel@tonic-gate 			}
730*0Sstevel@tonic-gate 		} else
731*0Sstevel@tonic-gate 			pfn = hat_getpfnum(hat_p, vaddr);
732*0Sstevel@tonic-gate 		if (pfn == PFN_INVALID)
733*0Sstevel@tonic-gate 			goto err_badpfn;
734*0Sstevel@tonic-gate 		PCI_SET_MP_PFN1(mp, i, (iopfn_t)pfn);
735*0Sstevel@tonic-gate 		DEBUG3(DBG_DMA_MAP, dip, "pci_dma_vapfn: mp=%p pfnlst[%x]=%x\n",
736*0Sstevel@tonic-gate 			mp, i, (iopfn_t)pfn);
737*0Sstevel@tonic-gate 	}
738*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
739*0Sstevel@tonic-gate err_badpfn:
740*0Sstevel@tonic-gate 	cmn_err(CE_WARN, "%s%d: bad page frame vaddr=%p", NAMEINST(dip), vaddr);
741*0Sstevel@tonic-gate 	return (DDI_DMA_NOMAPPING);
742*0Sstevel@tonic-gate }
743*0Sstevel@tonic-gate 
744*0Sstevel@tonic-gate /*
745*0Sstevel@tonic-gate  * pci_dma_pfn - Fills pfn list for all pages being DMA-ed.
746*0Sstevel@tonic-gate  *
747*0Sstevel@tonic-gate  * dependencies:
748*0Sstevel@tonic-gate  *	mp->dmai_ndvmapages	- set to total # of dma pages
749*0Sstevel@tonic-gate  *
750*0Sstevel@tonic-gate  * return value:
751*0Sstevel@tonic-gate  *	DDI_SUCCESS
752*0Sstevel@tonic-gate  *	DDI_DMA_NOMAPPING
753*0Sstevel@tonic-gate  */
754*0Sstevel@tonic-gate int
755*0Sstevel@tonic-gate pci_dma_pfn(pci_t *pci_p, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp)
756*0Sstevel@tonic-gate {
757*0Sstevel@tonic-gate 	uint32_t npages = mp->dmai_ndvmapages;
758*0Sstevel@tonic-gate 	int (*waitfp)(caddr_t) = dmareq->dmar_fp;
759*0Sstevel@tonic-gate 	int i, ret, peer = PCI_DMA_ISPTP(mp);
760*0Sstevel@tonic-gate 
761*0Sstevel@tonic-gate 	pbm_t *pbm_p = pci_p->pci_pbm_p;
762*0Sstevel@tonic-gate 	iopfn_t pfn_base = pbm_p->pbm_base_pfn;
763*0Sstevel@tonic-gate 	iopfn_t pfn_last = pbm_p->pbm_last_pfn;
764*0Sstevel@tonic-gate 	iopfn_t pfn_adj = peer ? pfn_base : 0;
765*0Sstevel@tonic-gate 
766*0Sstevel@tonic-gate 	DEBUG2(DBG_DMA_MAP, pci_p->pci_dip, "pci_dma_pfn: mp=%p pfn0=%x\n",
767*0Sstevel@tonic-gate 		mp, MP_PFN0(mp) - pfn_adj);
768*0Sstevel@tonic-gate 	/* 1 page: no array alloc/fill, no mixed mode check */
769*0Sstevel@tonic-gate 	if (npages == 1) {
770*0Sstevel@tonic-gate 		PCI_SET_MP_PFN(mp, 0, MP_PFN0(mp) - pfn_adj);
771*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
772*0Sstevel@tonic-gate 	}
773*0Sstevel@tonic-gate 	/* allocate pfn array */
774*0Sstevel@tonic-gate 	if (!(mp->dmai_pfnlst = kmem_alloc(npages * sizeof (iopfn_t),
775*0Sstevel@tonic-gate 		waitfp == DDI_DMA_SLEEP ? KM_SLEEP : KM_NOSLEEP))) {
776*0Sstevel@tonic-gate 		if (waitfp != DDI_DMA_DONTWAIT)
777*0Sstevel@tonic-gate 			ddi_set_callback(waitfp, dmareq->dmar_arg,
778*0Sstevel@tonic-gate 				&pci_kmem_clid);
779*0Sstevel@tonic-gate 		return (DDI_DMA_NORESOURCES);
780*0Sstevel@tonic-gate 	}
781*0Sstevel@tonic-gate 	/* fill pfn array */
782*0Sstevel@tonic-gate 	PCI_SET_MP_PFN(mp, 0, MP_PFN0(mp) - pfn_adj);	/* pfnlst[0] */
783*0Sstevel@tonic-gate 	if ((ret = PCI_DMA_ISPGPFN(mp) ? pci_dma_pgpfn(pci_p, mp, npages) :
784*0Sstevel@tonic-gate 		pci_dma_vapfn(pci_p, dmareq, mp, npages)) != DDI_SUCCESS)
785*0Sstevel@tonic-gate 		goto err;
786*0Sstevel@tonic-gate 
787*0Sstevel@tonic-gate 	/* skip pfn0, check mixed mode and adjust peer to peer pfn */
788*0Sstevel@tonic-gate 	for (i = 1; i < npages; i++) {
789*0Sstevel@tonic-gate 		iopfn_t pfn = PCI_GET_MP_PFN1(mp, i);
790*0Sstevel@tonic-gate 		if (peer ^ TGT_PFN_INBETWEEN(pfn, pfn_base, pfn_last)) {
791*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d mixed mode DMA %x %x",
792*0Sstevel@tonic-gate 				NAMEINST(mp->dmai_rdip), MP_PFN0(mp), pfn);
793*0Sstevel@tonic-gate 			ret = DDI_DMA_NOMAPPING;	/* mixed mode */
794*0Sstevel@tonic-gate 			goto err;
795*0Sstevel@tonic-gate 		}
796*0Sstevel@tonic-gate 		DEBUG3(DBG_DMA_MAP, pci_p->pci_dip,
797*0Sstevel@tonic-gate 			"pci_dma_pfn: pfnlst[%x]=%x-%x\n", i, pfn, pfn_adj);
798*0Sstevel@tonic-gate 		if (pfn_adj)
799*0Sstevel@tonic-gate 			PCI_SET_MP_PFN1(mp, i, pfn - pfn_adj);
800*0Sstevel@tonic-gate 	}
801*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
802*0Sstevel@tonic-gate err:
803*0Sstevel@tonic-gate 	pci_dvma_unregister_callbacks(pci_p, mp);
804*0Sstevel@tonic-gate 	pci_dma_freepfn(mp);
805*0Sstevel@tonic-gate 	return (ret);
806*0Sstevel@tonic-gate }
807*0Sstevel@tonic-gate 
808*0Sstevel@tonic-gate /*
809*0Sstevel@tonic-gate  * pci_dvma_win() - trim requested DVMA size down to window size
810*0Sstevel@tonic-gate  *	The 1st window starts from offset and ends at page-aligned boundary.
811*0Sstevel@tonic-gate  *	From the 2nd window on, each window starts and ends at page-aligned
812*0Sstevel@tonic-gate  *	boundary except the last window ends at wherever requested.
813*0Sstevel@tonic-gate  *
814*0Sstevel@tonic-gate  *	accesses the following mp-> members:
815*0Sstevel@tonic-gate  *	mp->dmai_attr.dma_attr_count_max
816*0Sstevel@tonic-gate  *	mp->dmai_attr.dma_attr_seg
817*0Sstevel@tonic-gate  *	mp->dmai_roffset   - start offset of 1st window
818*0Sstevel@tonic-gate  *	mp->dmai_rflags (redzone)
819*0Sstevel@tonic-gate  *	mp->dmai_ndvmapages (for 1 page fast path)
820*0Sstevel@tonic-gate  *
821*0Sstevel@tonic-gate  *	sets the following mp-> members:
822*0Sstevel@tonic-gate  *	mp->dmai_size	   - xfer size, != winsize if 1st/last win  (not fixed)
823*0Sstevel@tonic-gate  *	mp->dmai_winsize   - window size (no redzone), n * page size    (fixed)
824*0Sstevel@tonic-gate  *	mp->dmai_nwin	   - # of DMA windows of entire object		(fixed)
825*0Sstevel@tonic-gate  *	mp->dmai_rflags	   - remove partial flag if nwin == 1		(fixed)
826*0Sstevel@tonic-gate  *	mp->dmai_winlst	   - NULL, window objects not used for DVMA	(fixed)
827*0Sstevel@tonic-gate  *
828*0Sstevel@tonic-gate  *	fixed - not changed across different DMA windows
829*0Sstevel@tonic-gate  */
830*0Sstevel@tonic-gate /*ARGSUSED*/
831*0Sstevel@tonic-gate int
832*0Sstevel@tonic-gate pci_dvma_win(pci_t *pci_p, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp)
833*0Sstevel@tonic-gate {
834*0Sstevel@tonic-gate 	uint32_t redzone_sz	= HAS_REDZONE(mp) ? IOMMU_PAGE_SIZE : 0;
835*0Sstevel@tonic-gate 	size_t obj_sz	= mp->dmai_object.dmao_size;
836*0Sstevel@tonic-gate 	size_t xfer_sz;
837*0Sstevel@tonic-gate 	ulong_t pg_off;
838*0Sstevel@tonic-gate 
839*0Sstevel@tonic-gate 	if ((mp->dmai_ndvmapages == 1) && !redzone_sz) {
840*0Sstevel@tonic-gate 		mp->dmai_rflags &= ~DDI_DMA_PARTIAL;
841*0Sstevel@tonic-gate 		mp->dmai_size = obj_sz;
842*0Sstevel@tonic-gate 		mp->dmai_winsize = IOMMU_PAGE_SIZE;
843*0Sstevel@tonic-gate 		mp->dmai_nwin = 1;
844*0Sstevel@tonic-gate 		goto done;
845*0Sstevel@tonic-gate 	}
846*0Sstevel@tonic-gate 
847*0Sstevel@tonic-gate 	pg_off	= mp->dmai_roffset;
848*0Sstevel@tonic-gate 	xfer_sz	= obj_sz + redzone_sz;
849*0Sstevel@tonic-gate 
850*0Sstevel@tonic-gate 	/* include redzone in nocross check */ {
851*0Sstevel@tonic-gate 		uint64_t nocross = mp->dmai_attr.dma_attr_seg;
852*0Sstevel@tonic-gate 		if (xfer_sz + pg_off - 1 > nocross)
853*0Sstevel@tonic-gate 			xfer_sz = nocross - pg_off + 1;
854*0Sstevel@tonic-gate 		if (redzone_sz && (xfer_sz <= redzone_sz)) {
855*0Sstevel@tonic-gate 			DEBUG5(DBG_DMA_MAP, pci_p->pci_dip,
856*0Sstevel@tonic-gate 			    "nocross too small %lx(%lx)+%lx+%x < %" PRIx64 "\n",
857*0Sstevel@tonic-gate 			    xfer_sz, obj_sz, pg_off, redzone_sz, nocross);
858*0Sstevel@tonic-gate 			return (DDI_DMA_TOOBIG);
859*0Sstevel@tonic-gate 		}
860*0Sstevel@tonic-gate 	}
861*0Sstevel@tonic-gate 	xfer_sz -= redzone_sz;		/* restore transfer size  */
862*0Sstevel@tonic-gate 	/* check counter max */ {
863*0Sstevel@tonic-gate 		uint32_t count_max = mp->dmai_attr.dma_attr_count_max;
864*0Sstevel@tonic-gate 		if (xfer_sz - 1 > count_max)
865*0Sstevel@tonic-gate 			xfer_sz = count_max + 1;
866*0Sstevel@tonic-gate 	}
867*0Sstevel@tonic-gate 	if (xfer_sz >= obj_sz) {
868*0Sstevel@tonic-gate 		mp->dmai_rflags &= ~DDI_DMA_PARTIAL;
869*0Sstevel@tonic-gate 		mp->dmai_size = xfer_sz;
870*0Sstevel@tonic-gate 		mp->dmai_winsize = P2ROUNDUP(xfer_sz + pg_off, IOMMU_PAGE_SIZE);
871*0Sstevel@tonic-gate 		mp->dmai_nwin = 1;
872*0Sstevel@tonic-gate 		goto done;
873*0Sstevel@tonic-gate 	}
874*0Sstevel@tonic-gate 	if (!(dmareq->dmar_flags & DDI_DMA_PARTIAL)) {
875*0Sstevel@tonic-gate 		DEBUG4(DBG_DMA_MAP, pci_p->pci_dip,
876*0Sstevel@tonic-gate 		    "too big: %lx+%lx+%x > %lx\n",
877*0Sstevel@tonic-gate 		    obj_sz, pg_off, redzone_sz, xfer_sz);
878*0Sstevel@tonic-gate 		return (DDI_DMA_TOOBIG);
879*0Sstevel@tonic-gate 	}
880*0Sstevel@tonic-gate 
881*0Sstevel@tonic-gate 	xfer_sz = IOMMU_PTOB(IOMMU_BTOP(xfer_sz + pg_off)); /* page align */
882*0Sstevel@tonic-gate 	mp->dmai_size = xfer_sz - pg_off;	/* 1st window xferrable size */
883*0Sstevel@tonic-gate 	mp->dmai_winsize = xfer_sz;		/* redzone not in winsize */
884*0Sstevel@tonic-gate 	mp->dmai_nwin = (obj_sz + pg_off + xfer_sz - 1) / xfer_sz;
885*0Sstevel@tonic-gate done:
886*0Sstevel@tonic-gate 	mp->dmai_winlst = NULL;
887*0Sstevel@tonic-gate 	dump_dma_handle(DBG_DMA_MAP, pci_p->pci_dip, mp);
888*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
889*0Sstevel@tonic-gate }
890*0Sstevel@tonic-gate 
891*0Sstevel@tonic-gate /*
892*0Sstevel@tonic-gate  * fast track cache entry to iommu context, inserts 3 0 bits between
893*0Sstevel@tonic-gate  * upper 6-bits and lower 3-bits of the 9-bit cache entry
894*0Sstevel@tonic-gate  */
895*0Sstevel@tonic-gate #define	IOMMU_FCE_TO_CTX(i)	(((i) << 3) | ((i) & 0x7) | 0x38)
896*0Sstevel@tonic-gate 
897*0Sstevel@tonic-gate /*
898*0Sstevel@tonic-gate  * pci_dvma_map_fast - attempts to map fast trackable DVMA
899*0Sstevel@tonic-gate  */
900*0Sstevel@tonic-gate int
901*0Sstevel@tonic-gate pci_dvma_map_fast(iommu_t *iommu_p, ddi_dma_impl_t *mp)
902*0Sstevel@tonic-gate {
903*0Sstevel@tonic-gate 	uint_t clustsz = pci_dvma_page_cache_clustsz;
904*0Sstevel@tonic-gate 	uint_t entries = pci_dvma_page_cache_entries;
905*0Sstevel@tonic-gate 	uint64_t *tte_addr;
906*0Sstevel@tonic-gate 	uint64_t tte = GET_TTE_TEMPLATE(mp);
907*0Sstevel@tonic-gate 	int i = iommu_p->iommu_dvma_addr_scan_start;
908*0Sstevel@tonic-gate 	uint8_t *lock_addr = iommu_p->iommu_dvma_cache_locks + i;
909*0Sstevel@tonic-gate 	iopfn_t *pfn_addr;
910*0Sstevel@tonic-gate 	dvma_addr_t dvma_pg;
911*0Sstevel@tonic-gate 	size_t npages = IOMMU_BTOP(mp->dmai_winsize);
912*0Sstevel@tonic-gate #ifdef DEBUG
913*0Sstevel@tonic-gate 	dev_info_t *dip = mp->dmai_rdip;
914*0Sstevel@tonic-gate #endif
915*0Sstevel@tonic-gate 	extern uint8_t ldstub(uint8_t *);
916*0Sstevel@tonic-gate 	ASSERT(IOMMU_PTOB(npages) == mp->dmai_winsize);
917*0Sstevel@tonic-gate 	ASSERT(npages + HAS_REDZONE(mp) <= clustsz);
918*0Sstevel@tonic-gate 
919*0Sstevel@tonic-gate 	for (; i < entries && ldstub(lock_addr); i++, lock_addr++);
920*0Sstevel@tonic-gate 	if (i >= entries) {
921*0Sstevel@tonic-gate 		lock_addr = iommu_p->iommu_dvma_cache_locks;
922*0Sstevel@tonic-gate 		i = 0;
923*0Sstevel@tonic-gate 		for (; i < entries && ldstub(lock_addr); i++, lock_addr++);
924*0Sstevel@tonic-gate 		if (i >= entries) {
925*0Sstevel@tonic-gate #ifdef PCI_DMA_PROF
926*0Sstevel@tonic-gate 			pci_dvmaft_exhaust++;
927*0Sstevel@tonic-gate #endif
928*0Sstevel@tonic-gate 			return (DDI_DMA_NORESOURCES);
929*0Sstevel@tonic-gate 		}
930*0Sstevel@tonic-gate 	}
931*0Sstevel@tonic-gate 	iommu_p->iommu_dvma_addr_scan_start = (i + 1) & (entries - 1);
932*0Sstevel@tonic-gate 	if (PCI_DMA_USECTX(mp)) {
933*0Sstevel@tonic-gate 		dvma_context_t ctx = IOMMU_FCE_TO_CTX(i);
934*0Sstevel@tonic-gate 		tte |= IOMMU_CTX2TTE(ctx);
935*0Sstevel@tonic-gate 		mp->dmai_flags |= DMAI_FLAGS_CONTEXT;
936*0Sstevel@tonic-gate 		DEBUG1(DBG_DMA_MAP, dip, "fast: ctx=0x%x\n", ctx);
937*0Sstevel@tonic-gate 	}
938*0Sstevel@tonic-gate 	i *= clustsz;
939*0Sstevel@tonic-gate 	tte_addr = iommu_p->iommu_tsb_vaddr + i;
940*0Sstevel@tonic-gate 	dvma_pg = iommu_p->dvma_base_pg + i;
941*0Sstevel@tonic-gate #ifdef DEBUG
942*0Sstevel@tonic-gate 	for (i = 0; i < clustsz; i++)
943*0Sstevel@tonic-gate 		ASSERT(TTE_IS_INVALID(tte_addr[i]));
944*0Sstevel@tonic-gate #endif
945*0Sstevel@tonic-gate 	*tte_addr = tte | IOMMU_PTOB(MP_PFN0(mp)); /* map page 0 */
946*0Sstevel@tonic-gate 	DEBUG5(DBG_DMA_MAP, dip, "fast %p:dvma_pg=%x tte0(%p)=%08x.%08x\n", mp,
947*0Sstevel@tonic-gate 		dvma_pg, tte_addr, HI32(*tte_addr), LO32(*tte_addr));
948*0Sstevel@tonic-gate 	if (npages == 1)
949*0Sstevel@tonic-gate 		goto tte_done;
950*0Sstevel@tonic-gate 	pfn_addr = PCI_GET_MP_PFN1_ADDR(mp); /* short iommu_map_pages() */
951*0Sstevel@tonic-gate 	for (tte_addr++, i = 1; i < npages; i++, tte_addr++, pfn_addr++) {
952*0Sstevel@tonic-gate 		*tte_addr = tte | IOMMU_PTOB(*pfn_addr);
953*0Sstevel@tonic-gate 		DEBUG5(DBG_DMA_MAP, dip, "fast %p:tte(%p, %p)=%08x.%08x\n", mp,
954*0Sstevel@tonic-gate 			tte_addr, pfn_addr, HI32(*tte_addr), LO32(*tte_addr));
955*0Sstevel@tonic-gate 	}
956*0Sstevel@tonic-gate tte_done:
957*0Sstevel@tonic-gate #ifdef PCI_DMA_PROF
958*0Sstevel@tonic-gate 	pci_dvmaft_success++;
959*0Sstevel@tonic-gate #endif
960*0Sstevel@tonic-gate 	mp->dmai_mapping = mp->dmai_roffset | IOMMU_PTOB(dvma_pg);
961*0Sstevel@tonic-gate 	mp->dmai_offset = 0;
962*0Sstevel@tonic-gate 	mp->dmai_flags |= DMAI_FLAGS_FASTTRACK;
963*0Sstevel@tonic-gate 	PCI_SAVE_MP_TTE(mp, tte);	/* save TTE template for unmapping */
964*0Sstevel@tonic-gate 	if (DVMA_DBG_ON(iommu_p))
965*0Sstevel@tonic-gate 		pci_dvma_alloc_debug(iommu_p, (char *)mp->dmai_mapping,
966*0Sstevel@tonic-gate 			mp->dmai_size, mp);
967*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
968*0Sstevel@tonic-gate }
969*0Sstevel@tonic-gate 
970*0Sstevel@tonic-gate /*
971*0Sstevel@tonic-gate  * pci_dvma_map: map non-fasttrack DMA
972*0Sstevel@tonic-gate  *		Use quantum cache if single page DMA.
973*0Sstevel@tonic-gate  */
974*0Sstevel@tonic-gate int
975*0Sstevel@tonic-gate pci_dvma_map(ddi_dma_impl_t *mp, ddi_dma_req_t *dmareq, iommu_t *iommu_p)
976*0Sstevel@tonic-gate {
977*0Sstevel@tonic-gate 	uint_t npages = PCI_DMA_WINNPGS(mp);
978*0Sstevel@tonic-gate 	dvma_addr_t dvma_pg, dvma_pg_index;
979*0Sstevel@tonic-gate 	void *dvma_addr;
980*0Sstevel@tonic-gate 	uint64_t tte = GET_TTE_TEMPLATE(mp);
981*0Sstevel@tonic-gate 	int sleep = dmareq->dmar_fp == DDI_DMA_SLEEP ? VM_SLEEP : VM_NOSLEEP;
982*0Sstevel@tonic-gate #ifdef DEBUG
983*0Sstevel@tonic-gate 	dev_info_t *dip = mp->dmai_rdip;
984*0Sstevel@tonic-gate #endif
985*0Sstevel@tonic-gate 	/*
986*0Sstevel@tonic-gate 	 * allocate dvma space resource and map in the first window.
987*0Sstevel@tonic-gate 	 * (vmem_t *vmp, size_t size,
988*0Sstevel@tonic-gate 	 *	size_t align, size_t phase, size_t nocross,
989*0Sstevel@tonic-gate 	 *	void *minaddr, void *maxaddr, int vmflag)
990*0Sstevel@tonic-gate 	 */
991*0Sstevel@tonic-gate 	if ((npages == 1) && HAS_NOSYSLIMIT(mp)) {
992*0Sstevel@tonic-gate 		dvma_addr = vmem_alloc(iommu_p->iommu_dvma_map,
993*0Sstevel@tonic-gate 			IOMMU_PAGE_SIZE, sleep);
994*0Sstevel@tonic-gate 		mp->dmai_flags |= DMAI_FLAGS_VMEMCACHE;
995*0Sstevel@tonic-gate #ifdef PCI_DMA_PROF
996*0Sstevel@tonic-gate 		pci_dvma_vmem_alloc++;
997*0Sstevel@tonic-gate #endif
998*0Sstevel@tonic-gate 	} else {
999*0Sstevel@tonic-gate 		dvma_addr = vmem_xalloc(iommu_p->iommu_dvma_map,
1000*0Sstevel@tonic-gate 			IOMMU_PTOB(npages + HAS_REDZONE(mp)),
1001*0Sstevel@tonic-gate 			MAX(mp->dmai_attr.dma_attr_align, IOMMU_PAGE_SIZE),
1002*0Sstevel@tonic-gate 			0,
1003*0Sstevel@tonic-gate 			mp->dmai_attr.dma_attr_seg + 1,
1004*0Sstevel@tonic-gate 			(void *)mp->dmai_attr.dma_attr_addr_lo,
1005*0Sstevel@tonic-gate 			(void *)(mp->dmai_attr.dma_attr_addr_hi + 1),
1006*0Sstevel@tonic-gate 			sleep);
1007*0Sstevel@tonic-gate #ifdef PCI_DMA_PROF
1008*0Sstevel@tonic-gate 		pci_dvma_vmem_xalloc++;
1009*0Sstevel@tonic-gate #endif
1010*0Sstevel@tonic-gate 	}
1011*0Sstevel@tonic-gate 	dvma_pg = IOMMU_BTOP((ulong_t)dvma_addr);
1012*0Sstevel@tonic-gate 	dvma_pg_index = dvma_pg - iommu_p->dvma_base_pg;
1013*0Sstevel@tonic-gate 	DEBUG2(DBG_DMA_MAP, dip, "fallback dvma_pages: dvma_pg=%x index=%x\n",
1014*0Sstevel@tonic-gate 		dvma_pg, dvma_pg_index);
1015*0Sstevel@tonic-gate 	if (dvma_pg == 0)
1016*0Sstevel@tonic-gate 		goto noresource;
1017*0Sstevel@tonic-gate 
1018*0Sstevel@tonic-gate 	/* allocate DVMA context */
1019*0Sstevel@tonic-gate 	if ((npages >= pci_context_minpages) && PCI_DMA_USECTX(mp)) {
1020*0Sstevel@tonic-gate 		dvma_context_t ctx;
1021*0Sstevel@tonic-gate 		if (ctx = pci_iommu_get_dvma_context(iommu_p, dvma_pg_index)) {
1022*0Sstevel@tonic-gate 			tte |= IOMMU_CTX2TTE(ctx);
1023*0Sstevel@tonic-gate 			mp->dmai_flags |= DMAI_FLAGS_CONTEXT;
1024*0Sstevel@tonic-gate 		}
1025*0Sstevel@tonic-gate 	}
1026*0Sstevel@tonic-gate 	mp->dmai_mapping = mp->dmai_roffset | IOMMU_PTOB(dvma_pg);
1027*0Sstevel@tonic-gate 	mp->dmai_offset = 0;
1028*0Sstevel@tonic-gate 	PCI_SAVE_MP_TTE(mp, tte);	/* mp->dmai_tte = tte */
1029*0Sstevel@tonic-gate 	iommu_map_pages(iommu_p, mp, dvma_pg, npages, 0);
1030*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
1031*0Sstevel@tonic-gate noresource:
1032*0Sstevel@tonic-gate 	if (dmareq->dmar_fp != DDI_DMA_DONTWAIT) {
1033*0Sstevel@tonic-gate 		DEBUG0(DBG_DMA_MAP, dip, "dvma_pg 0 - set callback\n");
1034*0Sstevel@tonic-gate 		ddi_set_callback(dmareq->dmar_fp, dmareq->dmar_arg,
1035*0Sstevel@tonic-gate 			&iommu_p->iommu_dvma_clid);
1036*0Sstevel@tonic-gate 	}
1037*0Sstevel@tonic-gate 	DEBUG0(DBG_DMA_MAP, dip, "vmem_xalloc - DDI_DMA_NORESOURCES\n");
1038*0Sstevel@tonic-gate 	return (DDI_DMA_NORESOURCES);
1039*0Sstevel@tonic-gate }
1040*0Sstevel@tonic-gate 
1041*0Sstevel@tonic-gate void
1042*0Sstevel@tonic-gate pci_dvma_unmap(iommu_t *iommu_p, ddi_dma_impl_t *mp)
1043*0Sstevel@tonic-gate {
1044*0Sstevel@tonic-gate 	size_t npages;
1045*0Sstevel@tonic-gate 	dvma_addr_t dvma_addr = (dvma_addr_t)mp->dmai_mapping;
1046*0Sstevel@tonic-gate 	dvma_addr_t dvma_pg = IOMMU_BTOP(dvma_addr);
1047*0Sstevel@tonic-gate 	dvma_addr = IOMMU_PTOB(dvma_pg);
1048*0Sstevel@tonic-gate 
1049*0Sstevel@tonic-gate 	if (mp->dmai_flags & DMAI_FLAGS_FASTTRACK) {
1050*0Sstevel@tonic-gate 		iopfn_t index = dvma_pg - iommu_p->dvma_base_pg;
1051*0Sstevel@tonic-gate 		ASSERT(index % pci_dvma_page_cache_clustsz == 0);
1052*0Sstevel@tonic-gate 		index /= pci_dvma_page_cache_clustsz;
1053*0Sstevel@tonic-gate 		ASSERT(index < pci_dvma_page_cache_entries);
1054*0Sstevel@tonic-gate 		iommu_p->iommu_dvma_cache_locks[index] = 0;
1055*0Sstevel@tonic-gate #ifdef PCI_DMA_PROF
1056*0Sstevel@tonic-gate 		pci_dvmaft_free++;
1057*0Sstevel@tonic-gate #endif
1058*0Sstevel@tonic-gate 		return;
1059*0Sstevel@tonic-gate 	}
1060*0Sstevel@tonic-gate 	npages = IOMMU_BTOP(mp->dmai_winsize) + HAS_REDZONE(mp);
1061*0Sstevel@tonic-gate 	pci_vmem_free(iommu_p, mp, (void *)dvma_addr, npages);
1062*0Sstevel@tonic-gate 
1063*0Sstevel@tonic-gate 	if (mp->dmai_flags & DMAI_FLAGS_CONTEXT)
1064*0Sstevel@tonic-gate 		pci_iommu_free_dvma_context(iommu_p, MP2CTX(mp));
1065*0Sstevel@tonic-gate }
1066*0Sstevel@tonic-gate 
1067*0Sstevel@tonic-gate void
1068*0Sstevel@tonic-gate pci_dma_sync_unmap(dev_info_t *dip, dev_info_t *rdip, ddi_dma_impl_t *mp)
1069*0Sstevel@tonic-gate {
1070*0Sstevel@tonic-gate 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
1071*0Sstevel@tonic-gate 	iommu_t *iommu_p = pci_p->pci_iommu_p;
1072*0Sstevel@tonic-gate 	uint64_t sync_buf_save = SYNC_BUF_PA(mp);
1073*0Sstevel@tonic-gate 	uint32_t fast_track = mp->dmai_flags & DMAI_FLAGS_FASTTRACK;
1074*0Sstevel@tonic-gate 
1075*0Sstevel@tonic-gate 	if (fast_track) {
1076*0Sstevel@tonic-gate 		dvma_addr_t dvma_pg = IOMMU_BTOP(mp->dmai_mapping);
1077*0Sstevel@tonic-gate 
1078*0Sstevel@tonic-gate 		SYNC_BUF_PA(mp) = IOMMU_PAGE_TTEPA(iommu_p, dvma_pg);
1079*0Sstevel@tonic-gate 		ASSERT(!(SYNC_BUF_PA(mp) & PCI_SYNC_FLAG_SIZE - 1));
1080*0Sstevel@tonic-gate 	}
1081*0Sstevel@tonic-gate 
1082*0Sstevel@tonic-gate 	if (pci_dvma_sync_before_unmap) {
1083*0Sstevel@tonic-gate 		pci_dma_sync(dip, rdip, (ddi_dma_handle_t)mp, 0, 0, 0);
1084*0Sstevel@tonic-gate 		iommu_unmap_window(iommu_p, mp);
1085*0Sstevel@tonic-gate 	} else {
1086*0Sstevel@tonic-gate 		iommu_unmap_window(iommu_p, mp);
1087*0Sstevel@tonic-gate 		pci_dma_sync(dip, rdip, (ddi_dma_handle_t)mp, 0, 0, 0);
1088*0Sstevel@tonic-gate 	}
1089*0Sstevel@tonic-gate 
1090*0Sstevel@tonic-gate 	if (fast_track)
1091*0Sstevel@tonic-gate 		SYNC_BUF_PA(mp) = sync_buf_save;
1092*0Sstevel@tonic-gate }
1093*0Sstevel@tonic-gate 
1094*0Sstevel@tonic-gate /*
1095*0Sstevel@tonic-gate  * DVMA mappings may have multiple windows, but each window always have
1096*0Sstevel@tonic-gate  * one segment.
1097*0Sstevel@tonic-gate  */
1098*0Sstevel@tonic-gate int
1099*0Sstevel@tonic-gate pci_dvma_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_impl_t *mp,
1100*0Sstevel@tonic-gate 	enum ddi_dma_ctlops cmd, off_t *offp, size_t *lenp, caddr_t *objp,
1101*0Sstevel@tonic-gate 	uint_t cache_flags)
1102*0Sstevel@tonic-gate {
1103*0Sstevel@tonic-gate 	switch (cmd) {
1104*0Sstevel@tonic-gate 	case DDI_DMA_SYNC:
1105*0Sstevel@tonic-gate 		return (pci_dma_sync(dip, rdip, (ddi_dma_handle_t)mp,
1106*0Sstevel@tonic-gate 		    *offp, *lenp, cache_flags));
1107*0Sstevel@tonic-gate 
1108*0Sstevel@tonic-gate 	case DDI_DMA_HTOC: {
1109*0Sstevel@tonic-gate 		int ret;
1110*0Sstevel@tonic-gate 		off_t wo_off, off = *offp;	/* wo_off: wnd's obj offset */
1111*0Sstevel@tonic-gate 		uint_t win_size = mp->dmai_winsize;
1112*0Sstevel@tonic-gate 		ddi_dma_cookie_t *cp = (ddi_dma_cookie_t *)objp;
1113*0Sstevel@tonic-gate 
1114*0Sstevel@tonic-gate 		if (off >= mp->dmai_object.dmao_size) {
1115*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d invalid dma_htoc offset %lx",
1116*0Sstevel@tonic-gate 				NAMEINST(mp->dmai_rdip), off);
1117*0Sstevel@tonic-gate 			return (DDI_FAILURE);
1118*0Sstevel@tonic-gate 		}
1119*0Sstevel@tonic-gate 		off += mp->dmai_roffset;
1120*0Sstevel@tonic-gate 		ret = pci_dma_win(dip, rdip, (ddi_dma_handle_t)mp,
1121*0Sstevel@tonic-gate 		    off / win_size, &wo_off, NULL, cp, NULL); /* lenp == NULL */
1122*0Sstevel@tonic-gate 		if (ret)
1123*0Sstevel@tonic-gate 			return (ret);
1124*0Sstevel@tonic-gate 		DEBUG4(DBG_DMA_CTL, dip, "HTOC:cookie=%x+%lx off=%lx,%lx\n",
1125*0Sstevel@tonic-gate 			cp->dmac_address, cp->dmac_size, off, *offp);
1126*0Sstevel@tonic-gate 
1127*0Sstevel@tonic-gate 		/* adjust cookie addr/len if we are not on window boundary */
1128*0Sstevel@tonic-gate 		ASSERT((off % win_size) == (off -
1129*0Sstevel@tonic-gate 			(PCI_DMA_CURWIN(mp) ? mp->dmai_roffset : 0) - wo_off));
1130*0Sstevel@tonic-gate 		off = PCI_DMA_CURWIN(mp) ? off % win_size : *offp;
1131*0Sstevel@tonic-gate 		ASSERT(cp->dmac_size > off);
1132*0Sstevel@tonic-gate 		cp->dmac_laddress += off;
1133*0Sstevel@tonic-gate 		cp->dmac_size -= off;
1134*0Sstevel@tonic-gate 		DEBUG5(DBG_DMA_CTL, dip,
1135*0Sstevel@tonic-gate 			"HTOC:mp=%p cookie=%x+%lx off=%lx,%lx\n",
1136*0Sstevel@tonic-gate 			mp, cp->dmac_address, cp->dmac_size, off, wo_off);
1137*0Sstevel@tonic-gate 		}
1138*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
1139*0Sstevel@tonic-gate 
1140*0Sstevel@tonic-gate 	case DDI_DMA_REPWIN:
1141*0Sstevel@tonic-gate 		*offp = mp->dmai_offset;
1142*0Sstevel@tonic-gate 		*lenp = mp->dmai_size;
1143*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
1144*0Sstevel@tonic-gate 
1145*0Sstevel@tonic-gate 	case DDI_DMA_MOVWIN: {
1146*0Sstevel@tonic-gate 		off_t off = *offp;
1147*0Sstevel@tonic-gate 		if (off >= mp->dmai_object.dmao_size)
1148*0Sstevel@tonic-gate 			return (DDI_FAILURE);
1149*0Sstevel@tonic-gate 		off += mp->dmai_roffset;
1150*0Sstevel@tonic-gate 		return (pci_dma_win(dip, rdip, (ddi_dma_handle_t)mp,
1151*0Sstevel@tonic-gate 		    off / mp->dmai_winsize, offp, lenp,
1152*0Sstevel@tonic-gate 		    (ddi_dma_cookie_t *)objp, NULL));
1153*0Sstevel@tonic-gate 		}
1154*0Sstevel@tonic-gate 
1155*0Sstevel@tonic-gate 	case DDI_DMA_NEXTWIN: {
1156*0Sstevel@tonic-gate 		window_t win = PCI_DMA_CURWIN(mp);
1157*0Sstevel@tonic-gate 		if (offp) {
1158*0Sstevel@tonic-gate 			if (*(window_t *)offp != win) {  /* window not active */
1159*0Sstevel@tonic-gate 				*(window_t *)objp = win; /* return cur win */
1160*0Sstevel@tonic-gate 				return (DDI_DMA_STALE);
1161*0Sstevel@tonic-gate 			}
1162*0Sstevel@tonic-gate 			win++;
1163*0Sstevel@tonic-gate 		} else	/* map win 0 */
1164*0Sstevel@tonic-gate 			win = 0;
1165*0Sstevel@tonic-gate 		if (win >= mp->dmai_nwin) {
1166*0Sstevel@tonic-gate 			*(window_t *)objp = win - 1;
1167*0Sstevel@tonic-gate 			return (DDI_DMA_DONE);
1168*0Sstevel@tonic-gate 		}
1169*0Sstevel@tonic-gate 		if (pci_dma_win(dip, rdip, (ddi_dma_handle_t)mp,
1170*0Sstevel@tonic-gate 		    win, 0, 0, 0, 0)) {
1171*0Sstevel@tonic-gate 			*(window_t *)objp = win - 1;
1172*0Sstevel@tonic-gate 			return (DDI_FAILURE);
1173*0Sstevel@tonic-gate 		}
1174*0Sstevel@tonic-gate 		*(window_t *)objp = win;
1175*0Sstevel@tonic-gate 		}
1176*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
1177*0Sstevel@tonic-gate 
1178*0Sstevel@tonic-gate 	case DDI_DMA_NEXTSEG:
1179*0Sstevel@tonic-gate 		if (*(window_t *)offp != PCI_DMA_CURWIN(mp))
1180*0Sstevel@tonic-gate 			return (DDI_DMA_STALE);
1181*0Sstevel@tonic-gate 		if (lenp)				/* only 1 seg allowed */
1182*0Sstevel@tonic-gate 			return (DDI_DMA_DONE);
1183*0Sstevel@tonic-gate 							/* return mp as seg 0 */
1184*0Sstevel@tonic-gate 		*(ddi_dma_seg_t *)objp = (ddi_dma_seg_t)mp;
1185*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
1186*0Sstevel@tonic-gate 
1187*0Sstevel@tonic-gate 	case DDI_DMA_SEGTOC:
1188*0Sstevel@tonic-gate 		MAKE_DMA_COOKIE((ddi_dma_cookie_t *)objp, mp->dmai_mapping,
1189*0Sstevel@tonic-gate 			mp->dmai_size);
1190*0Sstevel@tonic-gate 		*offp = mp->dmai_offset;
1191*0Sstevel@tonic-gate 		*lenp = mp->dmai_size;
1192*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
1193*0Sstevel@tonic-gate 
1194*0Sstevel@tonic-gate 	case DDI_DMA_COFF: {
1195*0Sstevel@tonic-gate 		ddi_dma_cookie_t *cp = (ddi_dma_cookie_t *)offp;
1196*0Sstevel@tonic-gate 		if (cp->dmac_address < mp->dmai_mapping ||
1197*0Sstevel@tonic-gate 		    (cp->dmac_address + cp->dmac_size) >
1198*0Sstevel@tonic-gate 		    (mp->dmai_mapping + mp->dmai_size))
1199*0Sstevel@tonic-gate 			return (DDI_FAILURE);
1200*0Sstevel@tonic-gate 		*objp = (caddr_t)(cp->dmac_address - mp->dmai_mapping +
1201*0Sstevel@tonic-gate 			mp->dmai_offset);
1202*0Sstevel@tonic-gate 		}
1203*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
1204*0Sstevel@tonic-gate 
1205*0Sstevel@tonic-gate 	case DDI_DMA_REMAP:
1206*0Sstevel@tonic-gate 		if (pci_dvma_remap_enabled)
1207*0Sstevel@tonic-gate 			return (pci_dvma_remap(dip, rdip, mp, *offp, *lenp));
1208*0Sstevel@tonic-gate 		return (DDI_FAILURE);
1209*0Sstevel@tonic-gate 
1210*0Sstevel@tonic-gate 	default:
1211*0Sstevel@tonic-gate 		DEBUG3(DBG_DMA_CTL, dip, "unknown command (%x): rdip=%s%d\n",
1212*0Sstevel@tonic-gate 			cmd, ddi_driver_name(rdip), ddi_get_instance(rdip));
1213*0Sstevel@tonic-gate 		break;
1214*0Sstevel@tonic-gate 	}
1215*0Sstevel@tonic-gate 	return (DDI_FAILURE);
1216*0Sstevel@tonic-gate }
1217*0Sstevel@tonic-gate 
1218*0Sstevel@tonic-gate void
1219*0Sstevel@tonic-gate pci_dma_freewin(ddi_dma_impl_t *mp)
1220*0Sstevel@tonic-gate {
1221*0Sstevel@tonic-gate 	pci_dma_win_t *win_p = mp->dmai_winlst, *win2_p;
1222*0Sstevel@tonic-gate 	for (win2_p = win_p; win_p; win2_p = win_p) {
1223*0Sstevel@tonic-gate 		win_p = win2_p->win_next;
1224*0Sstevel@tonic-gate 		kmem_free(win2_p, sizeof (pci_dma_win_t) +
1225*0Sstevel@tonic-gate 			sizeof (ddi_dma_cookie_t) * win2_p->win_ncookies);
1226*0Sstevel@tonic-gate 	}
1227*0Sstevel@tonic-gate 	mp->dmai_nwin = 0;
1228*0Sstevel@tonic-gate 	mp->dmai_winlst = NULL;
1229*0Sstevel@tonic-gate }
1230*0Sstevel@tonic-gate 
1231*0Sstevel@tonic-gate /*
1232*0Sstevel@tonic-gate  * pci_dma_newwin - create a dma window object and cookies
1233*0Sstevel@tonic-gate  *
1234*0Sstevel@tonic-gate  *	After the initial scan in pci_dma_physwin(), which identifies
1235*0Sstevel@tonic-gate  *	a portion of the pfn array that belongs to a dma window,
1236*0Sstevel@tonic-gate  *	we are called to allocate and initialize representing memory
1237*0Sstevel@tonic-gate  *	resources. We know from the 1st scan the number of cookies
1238*0Sstevel@tonic-gate  *	or dma segment in this window so we can allocate a contiguous
1239*0Sstevel@tonic-gate  *	memory array for the dma cookies (The implementation of
1240*0Sstevel@tonic-gate  *	ddi_dma_nextcookie(9f) dictates dma cookies be contiguous).
1241*0Sstevel@tonic-gate  *
1242*0Sstevel@tonic-gate  *	A second round scan is done on the pfn array to identify
1243*0Sstevel@tonic-gate  *	each dma segment and initialize its corresponding dma cookie.
1244*0Sstevel@tonic-gate  *	We don't need to do all the safety checking and we know they
1245*0Sstevel@tonic-gate  *	all belong to the same dma window.
1246*0Sstevel@tonic-gate  *
1247*0Sstevel@tonic-gate  *	Input:	cookie_no - # of cookies identified by the 1st scan
1248*0Sstevel@tonic-gate  *		start_idx - subscript of the pfn array for the starting pfn
1249*0Sstevel@tonic-gate  *		end_idx   - subscript of the last pfn in dma window
1250*0Sstevel@tonic-gate  *		win_pp    - pointer to win_next member of previous window
1251*0Sstevel@tonic-gate  *	Return:	DDI_SUCCESS - with **win_pp as newly created window object
1252*0Sstevel@tonic-gate  *		DDI_DMA_NORESROUCE - caller frees all previous window objs
1253*0Sstevel@tonic-gate  *	Note:	Each cookie and window size are all initialized on page
1254*0Sstevel@tonic-gate  *		boundary. This is not true for the 1st cookie of the 1st
1255*0Sstevel@tonic-gate  *		window and the last cookie of the last window.
1256*0Sstevel@tonic-gate  *		We fix that later in upper layer which has access to size
1257*0Sstevel@tonic-gate  *		and offset info.
1258*0Sstevel@tonic-gate  *
1259*0Sstevel@tonic-gate  */
1260*0Sstevel@tonic-gate static int
1261*0Sstevel@tonic-gate pci_dma_newwin(ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp, uint32_t cookie_no,
1262*0Sstevel@tonic-gate 	uint32_t start_idx, uint32_t end_idx, pci_dma_win_t **win_pp,
1263*0Sstevel@tonic-gate 	uint64_t count_max, uint64_t bypass_prefix)
1264*0Sstevel@tonic-gate {
1265*0Sstevel@tonic-gate 	int (*waitfp)(caddr_t) = dmareq->dmar_fp;
1266*0Sstevel@tonic-gate 	ddi_dma_cookie_t *cookie_p;
1267*0Sstevel@tonic-gate 	uint32_t pfn_no = 1;
1268*0Sstevel@tonic-gate 	iopfn_t pfn = PCI_GET_MP_PFN(mp, start_idx);
1269*0Sstevel@tonic-gate 	iopfn_t prev_pfn = pfn;
1270*0Sstevel@tonic-gate 	uint64_t seg_pfn0 = pfn;
1271*0Sstevel@tonic-gate 	size_t sz = cookie_no * sizeof (ddi_dma_cookie_t);
1272*0Sstevel@tonic-gate 	pci_dma_win_t *win_p = kmem_alloc(sizeof (pci_dma_win_t) + sz,
1273*0Sstevel@tonic-gate 		waitfp == DDI_DMA_SLEEP ? KM_SLEEP : KM_NOSLEEP);
1274*0Sstevel@tonic-gate 	if (!win_p)
1275*0Sstevel@tonic-gate 		goto noresource;
1276*0Sstevel@tonic-gate 
1277*0Sstevel@tonic-gate 	win_p->win_next = NULL;
1278*0Sstevel@tonic-gate 	win_p->win_ncookies = cookie_no;
1279*0Sstevel@tonic-gate 	win_p->win_curseg = 0;	/* start from segment 0 */
1280*0Sstevel@tonic-gate 	win_p->win_size = IOMMU_PTOB(end_idx - start_idx + 1);
1281*0Sstevel@tonic-gate 	/* win_p->win_offset is left uninitialized */
1282*0Sstevel@tonic-gate 
1283*0Sstevel@tonic-gate 	cookie_p = (ddi_dma_cookie_t *)(win_p + 1);
1284*0Sstevel@tonic-gate 	start_idx++;
1285*0Sstevel@tonic-gate 	for (; start_idx <= end_idx; start_idx++, prev_pfn = pfn, pfn_no++) {
1286*0Sstevel@tonic-gate 		pfn = PCI_GET_MP_PFN1(mp, start_idx);
1287*0Sstevel@tonic-gate 		if ((pfn == prev_pfn + 1) &&
1288*0Sstevel@tonic-gate 			(IOMMU_PTOB(pfn_no + 1) - 1 <= count_max))
1289*0Sstevel@tonic-gate 			continue;
1290*0Sstevel@tonic-gate 
1291*0Sstevel@tonic-gate 		/* close up the cookie up to (including) prev_pfn */
1292*0Sstevel@tonic-gate 		MAKE_DMA_COOKIE(cookie_p, IOMMU_PTOB(seg_pfn0) | bypass_prefix,
1293*0Sstevel@tonic-gate 			IOMMU_PTOB(pfn_no));
1294*0Sstevel@tonic-gate 		DEBUG2(DBG_BYPASS, mp->dmai_rdip, "cookie %p (%x pages)\n",
1295*0Sstevel@tonic-gate 			IOMMU_PTOB(seg_pfn0) | bypass_prefix, pfn_no);
1296*0Sstevel@tonic-gate 
1297*0Sstevel@tonic-gate 		cookie_p++;	/* advance to next available cookie cell */
1298*0Sstevel@tonic-gate 		pfn_no = 0;
1299*0Sstevel@tonic-gate 		seg_pfn0 = pfn;	/* start a new segment from current pfn */
1300*0Sstevel@tonic-gate 	}
1301*0Sstevel@tonic-gate 	MAKE_DMA_COOKIE(cookie_p, IOMMU_PTOB(seg_pfn0) | bypass_prefix,
1302*0Sstevel@tonic-gate 		IOMMU_PTOB(pfn_no));
1303*0Sstevel@tonic-gate 	DEBUG3(DBG_BYPASS, mp->dmai_rdip, "cookie %p (%x pages) of total %x\n",
1304*0Sstevel@tonic-gate 		IOMMU_PTOB(seg_pfn0) | bypass_prefix, pfn_no, cookie_no);
1305*0Sstevel@tonic-gate #ifdef DEBUG
1306*0Sstevel@tonic-gate 	cookie_p++;
1307*0Sstevel@tonic-gate 	ASSERT((cookie_p - (ddi_dma_cookie_t *)(win_p + 1)) == cookie_no);
1308*0Sstevel@tonic-gate #endif
1309*0Sstevel@tonic-gate 	*win_pp = win_p;
1310*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
1311*0Sstevel@tonic-gate noresource:
1312*0Sstevel@tonic-gate 	if (waitfp != DDI_DMA_DONTWAIT)
1313*0Sstevel@tonic-gate 		ddi_set_callback(waitfp, dmareq->dmar_arg, &pci_kmem_clid);
1314*0Sstevel@tonic-gate 	return (DDI_DMA_NORESOURCES);
1315*0Sstevel@tonic-gate }
1316*0Sstevel@tonic-gate 
1317*0Sstevel@tonic-gate /*
1318*0Sstevel@tonic-gate  * pci_dma_adjust - adjust 1st and last cookie and window sizes
1319*0Sstevel@tonic-gate  *	remove initial dma page offset from 1st cookie and window size
1320*0Sstevel@tonic-gate  *	remove last dma page remainder from last cookie and window size
1321*0Sstevel@tonic-gate  *	fill win_offset of each dma window according to just fixed up
1322*0Sstevel@tonic-gate  *		each window sizes
1323*0Sstevel@tonic-gate  *	pci_dma_win_t members modified:
1324*0Sstevel@tonic-gate  *	win_p->win_offset - this window's offset within entire DMA object
1325*0Sstevel@tonic-gate  *	win_p->win_size	  - xferrable size (in bytes) for this window
1326*0Sstevel@tonic-gate  *
1327*0Sstevel@tonic-gate  *	ddi_dma_impl_t members modified:
1328*0Sstevel@tonic-gate  *	mp->dmai_size	  - 1st window xferrable size
1329*0Sstevel@tonic-gate  *	mp->dmai_offset   - 0, which is the dma offset of the 1st window
1330*0Sstevel@tonic-gate  *
1331*0Sstevel@tonic-gate  *	ddi_dma_cookie_t members modified:
1332*0Sstevel@tonic-gate  *	cookie_p->dmac_size - 1st and last cookie remove offset or remainder
1333*0Sstevel@tonic-gate  *	cookie_p->dmac_laddress - 1st cookie add page offset
1334*0Sstevel@tonic-gate  */
1335*0Sstevel@tonic-gate static void
1336*0Sstevel@tonic-gate pci_dma_adjust(ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp, pci_dma_win_t *win_p)
1337*0Sstevel@tonic-gate {
1338*0Sstevel@tonic-gate 	ddi_dma_cookie_t *cookie_p = (ddi_dma_cookie_t *)(win_p + 1);
1339*0Sstevel@tonic-gate 	size_t pg_offset = mp->dmai_roffset;
1340*0Sstevel@tonic-gate 	size_t win_offset = 0;
1341*0Sstevel@tonic-gate 
1342*0Sstevel@tonic-gate 	cookie_p->dmac_size -= pg_offset;
1343*0Sstevel@tonic-gate 	cookie_p->dmac_laddress |= pg_offset;
1344*0Sstevel@tonic-gate 	win_p->win_size -= pg_offset;
1345*0Sstevel@tonic-gate 	DEBUG1(DBG_BYPASS, mp->dmai_rdip, "pg0 adjust %lx\n", pg_offset);
1346*0Sstevel@tonic-gate 
1347*0Sstevel@tonic-gate 	mp->dmai_size = win_p->win_size;
1348*0Sstevel@tonic-gate 	mp->dmai_offset = 0;
1349*0Sstevel@tonic-gate 
1350*0Sstevel@tonic-gate 	pg_offset += mp->dmai_object.dmao_size;
1351*0Sstevel@tonic-gate 	pg_offset &= IOMMU_PAGE_OFFSET;
1352*0Sstevel@tonic-gate 	if (pg_offset)
1353*0Sstevel@tonic-gate 		pg_offset = IOMMU_PAGE_SIZE - pg_offset;
1354*0Sstevel@tonic-gate 	DEBUG1(DBG_BYPASS, mp->dmai_rdip, "last pg adjust %lx\n", pg_offset);
1355*0Sstevel@tonic-gate 
1356*0Sstevel@tonic-gate 	for (; win_p->win_next; win_p = win_p->win_next) {
1357*0Sstevel@tonic-gate 		DEBUG1(DBG_BYPASS, mp->dmai_rdip, "win off %p\n", win_offset);
1358*0Sstevel@tonic-gate 		win_p->win_offset = win_offset;
1359*0Sstevel@tonic-gate 		win_offset += win_p->win_size;
1360*0Sstevel@tonic-gate 	}
1361*0Sstevel@tonic-gate 	/* last window */
1362*0Sstevel@tonic-gate 	win_p->win_offset = win_offset;
1363*0Sstevel@tonic-gate 	cookie_p = (ddi_dma_cookie_t *)(win_p + 1);
1364*0Sstevel@tonic-gate 	cookie_p[win_p->win_ncookies - 1].dmac_size -= pg_offset;
1365*0Sstevel@tonic-gate 	win_p->win_size -= pg_offset;
1366*0Sstevel@tonic-gate 	ASSERT((win_offset + win_p->win_size) == mp->dmai_object.dmao_size);
1367*0Sstevel@tonic-gate }
1368*0Sstevel@tonic-gate 
1369*0Sstevel@tonic-gate /*
1370*0Sstevel@tonic-gate  * pci_dma_physwin() - carve up dma windows using physical addresses.
1371*0Sstevel@tonic-gate  *	Called to handle iommu bypass and pci peer-to-peer transfers.
1372*0Sstevel@tonic-gate  *	Calls pci_dma_newwin() to allocate window objects.
1373*0Sstevel@tonic-gate  *
1374*0Sstevel@tonic-gate  * Dependency: mp->dmai_pfnlst points to an array of pfns
1375*0Sstevel@tonic-gate  *
1376*0Sstevel@tonic-gate  * 1. Each dma window is represented by a pci_dma_win_t object.
1377*0Sstevel@tonic-gate  *	The object will be casted to ddi_dma_win_t and returned
1378*0Sstevel@tonic-gate  *	to leaf driver through the DDI interface.
1379*0Sstevel@tonic-gate  * 2. Each dma window can have several dma segments with each
1380*0Sstevel@tonic-gate  *	segment representing a physically contiguous either memory
1381*0Sstevel@tonic-gate  *	space (if we are doing an iommu bypass transfer) or pci address
1382*0Sstevel@tonic-gate  *	space (if we are doing a peer-to-peer transfer).
1383*0Sstevel@tonic-gate  * 3. Each segment has a DMA cookie to program the DMA engine.
1384*0Sstevel@tonic-gate  *	The cookies within each DMA window must be located in a
1385*0Sstevel@tonic-gate  *	contiguous array per ddi_dma_nextcookie(9f).
1386*0Sstevel@tonic-gate  * 4. The number of DMA segments within each DMA window cannot exceed
1387*0Sstevel@tonic-gate  *	mp->dmai_attr.dma_attr_sgllen. If the transfer size is
1388*0Sstevel@tonic-gate  *	too large to fit in the sgllen, the rest needs to be
1389*0Sstevel@tonic-gate  *	relocated to the next dma window.
1390*0Sstevel@tonic-gate  * 5. Peer-to-peer DMA segment follows device hi, lo, count_max,
1391*0Sstevel@tonic-gate  *	and nocross restrictions while bypass DMA follows the set of
1392*0Sstevel@tonic-gate  *	restrictions with system limits factored in.
1393*0Sstevel@tonic-gate  *
1394*0Sstevel@tonic-gate  * Return:
1395*0Sstevel@tonic-gate  *	mp->dmai_winlst	 - points to a link list of pci_dma_win_t objects.
1396*0Sstevel@tonic-gate  *		Each pci_dma_win_t object on the link list contains
1397*0Sstevel@tonic-gate  *		infomation such as its window size (# of pages),
1398*0Sstevel@tonic-gate  *		starting offset (also see Restriction), an array of
1399*0Sstevel@tonic-gate  *		DMA cookies, and # of cookies in the array.
1400*0Sstevel@tonic-gate  *	mp->dmai_pfnlst	 - NULL, the pfn list is freed to conserve memory.
1401*0Sstevel@tonic-gate  *	mp->dmai_nwin	 - # of total DMA windows on mp->dmai_winlst.
1402*0Sstevel@tonic-gate  *	mp->dmai_mapping - starting cookie address
1403*0Sstevel@tonic-gate  *	mp->dmai_rflags	 - consistent, nosync, no redzone
1404*0Sstevel@tonic-gate  *	mp->dmai_cookie	 - start of cookie table of the 1st DMA window
1405*0Sstevel@tonic-gate  *
1406*0Sstevel@tonic-gate  * Restriction:
1407*0Sstevel@tonic-gate  *	Each pci_dma_win_t object can theoratically start from any offset
1408*0Sstevel@tonic-gate  *	since the iommu is not involved. However, this implementation
1409*0Sstevel@tonic-gate  *	always make windows start from page aligned offset (except
1410*0Sstevel@tonic-gate  *	the 1st window, which follows the requested offset) due to the
1411*0Sstevel@tonic-gate  *	fact that we are handed a pfn list. This does require device's
1412*0Sstevel@tonic-gate  *	count_max and attr_seg to be at least IOMMU_PAGE_SIZE aligned.
1413*0Sstevel@tonic-gate  */
1414*0Sstevel@tonic-gate int
1415*0Sstevel@tonic-gate pci_dma_physwin(pci_t *pci_p, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp)
1416*0Sstevel@tonic-gate {
1417*0Sstevel@tonic-gate 	uint_t npages = mp->dmai_ndvmapages;
1418*0Sstevel@tonic-gate 	int ret, sgllen = mp->dmai_attr.dma_attr_sgllen;
1419*0Sstevel@tonic-gate 	iopfn_t pfn_lo, pfn_hi, prev_pfn, bypass_pfn;
1420*0Sstevel@tonic-gate 	iopfn_t pfn = PCI_GET_MP_PFN(mp, 0);
1421*0Sstevel@tonic-gate 	uint32_t i, win_no = 0, pfn_no = 1, win_pfn0_index = 0, cookie_no = 0;
1422*0Sstevel@tonic-gate 	uint64_t count_max, bypass = PCI_DMA_BYPASS_PREFIX(mp, pfn);
1423*0Sstevel@tonic-gate 	pci_dma_win_t **win_pp = (pci_dma_win_t **)&mp->dmai_winlst;
1424*0Sstevel@tonic-gate 	ddi_dma_cookie_t *cookie0_p;
1425*0Sstevel@tonic-gate 
1426*0Sstevel@tonic-gate 	if (PCI_DMA_ISPTP(mp)) { /* ignore sys limits for peer-to-peer */
1427*0Sstevel@tonic-gate 		ddi_dma_attr_t *dev_attr_p = DEV_ATTR(mp);
1428*0Sstevel@tonic-gate 		iopfn_t pfn_base = pci_p->pci_pbm_p->pbm_base_pfn;
1429*0Sstevel@tonic-gate 		iopfn_t pfn_last = pci_p->pci_pbm_p->pbm_last_pfn - pfn_base;
1430*0Sstevel@tonic-gate 		uint64_t nocross = dev_attr_p->dma_attr_seg;
1431*0Sstevel@tonic-gate 		if (nocross && (nocross < UINT32_MAX))
1432*0Sstevel@tonic-gate 			return (DDI_DMA_NOMAPPING);
1433*0Sstevel@tonic-gate 		if (dev_attr_p->dma_attr_align > IOMMU_PAGE_SIZE)
1434*0Sstevel@tonic-gate 			return (DDI_DMA_NOMAPPING);
1435*0Sstevel@tonic-gate 		pfn_lo = IOMMU_BTOP(dev_attr_p->dma_attr_addr_lo);
1436*0Sstevel@tonic-gate 		pfn_hi = IOMMU_BTOP(dev_attr_p->dma_attr_addr_hi);
1437*0Sstevel@tonic-gate 		pfn_hi = MIN(pfn_hi, pfn_last);
1438*0Sstevel@tonic-gate 		if ((pfn_lo > pfn_hi) || (pfn < pfn_lo))
1439*0Sstevel@tonic-gate 			return (DDI_DMA_NOMAPPING);
1440*0Sstevel@tonic-gate 		count_max = dev_attr_p->dma_attr_count_max;
1441*0Sstevel@tonic-gate 		count_max = MIN(count_max, nocross);
1442*0Sstevel@tonic-gate 		/*
1443*0Sstevel@tonic-gate 		 * the following count_max trim is not done because we are
1444*0Sstevel@tonic-gate 		 * making sure pfn_lo <= pfn <= pfn_hi inside the loop
1445*0Sstevel@tonic-gate 		 * count_max=MIN(count_max, IOMMU_PTOB(pfn_hi - pfn_lo + 1)-1);
1446*0Sstevel@tonic-gate 		 */
1447*0Sstevel@tonic-gate 	} else { /* bypass hi/lo/count_max have been processed by attr2hdl() */
1448*0Sstevel@tonic-gate 		count_max = mp->dmai_attr.dma_attr_count_max;
1449*0Sstevel@tonic-gate 		pfn_lo = IOMMU_BTOP(mp->dmai_attr.dma_attr_addr_lo);
1450*0Sstevel@tonic-gate 		pfn_hi = IOMMU_BTOP(mp->dmai_attr.dma_attr_addr_hi);
1451*0Sstevel@tonic-gate 	}
1452*0Sstevel@tonic-gate 
1453*0Sstevel@tonic-gate 	bypass_pfn = IOMMU_BTOP(bypass);
1454*0Sstevel@tonic-gate 
1455*0Sstevel@tonic-gate 	for (prev_pfn = (bypass_pfn | pfn), i = 1; i < npages;
1456*0Sstevel@tonic-gate 	    i++, prev_pfn = pfn, pfn_no++) {
1457*0Sstevel@tonic-gate 		pfn = bypass_pfn | PCI_GET_MP_PFN1(mp, i);
1458*0Sstevel@tonic-gate 		if ((pfn == prev_pfn + 1) &&
1459*0Sstevel@tonic-gate 			(IOMMU_PTOB(pfn_no + 1) - 1 <= count_max))
1460*0Sstevel@tonic-gate 			continue;
1461*0Sstevel@tonic-gate 		if ((pfn < pfn_lo) || (prev_pfn > pfn_hi)) {
1462*0Sstevel@tonic-gate 			ret = DDI_DMA_NOMAPPING;
1463*0Sstevel@tonic-gate 			goto err;
1464*0Sstevel@tonic-gate 		}
1465*0Sstevel@tonic-gate 		cookie_no++;
1466*0Sstevel@tonic-gate 		pfn_no = 0;
1467*0Sstevel@tonic-gate 		if (cookie_no < sgllen)
1468*0Sstevel@tonic-gate 			continue;
1469*0Sstevel@tonic-gate 
1470*0Sstevel@tonic-gate 		DEBUG3(DBG_BYPASS, mp->dmai_rdip, "newwin pfn[%x-%x] %x cks\n",
1471*0Sstevel@tonic-gate 			win_pfn0_index, i - 1, cookie_no);
1472*0Sstevel@tonic-gate 		if (ret = pci_dma_newwin(dmareq, mp, cookie_no,
1473*0Sstevel@tonic-gate 			win_pfn0_index, i - 1, win_pp, count_max, bypass))
1474*0Sstevel@tonic-gate 			goto err;
1475*0Sstevel@tonic-gate 
1476*0Sstevel@tonic-gate 		win_pp = &(*win_pp)->win_next;	/* win_pp = *(win_pp) */
1477*0Sstevel@tonic-gate 		win_no++;
1478*0Sstevel@tonic-gate 		win_pfn0_index = i;
1479*0Sstevel@tonic-gate 		cookie_no = 0;
1480*0Sstevel@tonic-gate 	}
1481*0Sstevel@tonic-gate 	if (pfn > pfn_hi) {
1482*0Sstevel@tonic-gate 		ret = DDI_DMA_NOMAPPING;
1483*0Sstevel@tonic-gate 		goto err;
1484*0Sstevel@tonic-gate 	}
1485*0Sstevel@tonic-gate 	cookie_no++;
1486*0Sstevel@tonic-gate 	DEBUG3(DBG_BYPASS, mp->dmai_rdip, "newwin pfn[%x-%x] %x cks\n",
1487*0Sstevel@tonic-gate 		win_pfn0_index, i - 1, cookie_no);
1488*0Sstevel@tonic-gate 	if (ret = pci_dma_newwin(dmareq, mp, cookie_no, win_pfn0_index,
1489*0Sstevel@tonic-gate 		i - 1, win_pp, count_max, bypass))
1490*0Sstevel@tonic-gate 		goto err;
1491*0Sstevel@tonic-gate 	win_no++;
1492*0Sstevel@tonic-gate 	pci_dma_adjust(dmareq, mp, mp->dmai_winlst);
1493*0Sstevel@tonic-gate 	mp->dmai_nwin = win_no;
1494*0Sstevel@tonic-gate 	mp->dmai_rflags |= DDI_DMA_CONSISTENT;
1495*0Sstevel@tonic-gate 	if (!pci_p->pci_pbm_p->pbm_sync_reg_pa) {
1496*0Sstevel@tonic-gate 		mp->dmai_rflags |= DMP_NOSYNC;
1497*0Sstevel@tonic-gate 		mp->dmai_flags |= DMAI_FLAGS_NOSYNC;
1498*0Sstevel@tonic-gate 	}
1499*0Sstevel@tonic-gate 	mp->dmai_rflags &= ~DDI_DMA_REDZONE;
1500*0Sstevel@tonic-gate 	cookie0_p = (ddi_dma_cookie_t *)(WINLST(mp) + 1);
1501*0Sstevel@tonic-gate 	mp->dmai_cookie = WINLST(mp)->win_ncookies > 1 ? cookie0_p + 1 : 0;
1502*0Sstevel@tonic-gate 	mp->dmai_mapping = cookie0_p->dmac_laddress;
1503*0Sstevel@tonic-gate 
1504*0Sstevel@tonic-gate 	pci_dma_freepfn(mp);
1505*0Sstevel@tonic-gate 	return (DDI_DMA_MAPPED);
1506*0Sstevel@tonic-gate err:
1507*0Sstevel@tonic-gate 	pci_dma_freewin(mp);
1508*0Sstevel@tonic-gate 	return (ret);
1509*0Sstevel@tonic-gate }
1510*0Sstevel@tonic-gate 
1511*0Sstevel@tonic-gate /*ARGSUSED*/
1512*0Sstevel@tonic-gate int
1513*0Sstevel@tonic-gate pci_dma_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_impl_t *mp,
1514*0Sstevel@tonic-gate 	enum ddi_dma_ctlops cmd, off_t *offp, size_t *lenp, caddr_t *objp,
1515*0Sstevel@tonic-gate 	uint_t cache_flags)
1516*0Sstevel@tonic-gate {
1517*0Sstevel@tonic-gate 	switch (cmd) {
1518*0Sstevel@tonic-gate 	case DDI_DMA_SYNC: /* XXX */
1519*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
1520*0Sstevel@tonic-gate 
1521*0Sstevel@tonic-gate 	case DDI_DMA_HTOC: {
1522*0Sstevel@tonic-gate 		off_t off = *offp;
1523*0Sstevel@tonic-gate 		ddi_dma_cookie_t *loop_cp, *cp;
1524*0Sstevel@tonic-gate 		pci_dma_win_t *win_p = mp->dmai_winlst;
1525*0Sstevel@tonic-gate 
1526*0Sstevel@tonic-gate 		if (off >= mp->dmai_object.dmao_size)
1527*0Sstevel@tonic-gate 			return (DDI_FAILURE);
1528*0Sstevel@tonic-gate 
1529*0Sstevel@tonic-gate 		/* locate window */
1530*0Sstevel@tonic-gate 		while (win_p->win_offset + win_p->win_size <= off)
1531*0Sstevel@tonic-gate 			win_p = win_p->win_next;
1532*0Sstevel@tonic-gate 
1533*0Sstevel@tonic-gate 		loop_cp = cp = (ddi_dma_cookie_t *)(win_p + 1);
1534*0Sstevel@tonic-gate 		mp->dmai_offset = win_p->win_offset;
1535*0Sstevel@tonic-gate 		mp->dmai_size   = win_p->win_size;
1536*0Sstevel@tonic-gate 		mp->dmai_mapping = cp->dmac_laddress; /* cookie0 start addr */
1537*0Sstevel@tonic-gate 
1538*0Sstevel@tonic-gate 		/* adjust cookie addr/len if we are not on cookie boundary */
1539*0Sstevel@tonic-gate 		off -= win_p->win_offset;	   /* offset within window */
1540*0Sstevel@tonic-gate 		for (; off >= loop_cp->dmac_size; loop_cp++)
1541*0Sstevel@tonic-gate 			off -= loop_cp->dmac_size; /* offset within cookie */
1542*0Sstevel@tonic-gate 
1543*0Sstevel@tonic-gate 		mp->dmai_cookie = loop_cp + 1;
1544*0Sstevel@tonic-gate 		win_p->win_curseg = loop_cp - cp;
1545*0Sstevel@tonic-gate 		cp = (ddi_dma_cookie_t *)objp;
1546*0Sstevel@tonic-gate 		MAKE_DMA_COOKIE(cp, loop_cp->dmac_laddress + off,
1547*0Sstevel@tonic-gate 			loop_cp->dmac_size - off);
1548*0Sstevel@tonic-gate 
1549*0Sstevel@tonic-gate 		DEBUG2(DBG_DMA_CTL, dip,
1550*0Sstevel@tonic-gate 			"HTOC: cookie - dmac_laddress=%p dmac_size=%x\n",
1551*0Sstevel@tonic-gate 			cp->dmac_laddress, cp->dmac_size);
1552*0Sstevel@tonic-gate 		}
1553*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
1554*0Sstevel@tonic-gate 
1555*0Sstevel@tonic-gate 	case DDI_DMA_REPWIN:
1556*0Sstevel@tonic-gate 		*offp = mp->dmai_offset;
1557*0Sstevel@tonic-gate 		*lenp = mp->dmai_size;
1558*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
1559*0Sstevel@tonic-gate 
1560*0Sstevel@tonic-gate 	case DDI_DMA_MOVWIN: {
1561*0Sstevel@tonic-gate 		off_t off = *offp;
1562*0Sstevel@tonic-gate 		ddi_dma_cookie_t *cp;
1563*0Sstevel@tonic-gate 		pci_dma_win_t *win_p = mp->dmai_winlst;
1564*0Sstevel@tonic-gate 
1565*0Sstevel@tonic-gate 		if (off >= mp->dmai_object.dmao_size)
1566*0Sstevel@tonic-gate 			return (DDI_FAILURE);
1567*0Sstevel@tonic-gate 
1568*0Sstevel@tonic-gate 		/* locate window */
1569*0Sstevel@tonic-gate 		while (win_p->win_offset + win_p->win_size <= off)
1570*0Sstevel@tonic-gate 			win_p = win_p->win_next;
1571*0Sstevel@tonic-gate 
1572*0Sstevel@tonic-gate 		cp = (ddi_dma_cookie_t *)(win_p + 1);
1573*0Sstevel@tonic-gate 		mp->dmai_offset = win_p->win_offset;
1574*0Sstevel@tonic-gate 		mp->dmai_size   = win_p->win_size;
1575*0Sstevel@tonic-gate 		mp->dmai_mapping = cp->dmac_laddress;	/* cookie0 star addr */
1576*0Sstevel@tonic-gate 		mp->dmai_cookie = cp + 1;
1577*0Sstevel@tonic-gate 		win_p->win_curseg = 0;
1578*0Sstevel@tonic-gate 
1579*0Sstevel@tonic-gate 		*(ddi_dma_cookie_t *)objp = *cp;
1580*0Sstevel@tonic-gate 		*offp = win_p->win_offset;
1581*0Sstevel@tonic-gate 		*lenp = win_p->win_size;
1582*0Sstevel@tonic-gate 		DEBUG2(DBG_DMA_CTL, dip,
1583*0Sstevel@tonic-gate 			"HTOC: cookie - dmac_laddress=%p dmac_size=%x\n",
1584*0Sstevel@tonic-gate 			cp->dmac_laddress, cp->dmac_size);
1585*0Sstevel@tonic-gate 		}
1586*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
1587*0Sstevel@tonic-gate 
1588*0Sstevel@tonic-gate 	case DDI_DMA_NEXTWIN: {
1589*0Sstevel@tonic-gate 		pci_dma_win_t *win_p = *(pci_dma_win_t **)offp;
1590*0Sstevel@tonic-gate 		pci_dma_win_t **nw_pp = (pci_dma_win_t **)objp;
1591*0Sstevel@tonic-gate 		ddi_dma_cookie_t *cp;
1592*0Sstevel@tonic-gate 		if (!win_p) {
1593*0Sstevel@tonic-gate 			*nw_pp = mp->dmai_winlst;
1594*0Sstevel@tonic-gate 			return (DDI_SUCCESS);
1595*0Sstevel@tonic-gate 		}
1596*0Sstevel@tonic-gate 
1597*0Sstevel@tonic-gate 		if (win_p->win_offset != mp->dmai_offset)
1598*0Sstevel@tonic-gate 			return (DDI_DMA_STALE);
1599*0Sstevel@tonic-gate 		if (!win_p->win_next)
1600*0Sstevel@tonic-gate 			return (DDI_DMA_DONE);
1601*0Sstevel@tonic-gate 		win_p = win_p->win_next;
1602*0Sstevel@tonic-gate 		cp = (ddi_dma_cookie_t *)(win_p + 1);
1603*0Sstevel@tonic-gate 		mp->dmai_offset = win_p->win_offset;
1604*0Sstevel@tonic-gate 		mp->dmai_size   = win_p->win_size;
1605*0Sstevel@tonic-gate 		mp->dmai_mapping = cp->dmac_laddress;   /* cookie0 star addr */
1606*0Sstevel@tonic-gate 		mp->dmai_cookie = cp + 1;
1607*0Sstevel@tonic-gate 		win_p->win_curseg = 0;
1608*0Sstevel@tonic-gate 		*nw_pp = win_p;
1609*0Sstevel@tonic-gate 		}
1610*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
1611*0Sstevel@tonic-gate 
1612*0Sstevel@tonic-gate 	case DDI_DMA_NEXTSEG: {
1613*0Sstevel@tonic-gate 		pci_dma_win_t *w_p = *(pci_dma_win_t **)offp;
1614*0Sstevel@tonic-gate 		if (w_p->win_offset != mp->dmai_offset)
1615*0Sstevel@tonic-gate 			return (DDI_DMA_STALE);
1616*0Sstevel@tonic-gate 		if (w_p->win_curseg + 1 >= w_p->win_ncookies)
1617*0Sstevel@tonic-gate 			return (DDI_DMA_DONE);
1618*0Sstevel@tonic-gate 		w_p->win_curseg++;
1619*0Sstevel@tonic-gate 		}
1620*0Sstevel@tonic-gate 		*(ddi_dma_seg_t *)objp = (ddi_dma_seg_t)mp;
1621*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
1622*0Sstevel@tonic-gate 
1623*0Sstevel@tonic-gate 	case DDI_DMA_SEGTOC: {
1624*0Sstevel@tonic-gate 		pci_dma_win_t *win_p = mp->dmai_winlst;
1625*0Sstevel@tonic-gate 		off_t off = mp->dmai_offset;
1626*0Sstevel@tonic-gate 		ddi_dma_cookie_t *cp;
1627*0Sstevel@tonic-gate 		int i;
1628*0Sstevel@tonic-gate 
1629*0Sstevel@tonic-gate 		/* locate active window */
1630*0Sstevel@tonic-gate 		for (; win_p->win_offset != off; win_p = win_p->win_next);
1631*0Sstevel@tonic-gate 		cp = (ddi_dma_cookie_t *)(win_p + 1);
1632*0Sstevel@tonic-gate 		for (i = 0; i < win_p->win_curseg; i++, cp++)
1633*0Sstevel@tonic-gate 			off += cp->dmac_size;
1634*0Sstevel@tonic-gate 		*offp = off;
1635*0Sstevel@tonic-gate 		*lenp = cp->dmac_size;
1636*0Sstevel@tonic-gate 		*(ddi_dma_cookie_t *)objp = *cp;	/* copy cookie */
1637*0Sstevel@tonic-gate 		}
1638*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
1639*0Sstevel@tonic-gate 
1640*0Sstevel@tonic-gate 	case DDI_DMA_COFF: {
1641*0Sstevel@tonic-gate 		pci_dma_win_t *win_p;
1642*0Sstevel@tonic-gate 		ddi_dma_cookie_t *cp;
1643*0Sstevel@tonic-gate 		uint64_t addr, key = ((ddi_dma_cookie_t *)offp)->dmac_laddress;
1644*0Sstevel@tonic-gate 		size_t win_off;
1645*0Sstevel@tonic-gate 
1646*0Sstevel@tonic-gate 		for (win_p = mp->dmai_winlst; win_p; win_p = win_p->win_next) {
1647*0Sstevel@tonic-gate 			int i;
1648*0Sstevel@tonic-gate 			win_off = 0;
1649*0Sstevel@tonic-gate 			cp = (ddi_dma_cookie_t *)(win_p + 1);
1650*0Sstevel@tonic-gate 			for (i = 0; i < win_p->win_ncookies; i++, cp++) {
1651*0Sstevel@tonic-gate 				size_t sz = cp->dmac_size;
1652*0Sstevel@tonic-gate 
1653*0Sstevel@tonic-gate 				addr = cp->dmac_laddress;
1654*0Sstevel@tonic-gate 				if ((addr <= key) && (addr + sz >= key))
1655*0Sstevel@tonic-gate 					goto found;
1656*0Sstevel@tonic-gate 				win_off += sz;
1657*0Sstevel@tonic-gate 			}
1658*0Sstevel@tonic-gate 		}
1659*0Sstevel@tonic-gate 		return (DDI_FAILURE);
1660*0Sstevel@tonic-gate found:
1661*0Sstevel@tonic-gate 		*objp = (caddr_t)(win_p->win_offset + win_off + (key - addr));
1662*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
1663*0Sstevel@tonic-gate 		}
1664*0Sstevel@tonic-gate 
1665*0Sstevel@tonic-gate 	case DDI_DMA_REMAP:
1666*0Sstevel@tonic-gate 		return (DDI_FAILURE);
1667*0Sstevel@tonic-gate 
1668*0Sstevel@tonic-gate 	default:
1669*0Sstevel@tonic-gate 		DEBUG3(DBG_DMA_CTL, dip, "unknown command (%x): rdip=%s%d\n",
1670*0Sstevel@tonic-gate 			cmd, ddi_driver_name(rdip), ddi_get_instance(rdip));
1671*0Sstevel@tonic-gate 		break;
1672*0Sstevel@tonic-gate 	}
1673*0Sstevel@tonic-gate 	return (DDI_FAILURE);
1674*0Sstevel@tonic-gate }
1675*0Sstevel@tonic-gate 
1676*0Sstevel@tonic-gate static void
1677*0Sstevel@tonic-gate pci_dvma_debug_init(iommu_t *iommu_p)
1678*0Sstevel@tonic-gate {
1679*0Sstevel@tonic-gate 	size_t sz = sizeof (struct dvma_rec) * pci_dvma_debug_rec;
1680*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&iommu_p->dvma_debug_lock));
1681*0Sstevel@tonic-gate 	cmn_err(CE_NOTE, "PCI DVMA %p stat ON", iommu_p);
1682*0Sstevel@tonic-gate 
1683*0Sstevel@tonic-gate 	iommu_p->dvma_alloc_rec = kmem_zalloc(sz, KM_SLEEP);
1684*0Sstevel@tonic-gate 	iommu_p->dvma_free_rec = kmem_zalloc(sz, KM_SLEEP);
1685*0Sstevel@tonic-gate 
1686*0Sstevel@tonic-gate 	iommu_p->dvma_active_list = NULL;
1687*0Sstevel@tonic-gate 	iommu_p->dvma_alloc_rec_index = 0;
1688*0Sstevel@tonic-gate 	iommu_p->dvma_free_rec_index = 0;
1689*0Sstevel@tonic-gate 	iommu_p->dvma_active_count = 0;
1690*0Sstevel@tonic-gate }
1691*0Sstevel@tonic-gate 
1692*0Sstevel@tonic-gate void
1693*0Sstevel@tonic-gate pci_dvma_debug_fini(iommu_t *iommu_p)
1694*0Sstevel@tonic-gate {
1695*0Sstevel@tonic-gate 	struct dvma_rec *prev, *ptr;
1696*0Sstevel@tonic-gate 	size_t sz = sizeof (struct dvma_rec) * pci_dvma_debug_rec;
1697*0Sstevel@tonic-gate 	uint64_t mask = ~(1ull << iommu_p->iommu_inst);
1698*0Sstevel@tonic-gate 	cmn_err(CE_NOTE, "PCI DVMA %p stat OFF", iommu_p);
1699*0Sstevel@tonic-gate 
1700*0Sstevel@tonic-gate 	kmem_free(iommu_p->dvma_alloc_rec, sz);
1701*0Sstevel@tonic-gate 	kmem_free(iommu_p->dvma_free_rec, sz);
1702*0Sstevel@tonic-gate 	iommu_p->dvma_alloc_rec = iommu_p->dvma_free_rec = NULL;
1703*0Sstevel@tonic-gate 
1704*0Sstevel@tonic-gate 	prev = iommu_p->dvma_active_list;
1705*0Sstevel@tonic-gate 	if (!prev)
1706*0Sstevel@tonic-gate 		return;
1707*0Sstevel@tonic-gate 	for (ptr = prev->next; ptr; prev = ptr, ptr = ptr->next)
1708*0Sstevel@tonic-gate 		kmem_free(prev, sizeof (struct dvma_rec));
1709*0Sstevel@tonic-gate 	kmem_free(prev, sizeof (struct dvma_rec));
1710*0Sstevel@tonic-gate 
1711*0Sstevel@tonic-gate 	iommu_p->dvma_active_list = NULL;
1712*0Sstevel@tonic-gate 	iommu_p->dvma_alloc_rec_index = 0;
1713*0Sstevel@tonic-gate 	iommu_p->dvma_free_rec_index = 0;
1714*0Sstevel@tonic-gate 	iommu_p->dvma_active_count = 0;
1715*0Sstevel@tonic-gate 
1716*0Sstevel@tonic-gate 	pci_dvma_debug_on  &= mask;
1717*0Sstevel@tonic-gate 	pci_dvma_debug_off &= mask;
1718*0Sstevel@tonic-gate }
1719*0Sstevel@tonic-gate 
1720*0Sstevel@tonic-gate void
1721*0Sstevel@tonic-gate pci_dvma_alloc_debug(iommu_t *iommu_p, char *address, uint_t len,
1722*0Sstevel@tonic-gate 	ddi_dma_impl_t *mp)
1723*0Sstevel@tonic-gate {
1724*0Sstevel@tonic-gate 	struct dvma_rec *ptr;
1725*0Sstevel@tonic-gate 	mutex_enter(&iommu_p->dvma_debug_lock);
1726*0Sstevel@tonic-gate 
1727*0Sstevel@tonic-gate 	if (!iommu_p->dvma_alloc_rec)
1728*0Sstevel@tonic-gate 		pci_dvma_debug_init(iommu_p);
1729*0Sstevel@tonic-gate 	if (DVMA_DBG_OFF(iommu_p)) {
1730*0Sstevel@tonic-gate 		pci_dvma_debug_fini(iommu_p);
1731*0Sstevel@tonic-gate 		goto done;
1732*0Sstevel@tonic-gate 	}
1733*0Sstevel@tonic-gate 
1734*0Sstevel@tonic-gate 	ptr = &iommu_p->dvma_alloc_rec[iommu_p->dvma_alloc_rec_index];
1735*0Sstevel@tonic-gate 	ptr->dvma_addr = address;
1736*0Sstevel@tonic-gate 	ptr->len = len;
1737*0Sstevel@tonic-gate 	ptr->mp = mp;
1738*0Sstevel@tonic-gate 	if (++iommu_p->dvma_alloc_rec_index == pci_dvma_debug_rec)
1739*0Sstevel@tonic-gate 		iommu_p->dvma_alloc_rec_index = 0;
1740*0Sstevel@tonic-gate 
1741*0Sstevel@tonic-gate 	ptr = kmem_alloc(sizeof (struct dvma_rec), KM_SLEEP);
1742*0Sstevel@tonic-gate 	ptr->dvma_addr = address;
1743*0Sstevel@tonic-gate 	ptr->len = len;
1744*0Sstevel@tonic-gate 	ptr->mp = mp;
1745*0Sstevel@tonic-gate 
1746*0Sstevel@tonic-gate 	ptr->next = iommu_p->dvma_active_list;
1747*0Sstevel@tonic-gate 	iommu_p->dvma_active_list = ptr;
1748*0Sstevel@tonic-gate 	iommu_p->dvma_active_count++;
1749*0Sstevel@tonic-gate done:
1750*0Sstevel@tonic-gate 	mutex_exit(&iommu_p->dvma_debug_lock);
1751*0Sstevel@tonic-gate }
1752*0Sstevel@tonic-gate 
1753*0Sstevel@tonic-gate void
1754*0Sstevel@tonic-gate pci_dvma_free_debug(iommu_t *iommu_p, char *address, uint_t len,
1755*0Sstevel@tonic-gate 	ddi_dma_impl_t *mp)
1756*0Sstevel@tonic-gate {
1757*0Sstevel@tonic-gate 	struct dvma_rec *ptr, *ptr_save;
1758*0Sstevel@tonic-gate 	mutex_enter(&iommu_p->dvma_debug_lock);
1759*0Sstevel@tonic-gate 
1760*0Sstevel@tonic-gate 	if (!iommu_p->dvma_alloc_rec)
1761*0Sstevel@tonic-gate 		pci_dvma_debug_init(iommu_p);
1762*0Sstevel@tonic-gate 	if (DVMA_DBG_OFF(iommu_p)) {
1763*0Sstevel@tonic-gate 		pci_dvma_debug_fini(iommu_p);
1764*0Sstevel@tonic-gate 		goto done;
1765*0Sstevel@tonic-gate 	}
1766*0Sstevel@tonic-gate 
1767*0Sstevel@tonic-gate 	ptr = &iommu_p->dvma_free_rec[iommu_p->dvma_free_rec_index];
1768*0Sstevel@tonic-gate 	ptr->dvma_addr = address;
1769*0Sstevel@tonic-gate 	ptr->len = len;
1770*0Sstevel@tonic-gate 	ptr->mp = mp;
1771*0Sstevel@tonic-gate 	if (++iommu_p->dvma_free_rec_index == pci_dvma_debug_rec)
1772*0Sstevel@tonic-gate 		iommu_p->dvma_free_rec_index = 0;
1773*0Sstevel@tonic-gate 
1774*0Sstevel@tonic-gate 	ptr_save = iommu_p->dvma_active_list;
1775*0Sstevel@tonic-gate 	for (ptr = ptr_save; ptr; ptr = ptr->next) {
1776*0Sstevel@tonic-gate 		if ((ptr->dvma_addr == address) && (ptr->len = len))
1777*0Sstevel@tonic-gate 			break;
1778*0Sstevel@tonic-gate 		ptr_save = ptr;
1779*0Sstevel@tonic-gate 	}
1780*0Sstevel@tonic-gate 	if (!ptr) {
1781*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "bad dvma free addr=%lx len=%x",
1782*0Sstevel@tonic-gate 			(long)address, len);
1783*0Sstevel@tonic-gate 		goto done;
1784*0Sstevel@tonic-gate 	}
1785*0Sstevel@tonic-gate 	if (ptr == iommu_p->dvma_active_list)
1786*0Sstevel@tonic-gate 		iommu_p->dvma_active_list = ptr->next;
1787*0Sstevel@tonic-gate 	else
1788*0Sstevel@tonic-gate 		ptr_save->next = ptr->next;
1789*0Sstevel@tonic-gate 	kmem_free(ptr, sizeof (struct dvma_rec));
1790*0Sstevel@tonic-gate 	iommu_p->dvma_active_count--;
1791*0Sstevel@tonic-gate done:
1792*0Sstevel@tonic-gate 	mutex_exit(&iommu_p->dvma_debug_lock);
1793*0Sstevel@tonic-gate }
1794*0Sstevel@tonic-gate 
1795*0Sstevel@tonic-gate #ifdef DEBUG
1796*0Sstevel@tonic-gate void
1797*0Sstevel@tonic-gate dump_dma_handle(uint64_t flag, dev_info_t *dip, ddi_dma_impl_t *hp)
1798*0Sstevel@tonic-gate {
1799*0Sstevel@tonic-gate 	DEBUG4(flag, dip, "mp(%p): flags=%x mapping=%lx xfer_size=%x\n",
1800*0Sstevel@tonic-gate 		hp, hp->dmai_inuse, hp->dmai_mapping, hp->dmai_size);
1801*0Sstevel@tonic-gate 	DEBUG4(flag|DBG_CONT, dip, "\tnpages=%x roffset=%x rflags=%x nwin=%x\n",
1802*0Sstevel@tonic-gate 		hp->dmai_ndvmapages, hp->dmai_roffset, hp->dmai_rflags,
1803*0Sstevel@tonic-gate 		hp->dmai_nwin);
1804*0Sstevel@tonic-gate 	DEBUG4(flag|DBG_CONT, dip, "\twinsize=%x tte=%p pfnlst=%p pfn0=%p\n",
1805*0Sstevel@tonic-gate 		hp->dmai_winsize, hp->dmai_tte, hp->dmai_pfnlst, hp->dmai_pfn0);
1806*0Sstevel@tonic-gate 	DEBUG4(flag|DBG_CONT, dip, "\twinlst=%x obj=%p attr=%p ckp=%p\n",
1807*0Sstevel@tonic-gate 		hp->dmai_winlst, &hp->dmai_object, &hp->dmai_attr,
1808*0Sstevel@tonic-gate 		hp->dmai_cookie);
1809*0Sstevel@tonic-gate }
1810*0Sstevel@tonic-gate #endif
1811*0Sstevel@tonic-gate 
1812*0Sstevel@tonic-gate void
1813*0Sstevel@tonic-gate pci_vmem_do_free(iommu_t *iommu_p, void *base_addr, size_t npages,
1814*0Sstevel@tonic-gate     int vmemcache)
1815*0Sstevel@tonic-gate {
1816*0Sstevel@tonic-gate 	vmem_t *map_p = iommu_p->iommu_dvma_map;
1817*0Sstevel@tonic-gate 
1818*0Sstevel@tonic-gate 	if (vmemcache) {
1819*0Sstevel@tonic-gate 		vmem_free(map_p, base_addr, IOMMU_PAGE_SIZE);
1820*0Sstevel@tonic-gate #ifdef PCI_DMA_PROF
1821*0Sstevel@tonic-gate 		pci_dvma_vmem_free++;
1822*0Sstevel@tonic-gate #endif
1823*0Sstevel@tonic-gate 		return;
1824*0Sstevel@tonic-gate 	}
1825*0Sstevel@tonic-gate 
1826*0Sstevel@tonic-gate 	vmem_xfree(map_p, base_addr, IOMMU_PTOB(npages));
1827*0Sstevel@tonic-gate #ifdef PCI_DMA_PROF
1828*0Sstevel@tonic-gate 		pci_dvma_vmem_xfree++;
1829*0Sstevel@tonic-gate #endif
1830*0Sstevel@tonic-gate }
1831