xref: /titanic_52/usr/src/uts/sun4/io/px/px_dma.c (revision 84ab085a13f931bc78e7415e7ce921dbaa14fcb3)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * PCI Express nexus DVMA and DMA core routines:
31  *	dma_map/dma_bind_handle implementation
32  *	bypass and peer-to-peer support
33  *	fast track DVMA space allocation
34  *	runtime DVMA debug
35  */
36 #include <sys/types.h>
37 #include <sys/kmem.h>
38 #include <sys/async.h>
39 #include <sys/sysmacros.h>
40 #include <sys/sunddi.h>
41 #include <sys/ddi_impldefs.h>
42 #include "px_obj.h"
43 
44 /*LINTLIBRARY*/
45 
46 /*
47  * px_dma_allocmp - Allocate a pci dma implementation structure
48  *
49  * An extra ddi_dma_attr structure is bundled with the usual ddi_dma_impl
50  * to hold unmodified device limits. The ddi_dma_attr inside the
51  * ddi_dma_impl structure is augumented with system limits to enhance
52  * DVMA performance at runtime. The unaugumented device limits saved
53  * right after (accessed through (ddi_dma_attr_t *)(mp + 1)) is used
54  * strictly for peer-to-peer transfers which do not obey system limits.
55  *
56  * return: DDI_SUCCESS DDI_DMA_NORESOURCES
57  */
58 ddi_dma_impl_t *
59 px_dma_allocmp(dev_info_t *dip, dev_info_t *rdip, int (*waitfp)(caddr_t),
60 	caddr_t arg)
61 {
62 	register ddi_dma_impl_t *mp;
63 	int sleep = (waitfp == DDI_DMA_SLEEP) ? KM_SLEEP : KM_NOSLEEP;
64 
65 	/* Caution: we don't use zalloc to enhance performance! */
66 	if ((mp = kmem_alloc(sizeof (px_dma_hdl_t), sleep)) == 0) {
67 		DBG(DBG_DMA_MAP, dip, "can't alloc dma_handle\n");
68 		if (waitfp != DDI_DMA_DONTWAIT) {
69 			DBG(DBG_DMA_MAP, dip, "alloc_mp kmem cb\n");
70 			ddi_set_callback(waitfp, arg, &px_kmem_clid);
71 		}
72 		return (mp);
73 	}
74 
75 	mp->dmai_rdip = rdip;
76 	mp->dmai_flags = 0;
77 	mp->dmai_pfnlst = NULL;
78 	mp->dmai_winlst = NULL;
79 
80 	/*
81 	 * kmem_alloc debug: the following fields are not zero-ed
82 	 * mp->dmai_mapping = 0;
83 	 * mp->dmai_size = 0;
84 	 * mp->dmai_offset = 0;
85 	 * mp->dmai_minxfer = 0;
86 	 * mp->dmai_burstsizes = 0;
87 	 * mp->dmai_ndvmapages = 0;
88 	 * mp->dmai_pool/roffset = 0;
89 	 * mp->dmai_rflags = 0;
90 	 * mp->dmai_inuse/flags
91 	 * mp->dmai_nwin = 0;
92 	 * mp->dmai_winsize = 0;
93 	 * mp->dmai_nexus_private/tte = 0;
94 	 * mp->dmai_iopte/pfnlst
95 	 * mp->dmai_sbi/pfn0 = 0;
96 	 * mp->dmai_minfo/winlst/fdvma
97 	 * mp->dmai_rdip
98 	 * bzero(&mp->dmai_object, sizeof (ddi_dma_obj_t));
99 	 * bzero(&mp->dmai_attr, sizeof (ddi_dma_attr_t));
100 	 * mp->dmai_cookie = 0;
101 	 */
102 
103 	mp->dmai_attr.dma_attr_version = (uint_t)DMA_ATTR_VERSION;
104 	mp->dmai_attr.dma_attr_flags = (uint_t)0;
105 	mp->dmai_fault = 0;
106 	mp->dmai_fault_check = NULL;
107 	mp->dmai_fault_notify = NULL;
108 	return (mp);
109 }
110 
111 void
112 px_dma_freemp(ddi_dma_impl_t *mp)
113 {
114 	if (mp->dmai_ndvmapages > 1)
115 		px_dma_freepfn(mp);
116 	if (mp->dmai_winlst)
117 		px_dma_freewin(mp);
118 	kmem_free(mp, sizeof (px_dma_hdl_t));
119 }
120 
121 void
122 px_dma_freepfn(ddi_dma_impl_t *mp)
123 {
124 	void *addr = mp->dmai_pfnlst;
125 	if (addr) {
126 		size_t npages = mp->dmai_ndvmapages;
127 		if (npages > 1)
128 			kmem_free(addr, npages * sizeof (px_iopfn_t));
129 		mp->dmai_pfnlst = NULL;
130 	}
131 	mp->dmai_ndvmapages = 0;
132 }
133 
134 /*
135  * px_dma_lmts2hdl - alloate a ddi_dma_impl_t, validate practical limits
136  *			and convert dmareq->dmar_limits to mp->dmai_attr
137  *
138  * ddi_dma_impl_t member modified     input
139  * ------------------------------------------------------------------------
140  * mp->dmai_minxfer		    - dev
141  * mp->dmai_burstsizes		    - dev
142  * mp->dmai_flags		    - no limit? peer-to-peer only?
143  *
144  * ddi_dma_attr member modified       input
145  * ------------------------------------------------------------------------
146  * mp->dmai_attr.dma_attr_addr_lo   - dev lo, sys lo
147  * mp->dmai_attr.dma_attr_addr_hi   - dev hi, sys hi
148  * mp->dmai_attr.dma_attr_count_max - dev count max, dev/sys lo/hi delta
149  * mp->dmai_attr.dma_attr_seg       - 0         (no nocross   restriction)
150  * mp->dmai_attr.dma_attr_align     - 1         (no alignment restriction)
151  *
152  * The dlim_dmaspeed member of dmareq->dmar_limits is ignored.
153  */
154 ddi_dma_impl_t *
155 px_dma_lmts2hdl(dev_info_t *dip, dev_info_t *rdip, px_mmu_t *mmu_p,
156 	ddi_dma_req_t *dmareq)
157 {
158 	ddi_dma_impl_t *mp;
159 	ddi_dma_attr_t *attr_p;
160 	uint64_t syslo		= mmu_p->mmu_dvma_base;
161 	uint64_t syshi		= mmu_p->mmu_dvma_end;
162 	uint64_t fasthi		= mmu_p->mmu_dvma_fast_end;
163 	ddi_dma_lim_t *lim_p	= dmareq->dmar_limits;
164 	uint32_t count_max	= lim_p->dlim_cntr_max;
165 	uint64_t lo		= lim_p->dlim_addr_lo;
166 	uint64_t hi		= lim_p->dlim_addr_hi;
167 	if (hi <= lo) {
168 		DBG(DBG_DMA_MAP, dip, "Bad limits\n");
169 		return ((ddi_dma_impl_t *)DDI_DMA_NOMAPPING);
170 	}
171 	if (!count_max)
172 		count_max--;
173 
174 	if (!(mp = px_dma_allocmp(dip, rdip, dmareq->dmar_fp,
175 		dmareq->dmar_arg)))
176 		return (NULL);
177 
178 	/* store original dev input at the 2nd ddi_dma_attr */
179 	attr_p = DEV_ATTR(mp);
180 	SET_DMAATTR(attr_p, lo, hi, -1, count_max);
181 	SET_DMAALIGN(attr_p, 1);
182 
183 	lo = MAX(lo, syslo);
184 	hi = MIN(hi, syshi);
185 	if (hi <= lo)
186 		mp->dmai_flags |= DMAI_FLAGS_PEER_ONLY;
187 	count_max = MIN(count_max, hi - lo);
188 
189 	if (DEV_NOSYSLIMIT(lo, hi, syslo, fasthi, 1))
190 		mp->dmai_flags |= DMAI_FLAGS_NOFASTLIMIT |
191 			DMAI_FLAGS_NOSYSLIMIT;
192 	else {
193 		if (DEV_NOFASTLIMIT(lo, hi, syslo, syshi, 1))
194 			mp->dmai_flags |= DMAI_FLAGS_NOFASTLIMIT;
195 	}
196 	if (PX_DMA_NOCTX(rdip))
197 		mp->dmai_flags |= DMAI_FLAGS_NOCTX;
198 
199 	/* store augumented dev input to mp->dmai_attr */
200 	mp->dmai_minxfer	= lim_p->dlim_minxfer;
201 	mp->dmai_burstsizes	= lim_p->dlim_burstsizes;
202 	attr_p = &mp->dmai_attr;
203 	SET_DMAATTR(attr_p, lo, hi, -1, count_max);
204 	SET_DMAALIGN(attr_p, 1);
205 	return (mp);
206 }
207 
208 /*
209  * Called from px_attach to check for bypass dma support and set
210  * flags accordingly.
211  */
212 int
213 px_dma_attach(px_t *px_p)
214 {
215 	uint64_t baddr;
216 
217 	if (px_lib_iommu_getbypass(px_p->px_dip, 0ull,
218 			PCI_MAP_ATTR_WRITE|PCI_MAP_ATTR_READ,
219 			&baddr) != DDI_ENOTSUP)
220 		/* ignore all other errors */
221 		px_p->px_soft_state |= PX_BYPASS_DMA_ALLOWED;
222 
223 	return (DDI_SUCCESS);
224 }
225 
226 /*
227  * px_dma_attr2hdl
228  *
229  * This routine is called from the alloc handle entry point to sanity check the
230  * dma attribute structure.
231  *
232  * use by: px_dma_allochdl()
233  *
234  * return value:
235  *
236  *	DDI_SUCCESS		- on success
237  *	DDI_DMA_BADATTR		- attribute has invalid version number
238  *				  or address limits exclude dvma space
239  */
240 int
241 px_dma_attr2hdl(px_t *px_p, ddi_dma_impl_t *mp)
242 {
243 	px_mmu_t *mmu_p = px_p->px_mmu_p;
244 	uint64_t syslo, syshi;
245 	int	ret;
246 	ddi_dma_attr_t *attrp		= DEV_ATTR(mp);
247 	uint64_t hi			= attrp->dma_attr_addr_hi;
248 	uint64_t lo			= attrp->dma_attr_addr_lo;
249 	uint64_t align			= attrp->dma_attr_align;
250 	uint64_t nocross		= attrp->dma_attr_seg;
251 	uint64_t count_max		= attrp->dma_attr_count_max;
252 
253 	DBG(DBG_DMA_ALLOCH, px_p->px_dip, "attrp=%p cntr_max=%x.%08x\n",
254 		attrp, HI32(count_max), LO32(count_max));
255 	DBG(DBG_DMA_ALLOCH, px_p->px_dip, "hi=%x.%08x lo=%x.%08x\n",
256 		HI32(hi), LO32(hi), HI32(lo), LO32(lo));
257 	DBG(DBG_DMA_ALLOCH, px_p->px_dip, "seg=%x.%08x align=%x.%08x\n",
258 		HI32(nocross), LO32(nocross), HI32(align), LO32(align));
259 
260 	if (!nocross)
261 		nocross--;
262 	if (attrp->dma_attr_flags & DDI_DMA_FORCE_PHYSICAL) { /* BYPASS */
263 
264 		DBG(DBG_DMA_ALLOCH, px_p->px_dip, "bypass mode\n");
265 		/*
266 		 * If Bypass DMA is not supported, return error so that
267 		 * target driver can fall back to dvma mode of operation
268 		 */
269 		if (!(px_p->px_soft_state & PX_BYPASS_DMA_ALLOWED))
270 			return (DDI_DMA_BADATTR);
271 		mp->dmai_flags |= DMAI_FLAGS_BYPASSREQ;
272 		if (nocross != UINT64_MAX)
273 			return (DDI_DMA_BADATTR);
274 		if (align && (align > MMU_PAGE_SIZE))
275 			return (DDI_DMA_BADATTR);
276 		align = 1; /* align on 1 page boundary */
277 
278 		/* do a range check and get the limits */
279 		ret = px_lib_dma_bypass_rngchk(attrp, &syslo, &syshi);
280 		if (ret != DDI_SUCCESS)
281 			return (ret);
282 	} else { /* MMU_XLATE or PEER_TO_PEER */
283 		align = MAX(align, MMU_PAGE_SIZE) - 1;
284 		if ((align & nocross) != align) {
285 			dev_info_t *rdip = mp->dmai_rdip;
286 			cmn_err(CE_WARN, "%s%d dma_attr_seg not aligned",
287 				NAMEINST(rdip));
288 			return (DDI_DMA_BADATTR);
289 		}
290 		align = MMU_BTOP(align + 1);
291 		syslo = mmu_p->mmu_dvma_base;
292 		syshi = mmu_p->mmu_dvma_end;
293 	}
294 	if (hi <= lo) {
295 		dev_info_t *rdip = mp->dmai_rdip;
296 		cmn_err(CE_WARN, "%s%d limits out of range", NAMEINST(rdip));
297 		return (DDI_DMA_BADATTR);
298 	}
299 	lo = MAX(lo, syslo);
300 	hi = MIN(hi, syshi);
301 	if (!count_max)
302 		count_max--;
303 
304 	DBG(DBG_DMA_ALLOCH, px_p->px_dip, "hi=%x.%08x, lo=%x.%08x\n",
305 		HI32(hi), LO32(hi), HI32(lo), LO32(lo));
306 	if (hi <= lo) { /* peer transfers cannot have alignment & nocross */
307 		dev_info_t *rdip = mp->dmai_rdip;
308 		cmn_err(CE_WARN, "%s%d peer only dev %p", NAMEINST(rdip), mp);
309 		if ((nocross < UINT32_MAX) || (align > 1)) {
310 			cmn_err(CE_WARN, "%s%d peer only device bad attr",
311 				NAMEINST(rdip));
312 			return (DDI_DMA_BADATTR);
313 		}
314 		mp->dmai_flags |= DMAI_FLAGS_PEER_ONLY;
315 	} else /* set practical counter_max value */
316 		count_max = MIN(count_max, hi - lo);
317 
318 	if (DEV_NOSYSLIMIT(lo, hi, syslo, syshi, align))
319 		mp->dmai_flags |= DMAI_FLAGS_NOSYSLIMIT |
320 			DMAI_FLAGS_NOFASTLIMIT;
321 	else {
322 		syshi = mmu_p->mmu_dvma_fast_end;
323 		if (DEV_NOFASTLIMIT(lo, hi, syslo, syshi, align))
324 			mp->dmai_flags |= DMAI_FLAGS_NOFASTLIMIT;
325 	}
326 	if (PX_DMA_NOCTX(mp->dmai_rdip))
327 		mp->dmai_flags |= DMAI_FLAGS_NOCTX;
328 
329 	mp->dmai_minxfer	= attrp->dma_attr_minxfer;
330 	mp->dmai_burstsizes	= attrp->dma_attr_burstsizes;
331 	attrp = &mp->dmai_attr;
332 	SET_DMAATTR(attrp, lo, hi, nocross, count_max);
333 	return (DDI_SUCCESS);
334 }
335 
336 #define	TGT_PFN_INBETWEEN(pfn, bgn, end) ((pfn >= bgn) && (pfn <= end))
337 
338 /*
339  * px_dma_type - determine which of the three types DMA (peer-to-peer,
340  *		mmu bypass, or mmu translate) we are asked to do.
341  *		Also checks pfn0 and rejects any non-peer-to-peer
342  *		requests for peer-only devices.
343  *
344  *	return values:
345  *		DDI_DMA_NOMAPPING - can't get valid pfn0, or bad dma type
346  *		DDI_SUCCESS
347  *
348  *	dma handle members affected (set on exit):
349  *	mp->dmai_object		- dmareq->dmar_object
350  *	mp->dmai_rflags		- consistent?, nosync?, dmareq->dmar_flags
351  *	mp->dmai_flags   	- DMA type
352  *	mp->dmai_pfn0   	- 1st page pfn (if va/size pair and not shadow)
353  *	mp->dmai_roffset 	- initialized to starting MMU page offset
354  *	mp->dmai_ndvmapages	- # of total MMU pages of entire object
355  */
356 int
357 px_dma_type(px_t *px_p, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp)
358 {
359 	dev_info_t *dip = px_p->px_dip;
360 	ddi_dma_obj_t *dobj_p = &dmareq->dmar_object;
361 	px_pec_t *pec_p = px_p->px_pec_p;
362 	uint32_t offset;
363 	pfn_t pfn0;
364 
365 	mp->dmai_rflags = dmareq->dmar_flags & DMP_DDIFLAGS | DMP_NOSYNC;
366 
367 	switch (dobj_p->dmao_type) {
368 	case DMA_OTYP_BUFVADDR:
369 	case DMA_OTYP_VADDR: {
370 		page_t **pplist = dobj_p->dmao_obj.virt_obj.v_priv;
371 		caddr_t vaddr = dobj_p->dmao_obj.virt_obj.v_addr;
372 
373 		DBG(DBG_DMA_MAP, dip, "vaddr=%p pplist=%p\n", vaddr, pplist);
374 		offset = (ulong_t)vaddr & MMU_PAGE_OFFSET;
375 		if (pplist) {				/* shadow list */
376 			mp->dmai_flags |= DMAI_FLAGS_PGPFN;
377 			pfn0 = page_pptonum(*pplist);
378 		} else {
379 			struct as *as_p = dobj_p->dmao_obj.virt_obj.v_as;
380 			struct hat *hat_p = as_p ? as_p->a_hat : kas.a_hat;
381 			pfn0 = hat_getpfnum(hat_p, vaddr);
382 		}
383 		}
384 		break;
385 
386 	case DMA_OTYP_PAGES:
387 		offset = dobj_p->dmao_obj.pp_obj.pp_offset;
388 		mp->dmai_flags |= DMAI_FLAGS_PGPFN;
389 		pfn0 = page_pptonum(dobj_p->dmao_obj.pp_obj.pp_pp);
390 		break;
391 
392 	case DMA_OTYP_PADDR:
393 	default:
394 		cmn_err(CE_WARN, "%s%d requested unsupported dma type %x",
395 			NAMEINST(mp->dmai_rdip), dobj_p->dmao_type);
396 		return (DDI_DMA_NOMAPPING);
397 	}
398 	if (pfn0 == PFN_INVALID) {
399 		cmn_err(CE_WARN, "%s%d: invalid pfn0 for DMA object %p",
400 			NAMEINST(dip), dobj_p);
401 		return (DDI_DMA_NOMAPPING);
402 	}
403 	if (TGT_PFN_INBETWEEN(pfn0, pec_p->pec_base32_pfn,
404 			pec_p->pec_last32_pfn)) {
405 		mp->dmai_flags |= DMAI_FLAGS_PTP|DMAI_FLAGS_PTP32;
406 		goto done;	/* leave bypass and dvma flag as 0 */
407 	} else if (TGT_PFN_INBETWEEN(pfn0, pec_p->pec_base64_pfn,
408 			pec_p->pec_last64_pfn)) {
409 		mp->dmai_flags |= DMAI_FLAGS_PTP|DMAI_FLAGS_PTP64;
410 		goto done;	/* leave bypass and dvma flag as 0 */
411 	}
412 	if (PX_DMA_ISPEERONLY(mp)) {
413 		dev_info_t *rdip = mp->dmai_rdip;
414 		cmn_err(CE_WARN, "Bad peer-to-peer req %s%d", NAMEINST(rdip));
415 		return (DDI_DMA_NOMAPPING);
416 	}
417 	mp->dmai_flags |= (mp->dmai_flags & DMAI_FLAGS_BYPASSREQ) ?
418 		DMAI_FLAGS_BYPASS : DMAI_FLAGS_DVMA;
419 done:
420 	mp->dmai_object	 = *dobj_p;			/* whole object    */
421 	mp->dmai_pfn0	 = (void *)pfn0;		/* cache pfn0	   */
422 	mp->dmai_roffset = offset;			/* win0 pg0 offset */
423 	mp->dmai_ndvmapages = MMU_BTOPR(offset + mp->dmai_object.dmao_size);
424 	return (DDI_SUCCESS);
425 }
426 
427 /*
428  * px_dma_pgpfn - set up pfnlst array according to pages
429  *	VA/size pair: <shadow IO, bypass, peer-to-peer>, or OTYP_PAGES
430  */
431 /*ARGSUSED*/
432 static int
433 px_dma_pgpfn(px_t *px_p, ddi_dma_impl_t *mp, uint_t npages)
434 {
435 	int i;
436 	dev_info_t *dip = px_p->px_dip;
437 
438 	switch (mp->dmai_object.dmao_type) {
439 	case DMA_OTYP_BUFVADDR:
440 	case DMA_OTYP_VADDR: {
441 		page_t **pplist = mp->dmai_object.dmao_obj.virt_obj.v_priv;
442 		DBG(DBG_DMA_MAP, dip, "shadow pplist=%p, %x pages, pfns=",
443 			pplist, npages);
444 		for (i = 1; i < npages; i++) {
445 			px_iopfn_t pfn = page_pptonum(pplist[i]);
446 			PX_SET_MP_PFN1(mp, i, pfn);
447 			DBG(DBG_DMA_MAP|DBG_CONT, dip, "%x ", pfn);
448 		}
449 		DBG(DBG_DMA_MAP|DBG_CONT, dip, "\n");
450 		}
451 		break;
452 
453 	case DMA_OTYP_PAGES: {
454 		page_t *pp = mp->dmai_object.dmao_obj.pp_obj.pp_pp->p_next;
455 		DBG(DBG_DMA_MAP, dip, "pp=%p pfns=", pp);
456 		for (i = 1; i < npages; i++, pp = pp->p_next) {
457 			px_iopfn_t pfn = page_pptonum(pp);
458 			PX_SET_MP_PFN1(mp, i, pfn);
459 			DBG(DBG_DMA_MAP|DBG_CONT, dip, "%x ", pfn);
460 		}
461 		DBG(DBG_DMA_MAP|DBG_CONT, dip, "\n");
462 		}
463 		break;
464 
465 	default:	/* check is already done by px_dma_type */
466 		ASSERT(0);
467 		break;
468 	}
469 	return (DDI_SUCCESS);
470 }
471 
472 /*
473  * px_dma_vapfn - set up pfnlst array according to VA
474  *	VA/size pair: <normal, bypass, peer-to-peer>
475  *	pfn0 is skipped as it is already done.
476  *	In this case, the cached pfn0 is used to fill pfnlst[0]
477  */
478 static int
479 px_dma_vapfn(px_t *px_p, ddi_dma_impl_t *mp, uint_t npages)
480 {
481 	dev_info_t *dip = px_p->px_dip;
482 	int i;
483 	caddr_t vaddr = (caddr_t)mp->dmai_object.dmao_obj.virt_obj.v_as;
484 	struct hat *hat_p = vaddr ? ((struct as *)vaddr)->a_hat : kas.a_hat;
485 
486 	vaddr = mp->dmai_object.dmao_obj.virt_obj.v_addr + MMU_PAGE_SIZE;
487 	for (i = 1; i < npages; i++, vaddr += MMU_PAGE_SIZE) {
488 		px_iopfn_t pfn = hat_getpfnum(hat_p, vaddr);
489 		if (pfn == PFN_INVALID)
490 			goto err_badpfn;
491 		PX_SET_MP_PFN1(mp, i, pfn);
492 		DBG(DBG_DMA_BINDH, dip, "px_dma_vapfn: mp=%p pfnlst[%x]=%x\n",
493 			mp, i, pfn);
494 	}
495 	return (DDI_SUCCESS);
496 err_badpfn:
497 	cmn_err(CE_WARN, "%s%d: bad page frame vaddr=%p", NAMEINST(dip), vaddr);
498 	return (DDI_DMA_NOMAPPING);
499 }
500 
501 /*
502  * px_dma_pfn - Fills pfn list for all pages being DMA-ed.
503  *
504  * dependencies:
505  *	mp->dmai_ndvmapages	- set to total # of dma pages
506  *
507  * return value:
508  *	DDI_SUCCESS
509  *	DDI_DMA_NOMAPPING
510  */
511 int
512 px_dma_pfn(px_t *px_p, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp)
513 {
514 	uint32_t npages = mp->dmai_ndvmapages;
515 	int (*waitfp)(caddr_t) = dmareq->dmar_fp;
516 	int i, ret, peer = PX_DMA_ISPTP(mp);
517 	int peer32 = PX_DMA_ISPTP32(mp);
518 	dev_info_t *dip = px_p->px_dip;
519 
520 	px_pec_t *pec_p = px_p->px_pec_p;
521 	px_iopfn_t pfn_base = peer32 ? pec_p->pec_base32_pfn :
522 					pec_p->pec_base64_pfn;
523 	px_iopfn_t pfn_last = peer32 ? pec_p->pec_last32_pfn :
524 					pec_p->pec_last64_pfn;
525 	px_iopfn_t pfn_adj = peer ? pfn_base : 0;
526 
527 	DBG(DBG_DMA_BINDH, dip, "px_dma_pfn: mp=%p pfn0=%x\n",
528 		mp, MP_PFN0(mp) - pfn_adj);
529 	/* 1 page: no array alloc/fill, no mixed mode check */
530 	if (npages == 1) {
531 		PX_SET_MP_PFN(mp, 0, MP_PFN0(mp) - pfn_adj);
532 		return (DDI_SUCCESS);
533 	}
534 	/* allocate pfn array */
535 	if (!(mp->dmai_pfnlst = kmem_alloc(npages * sizeof (px_iopfn_t),
536 		waitfp == DDI_DMA_SLEEP ? KM_SLEEP : KM_NOSLEEP))) {
537 		if (waitfp != DDI_DMA_DONTWAIT)
538 			ddi_set_callback(waitfp, dmareq->dmar_arg,
539 				&px_kmem_clid);
540 		return (DDI_DMA_NORESOURCES);
541 	}
542 	/* fill pfn array */
543 	PX_SET_MP_PFN(mp, 0, MP_PFN0(mp) - pfn_adj);	/* pfnlst[0] */
544 	if ((ret = PX_DMA_ISPGPFN(mp) ? px_dma_pgpfn(px_p, mp, npages) :
545 		px_dma_vapfn(px_p, mp, npages)) != DDI_SUCCESS)
546 		goto err;
547 
548 	/* skip pfn0, check mixed mode and adjust peer to peer pfn */
549 	for (i = 1; i < npages; i++) {
550 		px_iopfn_t pfn = PX_GET_MP_PFN1(mp, i);
551 		if (peer ^ TGT_PFN_INBETWEEN(pfn, pfn_base, pfn_last)) {
552 			cmn_err(CE_WARN, "%s%d mixed mode DMA %x %x",
553 				NAMEINST(mp->dmai_rdip), MP_PFN0(mp), pfn);
554 			ret = DDI_DMA_NOMAPPING;	/* mixed mode */
555 			goto err;
556 		}
557 		DBG(DBG_DMA_MAP, dip,
558 			"px_dma_pfn: pfnlst[%x]=%x-%x\n", i, pfn, pfn_adj);
559 		if (pfn_adj)
560 			PX_SET_MP_PFN1(mp, i, pfn - pfn_adj);
561 	}
562 	return (DDI_SUCCESS);
563 err:
564 	px_dma_freepfn(mp);
565 	return (ret);
566 }
567 
568 /*
569  * px_dvma_win() - trim requested DVMA size down to window size
570  *	The 1st window starts from offset and ends at page-aligned boundary.
571  *	From the 2nd window on, each window starts and ends at page-aligned
572  *	boundary except the last window ends at wherever requested.
573  *
574  *	accesses the following mp-> members:
575  *	mp->dmai_attr.dma_attr_count_max
576  *	mp->dmai_attr.dma_attr_seg
577  *	mp->dmai_roffset   - start offset of 1st window
578  *	mp->dmai_rflags (redzone)
579  *	mp->dmai_ndvmapages (for 1 page fast path)
580  *
581  *	sets the following mp-> members:
582  *	mp->dmai_size	   - xfer size, != winsize if 1st/last win  (not fixed)
583  *	mp->dmai_winsize   - window size (no redzone), n * page size    (fixed)
584  *	mp->dmai_nwin	   - # of DMA windows of entire object		(fixed)
585  *	mp->dmai_rflags	   - remove partial flag if nwin == 1		(fixed)
586  *	mp->dmai_winlst	   - NULL, window objects not used for DVMA	(fixed)
587  *
588  *	fixed - not changed across different DMA windows
589  */
590 /*ARGSUSED*/
591 int
592 px_dvma_win(px_t *px_p, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp)
593 {
594 	uint32_t redzone_sz	= HAS_REDZONE(mp) ? MMU_PAGE_SIZE : 0;
595 	size_t obj_sz		= mp->dmai_object.dmao_size;
596 	size_t xfer_sz;
597 	ulong_t pg_off;
598 
599 	if ((mp->dmai_ndvmapages == 1) && !redzone_sz) {
600 		mp->dmai_rflags &= ~DDI_DMA_PARTIAL;
601 		mp->dmai_size = obj_sz;
602 		mp->dmai_winsize = MMU_PAGE_SIZE;
603 		mp->dmai_nwin = 1;
604 		goto done;
605 	}
606 
607 	pg_off	= mp->dmai_roffset;
608 	xfer_sz	= obj_sz + redzone_sz;
609 
610 	/* include redzone in nocross check */ {
611 		uint64_t nocross = mp->dmai_attr.dma_attr_seg;
612 		if (xfer_sz + pg_off - 1 > nocross)
613 			xfer_sz = nocross - pg_off + 1;
614 		if (redzone_sz && (xfer_sz <= redzone_sz)) {
615 			DBG(DBG_DMA_MAP, px_p->px_dip,
616 			    "nocross too small: "
617 			    "%lx(%lx)+%lx+%lx < %llx\n",
618 			    xfer_sz, obj_sz, pg_off, redzone_sz, nocross);
619 			return (DDI_DMA_TOOBIG);
620 		}
621 	}
622 	xfer_sz -= redzone_sz;		/* restore transfer size  */
623 	/* check counter max */ {
624 		uint32_t count_max = mp->dmai_attr.dma_attr_count_max;
625 		if (xfer_sz - 1 > count_max)
626 			xfer_sz = count_max + 1;
627 	}
628 	if (xfer_sz >= obj_sz) {
629 		mp->dmai_rflags &= ~DDI_DMA_PARTIAL;
630 		mp->dmai_size = xfer_sz;
631 		mp->dmai_winsize = P2ROUNDUP(xfer_sz + pg_off, MMU_PAGE_SIZE);
632 		mp->dmai_nwin = 1;
633 		goto done;
634 	}
635 	if (!(dmareq->dmar_flags & DDI_DMA_PARTIAL)) {
636 		DBG(DBG_DMA_MAP, px_p->px_dip, "too big: %lx+%lx+%lx > %lx\n",
637 			obj_sz, pg_off, redzone_sz, xfer_sz);
638 		return (DDI_DMA_TOOBIG);
639 	}
640 
641 	xfer_sz = MMU_PTOB(MMU_BTOP(xfer_sz + pg_off)); /* page align */
642 	mp->dmai_size = xfer_sz - pg_off;	/* 1st window xferrable size */
643 	mp->dmai_winsize = xfer_sz;		/* redzone not in winsize */
644 	mp->dmai_nwin = (obj_sz + pg_off + xfer_sz - 1) / xfer_sz;
645 done:
646 	mp->dmai_winlst = NULL;
647 	px_dump_dma_handle(DBG_DMA_MAP, px_p->px_dip, mp);
648 	return (DDI_SUCCESS);
649 }
650 
651 /*
652  * fast track cache entry to mmu context, inserts 3 0 bits between
653  * upper 6-bits and lower 3-bits of the 9-bit cache entry
654  */
655 #define	MMU_FCE_TO_CTX(i)	(((i) << 3) | ((i) & 0x7) | 0x38)
656 
657 /*
658  * px_dvma_map_fast - attempts to map fast trackable DVMA
659  */
660 /*ARGSUSED*/
661 int
662 px_dvma_map_fast(px_mmu_t *mmu_p, ddi_dma_impl_t *mp)
663 {
664 	uint_t clustsz = px_dvma_page_cache_clustsz;
665 	uint_t entries = px_dvma_page_cache_entries;
666 	io_attributes_t attr = PX_GET_TTE_ATTR(mp->dmai_rflags);
667 	int i = mmu_p->mmu_dvma_addr_scan_start;
668 	uint8_t *lock_addr = mmu_p->mmu_dvma_cache_locks + i;
669 	px_dvma_addr_t dvma_pg;
670 	size_t npages = MMU_BTOP(mp->dmai_winsize);
671 	dev_info_t *px_dip = mmu_p->mmu_px_p->px_dip;
672 
673 	extern uint8_t ldstub(uint8_t *);
674 	ASSERT(MMU_PTOB(npages) == mp->dmai_winsize);
675 	ASSERT(npages + HAS_REDZONE(mp) <= clustsz);
676 
677 	for (; i < entries && ldstub(lock_addr); i++, lock_addr++);
678 	if (i >= entries) {
679 		lock_addr = mmu_p->mmu_dvma_cache_locks;
680 		i = 0;
681 		for (; i < entries && ldstub(lock_addr); i++, lock_addr++);
682 		if (i >= entries) {
683 #ifdef	PX_DMA_PROF
684 			px_dvmaft_exhaust++;
685 #endif	/* PX_DMA_PROF */
686 			return (DDI_DMA_NORESOURCES);
687 		}
688 	}
689 	mmu_p->mmu_dvma_addr_scan_start = (i + 1) & (entries - 1);
690 
691 	i *= clustsz;
692 	dvma_pg = mmu_p->dvma_base_pg + i;
693 
694 	if (px_lib_iommu_map(px_dip, PCI_TSBID(0, i), npages, attr,
695 	    (void *)mp, 0, MMU_MAP_MP) != DDI_SUCCESS)
696 		return (DDI_FAILURE);
697 
698 #ifdef PX_DMA_PROF
699 	px_dvmaft_success++;
700 #endif
701 	mp->dmai_mapping = mp->dmai_roffset | MMU_PTOB(dvma_pg);
702 	mp->dmai_offset = 0;
703 	mp->dmai_flags |= DMAI_FLAGS_FASTTRACK;
704 	PX_SAVE_MP_TTE(mp, attr);	/* save TTE template for unmapping */
705 	if (DVMA_DBG_ON(mmu_p))
706 		px_dvma_alloc_debug(mmu_p, (char *)mp->dmai_mapping,
707 			mp->dmai_size, mp);
708 	return (DDI_SUCCESS);
709 }
710 
711 /*
712  * px_dvma_map: map non-fasttrack DMA
713  *		Use quantum cache if single page DMA.
714  */
715 int
716 px_dvma_map(ddi_dma_impl_t *mp, ddi_dma_req_t *dmareq, px_mmu_t *mmu_p)
717 {
718 	uint_t npages = PX_DMA_WINNPGS(mp);
719 	px_dvma_addr_t dvma_pg, dvma_pg_index;
720 	void *dvma_addr;
721 	uint64_t tte = PX_GET_TTE_ATTR(mp->dmai_rflags);
722 	int sleep = dmareq->dmar_fp == DDI_DMA_SLEEP ? VM_SLEEP : VM_NOSLEEP;
723 	dev_info_t *dip = mp->dmai_rdip;
724 	int	ret = DDI_SUCCESS;
725 
726 	/*
727 	 * allocate dvma space resource and map in the first window.
728 	 * (vmem_t *vmp, size_t size,
729 	 *	size_t align, size_t phase, size_t nocross,
730 	 *	void *minaddr, void *maxaddr, int vmflag)
731 	 */
732 	if ((npages == 1) && HAS_NOSYSLIMIT(mp)) {
733 		dvma_addr = vmem_alloc(mmu_p->mmu_dvma_map,
734 			MMU_PAGE_SIZE, sleep);
735 		mp->dmai_flags |= DMAI_FLAGS_VMEMCACHE;
736 #ifdef	PX_DMA_PROF
737 		px_dvma_vmem_alloc++;
738 #endif	/* PX_DMA_PROF */
739 	} else {
740 		dvma_addr = vmem_xalloc(mmu_p->mmu_dvma_map,
741 			MMU_PTOB(npages + HAS_REDZONE(mp)),
742 			MAX(mp->dmai_attr.dma_attr_align, MMU_PAGE_SIZE),
743 			0,
744 			mp->dmai_attr.dma_attr_seg + 1,
745 			(void *)mp->dmai_attr.dma_attr_addr_lo,
746 			(void *)(mp->dmai_attr.dma_attr_addr_hi + 1),
747 			sleep);
748 #ifdef	PX_DMA_PROF
749 		px_dvma_vmem_xalloc++;
750 #endif	/* PX_DMA_PROF */
751 	}
752 	dvma_pg = MMU_BTOP((ulong_t)dvma_addr);
753 	dvma_pg_index = dvma_pg - mmu_p->dvma_base_pg;
754 	DBG(DBG_DMA_MAP, dip, "fallback dvma_pages: dvma_pg=%x index=%x\n",
755 		dvma_pg, dvma_pg_index);
756 	if (dvma_pg == 0)
757 		goto noresource;
758 
759 	mp->dmai_mapping = mp->dmai_roffset | MMU_PTOB(dvma_pg);
760 	mp->dmai_offset = 0;
761 	PX_SAVE_MP_TTE(mp, tte);	/* mp->dmai_tte = tte */
762 
763 	if ((ret = px_mmu_map_pages(mmu_p,
764 	    mp, dvma_pg, npages, 0)) != DDI_SUCCESS) {
765 		if (mp->dmai_flags & DMAI_FLAGS_VMEMCACHE) {
766 			vmem_free(mmu_p->mmu_dvma_map, (void *)dvma_addr,
767 			    MMU_PAGE_SIZE);
768 #ifdef PX_DMA_PROF
769 			px_dvma_vmem_free++;
770 #endif /* PX_DMA_PROF */
771 		} else {
772 			vmem_xfree(mmu_p->mmu_dvma_map, (void *)dvma_addr,
773 			    MMU_PTOB(npages + HAS_REDZONE(mp)));
774 #ifdef PX_DMA_PROF
775 			px_dvma_vmem_xfree++;
776 #endif /* PX_DMA_PROF */
777 		}
778 	}
779 
780 	return (ret);
781 noresource:
782 	if (dmareq->dmar_fp != DDI_DMA_DONTWAIT) {
783 		DBG(DBG_DMA_MAP, dip, "dvma_pg 0 - set callback\n");
784 		ddi_set_callback(dmareq->dmar_fp, dmareq->dmar_arg,
785 			&mmu_p->mmu_dvma_clid);
786 	}
787 	DBG(DBG_DMA_MAP, dip, "vmem_xalloc - DDI_DMA_NORESOURCES\n");
788 	return (DDI_DMA_NORESOURCES);
789 }
790 
791 void
792 px_dvma_unmap(px_mmu_t *mmu_p, ddi_dma_impl_t *mp)
793 {
794 	px_dvma_addr_t dvma_addr = (px_dvma_addr_t)mp->dmai_mapping;
795 	px_dvma_addr_t dvma_pg = MMU_BTOP(dvma_addr);
796 	dvma_addr = MMU_PTOB(dvma_pg);
797 
798 	if (mp->dmai_flags & DMAI_FLAGS_FASTTRACK) {
799 		px_iopfn_t index = dvma_pg - mmu_p->dvma_base_pg;
800 		ASSERT(index % px_dvma_page_cache_clustsz == 0);
801 		index /= px_dvma_page_cache_clustsz;
802 		ASSERT(index < px_dvma_page_cache_entries);
803 		mmu_p->mmu_dvma_cache_locks[index] = 0;
804 #ifdef	PX_DMA_PROF
805 		px_dvmaft_free++;
806 #endif	/* PX_DMA_PROF */
807 		return;
808 	}
809 
810 	if (mp->dmai_flags & DMAI_FLAGS_VMEMCACHE) {
811 		vmem_free(mmu_p->mmu_dvma_map, (void *)dvma_addr,
812 			MMU_PAGE_SIZE);
813 #ifdef PX_DMA_PROF
814 		px_dvma_vmem_free++;
815 #endif /* PX_DMA_PROF */
816 	} else {
817 		size_t npages = MMU_BTOP(mp->dmai_winsize) + HAS_REDZONE(mp);
818 		vmem_xfree(mmu_p->mmu_dvma_map, (void *)dvma_addr,
819 			MMU_PTOB(npages));
820 #ifdef PX_DMA_PROF
821 		px_dvma_vmem_xfree++;
822 #endif /* PX_DMA_PROF */
823 	}
824 }
825 
826 /*
827  * DVMA mappings may have multiple windows, but each window always have
828  * one segment.
829  */
830 int
831 px_dvma_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_impl_t *mp,
832 	enum ddi_dma_ctlops cmd, off_t *offp, size_t *lenp, caddr_t *objp,
833 	uint_t cache_flags)
834 {
835 	switch (cmd) {
836 	case DDI_DMA_SYNC:
837 		return (px_lib_dma_sync(dip, rdip, (ddi_dma_handle_t)mp,
838 		    *offp, *lenp, cache_flags));
839 
840 	case DDI_DMA_HTOC: {
841 		int ret;
842 		off_t wo_off, off = *offp;	/* wo_off: wnd's obj offset */
843 		uint_t win_size = mp->dmai_winsize;
844 		ddi_dma_cookie_t *cp = (ddi_dma_cookie_t *)objp;
845 
846 		if (off >= mp->dmai_object.dmao_size) {
847 			cmn_err(CE_WARN, "%s%d invalid dma_htoc offset %lx",
848 				NAMEINST(mp->dmai_rdip), off);
849 			return (DDI_FAILURE);
850 		}
851 		off += mp->dmai_roffset;
852 		ret = px_dma_win(dip, rdip, (ddi_dma_handle_t)mp,
853 		    off / win_size, &wo_off, NULL, cp, NULL); /* lenp == NULL */
854 		if (ret)
855 			return (ret);
856 		DBG(DBG_DMA_CTL, dip, "HTOC:cookie=%x+%lx off=%lx,%lx\n",
857 			cp->dmac_address, cp->dmac_size, off, *offp);
858 
859 		/* adjust cookie addr/len if we are not on window boundary */
860 		ASSERT((off % win_size) == (off -
861 			(PX_DMA_CURWIN(mp) ? mp->dmai_roffset : 0) - wo_off));
862 		off = PX_DMA_CURWIN(mp) ? off % win_size : *offp;
863 		ASSERT(cp->dmac_size > off);
864 		cp->dmac_laddress += off;
865 		cp->dmac_size -= off;
866 		DBG(DBG_DMA_CTL, dip, "HTOC:mp=%p cookie=%x+%lx off=%lx,%lx\n",
867 			mp, cp->dmac_address, cp->dmac_size, off, wo_off);
868 		}
869 		return (DDI_SUCCESS);
870 
871 	case DDI_DMA_REPWIN:
872 		*offp = mp->dmai_offset;
873 		*lenp = mp->dmai_size;
874 		return (DDI_SUCCESS);
875 
876 	case DDI_DMA_MOVWIN: {
877 		off_t off = *offp;
878 		if (off >= mp->dmai_object.dmao_size)
879 			return (DDI_FAILURE);
880 		off += mp->dmai_roffset;
881 		return (px_dma_win(dip, rdip, (ddi_dma_handle_t)mp,
882 		    off / mp->dmai_winsize, offp, lenp,
883 		    (ddi_dma_cookie_t *)objp, NULL));
884 		}
885 
886 	case DDI_DMA_NEXTWIN: {
887 		px_window_t win = PX_DMA_CURWIN(mp);
888 		if (offp) {
889 			if (*(px_window_t *)offp != win) {
890 				/* window not active */
891 				*(px_window_t *)objp = win; /* return cur win */
892 				return (DDI_DMA_STALE);
893 			}
894 			win++;
895 		} else	/* map win 0 */
896 			win = 0;
897 		if (win >= mp->dmai_nwin) {
898 			*(px_window_t *)objp = win - 1;
899 			return (DDI_DMA_DONE);
900 		}
901 		if (px_dma_win(dip, rdip, (ddi_dma_handle_t)mp,
902 		    win, 0, 0, 0, 0)) {
903 			*(px_window_t *)objp = win - 1;
904 			return (DDI_FAILURE);
905 		}
906 		*(px_window_t *)objp = win;
907 		}
908 		return (DDI_SUCCESS);
909 
910 	case DDI_DMA_NEXTSEG:
911 		if (*(px_window_t *)offp != PX_DMA_CURWIN(mp))
912 			return (DDI_DMA_STALE);
913 		if (lenp)				/* only 1 seg allowed */
914 			return (DDI_DMA_DONE);
915 
916 		/* return mp as seg 0 */
917 		*(ddi_dma_seg_t *)objp = (ddi_dma_seg_t)mp;
918 		return (DDI_SUCCESS);
919 
920 	case DDI_DMA_SEGTOC:
921 		MAKE_DMA_COOKIE((ddi_dma_cookie_t *)objp, mp->dmai_mapping,
922 			mp->dmai_size);
923 		*offp = mp->dmai_offset;
924 		*lenp = mp->dmai_size;
925 		return (DDI_SUCCESS);
926 
927 	case DDI_DMA_COFF: {
928 		ddi_dma_cookie_t *cp = (ddi_dma_cookie_t *)offp;
929 		if (cp->dmac_address < mp->dmai_mapping ||
930 			(cp->dmac_address + cp->dmac_size) >
931 			(mp->dmai_mapping + mp->dmai_size))
932 			return (DDI_FAILURE);
933 		*objp = (caddr_t)(cp->dmac_address - mp->dmai_mapping +
934 			mp->dmai_offset);
935 		}
936 		return (DDI_SUCCESS);
937 	default:
938 		DBG(DBG_DMA_CTL, dip, "unknown command (%x): rdip=%s%d\n",
939 			cmd, ddi_driver_name(rdip), ddi_get_instance(rdip));
940 		break;
941 	}
942 	return (DDI_FAILURE);
943 }
944 
945 void
946 px_dma_freewin(ddi_dma_impl_t *mp)
947 {
948 	px_dma_win_t *win_p = mp->dmai_winlst, *win2_p;
949 	for (win2_p = win_p; win_p; win2_p = win_p) {
950 		win_p = win2_p->win_next;
951 		kmem_free(win2_p, sizeof (px_dma_win_t) +
952 			sizeof (ddi_dma_cookie_t) * win2_p->win_ncookies);
953 	}
954 	mp->dmai_nwin = 0;
955 	mp->dmai_winlst = NULL;
956 }
957 
958 /*
959  * px_dma_newwin - create a dma window object and cookies
960  *
961  *	After the initial scan in px_dma_physwin(), which identifies
962  *	a portion of the pfn array that belongs to a dma window,
963  *	we are called to allocate and initialize representing memory
964  *	resources. We know from the 1st scan the number of cookies
965  *	or dma segment in this window so we can allocate a contiguous
966  *	memory array for the dma cookies (The implementation of
967  *	ddi_dma_nextcookie(9f) dictates dma cookies be contiguous).
968  *
969  *	A second round scan is done on the pfn array to identify
970  *	each dma segment and initialize its corresponding dma cookie.
971  *	We don't need to do all the safety checking and we know they
972  *	all belong to the same dma window.
973  *
974  *	Input:	cookie_no - # of cookies identified by the 1st scan
975  *		start_idx - subscript of the pfn array for the starting pfn
976  *		end_idx   - subscript of the last pfn in dma window
977  *		win_pp    - pointer to win_next member of previous window
978  *	Return:	DDI_SUCCESS - with **win_pp as newly created window object
979  *		DDI_DMA_NORESROUCE - caller frees all previous window objs
980  *	Note:	Each cookie and window size are all initialized on page
981  *		boundary. This is not true for the 1st cookie of the 1st
982  *		window and the last cookie of the last window.
983  *		We fix that later in upper layer which has access to size
984  *		and offset info.
985  *
986  */
987 /*ARGSUSED*/
988 static int
989 px_dma_newwin(dev_info_t *dip, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp,
990 	uint32_t cookie_no, uint32_t start_idx, uint32_t end_idx,
991 	px_dma_win_t **win_pp, uint64_t count_max, uint64_t bypass)
992 {
993 	int (*waitfp)(caddr_t) = dmareq->dmar_fp;
994 	ddi_dma_cookie_t *cookie_p;
995 	uint32_t pfn_no = 1;
996 	px_iopfn_t pfn = PX_GET_MP_PFN(mp, start_idx);
997 	px_iopfn_t prev_pfn = pfn;
998 	uint64_t baddr, seg_pfn0 = pfn;
999 	size_t sz = cookie_no * sizeof (ddi_dma_cookie_t);
1000 	px_dma_win_t *win_p = kmem_zalloc(sizeof (px_dma_win_t) + sz,
1001 		waitfp == DDI_DMA_SLEEP ? KM_SLEEP : KM_NOSLEEP);
1002 	io_attributes_t	attr = PX_GET_TTE_ATTR(mp->dmai_rflags);
1003 
1004 	if (!win_p)
1005 		goto noresource;
1006 
1007 	win_p->win_next = NULL;
1008 	win_p->win_ncookies = cookie_no;
1009 	win_p->win_curseg = 0;	/* start from segment 0 */
1010 	win_p->win_size = MMU_PTOB(end_idx - start_idx + 1);
1011 	/* win_p->win_offset is left uninitialized */
1012 
1013 	cookie_p = (ddi_dma_cookie_t *)(win_p + 1);
1014 	start_idx++;
1015 	for (; start_idx <= end_idx; start_idx++, prev_pfn = pfn, pfn_no++) {
1016 		pfn = PX_GET_MP_PFN1(mp, start_idx);
1017 		if ((pfn == prev_pfn + 1) &&
1018 			(MMU_PTOB(pfn_no + 1) - 1 <= count_max))
1019 			continue;
1020 
1021 		/* close up the cookie up to (including) prev_pfn */
1022 		baddr = MMU_PTOB(seg_pfn0);
1023 		if (bypass && (px_lib_iommu_getbypass(dip,
1024 				baddr, attr, &baddr) != DDI_SUCCESS))
1025 			return (DDI_FAILURE);
1026 
1027 		MAKE_DMA_COOKIE(cookie_p, baddr, MMU_PTOB(pfn_no));
1028 		DBG(DBG_BYPASS, mp->dmai_rdip, "cookie %p (%x pages)\n",
1029 			MMU_PTOB(seg_pfn0), pfn_no);
1030 
1031 		cookie_p++;	/* advance to next available cookie cell */
1032 		pfn_no = 0;
1033 		seg_pfn0 = pfn;	/* start a new segment from current pfn */
1034 	}
1035 
1036 	baddr = MMU_PTOB(seg_pfn0);
1037 	if (bypass && (px_lib_iommu_getbypass(dip,
1038 			baddr, attr, &baddr) != DDI_SUCCESS))
1039 		return (DDI_FAILURE);
1040 
1041 	MAKE_DMA_COOKIE(cookie_p, baddr, MMU_PTOB(pfn_no));
1042 	DBG(DBG_BYPASS, mp->dmai_rdip, "cookie %p (%x pages) of total %x\n",
1043 		MMU_PTOB(seg_pfn0), pfn_no, cookie_no);
1044 #ifdef	DEBUG
1045 	cookie_p++;
1046 	ASSERT((cookie_p - (ddi_dma_cookie_t *)(win_p + 1)) == cookie_no);
1047 #endif	/* DEBUG */
1048 	*win_pp = win_p;
1049 	return (DDI_SUCCESS);
1050 noresource:
1051 	if (waitfp != DDI_DMA_DONTWAIT)
1052 		ddi_set_callback(waitfp, dmareq->dmar_arg, &px_kmem_clid);
1053 	return (DDI_DMA_NORESOURCES);
1054 }
1055 
1056 /*
1057  * px_dma_adjust - adjust 1st and last cookie and window sizes
1058  *	remove initial dma page offset from 1st cookie and window size
1059  *	remove last dma page remainder from last cookie and window size
1060  *	fill win_offset of each dma window according to just fixed up
1061  *		each window sizes
1062  *	px_dma_win_t members modified:
1063  *	win_p->win_offset - this window's offset within entire DMA object
1064  *	win_p->win_size	  - xferrable size (in bytes) for this window
1065  *
1066  *	ddi_dma_impl_t members modified:
1067  *	mp->dmai_size	  - 1st window xferrable size
1068  *	mp->dmai_offset   - 0, which is the dma offset of the 1st window
1069  *
1070  *	ddi_dma_cookie_t members modified:
1071  *	cookie_p->dmac_size - 1st and last cookie remove offset or remainder
1072  *	cookie_p->dmac_laddress - 1st cookie add page offset
1073  */
1074 static void
1075 px_dma_adjust(ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp, px_dma_win_t *win_p)
1076 {
1077 	ddi_dma_cookie_t *cookie_p = (ddi_dma_cookie_t *)(win_p + 1);
1078 	size_t pg_offset = mp->dmai_roffset;
1079 	size_t win_offset = 0;
1080 
1081 	cookie_p->dmac_size -= pg_offset;
1082 	cookie_p->dmac_laddress |= pg_offset;
1083 	win_p->win_size -= pg_offset;
1084 	DBG(DBG_BYPASS, mp->dmai_rdip, "pg0 adjust %lx\n", pg_offset);
1085 
1086 	mp->dmai_size = win_p->win_size;
1087 	mp->dmai_offset = 0;
1088 
1089 	pg_offset += mp->dmai_object.dmao_size;
1090 	pg_offset &= MMU_PAGE_OFFSET;
1091 	if (pg_offset)
1092 		pg_offset = MMU_PAGE_SIZE - pg_offset;
1093 	DBG(DBG_BYPASS, mp->dmai_rdip, "last pg adjust %lx\n", pg_offset);
1094 
1095 	for (; win_p->win_next; win_p = win_p->win_next) {
1096 		DBG(DBG_BYPASS, mp->dmai_rdip, "win off %p\n", win_offset);
1097 		win_p->win_offset = win_offset;
1098 		win_offset += win_p->win_size;
1099 	}
1100 	/* last window */
1101 	win_p->win_offset = win_offset;
1102 	cookie_p = (ddi_dma_cookie_t *)(win_p + 1);
1103 	cookie_p[win_p->win_ncookies - 1].dmac_size -= pg_offset;
1104 	win_p->win_size -= pg_offset;
1105 	ASSERT((win_offset + win_p->win_size) == mp->dmai_object.dmao_size);
1106 }
1107 
1108 /*
1109  * px_dma_physwin() - carve up dma windows using physical addresses.
1110  *	Called to handle mmu bypass and pci peer-to-peer transfers.
1111  *	Calls px_dma_newwin() to allocate window objects.
1112  *
1113  * Dependency: mp->dmai_pfnlst points to an array of pfns
1114  *
1115  * 1. Each dma window is represented by a px_dma_win_t object.
1116  *	The object will be casted to ddi_dma_win_t and returned
1117  *	to leaf driver through the DDI interface.
1118  * 2. Each dma window can have several dma segments with each
1119  *	segment representing a physically contiguous either memory
1120  *	space (if we are doing an mmu bypass transfer) or pci address
1121  *	space (if we are doing a peer-to-peer transfer).
1122  * 3. Each segment has a DMA cookie to program the DMA engine.
1123  *	The cookies within each DMA window must be located in a
1124  *	contiguous array per ddi_dma_nextcookie(9f).
1125  * 4. The number of DMA segments within each DMA window cannot exceed
1126  *	mp->dmai_attr.dma_attr_sgllen. If the transfer size is
1127  *	too large to fit in the sgllen, the rest needs to be
1128  *	relocated to the next dma window.
1129  * 5. Peer-to-peer DMA segment follows device hi, lo, count_max,
1130  *	and nocross restrictions while bypass DMA follows the set of
1131  *	restrictions with system limits factored in.
1132  *
1133  * Return:
1134  *	mp->dmai_winlst	 - points to a link list of px_dma_win_t objects.
1135  *		Each px_dma_win_t object on the link list contains
1136  *		infomation such as its window size (# of pages),
1137  *		starting offset (also see Restriction), an array of
1138  *		DMA cookies, and # of cookies in the array.
1139  *	mp->dmai_pfnlst	 - NULL, the pfn list is freed to conserve memory.
1140  *	mp->dmai_nwin	 - # of total DMA windows on mp->dmai_winlst.
1141  *	mp->dmai_mapping - starting cookie address
1142  *	mp->dmai_rflags	 - consistent, nosync, no redzone
1143  *	mp->dmai_cookie	 - start of cookie table of the 1st DMA window
1144  *
1145  * Restriction:
1146  *	Each px_dma_win_t object can theoratically start from any offset
1147  *	since the mmu is not involved. However, this implementation
1148  *	always make windows start from page aligned offset (except
1149  *	the 1st window, which follows the requested offset) due to the
1150  *	fact that we are handed a pfn list. This does require device's
1151  *	count_max and attr_seg to be at least MMU_PAGE_SIZE aligned.
1152  */
1153 int
1154 px_dma_physwin(px_t *px_p, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp)
1155 {
1156 	uint_t npages = mp->dmai_ndvmapages;
1157 	int ret, sgllen = mp->dmai_attr.dma_attr_sgllen;
1158 	px_iopfn_t pfn_lo, pfn_hi, prev_pfn;
1159 	px_iopfn_t pfn = PX_GET_MP_PFN(mp, 0);
1160 	uint32_t i, win_no = 0, pfn_no = 1, win_pfn0_index = 0, cookie_no = 0;
1161 	uint64_t count_max, bypass_addr = 0;
1162 	px_dma_win_t **win_pp = (px_dma_win_t **)&mp->dmai_winlst;
1163 	ddi_dma_cookie_t *cookie0_p;
1164 	io_attributes_t attr = PX_GET_TTE_ATTR(mp->dmai_rflags);
1165 	dev_info_t *dip = px_p->px_dip;
1166 
1167 	ASSERT(PX_DMA_ISPTP(mp) || PX_DMA_ISBYPASS(mp));
1168 	if (PX_DMA_ISPTP(mp)) { /* ignore sys limits for peer-to-peer */
1169 		ddi_dma_attr_t *dev_attr_p = DEV_ATTR(mp);
1170 		uint64_t nocross = dev_attr_p->dma_attr_seg;
1171 		px_pec_t *pec_p = px_p->px_pec_p;
1172 		px_iopfn_t pfn_last = PX_DMA_ISPTP32(mp) ?
1173 				pec_p->pec_last32_pfn - pec_p->pec_base32_pfn :
1174 				pec_p->pec_last64_pfn - pec_p->pec_base64_pfn;
1175 
1176 		if (nocross && (nocross < UINT32_MAX))
1177 			return (DDI_DMA_NOMAPPING);
1178 		if (dev_attr_p->dma_attr_align > MMU_PAGE_SIZE)
1179 			return (DDI_DMA_NOMAPPING);
1180 		pfn_lo = MMU_BTOP(dev_attr_p->dma_attr_addr_lo);
1181 		pfn_hi = MMU_BTOP(dev_attr_p->dma_attr_addr_hi);
1182 		pfn_hi = MIN(pfn_hi, pfn_last);
1183 		if ((pfn_lo > pfn_hi) || (pfn < pfn_lo))
1184 			return (DDI_DMA_NOMAPPING);
1185 
1186 		count_max = dev_attr_p->dma_attr_count_max;
1187 		count_max = MIN(count_max, nocross);
1188 		/*
1189 		 * the following count_max trim is not done because we are
1190 		 * making sure pfn_lo <= pfn <= pfn_hi inside the loop
1191 		 * count_max=MIN(count_max, MMU_PTOB(pfn_hi - pfn_lo + 1)-1);
1192 		 */
1193 	} else { /* bypass hi/lo/count_max have been processed by attr2hdl() */
1194 		count_max = mp->dmai_attr.dma_attr_count_max;
1195 		pfn_lo = MMU_BTOP(mp->dmai_attr.dma_attr_addr_lo);
1196 		pfn_hi = MMU_BTOP(mp->dmai_attr.dma_attr_addr_hi);
1197 
1198 		if (px_lib_iommu_getbypass(dip, MMU_PTOB(pfn),
1199 				attr, &bypass_addr) != DDI_SUCCESS) {
1200 			cmn_err(CE_WARN, "bypass cookie failure %llx\n", pfn);
1201 			return (DDI_DMA_NOMAPPING);
1202 		}
1203 		pfn = MMU_BTOP(bypass_addr);
1204 	}
1205 
1206 	/* pfn: absolute (bypass mode) or relative (p2p mode) */
1207 	for (prev_pfn = pfn, i = 1; i < npages;
1208 	    i++, prev_pfn = pfn, pfn_no++) {
1209 		pfn = PX_GET_MP_PFN1(mp, i);
1210 		if (bypass_addr) {
1211 			if (px_lib_iommu_getbypass(dip, MMU_PTOB(pfn), attr,
1212 					&bypass_addr) != DDI_SUCCESS) {
1213 				ret = DDI_DMA_NOMAPPING;
1214 				goto err;
1215 			}
1216 			pfn = MMU_BTOP(bypass_addr);
1217 		}
1218 		if ((pfn == prev_pfn + 1) &&
1219 				(MMU_PTOB(pfn_no + 1) - 1 <= count_max))
1220 			continue;
1221 		if ((pfn < pfn_lo) || (prev_pfn > pfn_hi)) {
1222 			ret = DDI_DMA_NOMAPPING;
1223 			goto err;
1224 		}
1225 		cookie_no++;
1226 		pfn_no = 0;
1227 		if (cookie_no < sgllen)
1228 			continue;
1229 
1230 		DBG(DBG_BYPASS, mp->dmai_rdip, "newwin pfn[%x-%x] %x cks\n",
1231 			win_pfn0_index, i - 1, cookie_no);
1232 		if (ret = px_dma_newwin(dip, dmareq, mp, cookie_no,
1233 			win_pfn0_index, i - 1, win_pp, count_max, bypass_addr))
1234 			goto err;
1235 
1236 		win_pp = &(*win_pp)->win_next;	/* win_pp = *(win_pp) */
1237 		win_no++;
1238 		win_pfn0_index = i;
1239 		cookie_no = 0;
1240 	}
1241 	if (pfn > pfn_hi) {
1242 		ret = DDI_DMA_NOMAPPING;
1243 		goto err;
1244 	}
1245 	cookie_no++;
1246 	DBG(DBG_BYPASS, mp->dmai_rdip, "newwin pfn[%x-%x] %x cks\n",
1247 		win_pfn0_index, i - 1, cookie_no);
1248 	if (ret = px_dma_newwin(dip, dmareq, mp, cookie_no, win_pfn0_index,
1249 		i - 1, win_pp, count_max, bypass_addr))
1250 		goto err;
1251 	win_no++;
1252 	px_dma_adjust(dmareq, mp, mp->dmai_winlst);
1253 	mp->dmai_nwin = win_no;
1254 	mp->dmai_rflags |= DDI_DMA_CONSISTENT | DMP_NOSYNC;
1255 	mp->dmai_rflags &= ~DDI_DMA_REDZONE;
1256 	mp->dmai_flags |= DMAI_FLAGS_NOSYNC;
1257 	cookie0_p = (ddi_dma_cookie_t *)(WINLST(mp) + 1);
1258 	mp->dmai_cookie = WINLST(mp)->win_ncookies > 1 ? cookie0_p + 1 : 0;
1259 	mp->dmai_mapping = cookie0_p->dmac_laddress;
1260 
1261 	px_dma_freepfn(mp);
1262 	return (DDI_DMA_MAPPED);
1263 err:
1264 	px_dma_freewin(mp);
1265 	return (ret);
1266 }
1267 
1268 int
1269 px_dma_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_impl_t *mp,
1270 	enum ddi_dma_ctlops cmd, off_t *offp, size_t *lenp, caddr_t *objp,
1271 	uint_t cache_flags)
1272 {
1273 	switch (cmd) {
1274 	case DDI_DMA_SYNC:
1275 		return (DDI_SUCCESS);
1276 
1277 	case DDI_DMA_HTOC: {
1278 		off_t off = *offp;
1279 		ddi_dma_cookie_t *loop_cp, *cp;
1280 		px_dma_win_t *win_p = mp->dmai_winlst;
1281 
1282 		if (off >= mp->dmai_object.dmao_size)
1283 			return (DDI_FAILURE);
1284 
1285 		/* locate window */
1286 		while (win_p->win_offset + win_p->win_size <= off)
1287 			win_p = win_p->win_next;
1288 
1289 		loop_cp = cp = (ddi_dma_cookie_t *)(win_p + 1);
1290 		mp->dmai_offset = win_p->win_offset;
1291 		mp->dmai_size   = win_p->win_size;
1292 		mp->dmai_mapping = cp->dmac_laddress; /* cookie0 start addr */
1293 
1294 		/* adjust cookie addr/len if we are not on cookie boundary */
1295 		off -= win_p->win_offset;	   /* offset within window */
1296 		for (; off >= loop_cp->dmac_size; loop_cp++)
1297 			off -= loop_cp->dmac_size; /* offset within cookie */
1298 
1299 		mp->dmai_cookie = loop_cp + 1;
1300 		win_p->win_curseg = loop_cp - cp;
1301 		cp = (ddi_dma_cookie_t *)objp;
1302 		MAKE_DMA_COOKIE(cp, loop_cp->dmac_laddress + off,
1303 			loop_cp->dmac_size - off);
1304 
1305 		DBG(DBG_DMA_CTL, dip,
1306 			"HTOC: cookie - dmac_laddress=%p dmac_size=%x\n",
1307 			cp->dmac_laddress, cp->dmac_size);
1308 		}
1309 		return (DDI_SUCCESS);
1310 
1311 	case DDI_DMA_REPWIN:
1312 		*offp = mp->dmai_offset;
1313 		*lenp = mp->dmai_size;
1314 		return (DDI_SUCCESS);
1315 
1316 	case DDI_DMA_MOVWIN: {
1317 		off_t off = *offp;
1318 		ddi_dma_cookie_t *cp;
1319 		px_dma_win_t *win_p = mp->dmai_winlst;
1320 
1321 		if (off >= mp->dmai_object.dmao_size)
1322 			return (DDI_FAILURE);
1323 
1324 		/* locate window */
1325 		while (win_p->win_offset + win_p->win_size <= off)
1326 			win_p = win_p->win_next;
1327 
1328 		cp = (ddi_dma_cookie_t *)(win_p + 1);
1329 		mp->dmai_offset = win_p->win_offset;
1330 		mp->dmai_size   = win_p->win_size;
1331 		mp->dmai_mapping = cp->dmac_laddress;	/* cookie0 star addr */
1332 		mp->dmai_cookie = cp + 1;
1333 		win_p->win_curseg = 0;
1334 
1335 		*(ddi_dma_cookie_t *)objp = *cp;
1336 		*offp = win_p->win_offset;
1337 		*lenp = win_p->win_size;
1338 		DBG(DBG_DMA_CTL, dip,
1339 			"HTOC: cookie - dmac_laddress=%p dmac_size=%x\n",
1340 			cp->dmac_laddress, cp->dmac_size);
1341 		}
1342 		return (DDI_SUCCESS);
1343 
1344 	case DDI_DMA_NEXTWIN: {
1345 		px_dma_win_t *win_p = *(px_dma_win_t **)offp;
1346 		px_dma_win_t **nw_pp = (px_dma_win_t **)objp;
1347 		ddi_dma_cookie_t *cp;
1348 		if (!win_p) {
1349 			*nw_pp = mp->dmai_winlst;
1350 			return (DDI_SUCCESS);
1351 		}
1352 
1353 		if (win_p->win_offset != mp->dmai_offset)
1354 			return (DDI_DMA_STALE);
1355 		if (!win_p->win_next)
1356 			return (DDI_DMA_DONE);
1357 		win_p = win_p->win_next;
1358 		cp = (ddi_dma_cookie_t *)(win_p + 1);
1359 		mp->dmai_offset = win_p->win_offset;
1360 		mp->dmai_size   = win_p->win_size;
1361 		mp->dmai_mapping = cp->dmac_laddress;   /* cookie0 star addr */
1362 		mp->dmai_cookie = cp + 1;
1363 		win_p->win_curseg = 0;
1364 		*nw_pp = win_p;
1365 		}
1366 		return (DDI_SUCCESS);
1367 
1368 	case DDI_DMA_NEXTSEG: {
1369 		px_dma_win_t *w_p = *(px_dma_win_t **)offp;
1370 		if (w_p->win_offset != mp->dmai_offset)
1371 			return (DDI_DMA_STALE);
1372 		if (w_p->win_curseg + 1 >= w_p->win_ncookies)
1373 			return (DDI_DMA_DONE);
1374 		w_p->win_curseg++;
1375 		}
1376 		*(ddi_dma_seg_t *)objp = (ddi_dma_seg_t)mp;
1377 		return (DDI_SUCCESS);
1378 
1379 	case DDI_DMA_SEGTOC: {
1380 		px_dma_win_t *win_p = mp->dmai_winlst;
1381 		off_t off = mp->dmai_offset;
1382 		ddi_dma_cookie_t *cp;
1383 		int i;
1384 
1385 		/* locate active window */
1386 		for (; win_p->win_offset != off; win_p = win_p->win_next);
1387 		cp = (ddi_dma_cookie_t *)(win_p + 1);
1388 		for (i = 0; i < win_p->win_curseg; i++, cp++)
1389 			off += cp->dmac_size;
1390 		*offp = off;
1391 		*lenp = cp->dmac_size;
1392 		*(ddi_dma_cookie_t *)objp = *cp;	/* copy cookie */
1393 		}
1394 		return (DDI_SUCCESS);
1395 
1396 	case DDI_DMA_COFF: {
1397 		px_dma_win_t *win_p;
1398 		ddi_dma_cookie_t *cp;
1399 		uint64_t addr, key = ((ddi_dma_cookie_t *)offp)->dmac_laddress;
1400 		size_t win_off;
1401 
1402 		for (win_p = mp->dmai_winlst; win_p; win_p = win_p->win_next) {
1403 			int i;
1404 			win_off = 0;
1405 			cp = (ddi_dma_cookie_t *)(win_p + 1);
1406 			for (i = 0; i < win_p->win_ncookies; i++, cp++) {
1407 				size_t sz = cp->dmac_size;
1408 
1409 				addr = cp->dmac_laddress;
1410 				if ((addr <= key) && (addr + sz >= key))
1411 					goto found;
1412 				win_off += sz;
1413 			}
1414 		}
1415 		return (DDI_FAILURE);
1416 found:
1417 		*objp = (caddr_t)(win_p->win_offset + win_off + (key - addr));
1418 		return (DDI_SUCCESS);
1419 		}
1420 	default:
1421 		DBG(DBG_DMA_CTL, dip, "unknown command (%x): rdip=%s%d\n",
1422 			cmd, ddi_driver_name(rdip), ddi_get_instance(rdip));
1423 		break;
1424 	}
1425 	return (DDI_FAILURE);
1426 }
1427 
1428 static void
1429 px_dvma_debug_init(px_mmu_t *mmu_p)
1430 {
1431 	size_t sz = sizeof (struct px_dvma_rec) * px_dvma_debug_rec;
1432 	ASSERT(MUTEX_HELD(&mmu_p->dvma_debug_lock));
1433 	cmn_err(CE_NOTE, "PCI Express DVMA %p stat ON", mmu_p);
1434 
1435 	mmu_p->dvma_alloc_rec = kmem_alloc(sz, KM_SLEEP);
1436 	mmu_p->dvma_free_rec = kmem_alloc(sz, KM_SLEEP);
1437 
1438 	mmu_p->dvma_active_list = NULL;
1439 	mmu_p->dvma_alloc_rec_index = 0;
1440 	mmu_p->dvma_free_rec_index = 0;
1441 	mmu_p->dvma_active_count = 0;
1442 }
1443 
1444 void
1445 px_dvma_debug_fini(px_mmu_t *mmu_p)
1446 {
1447 	struct px_dvma_rec *prev, *ptr;
1448 	size_t sz = sizeof (struct px_dvma_rec) * px_dvma_debug_rec;
1449 	uint64_t mask = ~(1ull << mmu_p->mmu_inst);
1450 	cmn_err(CE_NOTE, "PCI Express DVMA %p stat OFF", mmu_p);
1451 
1452 	kmem_free(mmu_p->dvma_alloc_rec, sz);
1453 	kmem_free(mmu_p->dvma_free_rec, sz);
1454 	mmu_p->dvma_alloc_rec = mmu_p->dvma_free_rec = NULL;
1455 
1456 	prev = mmu_p->dvma_active_list;
1457 	if (!prev)
1458 		return;
1459 	for (ptr = prev->next; ptr; prev = ptr, ptr = ptr->next)
1460 		kmem_free(prev, sizeof (struct px_dvma_rec));
1461 	kmem_free(prev, sizeof (struct px_dvma_rec));
1462 
1463 	mmu_p->dvma_active_list = NULL;
1464 	mmu_p->dvma_alloc_rec_index = 0;
1465 	mmu_p->dvma_free_rec_index = 0;
1466 	mmu_p->dvma_active_count = 0;
1467 
1468 	px_dvma_debug_off &= mask;
1469 	px_dvma_debug_on &= mask;
1470 }
1471 
1472 void
1473 px_dvma_alloc_debug(px_mmu_t *mmu_p, char *address, uint_t len,
1474 	ddi_dma_impl_t *mp)
1475 {
1476 	struct px_dvma_rec *ptr;
1477 	mutex_enter(&mmu_p->dvma_debug_lock);
1478 
1479 	if (!mmu_p->dvma_alloc_rec)
1480 		px_dvma_debug_init(mmu_p);
1481 	if (DVMA_DBG_OFF(mmu_p)) {
1482 		px_dvma_debug_fini(mmu_p);
1483 		goto done;
1484 	}
1485 
1486 	ptr = &mmu_p->dvma_alloc_rec[mmu_p->dvma_alloc_rec_index];
1487 	ptr->dvma_addr = address;
1488 	ptr->len = len;
1489 	ptr->mp = mp;
1490 	if (++mmu_p->dvma_alloc_rec_index == px_dvma_debug_rec)
1491 		mmu_p->dvma_alloc_rec_index = 0;
1492 
1493 	ptr = kmem_alloc(sizeof (struct px_dvma_rec), KM_SLEEP);
1494 	ptr->dvma_addr = address;
1495 	ptr->len = len;
1496 	ptr->mp = mp;
1497 
1498 	ptr->next = mmu_p->dvma_active_list;
1499 	mmu_p->dvma_active_list = ptr;
1500 	mmu_p->dvma_active_count++;
1501 done:
1502 	mutex_exit(&mmu_p->dvma_debug_lock);
1503 }
1504 
1505 void
1506 px_dvma_free_debug(px_mmu_t *mmu_p, char *address, uint_t len,
1507     ddi_dma_impl_t *mp)
1508 {
1509 	struct px_dvma_rec *ptr, *ptr_save;
1510 	mutex_enter(&mmu_p->dvma_debug_lock);
1511 
1512 	if (!mmu_p->dvma_alloc_rec)
1513 		px_dvma_debug_init(mmu_p);
1514 	if (DVMA_DBG_OFF(mmu_p)) {
1515 		px_dvma_debug_fini(mmu_p);
1516 		goto done;
1517 	}
1518 
1519 	ptr = &mmu_p->dvma_free_rec[mmu_p->dvma_free_rec_index];
1520 	ptr->dvma_addr = address;
1521 	ptr->len = len;
1522 	ptr->mp = mp;
1523 	if (++mmu_p->dvma_free_rec_index == px_dvma_debug_rec)
1524 		mmu_p->dvma_free_rec_index = 0;
1525 
1526 	ptr_save = mmu_p->dvma_active_list;
1527 	for (ptr = ptr_save; ptr; ptr = ptr->next) {
1528 		if ((ptr->dvma_addr == address) && (ptr->len = len))
1529 			break;
1530 		ptr_save = ptr;
1531 	}
1532 	if (!ptr) {
1533 		cmn_err(CE_WARN, "bad dvma free addr=%lx len=%x",
1534 			(long)address, len);
1535 		goto done;
1536 	}
1537 	if (ptr == mmu_p->dvma_active_list)
1538 		mmu_p->dvma_active_list = ptr->next;
1539 	else
1540 		ptr_save->next = ptr->next;
1541 	kmem_free(ptr, sizeof (struct px_dvma_rec));
1542 	mmu_p->dvma_active_count--;
1543 done:
1544 	mutex_exit(&mmu_p->dvma_debug_lock);
1545 }
1546 
1547 #ifdef	DEBUG
1548 void
1549 px_dump_dma_handle(uint64_t flag, dev_info_t *dip, ddi_dma_impl_t *hp)
1550 {
1551 	DBG(flag, dip, "mp(%p): flags=%x mapping=%lx xfer_size=%x\n",
1552 		hp, hp->dmai_inuse, hp->dmai_mapping, hp->dmai_size);
1553 	DBG(flag|DBG_CONT, dip, "\tnpages=%x roffset=%x rflags=%x nwin=%x\n",
1554 		hp->dmai_ndvmapages, hp->dmai_roffset, hp->dmai_rflags,
1555 		hp->dmai_nwin);
1556 	DBG(flag|DBG_CONT, dip, "\twinsize=%x tte=%p pfnlst=%p pfn0=%p\n",
1557 		hp->dmai_winsize, hp->dmai_tte, hp->dmai_pfnlst, hp->dmai_pfn0);
1558 	DBG(flag|DBG_CONT, dip, "\twinlst=%x obj=%p attr=%p ckp=%p\n",
1559 		hp->dmai_winlst, &hp->dmai_object, &hp->dmai_attr,
1560 		hp->dmai_cookie);
1561 }
1562 #endif	/* DEBUG */
1563