xref: /onnv-gate/usr/src/uts/i86pc/io/rootnex.c (revision 13070:87f89233b883)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51865Sdilpreet  * Common Development and Distribution License (the "License").
61865Sdilpreet  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
2212118Smark.r.johnson@oracle.com  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*
26509Smrj  * x86 root nexus driver
270Sstevel@tonic-gate  */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/sysmacros.h>
300Sstevel@tonic-gate #include <sys/conf.h>
310Sstevel@tonic-gate #include <sys/autoconf.h>
320Sstevel@tonic-gate #include <sys/sysmacros.h>
330Sstevel@tonic-gate #include <sys/debug.h>
340Sstevel@tonic-gate #include <sys/psw.h>
350Sstevel@tonic-gate #include <sys/ddidmareq.h>
360Sstevel@tonic-gate #include <sys/promif.h>
370Sstevel@tonic-gate #include <sys/devops.h>
380Sstevel@tonic-gate #include <sys/kmem.h>
390Sstevel@tonic-gate #include <sys/cmn_err.h>
400Sstevel@tonic-gate #include <vm/seg.h>
410Sstevel@tonic-gate #include <vm/seg_kmem.h>
420Sstevel@tonic-gate #include <vm/seg_dev.h>
430Sstevel@tonic-gate #include <sys/vmem.h>
440Sstevel@tonic-gate #include <sys/mman.h>
450Sstevel@tonic-gate #include <vm/hat.h>
460Sstevel@tonic-gate #include <vm/as.h>
470Sstevel@tonic-gate #include <vm/page.h>
480Sstevel@tonic-gate #include <sys/avintr.h>
490Sstevel@tonic-gate #include <sys/errno.h>
500Sstevel@tonic-gate #include <sys/modctl.h>
510Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
520Sstevel@tonic-gate #include <sys/sunddi.h>
530Sstevel@tonic-gate #include <sys/sunndi.h>
54916Sschwartz #include <sys/mach_intr.h>
550Sstevel@tonic-gate #include <sys/psm.h>
560Sstevel@tonic-gate #include <sys/ontrap.h>
57509Smrj #include <sys/atomic.h>
58509Smrj #include <sys/sdt.h>
59509Smrj #include <sys/rootnex.h>
60509Smrj #include <vm/hat_i86.h>
611865Sdilpreet #include <sys/ddifm.h>
625251Smrj #include <sys/ddi_isa.h>
6312683SJimmy.Vetayases@oracle.com #include <sys/apic.h>
64509Smrj 
655084Sjohnlev #ifdef __xpv
665084Sjohnlev #include <sys/bootinfo.h>
675084Sjohnlev #include <sys/hypervisor.h>
685084Sjohnlev #include <sys/bootconf.h>
695084Sjohnlev #include <vm/kboot_mmu.h>
7011600SVikram.Hegde@Sun.COM #endif
7111600SVikram.Hegde@Sun.COM 
7211600SVikram.Hegde@Sun.COM #if defined(__amd64) && !defined(__xpv)
7311600SVikram.Hegde@Sun.COM #include <sys/immu.h>
747613SVikram.Hegde@Sun.COM #endif
757613SVikram.Hegde@Sun.COM 
767589SVikram.Hegde@Sun.COM 
77509Smrj /*
78509Smrj  * enable/disable extra checking of function parameters. Useful for debugging
79509Smrj  * drivers.
80509Smrj  */
81509Smrj #ifdef	DEBUG
82509Smrj int rootnex_alloc_check_parms = 1;
83509Smrj int rootnex_bind_check_parms = 1;
84509Smrj int rootnex_bind_check_inuse = 1;
85509Smrj int rootnex_unbind_verify_buffer = 0;
86509Smrj int rootnex_sync_check_parms = 1;
87509Smrj #else
88509Smrj int rootnex_alloc_check_parms = 0;
89509Smrj int rootnex_bind_check_parms = 0;
90509Smrj int rootnex_bind_check_inuse = 0;
91509Smrj int rootnex_unbind_verify_buffer = 0;
92509Smrj int rootnex_sync_check_parms = 0;
93509Smrj #endif
94509Smrj 
9511600SVikram.Hegde@Sun.COM boolean_t rootnex_dmar_not_setup;
9611600SVikram.Hegde@Sun.COM 
971414Scindi /* Master Abort and Target Abort panic flag */
981414Scindi int rootnex_fm_ma_ta_panic_flag = 0;
991414Scindi 
100509Smrj /* Semi-temporary patchables to phase in bug fixes, test drivers, etc. */
1010Sstevel@tonic-gate int rootnex_bind_fail = 1;
1020Sstevel@tonic-gate int rootnex_bind_warn = 1;
1030Sstevel@tonic-gate uint8_t *rootnex_warn_list;
1040Sstevel@tonic-gate /* bitmasks for rootnex_warn_list. Up to 8 different warnings with uint8_t */
1050Sstevel@tonic-gate #define	ROOTNEX_BIND_WARNING	(0x1 << 0)
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate /*
108509Smrj  * revert back to old broken behavior of always sync'ing entire copy buffer.
109509Smrj  * This is useful if be have a buggy driver which doesn't correctly pass in
110509Smrj  * the offset and size into ddi_dma_sync().
1110Sstevel@tonic-gate  */
112509Smrj int rootnex_sync_ignore_params = 0;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate /*
115509Smrj  * For the 64-bit kernel, pre-alloc enough cookies for a 256K buffer plus 1
116509Smrj  * page for alignment. For the 32-bit kernel, pre-alloc enough cookies for a
117509Smrj  * 64K buffer plus 1 page for alignment (we have less kernel space in a 32-bit
118509Smrj  * kernel). Allocate enough windows to handle a 256K buffer w/ at least 65
119509Smrj  * sgllen DMA engine, and enough copybuf buffer state pages to handle 2 pages
120509Smrj  * (< 8K). We will still need to allocate the copy buffer during bind though
121509Smrj  * (if we need one). These can only be modified in /etc/system before rootnex
122509Smrj  * attach.
1230Sstevel@tonic-gate  */
124509Smrj #if defined(__amd64)
125509Smrj int rootnex_prealloc_cookies = 65;
126509Smrj int rootnex_prealloc_windows = 4;
127509Smrj int rootnex_prealloc_copybuf = 2;
128509Smrj #else
129509Smrj int rootnex_prealloc_cookies = 33;
130509Smrj int rootnex_prealloc_windows = 4;
131509Smrj int rootnex_prealloc_copybuf = 2;
132509Smrj #endif
133509Smrj 
134509Smrj /* driver global state */
135509Smrj static rootnex_state_t *rootnex_state;
136509Smrj 
13712837Sfrank.van.der.linden@oracle.com #ifdef DEBUG
138509Smrj /* shortcut to rootnex counters */
139509Smrj static uint64_t *rootnex_cnt;
14012837Sfrank.van.der.linden@oracle.com #endif
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate /*
143509Smrj  * XXX - does x86 even need these or are they left over from the SPARC days?
1440Sstevel@tonic-gate  */
145509Smrj /* statically defined integer/boolean properties for the root node */
146509Smrj static rootnex_intprop_t rootnex_intprp[] = {
147509Smrj 	{ "PAGESIZE",			PAGESIZE },
148509Smrj 	{ "MMU_PAGESIZE",		MMU_PAGESIZE },
149509Smrj 	{ "MMU_PAGEOFFSET",		MMU_PAGEOFFSET },
150509Smrj 	{ DDI_RELATIVE_ADDRESSING,	1 },
151509Smrj };
152509Smrj #define	NROOT_INTPROPS	(sizeof (rootnex_intprp) / sizeof (rootnex_intprop_t))
153509Smrj 
15413050Sfrank.van.der.linden@oracle.com /*
15513050Sfrank.van.der.linden@oracle.com  * If we're dom0, we're using a real device so we need to load
15613050Sfrank.van.der.linden@oracle.com  * the cookies with MFNs instead of PFNs.
15713050Sfrank.van.der.linden@oracle.com  */
1585084Sjohnlev #ifdef __xpv
1595084Sjohnlev typedef maddr_t rootnex_addr_t;
16013050Sfrank.van.der.linden@oracle.com #define	ROOTNEX_PADDR_TO_RBASE(pa)	\
16113050Sfrank.van.der.linden@oracle.com 	(DOMAIN_IS_INITDOMAIN(xen_info) ? pa_to_ma(pa) : (pa))
1625084Sjohnlev #else
1635084Sjohnlev typedef paddr_t rootnex_addr_t;
16413050Sfrank.van.der.linden@oracle.com #define	ROOTNEX_PADDR_TO_RBASE(pa)	(pa)
1655084Sjohnlev #endif
1665084Sjohnlev 
1677613SVikram.Hegde@Sun.COM #if !defined(__xpv)
16812683SJimmy.Vetayases@oracle.com char _depends_on[] = "misc/iommulib misc/acpica";
1697613SVikram.Hegde@Sun.COM #endif
170509Smrj 
171509Smrj static struct cb_ops rootnex_cb_ops = {
172509Smrj 	nodev,		/* open */
173509Smrj 	nodev,		/* close */
174509Smrj 	nodev,		/* strategy */
175509Smrj 	nodev,		/* print */
176509Smrj 	nodev,		/* dump */
177509Smrj 	nodev,		/* read */
178509Smrj 	nodev,		/* write */
179509Smrj 	nodev,		/* ioctl */
180509Smrj 	nodev,		/* devmap */
181509Smrj 	nodev,		/* mmap */
182509Smrj 	nodev,		/* segmap */
183509Smrj 	nochpoll,	/* chpoll */
184509Smrj 	ddi_prop_op,	/* cb_prop_op */
185509Smrj 	NULL,		/* struct streamtab */
186509Smrj 	D_NEW | D_MP | D_HOTPLUG, /* compatibility flags */
187509Smrj 	CB_REV,		/* Rev */
188509Smrj 	nodev,		/* cb_aread */
189509Smrj 	nodev		/* cb_awrite */
190509Smrj };
191509Smrj 
192509Smrj static int rootnex_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
1930Sstevel@tonic-gate     off_t offset, off_t len, caddr_t *vaddrp);
194509Smrj static int rootnex_map_fault(dev_info_t *dip, dev_info_t *rdip,
1950Sstevel@tonic-gate     struct hat *hat, struct seg *seg, caddr_t addr,
1960Sstevel@tonic-gate     struct devpage *dp, pfn_t pfn, uint_t prot, uint_t lock);
197509Smrj static int rootnex_dma_map(dev_info_t *dip, dev_info_t *rdip,
1980Sstevel@tonic-gate     struct ddi_dma_req *dmareq, ddi_dma_handle_t *handlep);
199509Smrj static int rootnex_dma_allochdl(dev_info_t *dip, dev_info_t *rdip,
200509Smrj     ddi_dma_attr_t *attr, int (*waitfp)(caddr_t), caddr_t arg,
201509Smrj     ddi_dma_handle_t *handlep);
202509Smrj static int rootnex_dma_freehdl(dev_info_t *dip, dev_info_t *rdip,
203509Smrj     ddi_dma_handle_t handle);
204509Smrj static int rootnex_dma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
205509Smrj     ddi_dma_handle_t handle, struct ddi_dma_req *dmareq,
206509Smrj     ddi_dma_cookie_t *cookiep, uint_t *ccountp);
207509Smrj static int rootnex_dma_unbindhdl(dev_info_t *dip, dev_info_t *rdip,
208509Smrj     ddi_dma_handle_t handle);
209509Smrj static int rootnex_dma_sync(dev_info_t *dip, dev_info_t *rdip,
210509Smrj     ddi_dma_handle_t handle, off_t off, size_t len, uint_t cache_flags);
211509Smrj static int rootnex_dma_win(dev_info_t *dip, dev_info_t *rdip,
212509Smrj     ddi_dma_handle_t handle, uint_t win, off_t *offp, size_t *lenp,
213509Smrj     ddi_dma_cookie_t *cookiep, uint_t *ccountp);
214509Smrj static int rootnex_dma_mctl(dev_info_t *dip, dev_info_t *rdip,
2150Sstevel@tonic-gate     ddi_dma_handle_t handle, enum ddi_dma_ctlops request,
2160Sstevel@tonic-gate     off_t *offp, size_t *lenp, caddr_t *objp, uint_t cache_flags);
217509Smrj static int rootnex_ctlops(dev_info_t *dip, dev_info_t *rdip,
218509Smrj     ddi_ctl_enum_t ctlop, void *arg, void *result);
2191865Sdilpreet static int rootnex_fm_init(dev_info_t *dip, dev_info_t *tdip, int tcap,
2201865Sdilpreet     ddi_iblock_cookie_t *ibc);
221509Smrj static int rootnex_intr_ops(dev_info_t *pdip, dev_info_t *rdip,
222509Smrj     ddi_intr_op_t intr_op, ddi_intr_handle_impl_t *hdlp, void *result);
22312683SJimmy.Vetayases@oracle.com static int rootnex_alloc_intr_fixed(dev_info_t *, ddi_intr_handle_impl_t *,
22412683SJimmy.Vetayases@oracle.com     void *);
22512683SJimmy.Vetayases@oracle.com static int rootnex_free_intr_fixed(dev_info_t *, ddi_intr_handle_impl_t *);
226509Smrj 
2277613SVikram.Hegde@Sun.COM static int rootnex_coredma_allochdl(dev_info_t *dip, dev_info_t *rdip,
2287613SVikram.Hegde@Sun.COM     ddi_dma_attr_t *attr, int (*waitfp)(caddr_t), caddr_t arg,
2297613SVikram.Hegde@Sun.COM     ddi_dma_handle_t *handlep);
2307613SVikram.Hegde@Sun.COM static int rootnex_coredma_freehdl(dev_info_t *dip, dev_info_t *rdip,
2317613SVikram.Hegde@Sun.COM     ddi_dma_handle_t handle);
2327613SVikram.Hegde@Sun.COM static int rootnex_coredma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
2337613SVikram.Hegde@Sun.COM     ddi_dma_handle_t handle, struct ddi_dma_req *dmareq,
2347613SVikram.Hegde@Sun.COM     ddi_dma_cookie_t *cookiep, uint_t *ccountp);
2357613SVikram.Hegde@Sun.COM static int rootnex_coredma_unbindhdl(dev_info_t *dip, dev_info_t *rdip,
2367613SVikram.Hegde@Sun.COM     ddi_dma_handle_t handle);
23711600SVikram.Hegde@Sun.COM #if defined(__amd64) && !defined(__xpv)
2387613SVikram.Hegde@Sun.COM static void rootnex_coredma_reset_cookies(dev_info_t *dip,
2397613SVikram.Hegde@Sun.COM     ddi_dma_handle_t handle);
2407613SVikram.Hegde@Sun.COM static int rootnex_coredma_get_cookies(dev_info_t *dip, ddi_dma_handle_t handle,
2418215SVikram.Hegde@Sun.COM     ddi_dma_cookie_t **cookiepp, uint_t *ccountp);
2428215SVikram.Hegde@Sun.COM static int rootnex_coredma_set_cookies(dev_info_t *dip, ddi_dma_handle_t handle,
2438215SVikram.Hegde@Sun.COM     ddi_dma_cookie_t *cookiep, uint_t ccount);
2448215SVikram.Hegde@Sun.COM static int rootnex_coredma_clear_cookies(dev_info_t *dip,
2458215SVikram.Hegde@Sun.COM     ddi_dma_handle_t handle);
2468215SVikram.Hegde@Sun.COM static int rootnex_coredma_get_sleep_flags(ddi_dma_handle_t handle);
2477617SVikram.Hegde@Sun.COM #endif
2487613SVikram.Hegde@Sun.COM static int rootnex_coredma_sync(dev_info_t *dip, dev_info_t *rdip,
2497613SVikram.Hegde@Sun.COM     ddi_dma_handle_t handle, off_t off, size_t len, uint_t cache_flags);
2507613SVikram.Hegde@Sun.COM static int rootnex_coredma_win(dev_info_t *dip, dev_info_t *rdip,
2517613SVikram.Hegde@Sun.COM     ddi_dma_handle_t handle, uint_t win, off_t *offp, size_t *lenp,
2527613SVikram.Hegde@Sun.COM     ddi_dma_cookie_t *cookiep, uint_t *ccountp);
2530Sstevel@tonic-gate 
25413053Sfrank.van.der.linden@oracle.com #if defined(__amd64) && !defined(__xpv)
25513050Sfrank.van.der.linden@oracle.com static int rootnex_coredma_hdl_setprivate(dev_info_t *dip, dev_info_t *rdip,
25613050Sfrank.van.der.linden@oracle.com     ddi_dma_handle_t handle, void *v);
25713050Sfrank.van.der.linden@oracle.com static void *rootnex_coredma_hdl_getprivate(dev_info_t *dip, dev_info_t *rdip,
25813050Sfrank.van.der.linden@oracle.com     ddi_dma_handle_t handle);
25913053Sfrank.van.der.linden@oracle.com #endif
26013050Sfrank.van.der.linden@oracle.com 
26113050Sfrank.van.der.linden@oracle.com 
2620Sstevel@tonic-gate static struct bus_ops rootnex_bus_ops = {
2630Sstevel@tonic-gate 	BUSO_REV,
2640Sstevel@tonic-gate 	rootnex_map,
2650Sstevel@tonic-gate 	NULL,
2660Sstevel@tonic-gate 	NULL,
2670Sstevel@tonic-gate 	NULL,
2680Sstevel@tonic-gate 	rootnex_map_fault,
2690Sstevel@tonic-gate 	rootnex_dma_map,
2700Sstevel@tonic-gate 	rootnex_dma_allochdl,
2710Sstevel@tonic-gate 	rootnex_dma_freehdl,
2720Sstevel@tonic-gate 	rootnex_dma_bindhdl,
2730Sstevel@tonic-gate 	rootnex_dma_unbindhdl,
274509Smrj 	rootnex_dma_sync,
2750Sstevel@tonic-gate 	rootnex_dma_win,
2760Sstevel@tonic-gate 	rootnex_dma_mctl,
2770Sstevel@tonic-gate 	rootnex_ctlops,
2780Sstevel@tonic-gate 	ddi_bus_prop_op,
2790Sstevel@tonic-gate 	i_ddi_rootnex_get_eventcookie,
2800Sstevel@tonic-gate 	i_ddi_rootnex_add_eventcall,
2810Sstevel@tonic-gate 	i_ddi_rootnex_remove_eventcall,
2820Sstevel@tonic-gate 	i_ddi_rootnex_post_event,
2830Sstevel@tonic-gate 	0,			/* bus_intr_ctl */
2840Sstevel@tonic-gate 	0,			/* bus_config */
2850Sstevel@tonic-gate 	0,			/* bus_unconfig */
2861865Sdilpreet 	rootnex_fm_init,	/* bus_fm_init */
2870Sstevel@tonic-gate 	NULL,			/* bus_fm_fini */
2880Sstevel@tonic-gate 	NULL,			/* bus_fm_access_enter */
2890Sstevel@tonic-gate 	NULL,			/* bus_fm_access_exit */
2900Sstevel@tonic-gate 	NULL,			/* bus_powr */
2910Sstevel@tonic-gate 	rootnex_intr_ops	/* bus_intr_op */
2920Sstevel@tonic-gate };
2930Sstevel@tonic-gate 
294509Smrj static int rootnex_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
295509Smrj static int rootnex_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
29611600SVikram.Hegde@Sun.COM static int rootnex_quiesce(dev_info_t *dip);
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate static struct dev_ops rootnex_ops = {
2990Sstevel@tonic-gate 	DEVO_REV,
300509Smrj 	0,
301509Smrj 	ddi_no_info,
302509Smrj 	nulldev,
3030Sstevel@tonic-gate 	nulldev,
3040Sstevel@tonic-gate 	rootnex_attach,
305509Smrj 	rootnex_detach,
306509Smrj 	nulldev,
307509Smrj 	&rootnex_cb_ops,
3087656SSherry.Moore@Sun.COM 	&rootnex_bus_ops,
3097656SSherry.Moore@Sun.COM 	NULL,
31011600SVikram.Hegde@Sun.COM 	rootnex_quiesce,		/* quiesce */
3110Sstevel@tonic-gate };
3120Sstevel@tonic-gate 
313509Smrj static struct modldrv rootnex_modldrv = {
314509Smrj 	&mod_driverops,
3157542SRichard.Bean@Sun.COM 	"i86pc root nexus",
316509Smrj 	&rootnex_ops
317509Smrj };
318509Smrj 
319509Smrj static struct modlinkage rootnex_modlinkage = {
320509Smrj 	MODREV_1,
321509Smrj 	(void *)&rootnex_modldrv,
322509Smrj 	NULL
323509Smrj };
324509Smrj 
32511600SVikram.Hegde@Sun.COM #if defined(__amd64) && !defined(__xpv)
3267613SVikram.Hegde@Sun.COM static iommulib_nexops_t iommulib_nexops = {
3277613SVikram.Hegde@Sun.COM 	IOMMU_NEXOPS_VERSION,
3287613SVikram.Hegde@Sun.COM 	"Rootnex IOMMU ops Vers 1.1",
3297613SVikram.Hegde@Sun.COM 	NULL,
3307613SVikram.Hegde@Sun.COM 	rootnex_coredma_allochdl,
3317613SVikram.Hegde@Sun.COM 	rootnex_coredma_freehdl,
3327613SVikram.Hegde@Sun.COM 	rootnex_coredma_bindhdl,
3337613SVikram.Hegde@Sun.COM 	rootnex_coredma_unbindhdl,
3347613SVikram.Hegde@Sun.COM 	rootnex_coredma_reset_cookies,
3357613SVikram.Hegde@Sun.COM 	rootnex_coredma_get_cookies,
3368215SVikram.Hegde@Sun.COM 	rootnex_coredma_set_cookies,
3378215SVikram.Hegde@Sun.COM 	rootnex_coredma_clear_cookies,
3388215SVikram.Hegde@Sun.COM 	rootnex_coredma_get_sleep_flags,
3397613SVikram.Hegde@Sun.COM 	rootnex_coredma_sync,
3407613SVikram.Hegde@Sun.COM 	rootnex_coredma_win,
34110216SVikram.Hegde@Sun.COM 	rootnex_dma_map,
34213050Sfrank.van.der.linden@oracle.com 	rootnex_dma_mctl,
34313050Sfrank.van.der.linden@oracle.com 	rootnex_coredma_hdl_setprivate,
34413050Sfrank.van.der.linden@oracle.com 	rootnex_coredma_hdl_getprivate
3457613SVikram.Hegde@Sun.COM };
3467617SVikram.Hegde@Sun.COM #endif
347509Smrj 
348509Smrj /*
349509Smrj  *  extern hacks
350509Smrj  */
351509Smrj extern struct seg_ops segdev_ops;
352509Smrj extern int ignore_hardware_nodes;	/* force flag from ddi_impl.c */
353509Smrj #ifdef	DDI_MAP_DEBUG
354509Smrj extern int ddi_map_debug_flag;
355509Smrj #define	ddi_map_debug	if (ddi_map_debug_flag) prom_printf
356509Smrj #endif
357509Smrj extern void i86_pp_map(page_t *pp, caddr_t kaddr);
358509Smrj extern void i86_va_map(caddr_t vaddr, struct as *asp, caddr_t kaddr);
359509Smrj extern int (*psm_intr_ops)(dev_info_t *, ddi_intr_handle_impl_t *,
360509Smrj     psm_intr_op_t, int *);
361509Smrj extern int impl_ddi_sunbus_initchild(dev_info_t *dip);
362509Smrj extern void impl_ddi_sunbus_removechild(dev_info_t *dip);
3635251Smrj 
364509Smrj /*
365509Smrj  * Use device arena to use for device control register mappings.
366509Smrj  * Various kernel memory walkers (debugger, dtrace) need to know
367509Smrj  * to avoid this address range to prevent undesired device activity.
368509Smrj  */
369509Smrj extern void *device_arena_alloc(size_t size, int vm_flag);
370509Smrj extern void device_arena_free(void * vaddr, size_t size);
371509Smrj 
372509Smrj 
3730Sstevel@tonic-gate /*
374509Smrj  *  Internal functions
3750Sstevel@tonic-gate  */
376509Smrj static int rootnex_dma_init();
377509Smrj static void rootnex_add_props(dev_info_t *);
378509Smrj static int rootnex_ctl_reportdev(dev_info_t *dip);
379509Smrj static struct intrspec *rootnex_get_ispec(dev_info_t *rdip, int inum);
380509Smrj static int rootnex_map_regspec(ddi_map_req_t *mp, caddr_t *vaddrp);
381509Smrj static int rootnex_unmap_regspec(ddi_map_req_t *mp, caddr_t *vaddrp);
382509Smrj static int rootnex_map_handle(ddi_map_req_t *mp);
383509Smrj static void rootnex_clean_dmahdl(ddi_dma_impl_t *hp);
384509Smrj static int rootnex_valid_alloc_parms(ddi_dma_attr_t *attr, uint_t maxsegsize);
385509Smrj static int rootnex_valid_bind_parms(ddi_dma_req_t *dmareq,
386509Smrj     ddi_dma_attr_t *attr);
387509Smrj static void rootnex_get_sgl(ddi_dma_obj_t *dmar_object, ddi_dma_cookie_t *sgl,
388509Smrj     rootnex_sglinfo_t *sglinfo);
38913050Sfrank.van.der.linden@oracle.com static void rootnex_dvma_get_sgl(ddi_dma_obj_t *dmar_object,
39013050Sfrank.van.der.linden@oracle.com     ddi_dma_cookie_t *sgl, rootnex_sglinfo_t *sglinfo);
391509Smrj static int rootnex_bind_slowpath(ddi_dma_impl_t *hp, struct ddi_dma_req *dmareq,
39213050Sfrank.van.der.linden@oracle.com     rootnex_dma_t *dma, ddi_dma_attr_t *attr, ddi_dma_obj_t *dmao, int kmflag);
393509Smrj static int rootnex_setup_copybuf(ddi_dma_impl_t *hp, struct ddi_dma_req *dmareq,
394509Smrj     rootnex_dma_t *dma, ddi_dma_attr_t *attr);
395509Smrj static void rootnex_teardown_copybuf(rootnex_dma_t *dma);
396509Smrj static int rootnex_setup_windows(ddi_dma_impl_t *hp, rootnex_dma_t *dma,
39713050Sfrank.van.der.linden@oracle.com     ddi_dma_attr_t *attr, ddi_dma_obj_t *dmao, int kmflag);
398509Smrj static void rootnex_teardown_windows(rootnex_dma_t *dma);
399509Smrj static void rootnex_init_win(ddi_dma_impl_t *hp, rootnex_dma_t *dma,
400509Smrj     rootnex_window_t *window, ddi_dma_cookie_t *cookie, off_t cur_offset);
401509Smrj static void rootnex_setup_cookie(ddi_dma_obj_t *dmar_object,
402509Smrj     rootnex_dma_t *dma, ddi_dma_cookie_t *cookie, off_t cur_offset,
403509Smrj     size_t *copybuf_used, page_t **cur_pp);
404509Smrj static int rootnex_sgllen_window_boundary(ddi_dma_impl_t *hp,
405509Smrj     rootnex_dma_t *dma, rootnex_window_t **windowp, ddi_dma_cookie_t *cookie,
406509Smrj     ddi_dma_attr_t *attr, off_t cur_offset);
407509Smrj static int rootnex_copybuf_window_boundary(ddi_dma_impl_t *hp,
408509Smrj     rootnex_dma_t *dma, rootnex_window_t **windowp,
409509Smrj     ddi_dma_cookie_t *cookie, off_t cur_offset, size_t *copybuf_used);
410509Smrj static int rootnex_maxxfer_window_boundary(ddi_dma_impl_t *hp,
411509Smrj     rootnex_dma_t *dma, rootnex_window_t **windowp, ddi_dma_cookie_t *cookie);
412509Smrj static int rootnex_valid_sync_parms(ddi_dma_impl_t *hp, rootnex_window_t *win,
413509Smrj     off_t offset, size_t size, uint_t cache_flags);
414509Smrj static int rootnex_verify_buffer(rootnex_dma_t *dma);
4151865Sdilpreet static int rootnex_dma_check(dev_info_t *dip, const void *handle,
4161865Sdilpreet     const void *comp_addr, const void *not_used);
41711793SMark.Johnson@Sun.COM static boolean_t rootnex_need_bounce_seg(ddi_dma_obj_t *dmar_object,
41811793SMark.Johnson@Sun.COM     rootnex_sglinfo_t *sglinfo);
41913050Sfrank.van.der.linden@oracle.com static struct as *rootnex_get_as(ddi_dma_obj_t *dmar_object);
420509Smrj 
421509Smrj /*
422509Smrj  * _init()
423509Smrj  *
424509Smrj  */
4250Sstevel@tonic-gate int
_init(void)4260Sstevel@tonic-gate _init(void)
4270Sstevel@tonic-gate {
428509Smrj 
429509Smrj 	rootnex_state = NULL;
430509Smrj 	return (mod_install(&rootnex_modlinkage));
4310Sstevel@tonic-gate }
4320Sstevel@tonic-gate 
433509Smrj 
434509Smrj /*
435509Smrj  * _info()
436509Smrj  *
437509Smrj  */
438509Smrj int
_info(struct modinfo * modinfop)439509Smrj _info(struct modinfo *modinfop)
440509Smrj {
441509Smrj 	return (mod_info(&rootnex_modlinkage, modinfop));
442509Smrj }
443509Smrj 
444509Smrj 
445509Smrj /*
446509Smrj  * _fini()
447509Smrj  *
448509Smrj  */
4490Sstevel@tonic-gate int
_fini(void)4500Sstevel@tonic-gate _fini(void)
4510Sstevel@tonic-gate {
4520Sstevel@tonic-gate 	return (EBUSY);
4530Sstevel@tonic-gate }
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate /*
457509Smrj  * rootnex_attach()
4580Sstevel@tonic-gate  *
4590Sstevel@tonic-gate  */
460509Smrj static int
rootnex_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)461509Smrj rootnex_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
462509Smrj {
4631414Scindi 	int fmcap;
464509Smrj 	int e;
465509Smrj 
466509Smrj 	switch (cmd) {
467509Smrj 	case DDI_ATTACH:
468509Smrj 		break;
469509Smrj 	case DDI_RESUME:
47011600SVikram.Hegde@Sun.COM #if defined(__amd64) && !defined(__xpv)
47111600SVikram.Hegde@Sun.COM 		return (immu_unquiesce());
47211600SVikram.Hegde@Sun.COM #else
473509Smrj 		return (DDI_SUCCESS);
47411600SVikram.Hegde@Sun.COM #endif
475509Smrj 	default:
476509Smrj 		return (DDI_FAILURE);
477509Smrj 	}
478509Smrj 
479509Smrj 	/*
480509Smrj 	 * We should only have one instance of rootnex. Save it away since we
481509Smrj 	 * don't have an easy way to get it back later.
482509Smrj 	 */
483509Smrj 	ASSERT(rootnex_state == NULL);
484509Smrj 	rootnex_state = kmem_zalloc(sizeof (rootnex_state_t), KM_SLEEP);
485509Smrj 
486509Smrj 	rootnex_state->r_dip = dip;
4871414Scindi 	rootnex_state->r_err_ibc = (ddi_iblock_cookie_t)ipltospl(15);
488509Smrj 	rootnex_state->r_reserved_msg_printed = B_FALSE;
48912837Sfrank.van.der.linden@oracle.com #ifdef DEBUG
490509Smrj 	rootnex_cnt = &rootnex_state->r_counters[0];
49112837Sfrank.van.der.linden@oracle.com #endif
492509Smrj 
4931414Scindi 	/*
4941414Scindi 	 * Set minimum fm capability level for i86pc platforms and then
4951414Scindi 	 * initialize error handling. Since we're the rootnex, we don't
4961414Scindi 	 * care what's returned in the fmcap field.
4971414Scindi 	 */
4981865Sdilpreet 	ddi_system_fmcap = DDI_FM_EREPORT_CAPABLE | DDI_FM_ERRCB_CAPABLE |
4991865Sdilpreet 	    DDI_FM_ACCCHK_CAPABLE | DDI_FM_DMACHK_CAPABLE;
5001414Scindi 	fmcap = ddi_system_fmcap;
5011414Scindi 	ddi_fm_init(dip, &fmcap, &rootnex_state->r_err_ibc);
5021414Scindi 
503509Smrj 	/* initialize DMA related state */
504509Smrj 	e = rootnex_dma_init();
505509Smrj 	if (e != DDI_SUCCESS) {
506509Smrj 		kmem_free(rootnex_state, sizeof (rootnex_state_t));
507509Smrj 		return (DDI_FAILURE);
508509Smrj 	}
509509Smrj 
510509Smrj 	/* Add static root node properties */
511509Smrj 	rootnex_add_props(dip);
512509Smrj 
513509Smrj 	/* since we can't call ddi_report_dev() */
514509Smrj 	cmn_err(CE_CONT, "?root nexus = %s\n", ddi_get_name(dip));
515509Smrj 
516509Smrj 	/* Initialize rootnex event handle */
517509Smrj 	i_ddi_rootnex_init_events(dip);
518509Smrj 
51911600SVikram.Hegde@Sun.COM #if defined(__amd64) && !defined(__xpv)
5207613SVikram.Hegde@Sun.COM 	e = iommulib_nexus_register(dip, &iommulib_nexops,
5217613SVikram.Hegde@Sun.COM 	    &rootnex_state->r_iommulib_handle);
5227613SVikram.Hegde@Sun.COM 
5237613SVikram.Hegde@Sun.COM 	ASSERT(e == DDI_SUCCESS);
5247613SVikram.Hegde@Sun.COM #endif
5257613SVikram.Hegde@Sun.COM 
526509Smrj 	return (DDI_SUCCESS);
527509Smrj }
528509Smrj 
529509Smrj 
530509Smrj /*
531509Smrj  * rootnex_detach()
532509Smrj  *
533509Smrj  */
5340Sstevel@tonic-gate /*ARGSUSED*/
5350Sstevel@tonic-gate static int
rootnex_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)536509Smrj rootnex_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
537509Smrj {
538509Smrj 	switch (cmd) {
539509Smrj 	case DDI_SUSPEND:
54011600SVikram.Hegde@Sun.COM #if defined(__amd64) && !defined(__xpv)
54111600SVikram.Hegde@Sun.COM 		return (immu_quiesce());
54211600SVikram.Hegde@Sun.COM #else
54311600SVikram.Hegde@Sun.COM 		return (DDI_SUCCESS);
54411600SVikram.Hegde@Sun.COM #endif
545509Smrj 	default:
546509Smrj 		return (DDI_FAILURE);
547509Smrj 	}
54811600SVikram.Hegde@Sun.COM 	/*NOTREACHED*/
54911600SVikram.Hegde@Sun.COM 
550509Smrj }
551509Smrj 
552509Smrj 
553509Smrj /*
554509Smrj  * rootnex_dma_init()
555509Smrj  *
556509Smrj  */
557509Smrj /*ARGSUSED*/
558509Smrj static int
rootnex_dma_init()559509Smrj rootnex_dma_init()
5600Sstevel@tonic-gate {
561509Smrj 	size_t bufsize;
562509Smrj 
563509Smrj 
564509Smrj 	/*
565509Smrj 	 * size of our cookie/window/copybuf state needed in dma bind that we
566509Smrj 	 * pre-alloc in dma_alloc_handle
567509Smrj 	 */
568509Smrj 	rootnex_state->r_prealloc_cookies = rootnex_prealloc_cookies;
569509Smrj 	rootnex_state->r_prealloc_size =
570509Smrj 	    (rootnex_state->r_prealloc_cookies * sizeof (ddi_dma_cookie_t)) +
571509Smrj 	    (rootnex_prealloc_windows * sizeof (rootnex_window_t)) +
572509Smrj 	    (rootnex_prealloc_copybuf * sizeof (rootnex_pgmap_t));
573509Smrj 
574509Smrj 	/*
575509Smrj 	 * setup DDI DMA handle kmem cache, align each handle on 64 bytes,
576509Smrj 	 * allocate 16 extra bytes for struct pointer alignment
577509Smrj 	 * (p->dmai_private & dma->dp_prealloc_buffer)
578509Smrj 	 */
579509Smrj 	bufsize = sizeof (ddi_dma_impl_t) + sizeof (rootnex_dma_t) +
580509Smrj 	    rootnex_state->r_prealloc_size + 0x10;
581509Smrj 	rootnex_state->r_dmahdl_cache = kmem_cache_create("rootnex_dmahdl",
582509Smrj 	    bufsize, 64, NULL, NULL, NULL, NULL, NULL, 0);
583509Smrj 	if (rootnex_state->r_dmahdl_cache == NULL) {
584509Smrj 		return (DDI_FAILURE);
585509Smrj 	}
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	/*
5880Sstevel@tonic-gate 	 * allocate array to track which major numbers we have printed warnings
5890Sstevel@tonic-gate 	 * for.
5900Sstevel@tonic-gate 	 */
5910Sstevel@tonic-gate 	rootnex_warn_list = kmem_zalloc(devcnt * sizeof (*rootnex_warn_list),
5920Sstevel@tonic-gate 	    KM_SLEEP);
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	return (DDI_SUCCESS);
5950Sstevel@tonic-gate }
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate /*
599509Smrj  * rootnex_add_props()
600509Smrj  *
6010Sstevel@tonic-gate  */
6020Sstevel@tonic-gate static void
rootnex_add_props(dev_info_t * dip)603509Smrj rootnex_add_props(dev_info_t *dip)
6040Sstevel@tonic-gate {
605509Smrj 	rootnex_intprop_t *rpp;
6060Sstevel@tonic-gate 	int i;
607509Smrj 
608509Smrj 	/* Add static integer/boolean properties to the root node */
609509Smrj 	rpp = rootnex_intprp;
610509Smrj 	for (i = 0; i < NROOT_INTPROPS; i++) {
611509Smrj 		(void) e_ddi_prop_update_int(DDI_DEV_T_NONE, dip,
612509Smrj 		    rpp[i].prop_name, rpp[i].prop_value);
6130Sstevel@tonic-gate 	}
6140Sstevel@tonic-gate }
6150Sstevel@tonic-gate 
616509Smrj 
617509Smrj 
618509Smrj /*
619509Smrj  * *************************
620509Smrj  *  ctlops related routines
621509Smrj  * *************************
622509Smrj  */
623509Smrj 
6240Sstevel@tonic-gate /*
625509Smrj  * rootnex_ctlops()
626509Smrj  *
6270Sstevel@tonic-gate  */
628693Sgovinda /*ARGSUSED*/
629509Smrj static int
rootnex_ctlops(dev_info_t * dip,dev_info_t * rdip,ddi_ctl_enum_t ctlop,void * arg,void * result)630509Smrj rootnex_ctlops(dev_info_t *dip, dev_info_t *rdip, ddi_ctl_enum_t ctlop,
631509Smrj     void *arg, void *result)
632509Smrj {
633509Smrj 	int n, *ptr;
634509Smrj 	struct ddi_parent_private_data *pdp;
635509Smrj 
636509Smrj 	switch (ctlop) {
637509Smrj 	case DDI_CTLOPS_DMAPMAPC:
638509Smrj 		/*
639509Smrj 		 * Return 'partial' to indicate that dma mapping
640509Smrj 		 * has to be done in the main MMU.
641509Smrj 		 */
642509Smrj 		return (DDI_DMA_PARTIAL);
643509Smrj 
644509Smrj 	case DDI_CTLOPS_BTOP:
645509Smrj 		/*
646509Smrj 		 * Convert byte count input to physical page units.
647509Smrj 		 * (byte counts that are not a page-size multiple
648509Smrj 		 * are rounded down)
649509Smrj 		 */
650509Smrj 		*(ulong_t *)result = btop(*(ulong_t *)arg);
651509Smrj 		return (DDI_SUCCESS);
652509Smrj 
653509Smrj 	case DDI_CTLOPS_PTOB:
654509Smrj 		/*
655509Smrj 		 * Convert size in physical pages to bytes
656509Smrj 		 */
657509Smrj 		*(ulong_t *)result = ptob(*(ulong_t *)arg);
658509Smrj 		return (DDI_SUCCESS);
659509Smrj 
660509Smrj 	case DDI_CTLOPS_BTOPR:
661509Smrj 		/*
662509Smrj 		 * Convert byte count input to physical page units
663509Smrj 		 * (byte counts that are not a page-size multiple
664509Smrj 		 * are rounded up)
665509Smrj 		 */
666509Smrj 		*(ulong_t *)result = btopr(*(ulong_t *)arg);
667509Smrj 		return (DDI_SUCCESS);
668509Smrj 
669509Smrj 	case DDI_CTLOPS_INITCHILD:
670509Smrj 		return (impl_ddi_sunbus_initchild(arg));
671509Smrj 
672509Smrj 	case DDI_CTLOPS_UNINITCHILD:
673509Smrj 		impl_ddi_sunbus_removechild(arg);
674509Smrj 		return (DDI_SUCCESS);
675509Smrj 
676509Smrj 	case DDI_CTLOPS_REPORTDEV:
677509Smrj 		return (rootnex_ctl_reportdev(rdip));
678509Smrj 
679509Smrj 	case DDI_CTLOPS_IOMIN:
680509Smrj 		/*
681509Smrj 		 * Nothing to do here but reflect back..
682509Smrj 		 */
683509Smrj 		return (DDI_SUCCESS);
684509Smrj 
685509Smrj 	case DDI_CTLOPS_REGSIZE:
686509Smrj 	case DDI_CTLOPS_NREGS:
687509Smrj 		break;
688509Smrj 
689509Smrj 	case DDI_CTLOPS_SIDDEV:
690509Smrj 		if (ndi_dev_is_prom_node(rdip))
691509Smrj 			return (DDI_SUCCESS);
692509Smrj 		if (ndi_dev_is_persistent_node(rdip))
693509Smrj 			return (DDI_SUCCESS);
694509Smrj 		return (DDI_FAILURE);
695509Smrj 
696509Smrj 	case DDI_CTLOPS_POWER:
697509Smrj 		return ((*pm_platform_power)((power_req_t *)arg));
698509Smrj 
699693Sgovinda 	case DDI_CTLOPS_RESERVED0: /* Was DDI_CTLOPS_NINTRS, obsolete */
700509Smrj 	case DDI_CTLOPS_RESERVED1: /* Was DDI_CTLOPS_POKE_INIT, obsolete */
701509Smrj 	case DDI_CTLOPS_RESERVED2: /* Was DDI_CTLOPS_POKE_FLUSH, obsolete */
702509Smrj 	case DDI_CTLOPS_RESERVED3: /* Was DDI_CTLOPS_POKE_FINI, obsolete */
703693Sgovinda 	case DDI_CTLOPS_RESERVED4: /* Was DDI_CTLOPS_INTR_HILEVEL, obsolete */
704693Sgovinda 	case DDI_CTLOPS_RESERVED5: /* Was DDI_CTLOPS_XLATE_INTRS, obsolete */
705509Smrj 		if (!rootnex_state->r_reserved_msg_printed) {
706509Smrj 			rootnex_state->r_reserved_msg_printed = B_TRUE;
707509Smrj 			cmn_err(CE_WARN, "Failing ddi_ctlops call(s) for "
708509Smrj 			    "1 or more reserved/obsolete operations.");
709509Smrj 		}
710509Smrj 		return (DDI_FAILURE);
711509Smrj 
712509Smrj 	default:
713509Smrj 		return (DDI_FAILURE);
714509Smrj 	}
715509Smrj 	/*
716509Smrj 	 * The rest are for "hardware" properties
717509Smrj 	 */
718509Smrj 	if ((pdp = ddi_get_parent_data(rdip)) == NULL)
719509Smrj 		return (DDI_FAILURE);
720509Smrj 
721509Smrj 	if (ctlop == DDI_CTLOPS_NREGS) {
722509Smrj 		ptr = (int *)result;
723509Smrj 		*ptr = pdp->par_nreg;
724509Smrj 	} else {
725509Smrj 		off_t *size = (off_t *)result;
726509Smrj 
727509Smrj 		ptr = (int *)arg;
728509Smrj 		n = *ptr;
729509Smrj 		if (n >= pdp->par_nreg) {
730509Smrj 			return (DDI_FAILURE);
731509Smrj 		}
732509Smrj 		*size = (off_t)pdp->par_reg[n].regspec_size;
733509Smrj 	}
734509Smrj 	return (DDI_SUCCESS);
735509Smrj }
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate /*
739509Smrj  * rootnex_ctl_reportdev()
740509Smrj  *
7410Sstevel@tonic-gate  */
7420Sstevel@tonic-gate static int
rootnex_ctl_reportdev(dev_info_t * dev)743509Smrj rootnex_ctl_reportdev(dev_info_t *dev)
7440Sstevel@tonic-gate {
745509Smrj 	int i, n, len, f_len = 0;
746509Smrj 	char *buf;
747509Smrj 
748509Smrj 	buf = kmem_alloc(REPORTDEV_BUFSIZE, KM_SLEEP);
749509Smrj 	f_len += snprintf(buf, REPORTDEV_BUFSIZE,
750509Smrj 	    "%s%d at root", ddi_driver_name(dev), ddi_get_instance(dev));
751509Smrj 	len = strlen(buf);
752509Smrj 
753509Smrj 	for (i = 0; i < sparc_pd_getnreg(dev); i++) {
754509Smrj 
755509Smrj 		struct regspec *rp = sparc_pd_getreg(dev, i);
756509Smrj 
757509Smrj 		if (i == 0)
758509Smrj 			f_len += snprintf(buf + len, REPORTDEV_BUFSIZE - len,
759509Smrj 			    ": ");
760509Smrj 		else
761509Smrj 			f_len += snprintf(buf + len, REPORTDEV_BUFSIZE - len,
762509Smrj 			    " and ");
763509Smrj 		len = strlen(buf);
764509Smrj 
765509Smrj 		switch (rp->regspec_bustype) {
766509Smrj 
767509Smrj 		case BTEISA:
768509Smrj 			f_len += snprintf(buf + len, REPORTDEV_BUFSIZE - len,
769509Smrj 			    "%s 0x%x", DEVI_EISA_NEXNAME, rp->regspec_addr);
7700Sstevel@tonic-gate 			break;
771509Smrj 
772509Smrj 		case BTISA:
773509Smrj 			f_len += snprintf(buf + len, REPORTDEV_BUFSIZE - len,
774509Smrj 			    "%s 0x%x", DEVI_ISA_NEXNAME, rp->regspec_addr);
7750Sstevel@tonic-gate 			break;
776509Smrj 
777509Smrj 		default:
778509Smrj 			f_len += snprintf(buf + len, REPORTDEV_BUFSIZE - len,
779509Smrj 			    "space %x offset %x",
780509Smrj 			    rp->regspec_bustype, rp->regspec_addr);
7810Sstevel@tonic-gate 			break;
7820Sstevel@tonic-gate 		}
783509Smrj 		len = strlen(buf);
7840Sstevel@tonic-gate 	}
785509Smrj 	for (i = 0, n = sparc_pd_getnintr(dev); i < n; i++) {
786509Smrj 		int pri;
787509Smrj 
788509Smrj 		if (i != 0) {
789509Smrj 			f_len += snprintf(buf + len, REPORTDEV_BUFSIZE - len,
790509Smrj 			    ",");
791509Smrj 			len = strlen(buf);
792509Smrj 		}
793509Smrj 		pri = INT_IPL(sparc_pd_getintr(dev, i)->intrspec_pri);
794509Smrj 		f_len += snprintf(buf + len, REPORTDEV_BUFSIZE - len,
795509Smrj 		    " sparc ipl %d", pri);
796509Smrj 		len = strlen(buf);
7970Sstevel@tonic-gate 	}
798509Smrj #ifdef DEBUG
799509Smrj 	if (f_len + 1 >= REPORTDEV_BUFSIZE) {
800509Smrj 		cmn_err(CE_NOTE, "next message is truncated: "
801509Smrj 		    "printed length 1024, real length %d", f_len);
802509Smrj 	}
803509Smrj #endif /* DEBUG */
804509Smrj 	cmn_err(CE_CONT, "?%s\n", buf);
805509Smrj 	kmem_free(buf, REPORTDEV_BUFSIZE);
8060Sstevel@tonic-gate 	return (DDI_SUCCESS);
8070Sstevel@tonic-gate }
8080Sstevel@tonic-gate 
809509Smrj 
810509Smrj /*
811509Smrj  * ******************
812509Smrj  *  map related code
813509Smrj  * ******************
814509Smrj  */
815509Smrj 
816509Smrj /*
817509Smrj  * rootnex_map()
818509Smrj  *
819509Smrj  */
8200Sstevel@tonic-gate static int
rootnex_map(dev_info_t * dip,dev_info_t * rdip,ddi_map_req_t * mp,off_t offset,off_t len,caddr_t * vaddrp)821509Smrj rootnex_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp, off_t offset,
822509Smrj     off_t len, caddr_t *vaddrp)
8230Sstevel@tonic-gate {
8240Sstevel@tonic-gate 	struct regspec *rp, tmp_reg;
8250Sstevel@tonic-gate 	ddi_map_req_t mr = *mp;		/* Get private copy of request */
8260Sstevel@tonic-gate 	int error;
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 	mp = &mr;
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 	switch (mp->map_op)  {
8310Sstevel@tonic-gate 	case DDI_MO_MAP_LOCKED:
8320Sstevel@tonic-gate 	case DDI_MO_UNMAP:
8330Sstevel@tonic-gate 	case DDI_MO_MAP_HANDLE:
8340Sstevel@tonic-gate 		break;
8350Sstevel@tonic-gate 	default:
8360Sstevel@tonic-gate #ifdef	DDI_MAP_DEBUG
8370Sstevel@tonic-gate 		cmn_err(CE_WARN, "rootnex_map: unimplemented map op %d.",
8380Sstevel@tonic-gate 		    mp->map_op);
8390Sstevel@tonic-gate #endif	/* DDI_MAP_DEBUG */
8400Sstevel@tonic-gate 		return (DDI_ME_UNIMPLEMENTED);
8410Sstevel@tonic-gate 	}
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	if (mp->map_flags & DDI_MF_USER_MAPPING)  {
8440Sstevel@tonic-gate #ifdef	DDI_MAP_DEBUG
8450Sstevel@tonic-gate 		cmn_err(CE_WARN, "rootnex_map: unimplemented map type: user.");
8460Sstevel@tonic-gate #endif	/* DDI_MAP_DEBUG */
8470Sstevel@tonic-gate 		return (DDI_ME_UNIMPLEMENTED);
8480Sstevel@tonic-gate 	}
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 	/*
8510Sstevel@tonic-gate 	 * First, if given an rnumber, convert it to a regspec...
8520Sstevel@tonic-gate 	 * (Presumably, this is on behalf of a child of the root node?)
8530Sstevel@tonic-gate 	 */
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 	if (mp->map_type == DDI_MT_RNUMBER)  {
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 		int rnumber = mp->map_obj.rnumber;
8580Sstevel@tonic-gate #ifdef	DDI_MAP_DEBUG
8590Sstevel@tonic-gate 		static char *out_of_range =
8600Sstevel@tonic-gate 		    "rootnex_map: Out of range rnumber <%d>, device <%s>";
8610Sstevel@tonic-gate #endif	/* DDI_MAP_DEBUG */
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 		rp = i_ddi_rnumber_to_regspec(rdip, rnumber);
8640Sstevel@tonic-gate 		if (rp == NULL)  {
8650Sstevel@tonic-gate #ifdef	DDI_MAP_DEBUG
8660Sstevel@tonic-gate 			cmn_err(CE_WARN, out_of_range, rnumber,
8670Sstevel@tonic-gate 			    ddi_get_name(rdip));
8680Sstevel@tonic-gate #endif	/* DDI_MAP_DEBUG */
8690Sstevel@tonic-gate 			return (DDI_ME_RNUMBER_RANGE);
8700Sstevel@tonic-gate 		}
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 		/*
8730Sstevel@tonic-gate 		 * Convert the given ddi_map_req_t from rnumber to regspec...
8740Sstevel@tonic-gate 		 */
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 		mp->map_type = DDI_MT_REGSPEC;
8770Sstevel@tonic-gate 		mp->map_obj.rp = rp;
8780Sstevel@tonic-gate 	}
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 	/*
8810Sstevel@tonic-gate 	 * Adjust offset and length correspnding to called values...
8820Sstevel@tonic-gate 	 * XXX: A non-zero length means override the one in the regspec
8830Sstevel@tonic-gate 	 * XXX: (regardless of what's in the parent's range?)
8840Sstevel@tonic-gate 	 */
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 	tmp_reg = *(mp->map_obj.rp);		/* Preserve underlying data */
8870Sstevel@tonic-gate 	rp = mp->map_obj.rp = &tmp_reg;		/* Use tmp_reg in request */
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate #ifdef	DDI_MAP_DEBUG
8905084Sjohnlev 	cmn_err(CE_CONT, "rootnex: <%s,%s> <0x%x, 0x%x, 0x%d> offset %d len %d "
8915084Sjohnlev 	    "handle 0x%x\n", ddi_get_name(dip), ddi_get_name(rdip),
8925084Sjohnlev 	    rp->regspec_bustype, rp->regspec_addr, rp->regspec_size, offset,
8935084Sjohnlev 	    len, mp->map_handlep);
8940Sstevel@tonic-gate #endif	/* DDI_MAP_DEBUG */
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate 	/*
8970Sstevel@tonic-gate 	 * I/O or memory mapping:
8980Sstevel@tonic-gate 	 *
8990Sstevel@tonic-gate 	 *	<bustype=0, addr=x, len=x>: memory
9000Sstevel@tonic-gate 	 *	<bustype=1, addr=x, len=x>: i/o
9010Sstevel@tonic-gate 	 *	<bustype>1, addr=0, len=x>: x86-compatibility i/o
9020Sstevel@tonic-gate 	 */
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 	if (rp->regspec_bustype > 1 && rp->regspec_addr != 0) {
9050Sstevel@tonic-gate 		cmn_err(CE_WARN, "<%s,%s> invalid register spec"
9060Sstevel@tonic-gate 		    " <0x%x, 0x%x, 0x%x>", ddi_get_name(dip),
9070Sstevel@tonic-gate 		    ddi_get_name(rdip), rp->regspec_bustype,
9080Sstevel@tonic-gate 		    rp->regspec_addr, rp->regspec_size);
9090Sstevel@tonic-gate 		return (DDI_ME_INVAL);
9100Sstevel@tonic-gate 	}
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	if (rp->regspec_bustype > 1 && rp->regspec_addr == 0) {
9130Sstevel@tonic-gate 		/*
9140Sstevel@tonic-gate 		 * compatibility i/o mapping
9150Sstevel@tonic-gate 		 */
9160Sstevel@tonic-gate 		rp->regspec_bustype += (uint_t)offset;
9170Sstevel@tonic-gate 	} else {
9180Sstevel@tonic-gate 		/*
9190Sstevel@tonic-gate 		 * Normal memory or i/o mapping
9200Sstevel@tonic-gate 		 */
9210Sstevel@tonic-gate 		rp->regspec_addr += (uint_t)offset;
9220Sstevel@tonic-gate 	}
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 	if (len != 0)
9250Sstevel@tonic-gate 		rp->regspec_size = (uint_t)len;
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate #ifdef	DDI_MAP_DEBUG
9285084Sjohnlev 	cmn_err(CE_CONT, "             <%s,%s> <0x%x, 0x%x, 0x%d> offset %d "
9295084Sjohnlev 	    "len %d handle 0x%x\n", ddi_get_name(dip), ddi_get_name(rdip),
9305084Sjohnlev 	    rp->regspec_bustype, rp->regspec_addr, rp->regspec_size,
9315084Sjohnlev 	    offset, len, mp->map_handlep);
9320Sstevel@tonic-gate #endif	/* DDI_MAP_DEBUG */
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 	/*
9350Sstevel@tonic-gate 	 * Apply any parent ranges at this level, if applicable.
9360Sstevel@tonic-gate 	 * (This is where nexus specific regspec translation takes place.
9370Sstevel@tonic-gate 	 * Use of this function is implicit agreement that translation is
9380Sstevel@tonic-gate 	 * provided via ddi_apply_range.)
9390Sstevel@tonic-gate 	 */
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate #ifdef	DDI_MAP_DEBUG
9420Sstevel@tonic-gate 	ddi_map_debug("applying range of parent <%s> to child <%s>...\n",
9430Sstevel@tonic-gate 	    ddi_get_name(dip), ddi_get_name(rdip));
9440Sstevel@tonic-gate #endif	/* DDI_MAP_DEBUG */
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 	if ((error = i_ddi_apply_range(dip, rdip, mp->map_obj.rp)) != 0)
9470Sstevel@tonic-gate 		return (error);
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate 	switch (mp->map_op)  {
9500Sstevel@tonic-gate 	case DDI_MO_MAP_LOCKED:
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 		/*
9530Sstevel@tonic-gate 		 * Set up the locked down kernel mapping to the regspec...
9540Sstevel@tonic-gate 		 */
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 		return (rootnex_map_regspec(mp, vaddrp));
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 	case DDI_MO_UNMAP:
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 		/*
9610Sstevel@tonic-gate 		 * Release mapping...
9620Sstevel@tonic-gate 		 */
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 		return (rootnex_unmap_regspec(mp, vaddrp));
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 	case DDI_MO_MAP_HANDLE:
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 		return (rootnex_map_handle(mp));
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 	default:
9710Sstevel@tonic-gate 		return (DDI_ME_UNIMPLEMENTED);
9720Sstevel@tonic-gate 	}
9730Sstevel@tonic-gate }
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate /*
977509Smrj  * rootnex_map_fault()
9780Sstevel@tonic-gate  *
9790Sstevel@tonic-gate  *	fault in mappings for requestors
9800Sstevel@tonic-gate  */
9810Sstevel@tonic-gate /*ARGSUSED*/
9820Sstevel@tonic-gate static int
rootnex_map_fault(dev_info_t * dip,dev_info_t * rdip,struct hat * hat,struct seg * seg,caddr_t addr,struct devpage * dp,pfn_t pfn,uint_t prot,uint_t lock)983509Smrj rootnex_map_fault(dev_info_t *dip, dev_info_t *rdip, struct hat *hat,
984509Smrj     struct seg *seg, caddr_t addr, struct devpage *dp, pfn_t pfn, uint_t prot,
985509Smrj     uint_t lock)
9860Sstevel@tonic-gate {
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate #ifdef	DDI_MAP_DEBUG
9890Sstevel@tonic-gate 	ddi_map_debug("rootnex_map_fault: address <%x> pfn <%x>", addr, pfn);
9900Sstevel@tonic-gate 	ddi_map_debug(" Seg <%s>\n",
9910Sstevel@tonic-gate 	    seg->s_ops == &segdev_ops ? "segdev" :
9920Sstevel@tonic-gate 	    seg == &kvseg ? "segkmem" : "NONE!");
9930Sstevel@tonic-gate #endif	/* DDI_MAP_DEBUG */
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate 	/*
9960Sstevel@tonic-gate 	 * This is all terribly broken, but it is a start
9970Sstevel@tonic-gate 	 *
9980Sstevel@tonic-gate 	 * XXX	Note that this test means that segdev_ops
9990Sstevel@tonic-gate 	 *	must be exported from seg_dev.c.
10000Sstevel@tonic-gate 	 * XXX	What about devices with their own segment drivers?
10010Sstevel@tonic-gate 	 */
10020Sstevel@tonic-gate 	if (seg->s_ops == &segdev_ops) {
10035084Sjohnlev 		struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 		if (hat == NULL) {
10060Sstevel@tonic-gate 			/*
10070Sstevel@tonic-gate 			 * This is one plausible interpretation of
10080Sstevel@tonic-gate 			 * a null hat i.e. use the first hat on the
10090Sstevel@tonic-gate 			 * address space hat list which by convention is
10100Sstevel@tonic-gate 			 * the hat of the system MMU.  At alternative
10110Sstevel@tonic-gate 			 * would be to panic .. this might well be better ..
10120Sstevel@tonic-gate 			 */
10130Sstevel@tonic-gate 			ASSERT(AS_READ_HELD(seg->s_as, &seg->s_as->a_lock));
10140Sstevel@tonic-gate 			hat = seg->s_as->a_hat;
10150Sstevel@tonic-gate 			cmn_err(CE_NOTE, "rootnex_map_fault: nil hat");
10160Sstevel@tonic-gate 		}
10170Sstevel@tonic-gate 		hat_devload(hat, addr, MMU_PAGESIZE, pfn, prot | sdp->hat_attr,
10180Sstevel@tonic-gate 		    (lock ? HAT_LOAD_LOCK : HAT_LOAD));
10190Sstevel@tonic-gate 	} else if (seg == &kvseg && dp == NULL) {
10200Sstevel@tonic-gate 		hat_devload(kas.a_hat, addr, MMU_PAGESIZE, pfn, prot,
10210Sstevel@tonic-gate 		    HAT_LOAD_LOCK);
10220Sstevel@tonic-gate 	} else
10230Sstevel@tonic-gate 		return (DDI_FAILURE);
10240Sstevel@tonic-gate 	return (DDI_SUCCESS);
10250Sstevel@tonic-gate }
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate /*
1029509Smrj  * rootnex_map_regspec()
1030509Smrj  *     we don't support mapping of I/O cards above 4Gb
10310Sstevel@tonic-gate  */
1032509Smrj static int
rootnex_map_regspec(ddi_map_req_t * mp,caddr_t * vaddrp)1033509Smrj rootnex_map_regspec(ddi_map_req_t *mp, caddr_t *vaddrp)
1034509Smrj {
10355084Sjohnlev 	rootnex_addr_t rbase;
1036509Smrj 	void *cvaddr;
1037509Smrj 	uint_t npages, pgoffset;
1038509Smrj 	struct regspec *rp;
1039509Smrj 	ddi_acc_hdl_t *hp;
1040509Smrj 	ddi_acc_impl_t *ap;
1041509Smrj 	uint_t	hat_acc_flags;
10425084Sjohnlev 	paddr_t pbase;
1043509Smrj 
1044509Smrj 	rp = mp->map_obj.rp;
1045509Smrj 	hp = mp->map_handlep;
1046509Smrj 
1047509Smrj #ifdef	DDI_MAP_DEBUG
1048509Smrj 	ddi_map_debug(
1049509Smrj 	    "rootnex_map_regspec: <0x%x 0x%x 0x%x> handle 0x%x\n",
1050509Smrj 	    rp->regspec_bustype, rp->regspec_addr,
1051509Smrj 	    rp->regspec_size, mp->map_handlep);
1052509Smrj #endif	/* DDI_MAP_DEBUG */
1053509Smrj 
1054509Smrj 	/*
1055509Smrj 	 * I/O or memory mapping
1056509Smrj 	 *
1057509Smrj 	 *	<bustype=0, addr=x, len=x>: memory
1058509Smrj 	 *	<bustype=1, addr=x, len=x>: i/o
1059509Smrj 	 *	<bustype>1, addr=0, len=x>: x86-compatibility i/o
1060509Smrj 	 */
1061509Smrj 
1062509Smrj 	if (rp->regspec_bustype > 1 && rp->regspec_addr != 0) {
1063509Smrj 		cmn_err(CE_WARN, "rootnex: invalid register spec"
1064509Smrj 		    " <0x%x, 0x%x, 0x%x>", rp->regspec_bustype,
1065509Smrj 		    rp->regspec_addr, rp->regspec_size);
1066509Smrj 		return (DDI_FAILURE);
1067509Smrj 	}
1068509Smrj 
1069509Smrj 	if (rp->regspec_bustype != 0) {
1070509Smrj 		/*
1071509Smrj 		 * I/O space - needs a handle.
1072509Smrj 		 */
1073509Smrj 		if (hp == NULL) {
1074509Smrj 			return (DDI_FAILURE);
1075509Smrj 		}
1076509Smrj 		ap = (ddi_acc_impl_t *)hp->ah_platform_private;
1077509Smrj 		ap->ahi_acc_attr |= DDI_ACCATTR_IO_SPACE;
1078509Smrj 		impl_acc_hdl_init(hp);
1079509Smrj 
1080509Smrj 		if (mp->map_flags & DDI_MF_DEVICE_MAPPING) {
1081509Smrj #ifdef  DDI_MAP_DEBUG
10825084Sjohnlev 			ddi_map_debug("rootnex_map_regspec: mmap() "
10835084Sjohnlev 			    "to I/O space is not supported.\n");
1084509Smrj #endif  /* DDI_MAP_DEBUG */
1085509Smrj 			return (DDI_ME_INVAL);
1086509Smrj 		} else {
1087509Smrj 			/*
1088509Smrj 			 * 1275-compliant vs. compatibility i/o mapping
1089509Smrj 			 */
1090509Smrj 			*vaddrp =
1091509Smrj 			    (rp->regspec_bustype > 1 && rp->regspec_addr == 0) ?
10925084Sjohnlev 			    ((caddr_t)(uintptr_t)rp->regspec_bustype) :
10935084Sjohnlev 			    ((caddr_t)(uintptr_t)rp->regspec_addr);
10945084Sjohnlev #ifdef __xpv
10955084Sjohnlev 			if (DOMAIN_IS_INITDOMAIN(xen_info)) {
10965084Sjohnlev 				hp->ah_pfn = xen_assign_pfn(
10975084Sjohnlev 				    mmu_btop((ulong_t)rp->regspec_addr &
10985084Sjohnlev 				    MMU_PAGEMASK));
10995084Sjohnlev 			} else {
11005084Sjohnlev 				hp->ah_pfn = mmu_btop(
11015084Sjohnlev 				    (ulong_t)rp->regspec_addr & MMU_PAGEMASK);
11025084Sjohnlev 			}
11035084Sjohnlev #else
11041865Sdilpreet 			hp->ah_pfn = mmu_btop((ulong_t)rp->regspec_addr &
11055084Sjohnlev 			    MMU_PAGEMASK);
11065084Sjohnlev #endif
11071865Sdilpreet 			hp->ah_pnum = mmu_btopr(rp->regspec_size +
11081865Sdilpreet 			    (ulong_t)rp->regspec_addr & MMU_PAGEOFFSET);
1109509Smrj 		}
1110509Smrj 
1111509Smrj #ifdef	DDI_MAP_DEBUG
1112509Smrj 		ddi_map_debug(
1113509Smrj 	    "rootnex_map_regspec: \"Mapping\" %d bytes I/O space at 0x%x\n",
1114509Smrj 		    rp->regspec_size, *vaddrp);
1115509Smrj #endif	/* DDI_MAP_DEBUG */
1116509Smrj 		return (DDI_SUCCESS);
1117509Smrj 	}
1118509Smrj 
1119509Smrj 	/*
1120509Smrj 	 * Memory space
1121509Smrj 	 */
1122509Smrj 
1123509Smrj 	if (hp != NULL) {
1124509Smrj 		/*
1125509Smrj 		 * hat layer ignores
1126509Smrj 		 * hp->ah_acc.devacc_attr_endian_flags.
1127509Smrj 		 */
1128509Smrj 		switch (hp->ah_acc.devacc_attr_dataorder) {
1129509Smrj 		case DDI_STRICTORDER_ACC:
1130509Smrj 			hat_acc_flags = HAT_STRICTORDER;
1131509Smrj 			break;
1132509Smrj 		case DDI_UNORDERED_OK_ACC:
1133509Smrj 			hat_acc_flags = HAT_UNORDERED_OK;
1134509Smrj 			break;
1135509Smrj 		case DDI_MERGING_OK_ACC:
1136509Smrj 			hat_acc_flags = HAT_MERGING_OK;
1137509Smrj 			break;
1138509Smrj 		case DDI_LOADCACHING_OK_ACC:
1139509Smrj 			hat_acc_flags = HAT_LOADCACHING_OK;
1140509Smrj 			break;
1141509Smrj 		case DDI_STORECACHING_OK_ACC:
1142509Smrj 			hat_acc_flags = HAT_STORECACHING_OK;
1143509Smrj 			break;
1144509Smrj 		}
1145509Smrj 		ap = (ddi_acc_impl_t *)hp->ah_platform_private;
1146509Smrj 		ap->ahi_acc_attr |= DDI_ACCATTR_CPU_VADDR;
1147509Smrj 		impl_acc_hdl_init(hp);
1148509Smrj 		hp->ah_hat_flags = hat_acc_flags;
1149509Smrj 	} else {
1150509Smrj 		hat_acc_flags = HAT_STRICTORDER;
1151509Smrj 	}
1152509Smrj 
11535084Sjohnlev 	rbase = (rootnex_addr_t)(rp->regspec_addr & MMU_PAGEMASK);
11545084Sjohnlev #ifdef __xpv
11555084Sjohnlev 	/*
11565084Sjohnlev 	 * If we're dom0, we're using a real device so we need to translate
11575084Sjohnlev 	 * the MA to a PA.
11585084Sjohnlev 	 */
11595084Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
11605084Sjohnlev 		pbase = pfn_to_pa(xen_assign_pfn(mmu_btop(rbase)));
11615084Sjohnlev 	} else {
11625084Sjohnlev 		pbase = rbase;
11635084Sjohnlev 	}
11645084Sjohnlev #else
11655084Sjohnlev 	pbase = rbase;
11665084Sjohnlev #endif
11675084Sjohnlev 	pgoffset = (ulong_t)rp->regspec_addr & MMU_PAGEOFFSET;
1168509Smrj 
1169509Smrj 	if (rp->regspec_size == 0) {
1170509Smrj #ifdef  DDI_MAP_DEBUG
1171509Smrj 		ddi_map_debug("rootnex_map_regspec: zero regspec_size\n");
1172509Smrj #endif  /* DDI_MAP_DEBUG */
1173509Smrj 		return (DDI_ME_INVAL);
1174509Smrj 	}
1175509Smrj 
1176509Smrj 	if (mp->map_flags & DDI_MF_DEVICE_MAPPING) {
11775084Sjohnlev 		/* extra cast to make gcc happy */
11785084Sjohnlev 		*vaddrp = (caddr_t)((uintptr_t)mmu_btop(pbase));
1179509Smrj 	} else {
1180509Smrj 		npages = mmu_btopr(rp->regspec_size + pgoffset);
1181509Smrj 
1182509Smrj #ifdef	DDI_MAP_DEBUG
11835084Sjohnlev 		ddi_map_debug("rootnex_map_regspec: Mapping %d pages "
11845084Sjohnlev 		    "physical %llx", npages, pbase);
1185509Smrj #endif	/* DDI_MAP_DEBUG */
1186509Smrj 
1187509Smrj 		cvaddr = device_arena_alloc(ptob(npages), VM_NOSLEEP);
1188509Smrj 		if (cvaddr == NULL)
1189509Smrj 			return (DDI_ME_NORESOURCES);
1190509Smrj 
1191509Smrj 		/*
1192509Smrj 		 * Now map in the pages we've allocated...
1193509Smrj 		 */
11945084Sjohnlev 		hat_devload(kas.a_hat, cvaddr, mmu_ptob(npages),
11955084Sjohnlev 		    mmu_btop(pbase), mp->map_prot | hat_acc_flags,
11965084Sjohnlev 		    HAT_LOAD_LOCK);
1197509Smrj 		*vaddrp = (caddr_t)cvaddr + pgoffset;
11981865Sdilpreet 
11991865Sdilpreet 		/* save away pfn and npages for FMA */
12001865Sdilpreet 		hp = mp->map_handlep;
12011865Sdilpreet 		if (hp) {
12025084Sjohnlev 			hp->ah_pfn = mmu_btop(pbase);
12031865Sdilpreet 			hp->ah_pnum = npages;
12041865Sdilpreet 		}
1205509Smrj 	}
1206509Smrj 
1207509Smrj #ifdef	DDI_MAP_DEBUG
1208509Smrj 	ddi_map_debug("at virtual 0x%x\n", *vaddrp);
1209509Smrj #endif	/* DDI_MAP_DEBUG */
1210509Smrj 	return (DDI_SUCCESS);
1211509Smrj }
1212509Smrj 
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate /*
1215509Smrj  * rootnex_unmap_regspec()
1216509Smrj  *
1217509Smrj  */
1218509Smrj static int
rootnex_unmap_regspec(ddi_map_req_t * mp,caddr_t * vaddrp)1219509Smrj rootnex_unmap_regspec(ddi_map_req_t *mp, caddr_t *vaddrp)
1220509Smrj {
1221509Smrj 	caddr_t addr = (caddr_t)*vaddrp;
1222509Smrj 	uint_t npages, pgoffset;
1223509Smrj 	struct regspec *rp;
1224509Smrj 
1225509Smrj 	if (mp->map_flags & DDI_MF_DEVICE_MAPPING)
1226509Smrj 		return (0);
1227509Smrj 
1228509Smrj 	rp = mp->map_obj.rp;
1229509Smrj 
1230509Smrj 	if (rp->regspec_size == 0) {
1231509Smrj #ifdef  DDI_MAP_DEBUG
1232509Smrj 		ddi_map_debug("rootnex_unmap_regspec: zero regspec_size\n");
1233509Smrj #endif  /* DDI_MAP_DEBUG */
1234509Smrj 		return (DDI_ME_INVAL);
1235509Smrj 	}
1236509Smrj 
1237509Smrj 	/*
1238509Smrj 	 * I/O or memory mapping:
1239509Smrj 	 *
1240509Smrj 	 *	<bustype=0, addr=x, len=x>: memory
1241509Smrj 	 *	<bustype=1, addr=x, len=x>: i/o
1242509Smrj 	 *	<bustype>1, addr=0, len=x>: x86-compatibility i/o
1243509Smrj 	 */
1244509Smrj 	if (rp->regspec_bustype != 0) {
1245509Smrj 		/*
1246509Smrj 		 * This is I/O space, which requires no particular
1247509Smrj 		 * processing on unmap since it isn't mapped in the
1248509Smrj 		 * first place.
1249509Smrj 		 */
1250509Smrj 		return (DDI_SUCCESS);
1251509Smrj 	}
1252509Smrj 
1253509Smrj 	/*
1254509Smrj 	 * Memory space
1255509Smrj 	 */
1256509Smrj 	pgoffset = (uintptr_t)addr & MMU_PAGEOFFSET;
1257509Smrj 	npages = mmu_btopr(rp->regspec_size + pgoffset);
1258509Smrj 	hat_unload(kas.a_hat, addr - pgoffset, ptob(npages), HAT_UNLOAD_UNLOCK);
1259509Smrj 	device_arena_free(addr - pgoffset, ptob(npages));
1260509Smrj 
1261509Smrj 	/*
1262509Smrj 	 * Destroy the pointer - the mapping has logically gone
1263509Smrj 	 */
1264509Smrj 	*vaddrp = NULL;
1265509Smrj 
1266509Smrj 	return (DDI_SUCCESS);
1267509Smrj }
1268509Smrj 
1269509Smrj 
1270509Smrj /*
1271509Smrj  * rootnex_map_handle()
1272509Smrj  *
12730Sstevel@tonic-gate  */
1274509Smrj static int
rootnex_map_handle(ddi_map_req_t * mp)1275509Smrj rootnex_map_handle(ddi_map_req_t *mp)
1276509Smrj {
12775084Sjohnlev 	rootnex_addr_t rbase;
1278509Smrj 	ddi_acc_hdl_t *hp;
1279509Smrj 	uint_t pgoffset;
1280509Smrj 	struct regspec *rp;
12815084Sjohnlev 	paddr_t pbase;
1282509Smrj 
1283509Smrj 	rp = mp->map_obj.rp;
1284509Smrj 
1285509Smrj #ifdef	DDI_MAP_DEBUG
1286509Smrj 	ddi_map_debug(
1287509Smrj 	    "rootnex_map_handle: <0x%x 0x%x 0x%x> handle 0x%x\n",
1288509Smrj 	    rp->regspec_bustype, rp->regspec_addr,
1289509Smrj 	    rp->regspec_size, mp->map_handlep);
1290509Smrj #endif	/* DDI_MAP_DEBUG */
1291509Smrj 
1292509Smrj 	/*
1293509Smrj 	 * I/O or memory mapping:
1294509Smrj 	 *
1295509Smrj 	 *	<bustype=0, addr=x, len=x>: memory
1296509Smrj 	 *	<bustype=1, addr=x, len=x>: i/o
1297509Smrj 	 *	<bustype>1, addr=0, len=x>: x86-compatibility i/o
1298509Smrj 	 */
1299509Smrj 	if (rp->regspec_bustype != 0) {
1300509Smrj 		/*
1301509Smrj 		 * This refers to I/O space, and we don't support "mapping"
1302509Smrj 		 * I/O space to a user.
1303509Smrj 		 */
1304509Smrj 		return (DDI_FAILURE);
1305509Smrj 	}
1306509Smrj 
1307509Smrj 	/*
1308509Smrj 	 * Set up the hat_flags for the mapping.
1309509Smrj 	 */
1310509Smrj 	hp = mp->map_handlep;
1311509Smrj 
1312509Smrj 	switch (hp->ah_acc.devacc_attr_endian_flags) {
1313509Smrj 	case DDI_NEVERSWAP_ACC:
1314509Smrj 		hp->ah_hat_flags = HAT_NEVERSWAP | HAT_STRICTORDER;
1315509Smrj 		break;
1316509Smrj 	case DDI_STRUCTURE_LE_ACC:
1317509Smrj 		hp->ah_hat_flags = HAT_STRUCTURE_LE;
1318509Smrj 		break;
1319509Smrj 	case DDI_STRUCTURE_BE_ACC:
1320509Smrj 		return (DDI_FAILURE);
1321509Smrj 	default:
1322509Smrj 		return (DDI_REGS_ACC_CONFLICT);
1323509Smrj 	}
1324509Smrj 
1325509Smrj 	switch (hp->ah_acc.devacc_attr_dataorder) {
1326509Smrj 	case DDI_STRICTORDER_ACC:
1327509Smrj 		break;
1328509Smrj 	case DDI_UNORDERED_OK_ACC:
1329509Smrj 		hp->ah_hat_flags |= HAT_UNORDERED_OK;
1330509Smrj 		break;
1331509Smrj 	case DDI_MERGING_OK_ACC:
1332509Smrj 		hp->ah_hat_flags |= HAT_MERGING_OK;
1333509Smrj 		break;
1334509Smrj 	case DDI_LOADCACHING_OK_ACC:
1335509Smrj 		hp->ah_hat_flags |= HAT_LOADCACHING_OK;
1336509Smrj 		break;
1337509Smrj 	case DDI_STORECACHING_OK_ACC:
1338509Smrj 		hp->ah_hat_flags |= HAT_STORECACHING_OK;
1339509Smrj 		break;
1340509Smrj 	default:
1341509Smrj 		return (DDI_FAILURE);
1342509Smrj 	}
1343509Smrj 
13445084Sjohnlev 	rbase = (rootnex_addr_t)rp->regspec_addr &
13455084Sjohnlev 	    (~(rootnex_addr_t)MMU_PAGEOFFSET);
13465084Sjohnlev 	pgoffset = (ulong_t)rp->regspec_addr & MMU_PAGEOFFSET;
1347509Smrj 
1348509Smrj 	if (rp->regspec_size == 0)
1349509Smrj 		return (DDI_ME_INVAL);
1350509Smrj 
13515084Sjohnlev #ifdef __xpv
13525084Sjohnlev 	/*
13535084Sjohnlev 	 * If we're dom0, we're using a real device so we need to translate
13545084Sjohnlev 	 * the MA to a PA.
13555084Sjohnlev 	 */
13565084Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
13575084Sjohnlev 		pbase = pfn_to_pa(xen_assign_pfn(mmu_btop(rbase))) |
13585084Sjohnlev 		    (rbase & MMU_PAGEOFFSET);
13595084Sjohnlev 	} else {
13605084Sjohnlev 		pbase = rbase;
13615084Sjohnlev 	}
13625084Sjohnlev #else
13635084Sjohnlev 	pbase = rbase;
13645084Sjohnlev #endif
13655084Sjohnlev 
13665084Sjohnlev 	hp->ah_pfn = mmu_btop(pbase);
1367509Smrj 	hp->ah_pnum = mmu_btopr(rp->regspec_size + pgoffset);
1368509Smrj 
1369509Smrj 	return (DDI_SUCCESS);
1370509Smrj }
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate 
13730Sstevel@tonic-gate 
13740Sstevel@tonic-gate /*
1375509Smrj  * ************************
1376509Smrj  *  interrupt related code
1377509Smrj  * ************************
13780Sstevel@tonic-gate  */
13790Sstevel@tonic-gate 
13800Sstevel@tonic-gate /*
1381509Smrj  * rootnex_intr_ops()
13820Sstevel@tonic-gate  *	bus_intr_op() function for interrupt support
13830Sstevel@tonic-gate  */
13840Sstevel@tonic-gate /* ARGSUSED */
13850Sstevel@tonic-gate static int
rootnex_intr_ops(dev_info_t * pdip,dev_info_t * rdip,ddi_intr_op_t intr_op,ddi_intr_handle_impl_t * hdlp,void * result)13860Sstevel@tonic-gate rootnex_intr_ops(dev_info_t *pdip, dev_info_t *rdip, ddi_intr_op_t intr_op,
13870Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, void *result)
13880Sstevel@tonic-gate {
13890Sstevel@tonic-gate 	struct intrspec			*ispec;
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate 	DDI_INTR_NEXDBG((CE_CONT,
13920Sstevel@tonic-gate 	    "rootnex_intr_ops: pdip = %p, rdip = %p, intr_op = %x, hdlp = %p\n",
13930Sstevel@tonic-gate 	    (void *)pdip, (void *)rdip, intr_op, (void *)hdlp));
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate 	/* Process the interrupt operation */
13960Sstevel@tonic-gate 	switch (intr_op) {
13970Sstevel@tonic-gate 	case DDI_INTROP_GETCAP:
13980Sstevel@tonic-gate 		/* First check with pcplusmp */
13990Sstevel@tonic-gate 		if (psm_intr_ops == NULL)
14000Sstevel@tonic-gate 			return (DDI_FAILURE);
14010Sstevel@tonic-gate 
14020Sstevel@tonic-gate 		if ((*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_GET_CAP, result)) {
14030Sstevel@tonic-gate 			*(int *)result = 0;
14040Sstevel@tonic-gate 			return (DDI_FAILURE);
14050Sstevel@tonic-gate 		}
14060Sstevel@tonic-gate 		break;
14070Sstevel@tonic-gate 	case DDI_INTROP_SETCAP:
14080Sstevel@tonic-gate 		if (psm_intr_ops == NULL)
14090Sstevel@tonic-gate 			return (DDI_FAILURE);
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate 		if ((*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_SET_CAP, result))
14120Sstevel@tonic-gate 			return (DDI_FAILURE);
14130Sstevel@tonic-gate 		break;
14140Sstevel@tonic-gate 	case DDI_INTROP_ALLOC:
141512683SJimmy.Vetayases@oracle.com 		ASSERT(hdlp->ih_type == DDI_INTR_TYPE_FIXED);
141612683SJimmy.Vetayases@oracle.com 		return (rootnex_alloc_intr_fixed(rdip, hdlp, result));
14170Sstevel@tonic-gate 	case DDI_INTROP_FREE:
141812683SJimmy.Vetayases@oracle.com 		ASSERT(hdlp->ih_type == DDI_INTR_TYPE_FIXED);
141912683SJimmy.Vetayases@oracle.com 		return (rootnex_free_intr_fixed(rdip, hdlp));
14200Sstevel@tonic-gate 	case DDI_INTROP_GETPRI:
14210Sstevel@tonic-gate 		if ((ispec = rootnex_get_ispec(rdip, hdlp->ih_inum)) == NULL)
14220Sstevel@tonic-gate 			return (DDI_FAILURE);
14230Sstevel@tonic-gate 		*(int *)result = ispec->intrspec_pri;
14240Sstevel@tonic-gate 		break;
14250Sstevel@tonic-gate 	case DDI_INTROP_SETPRI:
14260Sstevel@tonic-gate 		/* Validate the interrupt priority passed to us */
14270Sstevel@tonic-gate 		if (*(int *)result > LOCK_LEVEL)
14280Sstevel@tonic-gate 			return (DDI_FAILURE);
14290Sstevel@tonic-gate 
14300Sstevel@tonic-gate 		/* Ensure that PSM is all initialized and ispec is ok */
14310Sstevel@tonic-gate 		if ((psm_intr_ops == NULL) ||
14320Sstevel@tonic-gate 		    ((ispec = rootnex_get_ispec(rdip, hdlp->ih_inum)) == NULL))
14330Sstevel@tonic-gate 			return (DDI_FAILURE);
14340Sstevel@tonic-gate 
14350Sstevel@tonic-gate 		/* Change the priority */
14360Sstevel@tonic-gate 		if ((*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_SET_PRI, result) ==
14370Sstevel@tonic-gate 		    PSM_FAILURE)
14380Sstevel@tonic-gate 			return (DDI_FAILURE);
14390Sstevel@tonic-gate 
14400Sstevel@tonic-gate 		/* update the ispec with the new priority */
14410Sstevel@tonic-gate 		ispec->intrspec_pri =  *(int *)result;
14420Sstevel@tonic-gate 		break;
14430Sstevel@tonic-gate 	case DDI_INTROP_ADDISR:
14440Sstevel@tonic-gate 		if ((ispec = rootnex_get_ispec(rdip, hdlp->ih_inum)) == NULL)
14450Sstevel@tonic-gate 			return (DDI_FAILURE);
14460Sstevel@tonic-gate 		ispec->intrspec_func = hdlp->ih_cb_func;
14470Sstevel@tonic-gate 		break;
14480Sstevel@tonic-gate 	case DDI_INTROP_REMISR:
14490Sstevel@tonic-gate 		if ((ispec = rootnex_get_ispec(rdip, hdlp->ih_inum)) == NULL)
14500Sstevel@tonic-gate 			return (DDI_FAILURE);
14510Sstevel@tonic-gate 		ispec->intrspec_func = (uint_t (*)()) 0;
14520Sstevel@tonic-gate 		break;
14530Sstevel@tonic-gate 	case DDI_INTROP_ENABLE:
14540Sstevel@tonic-gate 		if ((ispec = rootnex_get_ispec(rdip, hdlp->ih_inum)) == NULL)
14550Sstevel@tonic-gate 			return (DDI_FAILURE);
14560Sstevel@tonic-gate 
14570Sstevel@tonic-gate 		/* Call psmi to translate irq with the dip */
14580Sstevel@tonic-gate 		if (psm_intr_ops == NULL)
14590Sstevel@tonic-gate 			return (DDI_FAILURE);
14600Sstevel@tonic-gate 
1461916Sschwartz 		((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp = ispec;
146211465SKerry.Shu@Sun.COM 		if ((*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_XLATE_VECTOR,
146311465SKerry.Shu@Sun.COM 		    (int *)&hdlp->ih_vector) == PSM_FAILURE)
146411465SKerry.Shu@Sun.COM 			return (DDI_FAILURE);
14650Sstevel@tonic-gate 
14660Sstevel@tonic-gate 		/* Add the interrupt handler */
14670Sstevel@tonic-gate 		if (!add_avintr((void *)hdlp, ispec->intrspec_pri,
14680Sstevel@tonic-gate 		    hdlp->ih_cb_func, DEVI(rdip)->devi_name, hdlp->ih_vector,
1469916Sschwartz 		    hdlp->ih_cb_arg1, hdlp->ih_cb_arg2, NULL, rdip))
14700Sstevel@tonic-gate 			return (DDI_FAILURE);
14710Sstevel@tonic-gate 		break;
14720Sstevel@tonic-gate 	case DDI_INTROP_DISABLE:
14730Sstevel@tonic-gate 		if ((ispec = rootnex_get_ispec(rdip, hdlp->ih_inum)) == NULL)
14740Sstevel@tonic-gate 			return (DDI_FAILURE);
14750Sstevel@tonic-gate 
14760Sstevel@tonic-gate 		/* Call psm_ops() to translate irq with the dip */
14770Sstevel@tonic-gate 		if (psm_intr_ops == NULL)
14780Sstevel@tonic-gate 			return (DDI_FAILURE);
14790Sstevel@tonic-gate 
1480916Sschwartz 		((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp = ispec;
14810Sstevel@tonic-gate 		(void) (*psm_intr_ops)(rdip, hdlp,
14820Sstevel@tonic-gate 		    PSM_INTR_OP_XLATE_VECTOR, (int *)&hdlp->ih_vector);
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate 		/* Remove the interrupt handler */
14850Sstevel@tonic-gate 		rem_avintr((void *)hdlp, ispec->intrspec_pri,
14860Sstevel@tonic-gate 		    hdlp->ih_cb_func, hdlp->ih_vector);
14870Sstevel@tonic-gate 		break;
14880Sstevel@tonic-gate 	case DDI_INTROP_SETMASK:
14890Sstevel@tonic-gate 		if (psm_intr_ops == NULL)
14900Sstevel@tonic-gate 			return (DDI_FAILURE);
14910Sstevel@tonic-gate 
14920Sstevel@tonic-gate 		if ((*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_SET_MASK, NULL))
14930Sstevel@tonic-gate 			return (DDI_FAILURE);
14940Sstevel@tonic-gate 		break;
14950Sstevel@tonic-gate 	case DDI_INTROP_CLRMASK:
14960Sstevel@tonic-gate 		if (psm_intr_ops == NULL)
14970Sstevel@tonic-gate 			return (DDI_FAILURE);
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate 		if ((*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_CLEAR_MASK, NULL))
15000Sstevel@tonic-gate 			return (DDI_FAILURE);
15010Sstevel@tonic-gate 		break;
15020Sstevel@tonic-gate 	case DDI_INTROP_GETPENDING:
15030Sstevel@tonic-gate 		if (psm_intr_ops == NULL)
15040Sstevel@tonic-gate 			return (DDI_FAILURE);
15050Sstevel@tonic-gate 
15060Sstevel@tonic-gate 		if ((*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_GET_PENDING,
15070Sstevel@tonic-gate 		    result)) {
15080Sstevel@tonic-gate 			*(int *)result = 0;
15090Sstevel@tonic-gate 			return (DDI_FAILURE);
15100Sstevel@tonic-gate 		}
15110Sstevel@tonic-gate 		break;
15122580Sanish 	case DDI_INTROP_NAVAIL:
15130Sstevel@tonic-gate 	case DDI_INTROP_NINTRS:
15142580Sanish 		*(int *)result = i_ddi_get_intx_nintrs(rdip);
15152580Sanish 		if (*(int *)result == 0) {
15160Sstevel@tonic-gate 			/*
15170Sstevel@tonic-gate 			 * Special case for 'pcic' driver' only. This driver
15180Sstevel@tonic-gate 			 * driver is a child of 'isa' and 'rootnex' drivers.
15190Sstevel@tonic-gate 			 *
15200Sstevel@tonic-gate 			 * See detailed comments on this in the function
15210Sstevel@tonic-gate 			 * rootnex_get_ispec().
15220Sstevel@tonic-gate 			 *
15230Sstevel@tonic-gate 			 * Children of 'pcic' send 'NINITR' request all the
15240Sstevel@tonic-gate 			 * way to rootnex driver. But, the 'pdp->par_nintr'
15250Sstevel@tonic-gate 			 * field may not initialized. So, we fake it here
15260Sstevel@tonic-gate 			 * to return 1 (a la what PCMCIA nexus does).
15270Sstevel@tonic-gate 			 */
15280Sstevel@tonic-gate 			if (strcmp(ddi_get_name(rdip), "pcic") == 0)
15290Sstevel@tonic-gate 				*(int *)result = 1;
15302580Sanish 			else
15312580Sanish 				return (DDI_FAILURE);
15320Sstevel@tonic-gate 		}
15330Sstevel@tonic-gate 		break;
15340Sstevel@tonic-gate 	case DDI_INTROP_SUPPORTED_TYPES:
15352580Sanish 		*(int *)result = DDI_INTR_TYPE_FIXED;	/* Always ... */
15360Sstevel@tonic-gate 		break;
15370Sstevel@tonic-gate 	default:
15380Sstevel@tonic-gate 		return (DDI_FAILURE);
15390Sstevel@tonic-gate 	}
15400Sstevel@tonic-gate 
15410Sstevel@tonic-gate 	return (DDI_SUCCESS);
15420Sstevel@tonic-gate }
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate 
15450Sstevel@tonic-gate /*
1546509Smrj  * rootnex_get_ispec()
1547509Smrj  *	convert an interrupt number to an interrupt specification.
1548509Smrj  *	The interrupt number determines which interrupt spec will be
1549509Smrj  *	returned if more than one exists.
1550509Smrj  *
1551509Smrj  *	Look into the parent private data area of the 'rdip' to find out
1552509Smrj  *	the interrupt specification.  First check to make sure there is
1553509Smrj  *	one that matchs "inumber" and then return a pointer to it.
1554509Smrj  *
1555509Smrj  *	Return NULL if one could not be found.
1556509Smrj  *
1557509Smrj  *	NOTE: This is needed for rootnex_intr_ops()
1558509Smrj  */
1559509Smrj static struct intrspec *
rootnex_get_ispec(dev_info_t * rdip,int inum)1560509Smrj rootnex_get_ispec(dev_info_t *rdip, int inum)
1561509Smrj {
1562509Smrj 	struct ddi_parent_private_data *pdp = ddi_get_parent_data(rdip);
1563509Smrj 
1564509Smrj 	/*
1565509Smrj 	 * Special case handling for drivers that provide their own
1566509Smrj 	 * intrspec structures instead of relying on the DDI framework.
1567509Smrj 	 *
1568509Smrj 	 * A broken hardware driver in ON could potentially provide its
1569509Smrj 	 * own intrspec structure, instead of relying on the hardware.
1570509Smrj 	 * If these drivers are children of 'rootnex' then we need to
1571509Smrj 	 * continue to provide backward compatibility to them here.
1572509Smrj 	 *
1573509Smrj 	 * Following check is a special case for 'pcic' driver which
1574509Smrj 	 * was found to have broken hardwre andby provides its own intrspec.
1575509Smrj 	 *
1576509Smrj 	 * Verbatim comments from this driver are shown here:
1577509Smrj 	 * "Don't use the ddi_add_intr since we don't have a
1578509Smrj 	 * default intrspec in all cases."
1579509Smrj 	 *
1580509Smrj 	 * Since an 'ispec' may not be always created for it,
1581509Smrj 	 * check for that and create one if so.
1582509Smrj 	 *
1583509Smrj 	 * NOTE: Currently 'pcic' is the only driver found to do this.
1584509Smrj 	 */
1585509Smrj 	if (!pdp->par_intr && strcmp(ddi_get_name(rdip), "pcic") == 0) {
1586509Smrj 		pdp->par_nintr = 1;
1587509Smrj 		pdp->par_intr = kmem_zalloc(sizeof (struct intrspec) *
1588509Smrj 		    pdp->par_nintr, KM_SLEEP);
1589509Smrj 	}
1590509Smrj 
1591509Smrj 	/* Validate the interrupt number */
1592509Smrj 	if (inum >= pdp->par_nintr)
1593509Smrj 		return (NULL);
1594509Smrj 
1595509Smrj 	/* Get the interrupt structure pointer and return that */
1596509Smrj 	return ((struct intrspec *)&pdp->par_intr[inum]);
1597509Smrj }
1598509Smrj 
159912683SJimmy.Vetayases@oracle.com /*
160012683SJimmy.Vetayases@oracle.com  * Allocate interrupt vector for FIXED (legacy) type.
160112683SJimmy.Vetayases@oracle.com  */
160212683SJimmy.Vetayases@oracle.com static int
rootnex_alloc_intr_fixed(dev_info_t * rdip,ddi_intr_handle_impl_t * hdlp,void * result)160312683SJimmy.Vetayases@oracle.com rootnex_alloc_intr_fixed(dev_info_t *rdip, ddi_intr_handle_impl_t *hdlp,
160412683SJimmy.Vetayases@oracle.com     void *result)
160512683SJimmy.Vetayases@oracle.com {
160612683SJimmy.Vetayases@oracle.com 	struct intrspec		*ispec;
160712683SJimmy.Vetayases@oracle.com 	ddi_intr_handle_impl_t	info_hdl;
160812683SJimmy.Vetayases@oracle.com 	int			ret;
160912683SJimmy.Vetayases@oracle.com 	int			free_phdl = 0;
161012683SJimmy.Vetayases@oracle.com 	apic_get_type_t		type_info;
161112683SJimmy.Vetayases@oracle.com 
161212683SJimmy.Vetayases@oracle.com 	if (psm_intr_ops == NULL)
161312683SJimmy.Vetayases@oracle.com 		return (DDI_FAILURE);
161412683SJimmy.Vetayases@oracle.com 
161512683SJimmy.Vetayases@oracle.com 	if ((ispec = rootnex_get_ispec(rdip, hdlp->ih_inum)) == NULL)
161612683SJimmy.Vetayases@oracle.com 		return (DDI_FAILURE);
161712683SJimmy.Vetayases@oracle.com 
161812683SJimmy.Vetayases@oracle.com 	/*
161912683SJimmy.Vetayases@oracle.com 	 * If the PSM module is "APIX" then pass the request for it
162012683SJimmy.Vetayases@oracle.com 	 * to allocate the vector now.
162112683SJimmy.Vetayases@oracle.com 	 */
162212683SJimmy.Vetayases@oracle.com 	bzero(&info_hdl, sizeof (ddi_intr_handle_impl_t));
162312683SJimmy.Vetayases@oracle.com 	info_hdl.ih_private = &type_info;
162412683SJimmy.Vetayases@oracle.com 	if ((*psm_intr_ops)(NULL, &info_hdl, PSM_INTR_OP_APIC_TYPE, NULL) ==
162512683SJimmy.Vetayases@oracle.com 	    PSM_SUCCESS && strcmp(type_info.avgi_type, APIC_APIX_NAME) == 0) {
162612683SJimmy.Vetayases@oracle.com 		if (hdlp->ih_private == NULL) { /* allocate phdl structure */
162712683SJimmy.Vetayases@oracle.com 			free_phdl = 1;
162812683SJimmy.Vetayases@oracle.com 			i_ddi_alloc_intr_phdl(hdlp);
162912683SJimmy.Vetayases@oracle.com 		}
163012683SJimmy.Vetayases@oracle.com 		((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp = ispec;
163112683SJimmy.Vetayases@oracle.com 		ret = (*psm_intr_ops)(rdip, hdlp,
163212683SJimmy.Vetayases@oracle.com 		    PSM_INTR_OP_ALLOC_VECTORS, result);
163312683SJimmy.Vetayases@oracle.com 		if (free_phdl) { /* free up the phdl structure */
163412683SJimmy.Vetayases@oracle.com 			free_phdl = 0;
163512683SJimmy.Vetayases@oracle.com 			i_ddi_free_intr_phdl(hdlp);
163612683SJimmy.Vetayases@oracle.com 			hdlp->ih_private = NULL;
163712683SJimmy.Vetayases@oracle.com 		}
163812683SJimmy.Vetayases@oracle.com 	} else {
163912683SJimmy.Vetayases@oracle.com 		/*
164012683SJimmy.Vetayases@oracle.com 		 * No APIX module; fall back to the old scheme where the
164112683SJimmy.Vetayases@oracle.com 		 * interrupt vector is allocated during ddi_enable_intr() call.
164212683SJimmy.Vetayases@oracle.com 		 */
164312683SJimmy.Vetayases@oracle.com 		hdlp->ih_pri = ispec->intrspec_pri;
164412683SJimmy.Vetayases@oracle.com 		*(int *)result = hdlp->ih_scratch1;
164512683SJimmy.Vetayases@oracle.com 		ret = DDI_SUCCESS;
164612683SJimmy.Vetayases@oracle.com 	}
164712683SJimmy.Vetayases@oracle.com 
164812683SJimmy.Vetayases@oracle.com 	return (ret);
164912683SJimmy.Vetayases@oracle.com }
165012683SJimmy.Vetayases@oracle.com 
165112683SJimmy.Vetayases@oracle.com /*
165212683SJimmy.Vetayases@oracle.com  * Free up interrupt vector for FIXED (legacy) type.
165312683SJimmy.Vetayases@oracle.com  */
165412683SJimmy.Vetayases@oracle.com static int
rootnex_free_intr_fixed(dev_info_t * rdip,ddi_intr_handle_impl_t * hdlp)165512683SJimmy.Vetayases@oracle.com rootnex_free_intr_fixed(dev_info_t *rdip, ddi_intr_handle_impl_t *hdlp)
165612683SJimmy.Vetayases@oracle.com {
165712683SJimmy.Vetayases@oracle.com 	struct intrspec			*ispec;
165812683SJimmy.Vetayases@oracle.com 	struct ddi_parent_private_data	*pdp;
165912683SJimmy.Vetayases@oracle.com 	ddi_intr_handle_impl_t		info_hdl;
166012683SJimmy.Vetayases@oracle.com 	int				ret;
166112683SJimmy.Vetayases@oracle.com 	apic_get_type_t			type_info;
166212683SJimmy.Vetayases@oracle.com 
166312683SJimmy.Vetayases@oracle.com 	if (psm_intr_ops == NULL)
166412683SJimmy.Vetayases@oracle.com 		return (DDI_FAILURE);
166512683SJimmy.Vetayases@oracle.com 
166612683SJimmy.Vetayases@oracle.com 	/*
166712683SJimmy.Vetayases@oracle.com 	 * If the PSM module is "APIX" then pass the request for it
166812683SJimmy.Vetayases@oracle.com 	 * to free up the vector now.
166912683SJimmy.Vetayases@oracle.com 	 */
167012683SJimmy.Vetayases@oracle.com 	bzero(&info_hdl, sizeof (ddi_intr_handle_impl_t));
167112683SJimmy.Vetayases@oracle.com 	info_hdl.ih_private = &type_info;
167212683SJimmy.Vetayases@oracle.com 	if ((*psm_intr_ops)(NULL, &info_hdl, PSM_INTR_OP_APIC_TYPE, NULL) ==
167312683SJimmy.Vetayases@oracle.com 	    PSM_SUCCESS && strcmp(type_info.avgi_type, APIC_APIX_NAME) == 0) {
167412683SJimmy.Vetayases@oracle.com 		if ((ispec = rootnex_get_ispec(rdip, hdlp->ih_inum)) == NULL)
167512683SJimmy.Vetayases@oracle.com 			return (DDI_FAILURE);
167612683SJimmy.Vetayases@oracle.com 		((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp = ispec;
167712683SJimmy.Vetayases@oracle.com 		ret = (*psm_intr_ops)(rdip, hdlp,
167812683SJimmy.Vetayases@oracle.com 		    PSM_INTR_OP_FREE_VECTORS, NULL);
167912683SJimmy.Vetayases@oracle.com 	} else {
168012683SJimmy.Vetayases@oracle.com 		/*
168112683SJimmy.Vetayases@oracle.com 		 * No APIX module; fall back to the old scheme where
168212683SJimmy.Vetayases@oracle.com 		 * the interrupt vector was already freed during
168312683SJimmy.Vetayases@oracle.com 		 * ddi_disable_intr() call.
168412683SJimmy.Vetayases@oracle.com 		 */
168512683SJimmy.Vetayases@oracle.com 		ret = DDI_SUCCESS;
168612683SJimmy.Vetayases@oracle.com 	}
168712683SJimmy.Vetayases@oracle.com 
168812683SJimmy.Vetayases@oracle.com 	pdp = ddi_get_parent_data(rdip);
168912683SJimmy.Vetayases@oracle.com 
169012683SJimmy.Vetayases@oracle.com 	/*
169112683SJimmy.Vetayases@oracle.com 	 * Special case for 'pcic' driver' only.
169212683SJimmy.Vetayases@oracle.com 	 * If an intrspec was created for it, clean it up here
169312683SJimmy.Vetayases@oracle.com 	 * See detailed comments on this in the function
169412683SJimmy.Vetayases@oracle.com 	 * rootnex_get_ispec().
169512683SJimmy.Vetayases@oracle.com 	 */
169612683SJimmy.Vetayases@oracle.com 	if (pdp->par_intr && strcmp(ddi_get_name(rdip), "pcic") == 0) {
169712683SJimmy.Vetayases@oracle.com 		kmem_free(pdp->par_intr, sizeof (struct intrspec) *
169812683SJimmy.Vetayases@oracle.com 		    pdp->par_nintr);
169912683SJimmy.Vetayases@oracle.com 		/*
170012683SJimmy.Vetayases@oracle.com 		 * Set it to zero; so that
170112683SJimmy.Vetayases@oracle.com 		 * DDI framework doesn't free it again
170212683SJimmy.Vetayases@oracle.com 		 */
170312683SJimmy.Vetayases@oracle.com 		pdp->par_intr = NULL;
170412683SJimmy.Vetayases@oracle.com 		pdp->par_nintr = 0;
170512683SJimmy.Vetayases@oracle.com 	}
170612683SJimmy.Vetayases@oracle.com 
170712683SJimmy.Vetayases@oracle.com 	return (ret);
170812683SJimmy.Vetayases@oracle.com }
170912683SJimmy.Vetayases@oracle.com 
1710509Smrj 
1711509Smrj /*
1712509Smrj  * ******************
1713509Smrj  *  dma related code
1714509Smrj  * ******************
1715509Smrj  */
1716509Smrj 
1717509Smrj /*ARGSUSED*/
1718509Smrj static int
rootnex_coredma_allochdl(dev_info_t * dip,dev_info_t * rdip,ddi_dma_attr_t * attr,int (* waitfp)(caddr_t),caddr_t arg,ddi_dma_handle_t * handlep)17197613SVikram.Hegde@Sun.COM rootnex_coredma_allochdl(dev_info_t *dip, dev_info_t *rdip,
17207613SVikram.Hegde@Sun.COM     ddi_dma_attr_t *attr, int (*waitfp)(caddr_t), caddr_t arg,
17217613SVikram.Hegde@Sun.COM     ddi_dma_handle_t *handlep)
1722509Smrj {
1723509Smrj 	uint64_t maxsegmentsize_ll;
1724509Smrj 	uint_t maxsegmentsize;
1725509Smrj 	ddi_dma_impl_t *hp;
1726509Smrj 	rootnex_dma_t *dma;
1727509Smrj 	uint64_t count_max;
1728509Smrj 	uint64_t seg;
1729509Smrj 	int kmflag;
1730509Smrj 	int e;
1731509Smrj 
1732509Smrj 
1733509Smrj 	/* convert our sleep flags */
1734509Smrj 	if (waitfp == DDI_DMA_SLEEP) {
1735509Smrj 		kmflag = KM_SLEEP;
1736509Smrj 	} else {
1737509Smrj 		kmflag = KM_NOSLEEP;
1738509Smrj 	}
1739509Smrj 
1740509Smrj 	/*
1741509Smrj 	 * We try to do only one memory allocation here. We'll do a little
1742509Smrj 	 * pointer manipulation later. If the bind ends up taking more than
1743509Smrj 	 * our prealloc's space, we'll have to allocate more memory in the
1744509Smrj 	 * bind operation. Not great, but much better than before and the
1745509Smrj 	 * best we can do with the current bind interfaces.
1746509Smrj 	 */
1747509Smrj 	hp = kmem_cache_alloc(rootnex_state->r_dmahdl_cache, kmflag);
174813050Sfrank.van.der.linden@oracle.com 	if (hp == NULL)
1749509Smrj 		return (DDI_DMA_NORESOURCES);
1750509Smrj 
1751509Smrj 	/* Do our pointer manipulation now, align the structures */
1752509Smrj 	hp->dmai_private = (void *)(((uintptr_t)hp +
1753509Smrj 	    (uintptr_t)sizeof (ddi_dma_impl_t) + 0x7) & ~0x7);
1754509Smrj 	dma = (rootnex_dma_t *)hp->dmai_private;
1755509Smrj 	dma->dp_prealloc_buffer = (uchar_t *)(((uintptr_t)dma +
1756509Smrj 	    sizeof (rootnex_dma_t) + 0x7) & ~0x7);
1757509Smrj 
1758509Smrj 	/* setup the handle */
1759509Smrj 	rootnex_clean_dmahdl(hp);
176012027SStephen.Hanson@Sun.COM 	hp->dmai_error.err_fep = NULL;
176112027SStephen.Hanson@Sun.COM 	hp->dmai_error.err_cf = NULL;
1762509Smrj 	dma->dp_dip = rdip;
176313050Sfrank.van.der.linden@oracle.com 	dma->dp_sglinfo.si_flags = attr->dma_attr_flags;
1764509Smrj 	dma->dp_sglinfo.si_min_addr = attr->dma_attr_addr_lo;
176513050Sfrank.van.der.linden@oracle.com 
176613050Sfrank.van.der.linden@oracle.com 	/*
176713050Sfrank.van.der.linden@oracle.com 	 * The BOUNCE_ON_SEG workaround is not needed when an IOMMU
176813050Sfrank.van.der.linden@oracle.com 	 * is being used. Set the upper limit to the seg value.
176913050Sfrank.van.der.linden@oracle.com 	 * There will be enough DVMA space to always get addresses
177013050Sfrank.van.der.linden@oracle.com 	 * that will match the constraints.
177113050Sfrank.van.der.linden@oracle.com 	 */
177213050Sfrank.van.der.linden@oracle.com 	if (IOMMU_USED(rdip) &&
177313050Sfrank.van.der.linden@oracle.com 	    (attr->dma_attr_flags & _DDI_DMA_BOUNCE_ON_SEG)) {
177413050Sfrank.van.der.linden@oracle.com 		dma->dp_sglinfo.si_max_addr = attr->dma_attr_seg;
177513050Sfrank.van.der.linden@oracle.com 		dma->dp_sglinfo.si_flags &= ~_DDI_DMA_BOUNCE_ON_SEG;
177613050Sfrank.van.der.linden@oracle.com 	} else
177713050Sfrank.van.der.linden@oracle.com 		dma->dp_sglinfo.si_max_addr = attr->dma_attr_addr_hi;
177813050Sfrank.van.der.linden@oracle.com 
1779509Smrj 	hp->dmai_minxfer = attr->dma_attr_minxfer;
1780509Smrj 	hp->dmai_burstsizes = attr->dma_attr_burstsizes;
1781509Smrj 	hp->dmai_rdip = rdip;
1782509Smrj 	hp->dmai_attr = *attr;
1783509Smrj 
178413050Sfrank.van.der.linden@oracle.com 	if (attr->dma_attr_seg >= dma->dp_sglinfo.si_max_addr)
178513050Sfrank.van.der.linden@oracle.com 		dma->dp_sglinfo.si_cancross = B_FALSE;
178613050Sfrank.van.der.linden@oracle.com 	else
178713050Sfrank.van.der.linden@oracle.com 		dma->dp_sglinfo.si_cancross = B_TRUE;
178813050Sfrank.van.der.linden@oracle.com 
1789509Smrj 	/* we don't need to worry about the SPL since we do a tryenter */
1790509Smrj 	mutex_init(&dma->dp_mutex, NULL, MUTEX_DRIVER, NULL);
1791509Smrj 
1792509Smrj 	/*
1793509Smrj 	 * Figure out our maximum segment size. If the segment size is greater
1794509Smrj 	 * than 4G, we will limit it to (4G - 1) since the max size of a dma
1795509Smrj 	 * object (ddi_dma_obj_t.dmao_size) is 32 bits. dma_attr_seg and
1796509Smrj 	 * dma_attr_count_max are size-1 type values.
1797509Smrj 	 *
1798509Smrj 	 * Maximum segment size is the largest physically contiguous chunk of
1799509Smrj 	 * memory that we can return from a bind (i.e. the maximum size of a
1800509Smrj 	 * single cookie).
1801509Smrj 	 */
1802509Smrj 
1803509Smrj 	/* handle the rollover cases */
1804509Smrj 	seg = attr->dma_attr_seg + 1;
1805509Smrj 	if (seg < attr->dma_attr_seg) {
1806509Smrj 		seg = attr->dma_attr_seg;
1807509Smrj 	}
1808509Smrj 	count_max = attr->dma_attr_count_max + 1;
1809509Smrj 	if (count_max < attr->dma_attr_count_max) {
1810509Smrj 		count_max = attr->dma_attr_count_max;
1811509Smrj 	}
1812509Smrj 
1813509Smrj 	/*
1814509Smrj 	 * granularity may or may not be a power of two. If it isn't, we can't
1815509Smrj 	 * use a simple mask.
1816509Smrj 	 */
1817509Smrj 	if (attr->dma_attr_granular & (attr->dma_attr_granular - 1)) {
1818509Smrj 		dma->dp_granularity_power_2 = B_FALSE;
1819509Smrj 	} else {
1820509Smrj 		dma->dp_granularity_power_2 = B_TRUE;
1821509Smrj 	}
1822509Smrj 
1823509Smrj 	/*
1824509Smrj 	 * maxxfer should be a whole multiple of granularity. If we're going to
1825509Smrj 	 * break up a window because we're greater than maxxfer, we might as
1826509Smrj 	 * well make sure it's maxxfer is a whole multiple so we don't have to
1827509Smrj 	 * worry about triming the window later on for this case.
1828509Smrj 	 */
1829509Smrj 	if (attr->dma_attr_granular > 1) {
1830509Smrj 		if (dma->dp_granularity_power_2) {
1831509Smrj 			dma->dp_maxxfer = attr->dma_attr_maxxfer -
1832509Smrj 			    (attr->dma_attr_maxxfer &
1833509Smrj 			    (attr->dma_attr_granular - 1));
1834509Smrj 		} else {
1835509Smrj 			dma->dp_maxxfer = attr->dma_attr_maxxfer -
1836509Smrj 			    (attr->dma_attr_maxxfer % attr->dma_attr_granular);
1837509Smrj 		}
1838509Smrj 	} else {
1839509Smrj 		dma->dp_maxxfer = attr->dma_attr_maxxfer;
1840509Smrj 	}
1841509Smrj 
1842509Smrj 	maxsegmentsize_ll = MIN(seg, dma->dp_maxxfer);
1843509Smrj 	maxsegmentsize_ll = MIN(maxsegmentsize_ll, count_max);
1844509Smrj 	if (maxsegmentsize_ll == 0 || (maxsegmentsize_ll > 0xFFFFFFFF)) {
1845509Smrj 		maxsegmentsize = 0xFFFFFFFF;
1846509Smrj 	} else {
1847509Smrj 		maxsegmentsize = maxsegmentsize_ll;
1848509Smrj 	}
1849509Smrj 	dma->dp_sglinfo.si_max_cookie_size = maxsegmentsize;
1850509Smrj 	dma->dp_sglinfo.si_segmask = attr->dma_attr_seg;
1851509Smrj 
1852509Smrj 	/* check the ddi_dma_attr arg to make sure it makes a little sense */
1853509Smrj 	if (rootnex_alloc_check_parms) {
1854509Smrj 		e = rootnex_valid_alloc_parms(attr, maxsegmentsize);
1855509Smrj 		if (e != DDI_SUCCESS) {
185612837Sfrank.van.der.linden@oracle.com 			ROOTNEX_DPROF_INC(&rootnex_cnt[ROOTNEX_CNT_ALLOC_FAIL]);
1857509Smrj 			(void) rootnex_dma_freehdl(dip, rdip,
1858509Smrj 			    (ddi_dma_handle_t)hp);
1859509Smrj 			return (e);
1860509Smrj 		}
1861509Smrj 	}
1862509Smrj 
1863509Smrj 	*handlep = (ddi_dma_handle_t)hp;
1864509Smrj 
186510902SMark.Johnson@Sun.COM 	ROOTNEX_DPROF_INC(&rootnex_cnt[ROOTNEX_CNT_ACTIVE_HDLS]);
186610902SMark.Johnson@Sun.COM 	ROOTNEX_DPROBE1(rootnex__alloc__handle, uint64_t,
1867509Smrj 	    rootnex_cnt[ROOTNEX_CNT_ACTIVE_HDLS]);
1868509Smrj 
1869509Smrj 	return (DDI_SUCCESS);
1870509Smrj }
1871509Smrj 
1872509Smrj 
1873509Smrj /*
18747613SVikram.Hegde@Sun.COM  * rootnex_dma_allochdl()
18757613SVikram.Hegde@Sun.COM  *    called from ddi_dma_alloc_handle().
1876509Smrj  */
18777613SVikram.Hegde@Sun.COM static int
rootnex_dma_allochdl(dev_info_t * dip,dev_info_t * rdip,ddi_dma_attr_t * attr,int (* waitfp)(caddr_t),caddr_t arg,ddi_dma_handle_t * handlep)18787613SVikram.Hegde@Sun.COM rootnex_dma_allochdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_attr_t *attr,
18797613SVikram.Hegde@Sun.COM     int (*waitfp)(caddr_t), caddr_t arg, ddi_dma_handle_t *handlep)
18807613SVikram.Hegde@Sun.COM {
188113050Sfrank.van.der.linden@oracle.com 	int retval = DDI_SUCCESS;
188211600SVikram.Hegde@Sun.COM #if defined(__amd64) && !defined(__xpv)
188313050Sfrank.van.der.linden@oracle.com 
188413050Sfrank.van.der.linden@oracle.com 	if (IOMMU_UNITIALIZED(rdip)) {
188513050Sfrank.van.der.linden@oracle.com 		retval = iommulib_nex_open(dip, rdip);
188613050Sfrank.van.der.linden@oracle.com 
188713050Sfrank.van.der.linden@oracle.com 		if (retval != DDI_SUCCESS && retval != DDI_ENOTSUP)
188813050Sfrank.van.der.linden@oracle.com 			return (retval);
18897613SVikram.Hegde@Sun.COM 	}
18907613SVikram.Hegde@Sun.COM 
189113050Sfrank.van.der.linden@oracle.com 	if (IOMMU_UNUSED(rdip)) {
189213050Sfrank.van.der.linden@oracle.com 		retval = rootnex_coredma_allochdl(dip, rdip, attr, waitfp, arg,
189313050Sfrank.van.der.linden@oracle.com 		    handlep);
189413050Sfrank.van.der.linden@oracle.com 	} else {
189513050Sfrank.van.der.linden@oracle.com 		retval = iommulib_nexdma_allochdl(dip, rdip, attr,
189613050Sfrank.van.der.linden@oracle.com 		    waitfp, arg, handlep);
189713050Sfrank.van.der.linden@oracle.com 	}
18987613SVikram.Hegde@Sun.COM #else
189912027SStephen.Hanson@Sun.COM 	retval = rootnex_coredma_allochdl(dip, rdip, attr, waitfp, arg,
190012027SStephen.Hanson@Sun.COM 	    handlep);
19017613SVikram.Hegde@Sun.COM #endif
190213050Sfrank.van.der.linden@oracle.com 	switch (retval) {
190313050Sfrank.van.der.linden@oracle.com 	case DDI_DMA_NORESOURCES:
190413050Sfrank.van.der.linden@oracle.com 		if (waitfp != DDI_DMA_DONTWAIT) {
190513050Sfrank.van.der.linden@oracle.com 			ddi_set_callback(waitfp, arg,
190613050Sfrank.van.der.linden@oracle.com 			    &rootnex_state->r_dvma_call_list_id);
190713050Sfrank.van.der.linden@oracle.com 		}
190813050Sfrank.van.der.linden@oracle.com 		break;
190913050Sfrank.van.der.linden@oracle.com 	case DDI_SUCCESS:
191012027SStephen.Hanson@Sun.COM 		ndi_fmc_insert(rdip, DMA_HANDLE, *handlep, NULL);
191113050Sfrank.van.der.linden@oracle.com 		break;
191213050Sfrank.van.der.linden@oracle.com 	default:
191313050Sfrank.van.der.linden@oracle.com 		break;
191413050Sfrank.van.der.linden@oracle.com 	}
191512027SStephen.Hanson@Sun.COM 	return (retval);
19167613SVikram.Hegde@Sun.COM }
19177613SVikram.Hegde@Sun.COM 
1918509Smrj /*ARGSUSED*/
1919509Smrj static int
rootnex_coredma_freehdl(dev_info_t * dip,dev_info_t * rdip,ddi_dma_handle_t handle)19207613SVikram.Hegde@Sun.COM rootnex_coredma_freehdl(dev_info_t *dip, dev_info_t *rdip,
19217613SVikram.Hegde@Sun.COM     ddi_dma_handle_t handle)
1922509Smrj {
1923509Smrj 	ddi_dma_impl_t *hp;
1924509Smrj 	rootnex_dma_t *dma;
1925509Smrj 
1926509Smrj 
1927509Smrj 	hp = (ddi_dma_impl_t *)handle;
1928509Smrj 	dma = (rootnex_dma_t *)hp->dmai_private;
1929509Smrj 
1930509Smrj 	/* unbind should have been called first */
1931509Smrj 	ASSERT(!dma->dp_inuse);
1932509Smrj 
1933509Smrj 	mutex_destroy(&dma->dp_mutex);
1934509Smrj 	kmem_cache_free(rootnex_state->r_dmahdl_cache, hp);
1935509Smrj 
193610902SMark.Johnson@Sun.COM 	ROOTNEX_DPROF_DEC(&rootnex_cnt[ROOTNEX_CNT_ACTIVE_HDLS]);
193710902SMark.Johnson@Sun.COM 	ROOTNEX_DPROBE1(rootnex__free__handle, uint64_t,
1938509Smrj 	    rootnex_cnt[ROOTNEX_CNT_ACTIVE_HDLS]);
1939509Smrj 
1940509Smrj 	return (DDI_SUCCESS);
1941509Smrj }
1942509Smrj 
1943509Smrj /*
19447613SVikram.Hegde@Sun.COM  * rootnex_dma_freehdl()
19457613SVikram.Hegde@Sun.COM  *    called from ddi_dma_free_handle().
1946509Smrj  */
19477613SVikram.Hegde@Sun.COM static int
rootnex_dma_freehdl(dev_info_t * dip,dev_info_t * rdip,ddi_dma_handle_t handle)19487613SVikram.Hegde@Sun.COM rootnex_dma_freehdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle)
19497613SVikram.Hegde@Sun.COM {
195013050Sfrank.van.der.linden@oracle.com 	int ret;
195113050Sfrank.van.der.linden@oracle.com 
195212027SStephen.Hanson@Sun.COM 	ndi_fmc_remove(rdip, DMA_HANDLE, handle);
195311600SVikram.Hegde@Sun.COM #if defined(__amd64) && !defined(__xpv)
195413050Sfrank.van.der.linden@oracle.com 	if (IOMMU_USED(rdip))
195513050Sfrank.van.der.linden@oracle.com 		ret = iommulib_nexdma_freehdl(dip, rdip, handle);
195613050Sfrank.van.der.linden@oracle.com 	else
19577613SVikram.Hegde@Sun.COM #endif
195813050Sfrank.van.der.linden@oracle.com 	ret = rootnex_coredma_freehdl(dip, rdip, handle);
195913050Sfrank.van.der.linden@oracle.com 
196013050Sfrank.van.der.linden@oracle.com 	if (rootnex_state->r_dvma_call_list_id)
196113050Sfrank.van.der.linden@oracle.com 		ddi_run_callback(&rootnex_state->r_dvma_call_list_id);
196213050Sfrank.van.der.linden@oracle.com 
196313050Sfrank.van.der.linden@oracle.com 	return (ret);
19647613SVikram.Hegde@Sun.COM }
19657613SVikram.Hegde@Sun.COM 
1966509Smrj /*ARGSUSED*/
1967509Smrj static int
rootnex_coredma_bindhdl(dev_info_t * dip,dev_info_t * rdip,ddi_dma_handle_t handle,struct ddi_dma_req * dmareq,ddi_dma_cookie_t * cookiep,uint_t * ccountp)19687613SVikram.Hegde@Sun.COM rootnex_coredma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
19697613SVikram.Hegde@Sun.COM     ddi_dma_handle_t handle, struct ddi_dma_req *dmareq,
19707613SVikram.Hegde@Sun.COM     ddi_dma_cookie_t *cookiep, uint_t *ccountp)
19710Sstevel@tonic-gate {
1972509Smrj 	rootnex_sglinfo_t *sinfo;
197313050Sfrank.van.der.linden@oracle.com 	ddi_dma_obj_t *dmao;
197413053Sfrank.van.der.linden@oracle.com #if defined(__amd64) && !defined(__xpv)
197513050Sfrank.van.der.linden@oracle.com 	struct dvmaseg *dvs;
197613050Sfrank.van.der.linden@oracle.com 	ddi_dma_cookie_t *cookie;
197713053Sfrank.van.der.linden@oracle.com #endif
1978509Smrj 	ddi_dma_attr_t *attr;
1979509Smrj 	ddi_dma_impl_t *hp;
1980509Smrj 	rootnex_dma_t *dma;
1981509Smrj 	int kmflag;
1982509Smrj 	int e;
198313050Sfrank.van.der.linden@oracle.com 	uint_t ncookies;
1984509Smrj 
1985509Smrj 	hp = (ddi_dma_impl_t *)handle;
1986509Smrj 	dma = (rootnex_dma_t *)hp->dmai_private;
198713053Sfrank.van.der.linden@oracle.com 	dmao = &dma->dp_dma;
1988509Smrj 	sinfo = &dma->dp_sglinfo;
1989509Smrj 	attr = &hp->dmai_attr;
1990509Smrj 
1991*13070SEthindra.Ramamurthy@Sun.COM 	/* convert the sleep flags */
19928215SVikram.Hegde@Sun.COM 	if (dmareq->dmar_fp == DDI_DMA_SLEEP) {
1993*13070SEthindra.Ramamurthy@Sun.COM 		dma->dp_sleep_flags = kmflag = KM_SLEEP;
19948215SVikram.Hegde@Sun.COM 	} else {
1995*13070SEthindra.Ramamurthy@Sun.COM 		dma->dp_sleep_flags = kmflag = KM_NOSLEEP;
19968215SVikram.Hegde@Sun.COM 	}
19978215SVikram.Hegde@Sun.COM 
1998509Smrj 	hp->dmai_rflags = dmareq->dmar_flags & DMP_DDIFLAGS;
1999509Smrj 
2000509Smrj 	/*
2001509Smrj 	 * This is useful for debugging a driver. Not as useful in a production
2002509Smrj 	 * system. The only time this will fail is if you have a driver bug.
2003509Smrj 	 */
2004509Smrj 	if (rootnex_bind_check_inuse) {
2005509Smrj 		/*
2006509Smrj 		 * No one else should ever have this lock unless someone else
2007509Smrj 		 * is trying to use this handle. So contention on the lock
2008509Smrj 		 * is the same as inuse being set.
2009509Smrj 		 */
2010509Smrj 		e = mutex_tryenter(&dma->dp_mutex);
2011509Smrj 		if (e == 0) {
201212837Sfrank.van.der.linden@oracle.com 			ROOTNEX_DPROF_INC(&rootnex_cnt[ROOTNEX_CNT_BIND_FAIL]);
2013509Smrj 			return (DDI_DMA_INUSE);
2014509Smrj 		}
2015509Smrj 		if (dma->dp_inuse) {
2016509Smrj 			mutex_exit(&dma->dp_mutex);
201712837Sfrank.van.der.linden@oracle.com 			ROOTNEX_DPROF_INC(&rootnex_cnt[ROOTNEX_CNT_BIND_FAIL]);
2018509Smrj 			return (DDI_DMA_INUSE);
2019509Smrj 		}
2020509Smrj 		dma->dp_inuse = B_TRUE;
2021509Smrj 		mutex_exit(&dma->dp_mutex);
2022509Smrj 	}
2023509Smrj 
2024509Smrj 	/* check the ddi_dma_attr arg to make sure it makes a little sense */
2025509Smrj 	if (rootnex_bind_check_parms) {
2026509Smrj 		e = rootnex_valid_bind_parms(dmareq, attr);
2027509Smrj 		if (e != DDI_SUCCESS) {
202812837Sfrank.van.der.linden@oracle.com 			ROOTNEX_DPROF_INC(&rootnex_cnt[ROOTNEX_CNT_BIND_FAIL]);
2029509Smrj 			rootnex_clean_dmahdl(hp);
2030509Smrj 			return (e);
2031509Smrj 		}
2032509Smrj 	}
2033509Smrj 
2034509Smrj 	/* save away the original bind info */
2035509Smrj 	dma->dp_dma = dmareq->dmar_object;
2036509Smrj 
203711600SVikram.Hegde@Sun.COM #if defined(__amd64) && !defined(__xpv)
203813050Sfrank.van.der.linden@oracle.com 	if (IOMMU_USED(rdip)) {
203913050Sfrank.van.der.linden@oracle.com 		dmao = &dma->dp_dvma;
204013050Sfrank.van.der.linden@oracle.com 		e = iommulib_nexdma_mapobject(dip, rdip, handle, dmareq, dmao);
204113050Sfrank.van.der.linden@oracle.com 		switch (e) {
204213050Sfrank.van.der.linden@oracle.com 		case DDI_SUCCESS:
204313050Sfrank.van.der.linden@oracle.com 			if (sinfo->si_cancross ||
204413050Sfrank.van.der.linden@oracle.com 			    dmao->dmao_obj.dvma_obj.dv_nseg != 1 ||
204513050Sfrank.van.der.linden@oracle.com 			    dmao->dmao_size > sinfo->si_max_cookie_size) {
204613050Sfrank.van.der.linden@oracle.com 				dma->dp_dvma_used = B_TRUE;
204713050Sfrank.van.der.linden@oracle.com 				break;
204813050Sfrank.van.der.linden@oracle.com 			}
204913050Sfrank.van.der.linden@oracle.com 			sinfo->si_sgl_size = 1;
205013050Sfrank.van.der.linden@oracle.com 			hp->dmai_rflags |= DMP_NOSYNC;
205113050Sfrank.van.der.linden@oracle.com 
205213050Sfrank.van.der.linden@oracle.com 			dma->dp_dvma_used = B_TRUE;
205313050Sfrank.van.der.linden@oracle.com 			dma->dp_need_to_free_cookie = B_FALSE;
205413050Sfrank.van.der.linden@oracle.com 
205513050Sfrank.van.der.linden@oracle.com 			dvs = &dmao->dmao_obj.dvma_obj.dv_seg[0];
205613050Sfrank.van.der.linden@oracle.com 			cookie = hp->dmai_cookie = dma->dp_cookies =
205713050Sfrank.van.der.linden@oracle.com 			    (ddi_dma_cookie_t *)dma->dp_prealloc_buffer;
205813050Sfrank.van.der.linden@oracle.com 			cookie->dmac_laddress = dvs->dvs_start +
205913050Sfrank.van.der.linden@oracle.com 			    dmao->dmao_obj.dvma_obj.dv_off;
206013050Sfrank.van.der.linden@oracle.com 			cookie->dmac_size = dvs->dvs_len;
206113050Sfrank.van.der.linden@oracle.com 			cookie->dmac_type = 0;
206213050Sfrank.van.der.linden@oracle.com 
206313050Sfrank.van.der.linden@oracle.com 			ROOTNEX_DPROBE1(rootnex__bind__dvmafast, dev_info_t *,
206413050Sfrank.van.der.linden@oracle.com 			    rdip);
206513050Sfrank.van.der.linden@oracle.com 			goto fast;
206613050Sfrank.van.der.linden@oracle.com 		case DDI_ENOTSUP:
206713050Sfrank.van.der.linden@oracle.com 			break;
206813050Sfrank.van.der.linden@oracle.com 		default:
206913050Sfrank.van.der.linden@oracle.com 			rootnex_clean_dmahdl(hp);
207013050Sfrank.van.der.linden@oracle.com 			return (e);
207113050Sfrank.van.der.linden@oracle.com 		}
20727589SVikram.Hegde@Sun.COM 	}
20737613SVikram.Hegde@Sun.COM #endif
20747589SVikram.Hegde@Sun.COM 
2075509Smrj 	/*
207613050Sfrank.van.der.linden@oracle.com 	 * Figure out a rough estimate of what maximum number of pages
207713050Sfrank.van.der.linden@oracle.com 	 * this buffer could use (a high estimate of course).
2078509Smrj 	 */
2079509Smrj 	sinfo->si_max_pages = mmu_btopr(dma->dp_dma.dmao_size) + 1;
2080509Smrj 
208113050Sfrank.van.der.linden@oracle.com 	if (dma->dp_dvma_used) {
208213050Sfrank.van.der.linden@oracle.com 		/*
208313050Sfrank.van.der.linden@oracle.com 		 * The number of physical pages is the worst case.
208413050Sfrank.van.der.linden@oracle.com 		 *
208513050Sfrank.van.der.linden@oracle.com 		 * For DVMA, the worst case is the length divided
208613050Sfrank.van.der.linden@oracle.com 		 * by the maximum cookie length, plus 1. Add to that
208713050Sfrank.van.der.linden@oracle.com 		 * the number of segment boundaries potentially crossed, and
208813050Sfrank.van.der.linden@oracle.com 		 * the additional number of DVMA segments that was returned.
208913050Sfrank.van.der.linden@oracle.com 		 *
209013050Sfrank.van.der.linden@oracle.com 		 * In the normal case, for modern devices, si_cancross will
209113050Sfrank.van.der.linden@oracle.com 		 * be false, and dv_nseg will be 1, and the fast path will
209213050Sfrank.van.der.linden@oracle.com 		 * have been taken above.
209313050Sfrank.van.der.linden@oracle.com 		 */
209413050Sfrank.van.der.linden@oracle.com 		ncookies = (dma->dp_dma.dmao_size / sinfo->si_max_cookie_size)
209513050Sfrank.van.der.linden@oracle.com 		    + 1;
209613050Sfrank.van.der.linden@oracle.com 		if (sinfo->si_cancross)
209713050Sfrank.van.der.linden@oracle.com 			ncookies +=
209813050Sfrank.van.der.linden@oracle.com 			    (dma->dp_dma.dmao_size / attr->dma_attr_seg) + 1;
209913050Sfrank.van.der.linden@oracle.com 		ncookies += (dmao->dmao_obj.dvma_obj.dv_nseg - 1);
210013050Sfrank.van.der.linden@oracle.com 
210113050Sfrank.van.der.linden@oracle.com 		sinfo->si_max_pages = MIN(sinfo->si_max_pages, ncookies);
210213050Sfrank.van.der.linden@oracle.com 	}
210313050Sfrank.van.der.linden@oracle.com 
2104509Smrj 	/*
2105509Smrj 	 * We'll use the pre-allocated cookies for any bind that will *always*
2106509Smrj 	 * fit (more important to be consistent, we don't want to create
2107509Smrj 	 * additional degenerate cases).
2108509Smrj 	 */
2109509Smrj 	if (sinfo->si_max_pages <= rootnex_state->r_prealloc_cookies) {
2110509Smrj 		dma->dp_cookies = (ddi_dma_cookie_t *)dma->dp_prealloc_buffer;
2111509Smrj 		dma->dp_need_to_free_cookie = B_FALSE;
211212837Sfrank.van.der.linden@oracle.com 		ROOTNEX_DPROBE2(rootnex__bind__prealloc, dev_info_t *, rdip,
2113509Smrj 		    uint_t, sinfo->si_max_pages);
2114509Smrj 
2115509Smrj 	/*
2116509Smrj 	 * For anything larger than that, we'll go ahead and allocate the
2117509Smrj 	 * maximum number of pages we expect to see. Hopefuly, we won't be
2118509Smrj 	 * seeing this path in the fast path for high performance devices very
2119509Smrj 	 * frequently.
2120509Smrj 	 *
2121509Smrj 	 * a ddi bind interface that allowed the driver to provide storage to
2122509Smrj 	 * the bind interface would speed this case up.
2123509Smrj 	 */
2124509Smrj 	} else {
2125509Smrj 		/*
2126509Smrj 		 * Save away how much memory we allocated. If we're doing a
2127509Smrj 		 * nosleep, the alloc could fail...
2128509Smrj 		 */
2129509Smrj 		dma->dp_cookie_size = sinfo->si_max_pages *
2130509Smrj 		    sizeof (ddi_dma_cookie_t);
2131509Smrj 		dma->dp_cookies = kmem_alloc(dma->dp_cookie_size, kmflag);
2132509Smrj 		if (dma->dp_cookies == NULL) {
213312837Sfrank.van.der.linden@oracle.com 			ROOTNEX_DPROF_INC(&rootnex_cnt[ROOTNEX_CNT_BIND_FAIL]);
2134509Smrj 			rootnex_clean_dmahdl(hp);
2135509Smrj 			return (DDI_DMA_NORESOURCES);
2136509Smrj 		}
2137509Smrj 		dma->dp_need_to_free_cookie = B_TRUE;
213812837Sfrank.van.der.linden@oracle.com 		ROOTNEX_DPROBE2(rootnex__bind__alloc, dev_info_t *, rdip,
213912837Sfrank.van.der.linden@oracle.com 		    uint_t, sinfo->si_max_pages);
2140509Smrj 	}
2141509Smrj 	hp->dmai_cookie = dma->dp_cookies;
2142509Smrj 
2143509Smrj 	/*
2144509Smrj 	 * Get the real sgl. rootnex_get_sgl will fill in cookie array while
214511600SVikram.Hegde@Sun.COM 	 * looking at the constraints in the dma structure. It will then put
214611600SVikram.Hegde@Sun.COM 	 * some additional state about the sgl in the dma struct (i.e. is
214711600SVikram.Hegde@Sun.COM 	 * the sgl clean, or do we need to do some munging; how many pages
214811600SVikram.Hegde@Sun.COM 	 * need to be copied, etc.)
2149509Smrj 	 */
215013050Sfrank.van.der.linden@oracle.com 	if (dma->dp_dvma_used)
215113050Sfrank.van.der.linden@oracle.com 		rootnex_dvma_get_sgl(dmao, dma->dp_cookies, &dma->dp_sglinfo);
215213050Sfrank.van.der.linden@oracle.com 	else
215313050Sfrank.van.der.linden@oracle.com 		rootnex_get_sgl(dmao, dma->dp_cookies, &dma->dp_sglinfo);
21547589SVikram.Hegde@Sun.COM 
215511600SVikram.Hegde@Sun.COM out:
2156509Smrj 	ASSERT(sinfo->si_sgl_size <= sinfo->si_max_pages);
2157509Smrj 	/* if we don't need a copy buffer, we don't need to sync */
2158509Smrj 	if (sinfo->si_copybuf_req == 0) {
2159509Smrj 		hp->dmai_rflags |= DMP_NOSYNC;
2160509Smrj 	}
2161509Smrj 
2162509Smrj 	/*
2163509Smrj 	 * if we don't need the copybuf and we don't need to do a partial,  we
2164509Smrj 	 * hit the fast path. All the high performance devices should be trying
2165509Smrj 	 * to hit this path. To hit this path, a device should be able to reach
2166509Smrj 	 * all of memory, shouldn't try to bind more than it can transfer, and
2167509Smrj 	 * the buffer shouldn't require more cookies than the driver/device can
2168509Smrj 	 * handle [sgllen]).
2169509Smrj 	 */
2170509Smrj 	if ((sinfo->si_copybuf_req == 0) &&
2171509Smrj 	    (sinfo->si_sgl_size <= attr->dma_attr_sgllen) &&
217213050Sfrank.van.der.linden@oracle.com 	    (dmao->dmao_size < dma->dp_maxxfer)) {
217313050Sfrank.van.der.linden@oracle.com fast:
2174509Smrj 		/*
21755591Sstephh 		 * If the driver supports FMA, insert the handle in the FMA DMA
21765591Sstephh 		 * handle cache.
21775591Sstephh 		 */
217812027SStephen.Hanson@Sun.COM 		if (attr->dma_attr_flags & DDI_DMA_FLAGERR)
21795591Sstephh 			hp->dmai_error.err_cf = rootnex_dma_check;
21805591Sstephh 
21815591Sstephh 		/*
2182509Smrj 		 * copy out the first cookie and ccountp, set the cookie
2183509Smrj 		 * pointer to the second cookie. The first cookie is passed
2184509Smrj 		 * back on the stack. Additional cookies are accessed via
2185509Smrj 		 * ddi_dma_nextcookie()
2186509Smrj 		 */
2187509Smrj 		*cookiep = dma->dp_cookies[0];
2188509Smrj 		*ccountp = sinfo->si_sgl_size;
2189509Smrj 		hp->dmai_cookie++;
2190509Smrj 		hp->dmai_rflags &= ~DDI_DMA_PARTIAL;
219112837Sfrank.van.der.linden@oracle.com 		ROOTNEX_DPROF_INC(&rootnex_cnt[ROOTNEX_CNT_ACTIVE_BINDS]);
219213050Sfrank.van.der.linden@oracle.com 		ROOTNEX_DPROBE4(rootnex__bind__fast, dev_info_t *, rdip,
219311600SVikram.Hegde@Sun.COM 		    uint64_t, rootnex_cnt[ROOTNEX_CNT_ACTIVE_BINDS],
219413050Sfrank.van.der.linden@oracle.com 		    uint_t, dmao->dmao_size, uint_t, *ccountp);
219511600SVikram.Hegde@Sun.COM 
219611600SVikram.Hegde@Sun.COM 
2197509Smrj 		return (DDI_DMA_MAPPED);
2198509Smrj 	}
2199509Smrj 
2200509Smrj 	/*
2201509Smrj 	 * go to the slow path, we may need to alloc more memory, create
2202509Smrj 	 * multiple windows, and munge up a sgl to make the device happy.
2203509Smrj 	 */
220413050Sfrank.van.der.linden@oracle.com 
220513050Sfrank.van.der.linden@oracle.com 	/*
220613050Sfrank.van.der.linden@oracle.com 	 * With the IOMMU mapobject method used, we should never hit
220713050Sfrank.van.der.linden@oracle.com 	 * the slow path. If we do, something is seriously wrong.
220813050Sfrank.van.der.linden@oracle.com 	 * Clean up and return an error.
220913050Sfrank.van.der.linden@oracle.com 	 */
221013050Sfrank.van.der.linden@oracle.com 
221113050Sfrank.van.der.linden@oracle.com 	if (dma->dp_dvma_used) {
221213050Sfrank.van.der.linden@oracle.com 		(void) iommulib_nexdma_unmapobject(dip, rdip, handle,
221313050Sfrank.van.der.linden@oracle.com 		    &dma->dp_dvma);
221413050Sfrank.van.der.linden@oracle.com 		e = DDI_DMA_NOMAPPING;
221513050Sfrank.van.der.linden@oracle.com 	} else {
221613050Sfrank.van.der.linden@oracle.com 		e = rootnex_bind_slowpath(hp, dmareq, dma, attr, &dma->dp_dma,
221713050Sfrank.van.der.linden@oracle.com 		    kmflag);
221813050Sfrank.van.der.linden@oracle.com 	}
2219509Smrj 	if ((e != DDI_DMA_MAPPED) && (e != DDI_DMA_PARTIAL_MAP)) {
2220509Smrj 		if (dma->dp_need_to_free_cookie) {
2221509Smrj 			kmem_free(dma->dp_cookies, dma->dp_cookie_size);
2222509Smrj 		}
222312837Sfrank.van.der.linden@oracle.com 		ROOTNEX_DPROF_INC(&rootnex_cnt[ROOTNEX_CNT_BIND_FAIL]);
2224509Smrj 		rootnex_clean_dmahdl(hp); /* must be after free cookie */
2225509Smrj 		return (e);
2226509Smrj 	}
2227509Smrj 
22285591Sstephh 	/*
22295591Sstephh 	 * If the driver supports FMA, insert the handle in the FMA DMA handle
22305591Sstephh 	 * cache.
22315591Sstephh 	 */
223212027SStephen.Hanson@Sun.COM 	if (attr->dma_attr_flags & DDI_DMA_FLAGERR)
22335591Sstephh 		hp->dmai_error.err_cf = rootnex_dma_check;
22345591Sstephh 
2235509Smrj 	/* if the first window uses the copy buffer, sync it for the device */
2236509Smrj 	if ((dma->dp_window[dma->dp_current_win].wd_dosync) &&
2237509Smrj 	    (hp->dmai_rflags & DDI_DMA_WRITE)) {
22388215SVikram.Hegde@Sun.COM 		(void) rootnex_coredma_sync(dip, rdip, handle, 0, 0,
2239509Smrj 		    DDI_DMA_SYNC_FORDEV);
2240509Smrj 	}
2241509Smrj 
2242509Smrj 	/*
2243509Smrj 	 * copy out the first cookie and ccountp, set the cookie pointer to the
2244509Smrj 	 * second cookie. Make sure the partial flag is set/cleared correctly.
2245509Smrj 	 * If we have a partial map (i.e. multiple windows), the number of
2246509Smrj 	 * cookies we return is the number of cookies in the first window.
2247509Smrj 	 */
2248509Smrj 	if (e == DDI_DMA_MAPPED) {
2249509Smrj 		hp->dmai_rflags &= ~DDI_DMA_PARTIAL;
2250509Smrj 		*ccountp = sinfo->si_sgl_size;
225111600SVikram.Hegde@Sun.COM 		hp->dmai_nwin = 1;
2252509Smrj 	} else {
2253509Smrj 		hp->dmai_rflags |= DDI_DMA_PARTIAL;
2254509Smrj 		*ccountp = dma->dp_window[dma->dp_current_win].wd_cookie_cnt;
2255509Smrj 		ASSERT(hp->dmai_nwin <= dma->dp_max_win);
2256509Smrj 	}
2257509Smrj 	*cookiep = dma->dp_cookies[0];
2258509Smrj 	hp->dmai_cookie++;
2259509Smrj 
226010902SMark.Johnson@Sun.COM 	ROOTNEX_DPROF_INC(&rootnex_cnt[ROOTNEX_CNT_ACTIVE_BINDS]);
226113050Sfrank.van.der.linden@oracle.com 	ROOTNEX_DPROBE4(rootnex__bind__slow, dev_info_t *, rdip, uint64_t,
2262509Smrj 	    rootnex_cnt[ROOTNEX_CNT_ACTIVE_BINDS], uint_t,
226313050Sfrank.van.der.linden@oracle.com 	    dmao->dmao_size, uint_t, *ccountp);
2264509Smrj 	return (e);
2265509Smrj }
2266509Smrj 
2267509Smrj /*
22687613SVikram.Hegde@Sun.COM  * rootnex_dma_bindhdl()
22697613SVikram.Hegde@Sun.COM  *    called from ddi_dma_addr_bind_handle() and ddi_dma_buf_bind_handle().
2270509Smrj  */
22717613SVikram.Hegde@Sun.COM static int
rootnex_dma_bindhdl(dev_info_t * dip,dev_info_t * rdip,ddi_dma_handle_t handle,struct ddi_dma_req * dmareq,ddi_dma_cookie_t * cookiep,uint_t * ccountp)22727613SVikram.Hegde@Sun.COM rootnex_dma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
22737613SVikram.Hegde@Sun.COM     ddi_dma_handle_t handle, struct ddi_dma_req *dmareq,
22747613SVikram.Hegde@Sun.COM     ddi_dma_cookie_t *cookiep, uint_t *ccountp)
22757613SVikram.Hegde@Sun.COM {
227613050Sfrank.van.der.linden@oracle.com 	int ret;
227711600SVikram.Hegde@Sun.COM #if defined(__amd64) && !defined(__xpv)
227813050Sfrank.van.der.linden@oracle.com 	if (IOMMU_USED(rdip))
227913050Sfrank.van.der.linden@oracle.com 		ret = iommulib_nexdma_bindhdl(dip, rdip, handle, dmareq,
228013050Sfrank.van.der.linden@oracle.com 		    cookiep, ccountp);
228113050Sfrank.van.der.linden@oracle.com 	else
228213050Sfrank.van.der.linden@oracle.com #endif
228313050Sfrank.van.der.linden@oracle.com 	ret = rootnex_coredma_bindhdl(dip, rdip, handle, dmareq,
228413050Sfrank.van.der.linden@oracle.com 	    cookiep, ccountp);
228513050Sfrank.van.der.linden@oracle.com 
228613050Sfrank.van.der.linden@oracle.com 	if (ret == DDI_DMA_NORESOURCES && dmareq->dmar_fp != DDI_DMA_DONTWAIT) {
228713050Sfrank.van.der.linden@oracle.com 		ddi_set_callback(dmareq->dmar_fp, dmareq->dmar_arg,
228813050Sfrank.van.der.linden@oracle.com 		    &rootnex_state->r_dvma_call_list_id);
22897613SVikram.Hegde@Sun.COM 	}
229013050Sfrank.van.der.linden@oracle.com 
229113050Sfrank.van.der.linden@oracle.com 	return (ret);
22927613SVikram.Hegde@Sun.COM }
22937613SVikram.Hegde@Sun.COM 
229411600SVikram.Hegde@Sun.COM 
229511600SVikram.Hegde@Sun.COM 
2296509Smrj /*ARGSUSED*/
2297509Smrj static int
rootnex_coredma_unbindhdl(dev_info_t * dip,dev_info_t * rdip,ddi_dma_handle_t handle)22987613SVikram.Hegde@Sun.COM rootnex_coredma_unbindhdl(dev_info_t *dip, dev_info_t *rdip,
2299509Smrj     ddi_dma_handle_t handle)
2300509Smrj {
2301509Smrj 	ddi_dma_impl_t *hp;
2302509Smrj 	rootnex_dma_t *dma;
2303509Smrj 	int e;
2304509Smrj 
2305509Smrj 	hp = (ddi_dma_impl_t *)handle;
2306509Smrj 	dma = (rootnex_dma_t *)hp->dmai_private;
2307509Smrj 
2308509Smrj 	/* make sure the buffer wasn't free'd before calling unbind */
2309509Smrj 	if (rootnex_unbind_verify_buffer) {
2310509Smrj 		e = rootnex_verify_buffer(dma);
2311509Smrj 		if (e != DDI_SUCCESS) {
2312509Smrj 			ASSERT(0);
2313509Smrj 			return (DDI_FAILURE);
2314509Smrj 		}
2315509Smrj 	}
2316509Smrj 
2317509Smrj 	/* sync the current window before unbinding the buffer */
2318509Smrj 	if (dma->dp_window && dma->dp_window[dma->dp_current_win].wd_dosync &&
2319509Smrj 	    (hp->dmai_rflags & DDI_DMA_READ)) {
23208215SVikram.Hegde@Sun.COM 		(void) rootnex_coredma_sync(dip, rdip, handle, 0, 0,
2321509Smrj 		    DDI_DMA_SYNC_FORCPU);
2322509Smrj 	}
2323509Smrj 
2324509Smrj 	/*
2325509Smrj 	 * cleanup and copy buffer or window state. if we didn't use the copy
2326509Smrj 	 * buffer or windows, there won't be much to do :-)
2327509Smrj 	 */
2328509Smrj 	rootnex_teardown_copybuf(dma);
2329509Smrj 	rootnex_teardown_windows(dma);
2330509Smrj 
233113050Sfrank.van.der.linden@oracle.com 	if (IOMMU_USED(rdip))
233213050Sfrank.van.der.linden@oracle.com 		(void) iommulib_nexdma_unmapobject(dip, rdip, handle,
233313050Sfrank.van.der.linden@oracle.com 		    &dma->dp_dvma);
23347589SVikram.Hegde@Sun.COM 
23357589SVikram.Hegde@Sun.COM 	/*
2336509Smrj 	 * If we had to allocate space to for the worse case sgl (it didn't
2337509Smrj 	 * fit into our pre-allocate buffer), free that up now
2338509Smrj 	 */
2339509Smrj 	if (dma->dp_need_to_free_cookie) {
2340509Smrj 		kmem_free(dma->dp_cookies, dma->dp_cookie_size);
2341509Smrj 	}
2342509Smrj 
2343509Smrj 	/*
2344509Smrj 	 * clean up the handle so it's ready for the next bind (i.e. if the
2345509Smrj 	 * handle is reused).
2346509Smrj 	 */
2347509Smrj 	rootnex_clean_dmahdl(hp);
234812027SStephen.Hanson@Sun.COM 	hp->dmai_error.err_cf = NULL;
2349509Smrj 
235010902SMark.Johnson@Sun.COM 	ROOTNEX_DPROF_DEC(&rootnex_cnt[ROOTNEX_CNT_ACTIVE_BINDS]);
235110902SMark.Johnson@Sun.COM 	ROOTNEX_DPROBE1(rootnex__unbind, uint64_t,
2352509Smrj 	    rootnex_cnt[ROOTNEX_CNT_ACTIVE_BINDS]);
2353509Smrj 
2354509Smrj 	return (DDI_SUCCESS);
2355509Smrj }
2356509Smrj 
23577613SVikram.Hegde@Sun.COM /*
23587613SVikram.Hegde@Sun.COM  * rootnex_dma_unbindhdl()
23597613SVikram.Hegde@Sun.COM  *    called from ddi_dma_unbind_handle()
23607613SVikram.Hegde@Sun.COM  */
23617613SVikram.Hegde@Sun.COM /*ARGSUSED*/
23627613SVikram.Hegde@Sun.COM static int
rootnex_dma_unbindhdl(dev_info_t * dip,dev_info_t * rdip,ddi_dma_handle_t handle)23637613SVikram.Hegde@Sun.COM rootnex_dma_unbindhdl(dev_info_t *dip, dev_info_t *rdip,
23647613SVikram.Hegde@Sun.COM     ddi_dma_handle_t handle)
23657613SVikram.Hegde@Sun.COM {
236613050Sfrank.van.der.linden@oracle.com 	int ret;
236713050Sfrank.van.der.linden@oracle.com 
236811600SVikram.Hegde@Sun.COM #if defined(__amd64) && !defined(__xpv)
236913050Sfrank.van.der.linden@oracle.com 	if (IOMMU_USED(rdip))
237013050Sfrank.van.der.linden@oracle.com 		ret = iommulib_nexdma_unbindhdl(dip, rdip, handle);
237113050Sfrank.van.der.linden@oracle.com 	else
23727613SVikram.Hegde@Sun.COM #endif
237313050Sfrank.van.der.linden@oracle.com 	ret = rootnex_coredma_unbindhdl(dip, rdip, handle);
237413050Sfrank.van.der.linden@oracle.com 
237513050Sfrank.van.der.linden@oracle.com 	if (rootnex_state->r_dvma_call_list_id)
237613050Sfrank.van.der.linden@oracle.com 		ddi_run_callback(&rootnex_state->r_dvma_call_list_id);
237713050Sfrank.van.der.linden@oracle.com 
237813050Sfrank.van.der.linden@oracle.com 	return (ret);
23797613SVikram.Hegde@Sun.COM }
23807613SVikram.Hegde@Sun.COM 
238111600SVikram.Hegde@Sun.COM #if defined(__amd64) && !defined(__xpv)
23828215SVikram.Hegde@Sun.COM 
23838215SVikram.Hegde@Sun.COM static int
rootnex_coredma_get_sleep_flags(ddi_dma_handle_t handle)23848215SVikram.Hegde@Sun.COM rootnex_coredma_get_sleep_flags(ddi_dma_handle_t handle)
23858215SVikram.Hegde@Sun.COM {
23868215SVikram.Hegde@Sun.COM 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
23878215SVikram.Hegde@Sun.COM 	rootnex_dma_t *dma = (rootnex_dma_t *)hp->dmai_private;
23888215SVikram.Hegde@Sun.COM 
23898215SVikram.Hegde@Sun.COM 	if (dma->dp_sleep_flags != KM_SLEEP &&
23908215SVikram.Hegde@Sun.COM 	    dma->dp_sleep_flags != KM_NOSLEEP)
23918215SVikram.Hegde@Sun.COM 		cmn_err(CE_PANIC, "kmem sleep flags not set in DMA handle");
23928215SVikram.Hegde@Sun.COM 	return (dma->dp_sleep_flags);
23938215SVikram.Hegde@Sun.COM }
23947613SVikram.Hegde@Sun.COM /*ARGSUSED*/
23957613SVikram.Hegde@Sun.COM static void
rootnex_coredma_reset_cookies(dev_info_t * dip,ddi_dma_handle_t handle)23967613SVikram.Hegde@Sun.COM rootnex_coredma_reset_cookies(dev_info_t *dip, ddi_dma_handle_t handle)
23977613SVikram.Hegde@Sun.COM {
23987613SVikram.Hegde@Sun.COM 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
23997613SVikram.Hegde@Sun.COM 	rootnex_dma_t *dma = (rootnex_dma_t *)hp->dmai_private;
24008215SVikram.Hegde@Sun.COM 	rootnex_window_t *window;
24018215SVikram.Hegde@Sun.COM 
24028215SVikram.Hegde@Sun.COM 	if (dma->dp_window) {
24038215SVikram.Hegde@Sun.COM 		window = &dma->dp_window[dma->dp_current_win];
24048215SVikram.Hegde@Sun.COM 		hp->dmai_cookie = window->wd_first_cookie;
24058215SVikram.Hegde@Sun.COM 	} else {
24068215SVikram.Hegde@Sun.COM 		hp->dmai_cookie = dma->dp_cookies;
24078215SVikram.Hegde@Sun.COM 	}
24087613SVikram.Hegde@Sun.COM 	hp->dmai_cookie++;
24097613SVikram.Hegde@Sun.COM }
24107613SVikram.Hegde@Sun.COM 
24117613SVikram.Hegde@Sun.COM /*ARGSUSED*/
24127613SVikram.Hegde@Sun.COM static int
rootnex_coredma_get_cookies(dev_info_t * dip,ddi_dma_handle_t handle,ddi_dma_cookie_t ** cookiepp,uint_t * ccountp)24137613SVikram.Hegde@Sun.COM rootnex_coredma_get_cookies(dev_info_t *dip, ddi_dma_handle_t handle,
24148215SVikram.Hegde@Sun.COM     ddi_dma_cookie_t **cookiepp, uint_t *ccountp)
24158215SVikram.Hegde@Sun.COM {
24168215SVikram.Hegde@Sun.COM 	int i;
24178215SVikram.Hegde@Sun.COM 	int km_flags;
24188215SVikram.Hegde@Sun.COM 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
24198215SVikram.Hegde@Sun.COM 	rootnex_dma_t *dma = (rootnex_dma_t *)hp->dmai_private;
24208215SVikram.Hegde@Sun.COM 	rootnex_window_t *window;
24218215SVikram.Hegde@Sun.COM 	ddi_dma_cookie_t *cp;
24228215SVikram.Hegde@Sun.COM 	ddi_dma_cookie_t *cookie;
24238215SVikram.Hegde@Sun.COM 
24248215SVikram.Hegde@Sun.COM 	ASSERT(*cookiepp == NULL);
24258215SVikram.Hegde@Sun.COM 	ASSERT(*ccountp == 0);
24268215SVikram.Hegde@Sun.COM 
24278215SVikram.Hegde@Sun.COM 	if (dma->dp_window) {
24288215SVikram.Hegde@Sun.COM 		window = &dma->dp_window[dma->dp_current_win];
24298215SVikram.Hegde@Sun.COM 		cp = window->wd_first_cookie;
24308215SVikram.Hegde@Sun.COM 		*ccountp = window->wd_cookie_cnt;
24318215SVikram.Hegde@Sun.COM 	} else {
24328215SVikram.Hegde@Sun.COM 		cp = dma->dp_cookies;
24338215SVikram.Hegde@Sun.COM 		*ccountp = dma->dp_sglinfo.si_sgl_size;
24348215SVikram.Hegde@Sun.COM 	}
24358215SVikram.Hegde@Sun.COM 
24368215SVikram.Hegde@Sun.COM 	km_flags = rootnex_coredma_get_sleep_flags(handle);
24378215SVikram.Hegde@Sun.COM 	cookie = kmem_zalloc(sizeof (ddi_dma_cookie_t) * (*ccountp), km_flags);
24388215SVikram.Hegde@Sun.COM 	if (cookie == NULL) {
24398215SVikram.Hegde@Sun.COM 		return (DDI_DMA_NORESOURCES);
24408215SVikram.Hegde@Sun.COM 	}
24418215SVikram.Hegde@Sun.COM 
24428215SVikram.Hegde@Sun.COM 	for (i = 0; i < *ccountp; i++) {
24438215SVikram.Hegde@Sun.COM 		cookie[i].dmac_notused = cp[i].dmac_notused;
24448215SVikram.Hegde@Sun.COM 		cookie[i].dmac_type = cp[i].dmac_type;
24458215SVikram.Hegde@Sun.COM 		cookie[i].dmac_address = cp[i].dmac_address;
24468215SVikram.Hegde@Sun.COM 		cookie[i].dmac_size = cp[i].dmac_size;
24478215SVikram.Hegde@Sun.COM 	}
24488215SVikram.Hegde@Sun.COM 
24498215SVikram.Hegde@Sun.COM 	*cookiepp = cookie;
24508215SVikram.Hegde@Sun.COM 
24518215SVikram.Hegde@Sun.COM 	return (DDI_SUCCESS);
24528215SVikram.Hegde@Sun.COM }
24538215SVikram.Hegde@Sun.COM 
24548215SVikram.Hegde@Sun.COM /*ARGSUSED*/
24558215SVikram.Hegde@Sun.COM static int
rootnex_coredma_set_cookies(dev_info_t * dip,ddi_dma_handle_t handle,ddi_dma_cookie_t * cookiep,uint_t ccount)24568215SVikram.Hegde@Sun.COM rootnex_coredma_set_cookies(dev_info_t *dip, ddi_dma_handle_t handle,
24578215SVikram.Hegde@Sun.COM     ddi_dma_cookie_t *cookiep, uint_t ccount)
24587613SVikram.Hegde@Sun.COM {
24597613SVikram.Hegde@Sun.COM 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
24607613SVikram.Hegde@Sun.COM 	rootnex_dma_t *dma = (rootnex_dma_t *)hp->dmai_private;
24618215SVikram.Hegde@Sun.COM 	rootnex_window_t *window;
24628215SVikram.Hegde@Sun.COM 	ddi_dma_cookie_t *cur_cookiep;
24638215SVikram.Hegde@Sun.COM 
24648215SVikram.Hegde@Sun.COM 	ASSERT(cookiep);
24658215SVikram.Hegde@Sun.COM 	ASSERT(ccount != 0);
24668215SVikram.Hegde@Sun.COM 	ASSERT(dma->dp_need_to_switch_cookies == B_FALSE);
24678215SVikram.Hegde@Sun.COM 
24688215SVikram.Hegde@Sun.COM 	if (dma->dp_window) {
24698215SVikram.Hegde@Sun.COM 		window = &dma->dp_window[dma->dp_current_win];
24708215SVikram.Hegde@Sun.COM 		dma->dp_saved_cookies = window->wd_first_cookie;
24718215SVikram.Hegde@Sun.COM 		window->wd_first_cookie = cookiep;
24728215SVikram.Hegde@Sun.COM 		ASSERT(ccount == window->wd_cookie_cnt);
24738215SVikram.Hegde@Sun.COM 		cur_cookiep = (hp->dmai_cookie - dma->dp_saved_cookies)
24748215SVikram.Hegde@Sun.COM 		    + window->wd_first_cookie;
24757613SVikram.Hegde@Sun.COM 	} else {
24768215SVikram.Hegde@Sun.COM 		dma->dp_saved_cookies = dma->dp_cookies;
24778215SVikram.Hegde@Sun.COM 		dma->dp_cookies = cookiep;
24788215SVikram.Hegde@Sun.COM 		ASSERT(ccount == dma->dp_sglinfo.si_sgl_size);
24798215SVikram.Hegde@Sun.COM 		cur_cookiep = (hp->dmai_cookie - dma->dp_saved_cookies)
24808215SVikram.Hegde@Sun.COM 		    + dma->dp_cookies;
24817613SVikram.Hegde@Sun.COM 	}
24828215SVikram.Hegde@Sun.COM 
24838215SVikram.Hegde@Sun.COM 	dma->dp_need_to_switch_cookies = B_TRUE;
24848215SVikram.Hegde@Sun.COM 	hp->dmai_cookie = cur_cookiep;
24857613SVikram.Hegde@Sun.COM 
24867613SVikram.Hegde@Sun.COM 	return (DDI_SUCCESS);
24877613SVikram.Hegde@Sun.COM }
24888215SVikram.Hegde@Sun.COM 
24898215SVikram.Hegde@Sun.COM /*ARGSUSED*/
24908215SVikram.Hegde@Sun.COM static int
rootnex_coredma_clear_cookies(dev_info_t * dip,ddi_dma_handle_t handle)24918215SVikram.Hegde@Sun.COM rootnex_coredma_clear_cookies(dev_info_t *dip, ddi_dma_handle_t handle)
24928215SVikram.Hegde@Sun.COM {
24938215SVikram.Hegde@Sun.COM 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
24948215SVikram.Hegde@Sun.COM 	rootnex_dma_t *dma = (rootnex_dma_t *)hp->dmai_private;
24958215SVikram.Hegde@Sun.COM 	rootnex_window_t *window;
24968215SVikram.Hegde@Sun.COM 	ddi_dma_cookie_t *cur_cookiep;
24978215SVikram.Hegde@Sun.COM 	ddi_dma_cookie_t *cookie_array;
24988215SVikram.Hegde@Sun.COM 	uint_t ccount;
24998215SVikram.Hegde@Sun.COM 
25008215SVikram.Hegde@Sun.COM 	/* check if cookies have not been switched */
25018215SVikram.Hegde@Sun.COM 	if (dma->dp_need_to_switch_cookies == B_FALSE)
25028215SVikram.Hegde@Sun.COM 		return (DDI_SUCCESS);
25038215SVikram.Hegde@Sun.COM 
25048215SVikram.Hegde@Sun.COM 	ASSERT(dma->dp_saved_cookies);
25058215SVikram.Hegde@Sun.COM 
25068215SVikram.Hegde@Sun.COM 	if (dma->dp_window) {
25078215SVikram.Hegde@Sun.COM 		window = &dma->dp_window[dma->dp_current_win];
25088215SVikram.Hegde@Sun.COM 		cookie_array = window->wd_first_cookie;
25098215SVikram.Hegde@Sun.COM 		window->wd_first_cookie = dma->dp_saved_cookies;
25108215SVikram.Hegde@Sun.COM 		dma->dp_saved_cookies = NULL;
25118215SVikram.Hegde@Sun.COM 		ccount = window->wd_cookie_cnt;
25128215SVikram.Hegde@Sun.COM 		cur_cookiep = (hp->dmai_cookie - cookie_array)
25138215SVikram.Hegde@Sun.COM 		    + window->wd_first_cookie;
25148215SVikram.Hegde@Sun.COM 	} else {
25158215SVikram.Hegde@Sun.COM 		cookie_array = dma->dp_cookies;
25168215SVikram.Hegde@Sun.COM 		dma->dp_cookies = dma->dp_saved_cookies;
25178215SVikram.Hegde@Sun.COM 		dma->dp_saved_cookies = NULL;
25188215SVikram.Hegde@Sun.COM 		ccount = dma->dp_sglinfo.si_sgl_size;
25198215SVikram.Hegde@Sun.COM 		cur_cookiep = (hp->dmai_cookie - cookie_array)
25208215SVikram.Hegde@Sun.COM 		    + dma->dp_cookies;
25218215SVikram.Hegde@Sun.COM 	}
25228215SVikram.Hegde@Sun.COM 
25238215SVikram.Hegde@Sun.COM 	kmem_free(cookie_array, sizeof (ddi_dma_cookie_t) * ccount);
25248215SVikram.Hegde@Sun.COM 
25258215SVikram.Hegde@Sun.COM 	hp->dmai_cookie = cur_cookiep;
25268215SVikram.Hegde@Sun.COM 
25278215SVikram.Hegde@Sun.COM 	dma->dp_need_to_switch_cookies = B_FALSE;
25288215SVikram.Hegde@Sun.COM 
25298215SVikram.Hegde@Sun.COM 	return (DDI_SUCCESS);
25308215SVikram.Hegde@Sun.COM }
25318215SVikram.Hegde@Sun.COM 
25327617SVikram.Hegde@Sun.COM #endif
2533509Smrj 
253413050Sfrank.van.der.linden@oracle.com static struct as *
rootnex_get_as(ddi_dma_obj_t * dmao)253513050Sfrank.van.der.linden@oracle.com rootnex_get_as(ddi_dma_obj_t *dmao)
253613050Sfrank.van.der.linden@oracle.com {
253713050Sfrank.van.der.linden@oracle.com 	struct as *asp;
253813050Sfrank.van.der.linden@oracle.com 
253913050Sfrank.van.der.linden@oracle.com 	switch (dmao->dmao_type) {
254013050Sfrank.van.der.linden@oracle.com 	case DMA_OTYP_VADDR:
254113050Sfrank.van.der.linden@oracle.com 	case DMA_OTYP_BUFVADDR:
254213050Sfrank.van.der.linden@oracle.com 		asp = dmao->dmao_obj.virt_obj.v_as;
254313050Sfrank.van.der.linden@oracle.com 		if (asp == NULL)
254413050Sfrank.van.der.linden@oracle.com 			asp = &kas;
254513050Sfrank.van.der.linden@oracle.com 		break;
254613050Sfrank.van.der.linden@oracle.com 	default:
254713050Sfrank.van.der.linden@oracle.com 		asp = NULL;
254813050Sfrank.van.der.linden@oracle.com 		break;
254913050Sfrank.van.der.linden@oracle.com 	}
255013050Sfrank.van.der.linden@oracle.com 	return (asp);
255113050Sfrank.van.der.linden@oracle.com }
255213050Sfrank.van.der.linden@oracle.com 
2553509Smrj /*
2554509Smrj  * rootnex_verify_buffer()
2555509Smrj  *   verify buffer wasn't free'd
2556509Smrj  */
2557509Smrj static int
rootnex_verify_buffer(rootnex_dma_t * dma)2558509Smrj rootnex_verify_buffer(rootnex_dma_t *dma)
2559509Smrj {
2560509Smrj 	page_t **pplist;
2561509Smrj 	caddr_t vaddr;
2562509Smrj 	uint_t pcnt;
2563509Smrj 	uint_t poff;
2564509Smrj 	page_t *pp;
25651865Sdilpreet 	char b;
2566509Smrj 	int i;
2567509Smrj 
2568509Smrj 	/* Figure out how many pages this buffer occupies */
2569509Smrj 	if (dma->dp_dma.dmao_type == DMA_OTYP_PAGES) {
2570509Smrj 		poff = dma->dp_dma.dmao_obj.pp_obj.pp_offset & MMU_PAGEOFFSET;
2571509Smrj 	} else {
2572509Smrj 		vaddr = dma->dp_dma.dmao_obj.virt_obj.v_addr;
2573509Smrj 		poff = (uintptr_t)vaddr & MMU_PAGEOFFSET;
2574509Smrj 	}
2575509Smrj 	pcnt = mmu_btopr(dma->dp_dma.dmao_size + poff);
2576509Smrj 
2577509Smrj 	switch (dma->dp_dma.dmao_type) {
25780Sstevel@tonic-gate 	case DMA_OTYP_PAGES:
2579509Smrj 		/*
2580509Smrj 		 * for a linked list of pp's walk through them to make sure
2581509Smrj 		 * they're locked and not free.
2582509Smrj 		 */
2583509Smrj 		pp = dma->dp_dma.dmao_obj.pp_obj.pp_pp;
2584509Smrj 		for (i = 0; i < pcnt; i++) {
2585509Smrj 			if (PP_ISFREE(pp) || !PAGE_LOCKED(pp)) {
2586509Smrj 				return (DDI_FAILURE);
25870Sstevel@tonic-gate 			}
2588509Smrj 			pp = pp->p_next;
25890Sstevel@tonic-gate 		}
25900Sstevel@tonic-gate 		break;
2591509Smrj 
25920Sstevel@tonic-gate 	case DMA_OTYP_VADDR:
25930Sstevel@tonic-gate 	case DMA_OTYP_BUFVADDR:
2594509Smrj 		pplist = dma->dp_dma.dmao_obj.virt_obj.v_priv;
2595509Smrj 		/*
2596509Smrj 		 * for an array of pp's walk through them to make sure they're
2597509Smrj 		 * not free. It's possible that they may not be locked.
2598509Smrj 		 */
2599509Smrj 		if (pplist) {
2600509Smrj 			for (i = 0; i < pcnt; i++) {
2601509Smrj 				if (PP_ISFREE(pplist[i])) {
2602509Smrj 					return (DDI_FAILURE);
2603509Smrj 				}
2604509Smrj 			}
2605509Smrj 
2606509Smrj 		/* For a virtual address, try to peek at each page */
2607509Smrj 		} else {
260813050Sfrank.van.der.linden@oracle.com 			if (rootnex_get_as(&dma->dp_dma) == &kas) {
2609509Smrj 				for (i = 0; i < pcnt; i++) {
26101865Sdilpreet 					if (ddi_peek8(NULL, vaddr, &b) ==
26111865Sdilpreet 					    DDI_FAILURE)
2612509Smrj 						return (DDI_FAILURE);
26131865Sdilpreet 					vaddr += MMU_PAGESIZE;
2614509Smrj 				}
2615509Smrj 			}
2616509Smrj 		}
2617509Smrj 		break;
2618509Smrj 
2619509Smrj 	default:
262013050Sfrank.van.der.linden@oracle.com 		cmn_err(CE_PANIC, "rootnex_verify_buffer: bad DMA object");
2621509Smrj 		break;
2622509Smrj 	}
2623509Smrj 
2624509Smrj 	return (DDI_SUCCESS);
2625509Smrj }
2626509Smrj 
2627509Smrj 
2628509Smrj /*
2629509Smrj  * rootnex_clean_dmahdl()
2630509Smrj  *    Clean the dma handle. This should be called on a handle alloc and an
2631509Smrj  *    unbind handle. Set the handle state to the default settings.
2632509Smrj  */
2633509Smrj static void
rootnex_clean_dmahdl(ddi_dma_impl_t * hp)2634509Smrj rootnex_clean_dmahdl(ddi_dma_impl_t *hp)
2635509Smrj {
2636509Smrj 	rootnex_dma_t *dma;
2637509Smrj 
2638509Smrj 
2639509Smrj 	dma = (rootnex_dma_t *)hp->dmai_private;
2640509Smrj 
2641509Smrj 	hp->dmai_nwin = 0;
2642509Smrj 	dma->dp_current_cookie = 0;
2643509Smrj 	dma->dp_copybuf_size = 0;
2644509Smrj 	dma->dp_window = NULL;
2645509Smrj 	dma->dp_cbaddr = NULL;
2646509Smrj 	dma->dp_inuse = B_FALSE;
264713050Sfrank.van.der.linden@oracle.com 	dma->dp_dvma_used = B_FALSE;
2648509Smrj 	dma->dp_need_to_free_cookie = B_FALSE;
26498215SVikram.Hegde@Sun.COM 	dma->dp_need_to_switch_cookies = B_FALSE;
26508215SVikram.Hegde@Sun.COM 	dma->dp_saved_cookies = NULL;
26518215SVikram.Hegde@Sun.COM 	dma->dp_sleep_flags = KM_PANIC;
2652509Smrj 	dma->dp_need_to_free_window = B_FALSE;
2653509Smrj 	dma->dp_partial_required = B_FALSE;
2654509Smrj 	dma->dp_trim_required = B_FALSE;
2655509Smrj 	dma->dp_sglinfo.si_copybuf_req = 0;
2656509Smrj #if !defined(__amd64)
2657509Smrj 	dma->dp_cb_remaping = B_FALSE;
2658509Smrj 	dma->dp_kva = NULL;
2659509Smrj #endif
2660509Smrj 
2661509Smrj 	/* FMA related initialization */
2662509Smrj 	hp->dmai_fault = 0;
2663509Smrj 	hp->dmai_fault_check = NULL;
2664509Smrj 	hp->dmai_fault_notify = NULL;
2665509Smrj 	hp->dmai_error.err_ena = 0;
2666509Smrj 	hp->dmai_error.err_status = DDI_FM_OK;
2667509Smrj 	hp->dmai_error.err_expected = DDI_FM_ERR_UNEXPECTED;
2668509Smrj 	hp->dmai_error.err_ontrap = NULL;
2669509Smrj }
2670509Smrj 
2671509Smrj 
2672509Smrj /*
2673509Smrj  * rootnex_valid_alloc_parms()
2674509Smrj  *    Called in ddi_dma_alloc_handle path to validate its parameters.
2675509Smrj  */
2676509Smrj static int
rootnex_valid_alloc_parms(ddi_dma_attr_t * attr,uint_t maxsegmentsize)2677509Smrj rootnex_valid_alloc_parms(ddi_dma_attr_t *attr, uint_t maxsegmentsize)
2678509Smrj {
2679509Smrj 	if ((attr->dma_attr_seg < MMU_PAGEOFFSET) ||
2680509Smrj 	    (attr->dma_attr_count_max < MMU_PAGEOFFSET) ||
2681509Smrj 	    (attr->dma_attr_granular > MMU_PAGESIZE) ||
2682509Smrj 	    (attr->dma_attr_maxxfer < MMU_PAGESIZE)) {
2683509Smrj 		return (DDI_DMA_BADATTR);
2684509Smrj 	}
2685509Smrj 
2686509Smrj 	if (attr->dma_attr_addr_hi <= attr->dma_attr_addr_lo) {
2687509Smrj 		return (DDI_DMA_BADATTR);
2688509Smrj 	}
2689509Smrj 
2690509Smrj 	if ((attr->dma_attr_seg & MMU_PAGEOFFSET) != MMU_PAGEOFFSET ||
2691509Smrj 	    MMU_PAGESIZE & (attr->dma_attr_granular - 1) ||
2692509Smrj 	    attr->dma_attr_sgllen <= 0) {
2693509Smrj 		return (DDI_DMA_BADATTR);
2694509Smrj 	}
2695509Smrj 
2696509Smrj 	/* We should be able to DMA into every byte offset in a page */
2697509Smrj 	if (maxsegmentsize < MMU_PAGESIZE) {
2698509Smrj 		return (DDI_DMA_BADATTR);
2699509Smrj 	}
2700509Smrj 
270111793SMark.Johnson@Sun.COM 	/* if we're bouncing on seg, seg must be <= addr_hi */
270211793SMark.Johnson@Sun.COM 	if ((attr->dma_attr_flags & _DDI_DMA_BOUNCE_ON_SEG) &&
270311793SMark.Johnson@Sun.COM 	    (attr->dma_attr_seg > attr->dma_attr_addr_hi)) {
270411793SMark.Johnson@Sun.COM 		return (DDI_DMA_BADATTR);
270511793SMark.Johnson@Sun.COM 	}
2706509Smrj 	return (DDI_SUCCESS);
2707509Smrj }
2708509Smrj 
2709509Smrj /*
2710509Smrj  * rootnex_valid_bind_parms()
2711509Smrj  *    Called in ddi_dma_*_bind_handle path to validate its parameters.
2712509Smrj  */
2713509Smrj /* ARGSUSED */
2714509Smrj static int
rootnex_valid_bind_parms(ddi_dma_req_t * dmareq,ddi_dma_attr_t * attr)2715509Smrj rootnex_valid_bind_parms(ddi_dma_req_t *dmareq, ddi_dma_attr_t *attr)
2716509Smrj {
2717509Smrj #if !defined(__amd64)
2718509Smrj 	/*
2719509Smrj 	 * we only support up to a 2G-1 transfer size on 32-bit kernels so
2720509Smrj 	 * we can track the offset for the obsoleted interfaces.
2721509Smrj 	 */
2722509Smrj 	if (dmareq->dmar_object.dmao_size > 0x7FFFFFFF) {
2723509Smrj 		return (DDI_DMA_TOOBIG);
2724509Smrj 	}
2725509Smrj #endif
2726509Smrj 
2727509Smrj 	return (DDI_SUCCESS);
2728509Smrj }
2729509Smrj 
2730509Smrj 
2731509Smrj /*
273211793SMark.Johnson@Sun.COM  * rootnex_need_bounce_seg()
273311793SMark.Johnson@Sun.COM  *    check to see if the buffer lives on both side of the seg.
273411793SMark.Johnson@Sun.COM  */
273511793SMark.Johnson@Sun.COM static boolean_t
rootnex_need_bounce_seg(ddi_dma_obj_t * dmar_object,rootnex_sglinfo_t * sglinfo)273611793SMark.Johnson@Sun.COM rootnex_need_bounce_seg(ddi_dma_obj_t *dmar_object, rootnex_sglinfo_t *sglinfo)
273711793SMark.Johnson@Sun.COM {
273811793SMark.Johnson@Sun.COM 	ddi_dma_atyp_t buftype;
273911793SMark.Johnson@Sun.COM 	rootnex_addr_t raddr;
274011793SMark.Johnson@Sun.COM 	boolean_t lower_addr;
274111793SMark.Johnson@Sun.COM 	boolean_t upper_addr;
274211793SMark.Johnson@Sun.COM 	uint64_t offset;
274311793SMark.Johnson@Sun.COM 	page_t **pplist;
274411793SMark.Johnson@Sun.COM 	uint64_t paddr;
274511793SMark.Johnson@Sun.COM 	uint32_t psize;
274611793SMark.Johnson@Sun.COM 	uint32_t size;
274711793SMark.Johnson@Sun.COM 	caddr_t vaddr;
274811793SMark.Johnson@Sun.COM 	uint_t pcnt;
274911793SMark.Johnson@Sun.COM 	page_t *pp;
275011793SMark.Johnson@Sun.COM 
275111793SMark.Johnson@Sun.COM 
275211793SMark.Johnson@Sun.COM 	/* shortcuts */
275311793SMark.Johnson@Sun.COM 	pplist = dmar_object->dmao_obj.virt_obj.v_priv;
275411793SMark.Johnson@Sun.COM 	vaddr = dmar_object->dmao_obj.virt_obj.v_addr;
275511793SMark.Johnson@Sun.COM 	buftype = dmar_object->dmao_type;
275611793SMark.Johnson@Sun.COM 	size = dmar_object->dmao_size;
275711793SMark.Johnson@Sun.COM 
275811793SMark.Johnson@Sun.COM 	lower_addr = B_FALSE;
275911793SMark.Johnson@Sun.COM 	upper_addr = B_FALSE;
276011793SMark.Johnson@Sun.COM 	pcnt = 0;
276111793SMark.Johnson@Sun.COM 
276211793SMark.Johnson@Sun.COM 	/*
276311793SMark.Johnson@Sun.COM 	 * Process the first page to handle the initial offset of the buffer.
276411793SMark.Johnson@Sun.COM 	 * We'll use the base address we get later when we loop through all
276511793SMark.Johnson@Sun.COM 	 * the pages.
276611793SMark.Johnson@Sun.COM 	 */
276711793SMark.Johnson@Sun.COM 	if (buftype == DMA_OTYP_PAGES) {
276811793SMark.Johnson@Sun.COM 		pp = dmar_object->dmao_obj.pp_obj.pp_pp;
276911793SMark.Johnson@Sun.COM 		offset =  dmar_object->dmao_obj.pp_obj.pp_offset &
277011793SMark.Johnson@Sun.COM 		    MMU_PAGEOFFSET;
277111793SMark.Johnson@Sun.COM 		paddr = pfn_to_pa(pp->p_pagenum) + offset;
277211793SMark.Johnson@Sun.COM 		psize = MIN(size, (MMU_PAGESIZE - offset));
277311793SMark.Johnson@Sun.COM 		pp = pp->p_next;
277411793SMark.Johnson@Sun.COM 		sglinfo->si_asp = NULL;
277511793SMark.Johnson@Sun.COM 	} else if (pplist != NULL) {
277611793SMark.Johnson@Sun.COM 		offset = (uintptr_t)vaddr & MMU_PAGEOFFSET;
277711793SMark.Johnson@Sun.COM 		sglinfo->si_asp = dmar_object->dmao_obj.virt_obj.v_as;
277811793SMark.Johnson@Sun.COM 		if (sglinfo->si_asp == NULL) {
277911793SMark.Johnson@Sun.COM 			sglinfo->si_asp = &kas;
278011793SMark.Johnson@Sun.COM 		}
278111793SMark.Johnson@Sun.COM 		paddr = pfn_to_pa(pplist[pcnt]->p_pagenum);
278211793SMark.Johnson@Sun.COM 		paddr += offset;
278311793SMark.Johnson@Sun.COM 		psize = MIN(size, (MMU_PAGESIZE - offset));
278411793SMark.Johnson@Sun.COM 		pcnt++;
278511793SMark.Johnson@Sun.COM 	} else {
278611793SMark.Johnson@Sun.COM 		offset = (uintptr_t)vaddr & MMU_PAGEOFFSET;
278711793SMark.Johnson@Sun.COM 		sglinfo->si_asp = dmar_object->dmao_obj.virt_obj.v_as;
278811793SMark.Johnson@Sun.COM 		if (sglinfo->si_asp == NULL) {
278911793SMark.Johnson@Sun.COM 			sglinfo->si_asp = &kas;
279011793SMark.Johnson@Sun.COM 		}
279111793SMark.Johnson@Sun.COM 		paddr = pfn_to_pa(hat_getpfnum(sglinfo->si_asp->a_hat, vaddr));
279211793SMark.Johnson@Sun.COM 		paddr += offset;
279311793SMark.Johnson@Sun.COM 		psize = MIN(size, (MMU_PAGESIZE - offset));
279411793SMark.Johnson@Sun.COM 		vaddr += psize;
279511793SMark.Johnson@Sun.COM 	}
279611793SMark.Johnson@Sun.COM 
279713050Sfrank.van.der.linden@oracle.com 	raddr = ROOTNEX_PADDR_TO_RBASE(paddr);
279811793SMark.Johnson@Sun.COM 
279911793SMark.Johnson@Sun.COM 	if ((raddr + psize) > sglinfo->si_segmask) {
280011793SMark.Johnson@Sun.COM 		upper_addr = B_TRUE;
280111793SMark.Johnson@Sun.COM 	} else {
280211793SMark.Johnson@Sun.COM 		lower_addr = B_TRUE;
280311793SMark.Johnson@Sun.COM 	}
280411793SMark.Johnson@Sun.COM 	size -= psize;
280511793SMark.Johnson@Sun.COM 
280611793SMark.Johnson@Sun.COM 	/*
280711793SMark.Johnson@Sun.COM 	 * Walk through the rest of the pages in the buffer. Track to see
280811793SMark.Johnson@Sun.COM 	 * if we have pages on both sides of the segment boundary.
280911793SMark.Johnson@Sun.COM 	 */
281011793SMark.Johnson@Sun.COM 	while (size > 0) {
281111793SMark.Johnson@Sun.COM 		/* partial or full page */
281211793SMark.Johnson@Sun.COM 		psize = MIN(size, MMU_PAGESIZE);
281311793SMark.Johnson@Sun.COM 
281411793SMark.Johnson@Sun.COM 		if (buftype == DMA_OTYP_PAGES) {
281511793SMark.Johnson@Sun.COM 			/* get the paddr from the page_t */
281611793SMark.Johnson@Sun.COM 			ASSERT(!PP_ISFREE(pp) && PAGE_LOCKED(pp));
281711793SMark.Johnson@Sun.COM 			paddr = pfn_to_pa(pp->p_pagenum);
281811793SMark.Johnson@Sun.COM 			pp = pp->p_next;
281911793SMark.Johnson@Sun.COM 		} else if (pplist != NULL) {
282011793SMark.Johnson@Sun.COM 			/* index into the array of page_t's to get the paddr */
282111793SMark.Johnson@Sun.COM 			ASSERT(!PP_ISFREE(pplist[pcnt]));
282211793SMark.Johnson@Sun.COM 			paddr = pfn_to_pa(pplist[pcnt]->p_pagenum);
282311793SMark.Johnson@Sun.COM 			pcnt++;
282411793SMark.Johnson@Sun.COM 		} else {
282511793SMark.Johnson@Sun.COM 			/* call into the VM to get the paddr */
282611793SMark.Johnson@Sun.COM 			paddr =  pfn_to_pa(hat_getpfnum(sglinfo->si_asp->a_hat,
282711793SMark.Johnson@Sun.COM 			    vaddr));
282811793SMark.Johnson@Sun.COM 			vaddr += psize;
282911793SMark.Johnson@Sun.COM 		}
283011793SMark.Johnson@Sun.COM 
283113050Sfrank.van.der.linden@oracle.com 		raddr = ROOTNEX_PADDR_TO_RBASE(paddr);
283211793SMark.Johnson@Sun.COM 
283311793SMark.Johnson@Sun.COM 		if ((raddr + psize) > sglinfo->si_segmask) {
283411793SMark.Johnson@Sun.COM 			upper_addr = B_TRUE;
283511793SMark.Johnson@Sun.COM 		} else {
283611793SMark.Johnson@Sun.COM 			lower_addr = B_TRUE;
283711793SMark.Johnson@Sun.COM 		}
283811793SMark.Johnson@Sun.COM 		/*
283911793SMark.Johnson@Sun.COM 		 * if the buffer lives both above and below the segment
284011793SMark.Johnson@Sun.COM 		 * boundary, or the current page is the page immediately
284111793SMark.Johnson@Sun.COM 		 * after the segment, we will use a copy/bounce buffer for
284211793SMark.Johnson@Sun.COM 		 * all pages > seg.
284311793SMark.Johnson@Sun.COM 		 */
284411793SMark.Johnson@Sun.COM 		if ((lower_addr && upper_addr) ||
284511793SMark.Johnson@Sun.COM 		    (raddr == (sglinfo->si_segmask + 1))) {
284611793SMark.Johnson@Sun.COM 			return (B_TRUE);
284711793SMark.Johnson@Sun.COM 		}
284811793SMark.Johnson@Sun.COM 
284911793SMark.Johnson@Sun.COM 		size -= psize;
285011793SMark.Johnson@Sun.COM 	}
285111793SMark.Johnson@Sun.COM 
285211793SMark.Johnson@Sun.COM 	return (B_FALSE);
285311793SMark.Johnson@Sun.COM }
285411793SMark.Johnson@Sun.COM 
285511793SMark.Johnson@Sun.COM /*
2856509Smrj  * rootnex_get_sgl()
2857509Smrj  *    Called in bind fastpath to get the sgl. Most of this will be replaced
2858509Smrj  *    with a call to the vm layer when vm2.0 comes around...
2859509Smrj  */
2860509Smrj static void
rootnex_get_sgl(ddi_dma_obj_t * dmar_object,ddi_dma_cookie_t * sgl,rootnex_sglinfo_t * sglinfo)2861509Smrj rootnex_get_sgl(ddi_dma_obj_t *dmar_object, ddi_dma_cookie_t *sgl,
2862509Smrj     rootnex_sglinfo_t *sglinfo)
2863509Smrj {
2864509Smrj 	ddi_dma_atyp_t buftype;
28655084Sjohnlev 	rootnex_addr_t raddr;
2866509Smrj 	uint64_t last_page;
2867509Smrj 	uint64_t offset;
2868509Smrj 	uint64_t addrhi;
2869509Smrj 	uint64_t addrlo;
2870509Smrj 	uint64_t maxseg;
2871509Smrj 	page_t **pplist;
2872509Smrj 	uint64_t paddr;
2873509Smrj 	uint32_t psize;
2874509Smrj 	uint32_t size;
2875509Smrj 	caddr_t vaddr;
2876509Smrj 	uint_t pcnt;
2877509Smrj 	page_t *pp;
2878509Smrj 	uint_t cnt;
2879509Smrj 
2880509Smrj 
2881509Smrj 	/* shortcuts */
2882509Smrj 	pplist = dmar_object->dmao_obj.virt_obj.v_priv;
2883509Smrj 	vaddr = dmar_object->dmao_obj.virt_obj.v_addr;
2884509Smrj 	maxseg = sglinfo->si_max_cookie_size;
2885509Smrj 	buftype = dmar_object->dmao_type;
2886509Smrj 	addrhi = sglinfo->si_max_addr;
2887509Smrj 	addrlo = sglinfo->si_min_addr;
2888509Smrj 	size = dmar_object->dmao_size;
2889509Smrj 
2890509Smrj 	pcnt = 0;
2891509Smrj 	cnt = 0;
2892509Smrj 
289311793SMark.Johnson@Sun.COM 
289411793SMark.Johnson@Sun.COM 	/*
289511793SMark.Johnson@Sun.COM 	 * check to see if we need to use the copy buffer for pages over
289611793SMark.Johnson@Sun.COM 	 * the segment attr.
289711793SMark.Johnson@Sun.COM 	 */
289811793SMark.Johnson@Sun.COM 	sglinfo->si_bounce_on_seg = B_FALSE;
289911793SMark.Johnson@Sun.COM 	if (sglinfo->si_flags & _DDI_DMA_BOUNCE_ON_SEG) {
290011793SMark.Johnson@Sun.COM 		sglinfo->si_bounce_on_seg = rootnex_need_bounce_seg(
290111793SMark.Johnson@Sun.COM 		    dmar_object, sglinfo);
290211793SMark.Johnson@Sun.COM 	}
290311793SMark.Johnson@Sun.COM 
2904509Smrj 	/*
2905509Smrj 	 * if we were passed down a linked list of pages, i.e. pointer to
2906509Smrj 	 * page_t, use this to get our physical address and buf offset.
2907509Smrj 	 */
2908509Smrj 	if (buftype == DMA_OTYP_PAGES) {
2909509Smrj 		pp = dmar_object->dmao_obj.pp_obj.pp_pp;
2910509Smrj 		ASSERT(!PP_ISFREE(pp) && PAGE_LOCKED(pp));
2911509Smrj 		offset =  dmar_object->dmao_obj.pp_obj.pp_offset &
2912509Smrj 		    MMU_PAGEOFFSET;
29135084Sjohnlev 		paddr = pfn_to_pa(pp->p_pagenum) + offset;
2914509Smrj 		psize = MIN(size, (MMU_PAGESIZE - offset));
2915509Smrj 		pp = pp->p_next;
2916509Smrj 		sglinfo->si_asp = NULL;
2917509Smrj 
2918509Smrj 	/*
2919509Smrj 	 * We weren't passed down a linked list of pages, but if we were passed
2920509Smrj 	 * down an array of pages, use this to get our physical address and buf
2921509Smrj 	 * offset.
2922509Smrj 	 */
2923509Smrj 	} else if (pplist != NULL) {
2924509Smrj 		ASSERT((buftype == DMA_OTYP_VADDR) ||
2925509Smrj 		    (buftype == DMA_OTYP_BUFVADDR));
2926509Smrj 
2927509Smrj 		offset = (uintptr_t)vaddr & MMU_PAGEOFFSET;
2928509Smrj 		sglinfo->si_asp = dmar_object->dmao_obj.virt_obj.v_as;
2929509Smrj 		if (sglinfo->si_asp == NULL) {
2930509Smrj 			sglinfo->si_asp = &kas;
2931509Smrj 		}
2932509Smrj 
2933509Smrj 		ASSERT(!PP_ISFREE(pplist[pcnt]));
29345084Sjohnlev 		paddr = pfn_to_pa(pplist[pcnt]->p_pagenum);
2935509Smrj 		paddr += offset;
2936509Smrj 		psize = MIN(size, (MMU_PAGESIZE - offset));
2937509Smrj 		pcnt++;
2938509Smrj 
2939509Smrj 	/*
2940509Smrj 	 * All we have is a virtual address, we'll need to call into the VM
2941509Smrj 	 * to get the physical address.
2942509Smrj 	 */
2943509Smrj 	} else {
2944509Smrj 		ASSERT((buftype == DMA_OTYP_VADDR) ||
2945509Smrj 		    (buftype == DMA_OTYP_BUFVADDR));
2946509Smrj 
2947509Smrj 		offset = (uintptr_t)vaddr & MMU_PAGEOFFSET;
2948509Smrj 		sglinfo->si_asp = dmar_object->dmao_obj.virt_obj.v_as;
2949509Smrj 		if (sglinfo->si_asp == NULL) {
2950509Smrj 			sglinfo->si_asp = &kas;
2951509Smrj 		}
2952509Smrj 
29535084Sjohnlev 		paddr = pfn_to_pa(hat_getpfnum(sglinfo->si_asp->a_hat, vaddr));
2954509Smrj 		paddr += offset;
2955509Smrj 		psize = MIN(size, (MMU_PAGESIZE - offset));
2956509Smrj 		vaddr += psize;
2957509Smrj 	}
2958509Smrj 
295913050Sfrank.van.der.linden@oracle.com 	raddr = ROOTNEX_PADDR_TO_RBASE(paddr);
29605084Sjohnlev 
2961509Smrj 	/*
2962509Smrj 	 * Setup the first cookie with the physical address of the page and the
2963509Smrj 	 * size of the page (which takes into account the initial offset into
2964509Smrj 	 * the page.
2965509Smrj 	 */
29665084Sjohnlev 	sgl[cnt].dmac_laddress = raddr;
2967509Smrj 	sgl[cnt].dmac_size = psize;
2968509Smrj 	sgl[cnt].dmac_type = 0;
2969509Smrj 
2970509Smrj 	/*
2971509Smrj 	 * Save away the buffer offset into the page. We'll need this later in
2972509Smrj 	 * the copy buffer code to help figure out the page index within the
2973509Smrj 	 * buffer and the offset into the current page.
2974509Smrj 	 */
2975509Smrj 	sglinfo->si_buf_offset = offset;
2976509Smrj 
2977509Smrj 	/*
297811793SMark.Johnson@Sun.COM 	 * If we are using the copy buffer for anything over the segment
297911793SMark.Johnson@Sun.COM 	 * boundary, and this page is over the segment boundary.
298011793SMark.Johnson@Sun.COM 	 *   OR
298111793SMark.Johnson@Sun.COM 	 * if the DMA engine can't reach the physical address.
2982509Smrj 	 */
298311793SMark.Johnson@Sun.COM 	if (((sglinfo->si_bounce_on_seg) &&
298411793SMark.Johnson@Sun.COM 	    ((raddr + psize) > sglinfo->si_segmask)) ||
298511793SMark.Johnson@Sun.COM 	    ((raddr < addrlo) || ((raddr + psize) > addrhi))) {
298611793SMark.Johnson@Sun.COM 		/*
298711793SMark.Johnson@Sun.COM 		 * Increase how much copy buffer we use. We always increase by
298811793SMark.Johnson@Sun.COM 		 * pagesize so we don't have to worry about converting offsets.
298911793SMark.Johnson@Sun.COM 		 * Set a flag in the cookies dmac_type to indicate that it uses
299011793SMark.Johnson@Sun.COM 		 * the copy buffer. If this isn't the last cookie, go to the
299111793SMark.Johnson@Sun.COM 		 * next cookie (since we separate each page which uses the copy
299211793SMark.Johnson@Sun.COM 		 * buffer in case the copy buffer is not physically contiguous.
299311793SMark.Johnson@Sun.COM 		 */
2994509Smrj 		sglinfo->si_copybuf_req += MMU_PAGESIZE;
2995509Smrj 		sgl[cnt].dmac_type = ROOTNEX_USES_COPYBUF;
2996509Smrj 		if ((cnt + 1) < sglinfo->si_max_pages) {
2997509Smrj 			cnt++;
2998509Smrj 			sgl[cnt].dmac_laddress = 0;
2999509Smrj 			sgl[cnt].dmac_size = 0;
3000509Smrj 			sgl[cnt].dmac_type = 0;
3001509Smrj 		}
3002509Smrj 	}
3003509Smrj 
3004509Smrj 	/*
3005509Smrj 	 * save this page's physical address so we can figure out if the next
3006509Smrj 	 * page is physically contiguous. Keep decrementing size until we are
3007509Smrj 	 * done with the buffer.
3008509Smrj 	 */
30095084Sjohnlev 	last_page = raddr & MMU_PAGEMASK;
3010509Smrj 	size -= psize;
3011509Smrj 
3012509Smrj 	while (size > 0) {
3013509Smrj 		/* Get the size for this page (i.e. partial or full page) */
3014509Smrj 		psize = MIN(size, MMU_PAGESIZE);
3015509Smrj 
3016509Smrj 		if (buftype == DMA_OTYP_PAGES) {
3017509Smrj 			/* get the paddr from the page_t */
3018509Smrj 			ASSERT(!PP_ISFREE(pp) && PAGE_LOCKED(pp));
30195084Sjohnlev 			paddr = pfn_to_pa(pp->p_pagenum);
3020509Smrj 			pp = pp->p_next;
3021509Smrj 		} else if (pplist != NULL) {
3022509Smrj 			/* index into the array of page_t's to get the paddr */
3023509Smrj 			ASSERT(!PP_ISFREE(pplist[pcnt]));
30245084Sjohnlev 			paddr = pfn_to_pa(pplist[pcnt]->p_pagenum);
3025509Smrj 			pcnt++;
30260Sstevel@tonic-gate 		} else {
3027509Smrj 			/* call into the VM to get the paddr */
30285084Sjohnlev 			paddr =  pfn_to_pa(hat_getpfnum(sglinfo->si_asp->a_hat,
3029509Smrj 			    vaddr));
3030509Smrj 			vaddr += psize;
3031509Smrj 		}
3032509Smrj 
303313050Sfrank.van.der.linden@oracle.com 		raddr = ROOTNEX_PADDR_TO_RBASE(paddr);
303411793SMark.Johnson@Sun.COM 
303511793SMark.Johnson@Sun.COM 		/*
303611793SMark.Johnson@Sun.COM 		 * If we are using the copy buffer for anything over the
303711793SMark.Johnson@Sun.COM 		 * segment boundary, and this page is over the segment
303811793SMark.Johnson@Sun.COM 		 * boundary.
303911793SMark.Johnson@Sun.COM 		 *   OR
304011793SMark.Johnson@Sun.COM 		 * if the DMA engine can't reach the physical address.
304111793SMark.Johnson@Sun.COM 		 */
304211793SMark.Johnson@Sun.COM 		if (((sglinfo->si_bounce_on_seg) &&
304311793SMark.Johnson@Sun.COM 		    ((raddr + psize) > sglinfo->si_segmask)) ||
304411793SMark.Johnson@Sun.COM 		    ((raddr < addrlo) || ((raddr + psize) > addrhi))) {
304511793SMark.Johnson@Sun.COM 
3046509Smrj 			sglinfo->si_copybuf_req += MMU_PAGESIZE;
3047509Smrj 
30480Sstevel@tonic-gate 			/*
3049509Smrj 			 * if there is something in the current cookie, go to
3050509Smrj 			 * the next one. We only want one page in a cookie which
3051509Smrj 			 * uses the copybuf since the copybuf doesn't have to
3052509Smrj 			 * be physically contiguous.
3053509Smrj 			 */
3054509Smrj 			if (sgl[cnt].dmac_size != 0) {
3055509Smrj 				cnt++;
3056509Smrj 			}
30575084Sjohnlev 			sgl[cnt].dmac_laddress = raddr;
3058509Smrj 			sgl[cnt].dmac_size = psize;
3059509Smrj #if defined(__amd64)
3060509Smrj 			sgl[cnt].dmac_type = ROOTNEX_USES_COPYBUF;
3061509Smrj #else
3062509Smrj 			/*
3063509Smrj 			 * save the buf offset for 32-bit kernel. used in the
3064509Smrj 			 * obsoleted interfaces.
3065509Smrj 			 */
3066509Smrj 			sgl[cnt].dmac_type = ROOTNEX_USES_COPYBUF |
3067509Smrj 			    (dmar_object->dmao_size - size);
3068509Smrj #endif
3069509Smrj 			/* if this isn't the last cookie, go to the next one */
3070509Smrj 			if ((cnt + 1) < sglinfo->si_max_pages) {
3071509Smrj 				cnt++;
3072509Smrj 				sgl[cnt].dmac_laddress = 0;
3073509Smrj 				sgl[cnt].dmac_size = 0;
3074509Smrj 				sgl[cnt].dmac_type = 0;
3075509Smrj 			}
3076509Smrj 
3077509Smrj 		/*
3078509Smrj 		 * this page didn't need the copy buffer, if it's not physically
3079509Smrj 		 * contiguous, or it would put us over a segment boundary, or it
3080509Smrj 		 * puts us over the max cookie size, or the current sgl doesn't
3081509Smrj 		 * have anything in it.
3082509Smrj 		 */
30835084Sjohnlev 		} else if (((last_page + MMU_PAGESIZE) != raddr) ||
30845084Sjohnlev 		    !(raddr & sglinfo->si_segmask) ||
3085509Smrj 		    ((sgl[cnt].dmac_size + psize) > maxseg) ||
3086509Smrj 		    (sgl[cnt].dmac_size == 0)) {
3087509Smrj 			/*
3088509Smrj 			 * if we're not already in a new cookie, go to the next
3089509Smrj 			 * cookie.
3090509Smrj 			 */
3091509Smrj 			if (sgl[cnt].dmac_size != 0) {
3092509Smrj 				cnt++;
3093509Smrj 			}
3094509Smrj 
3095509Smrj 			/* save the cookie information */
30965084Sjohnlev 			sgl[cnt].dmac_laddress = raddr;
3097509Smrj 			sgl[cnt].dmac_size = psize;
3098509Smrj #if defined(__amd64)
3099509Smrj 			sgl[cnt].dmac_type = 0;
3100509Smrj #else
3101509Smrj 			/*
3102509Smrj 			 * save the buf offset for 32-bit kernel. used in the
3103509Smrj 			 * obsoleted interfaces.
3104509Smrj 			 */
3105509Smrj 			sgl[cnt].dmac_type = dmar_object->dmao_size - size;
3106509Smrj #endif
3107509Smrj 
3108509Smrj 		/*
3109509Smrj 		 * this page didn't need the copy buffer, it is physically
3110509Smrj 		 * contiguous with the last page, and it's <= the max cookie
3111509Smrj 		 * size.
3112509Smrj 		 */
3113509Smrj 		} else {
3114509Smrj 			sgl[cnt].dmac_size += psize;
3115509Smrj 
3116509Smrj 			/*
3117509Smrj 			 * if this exactly ==  the maximum cookie size, and
3118509Smrj 			 * it isn't the last cookie, go to the next cookie.
3119509Smrj 			 */
3120509Smrj 			if (((sgl[cnt].dmac_size + psize) == maxseg) &&
3121509Smrj 			    ((cnt + 1) < sglinfo->si_max_pages)) {
3122509Smrj 				cnt++;
3123509Smrj 				sgl[cnt].dmac_laddress = 0;
3124509Smrj 				sgl[cnt].dmac_size = 0;
3125509Smrj 				sgl[cnt].dmac_type = 0;
3126509Smrj 			}
3127509Smrj 		}
3128509Smrj 
3129509Smrj 		/*
3130509Smrj 		 * save this page's physical address so we can figure out if the
3131509Smrj 		 * next page is physically contiguous. Keep decrementing size
3132509Smrj 		 * until we are done with the buffer.
3133509Smrj 		 */
31345084Sjohnlev 		last_page = raddr;
3135509Smrj 		size -= psize;
3136509Smrj 	}
3137509Smrj 
3138509Smrj 	/* we're done, save away how many cookies the sgl has */
3139509Smrj 	if (sgl[cnt].dmac_size == 0) {
3140509Smrj 		ASSERT(cnt < sglinfo->si_max_pages);
3141509Smrj 		sglinfo->si_sgl_size = cnt;
3142509Smrj 	} else {
3143509Smrj 		sglinfo->si_sgl_size = cnt + 1;
3144509Smrj 	}
3145509Smrj }
3146509Smrj 
314713050Sfrank.van.der.linden@oracle.com static void
rootnex_dvma_get_sgl(ddi_dma_obj_t * dmar_object,ddi_dma_cookie_t * sgl,rootnex_sglinfo_t * sglinfo)314813050Sfrank.van.der.linden@oracle.com rootnex_dvma_get_sgl(ddi_dma_obj_t *dmar_object, ddi_dma_cookie_t *sgl,
314913050Sfrank.van.der.linden@oracle.com     rootnex_sglinfo_t *sglinfo)
315013050Sfrank.van.der.linden@oracle.com {
315113050Sfrank.van.der.linden@oracle.com 	uint64_t offset;
315213050Sfrank.van.der.linden@oracle.com 	uint64_t maxseg;
315313050Sfrank.van.der.linden@oracle.com 	uint64_t dvaddr;
315413050Sfrank.van.der.linden@oracle.com 	struct dvmaseg *dvs;
315513050Sfrank.van.der.linden@oracle.com 	uint64_t paddr;
315613050Sfrank.van.der.linden@oracle.com 	uint32_t psize, ssize;
315713050Sfrank.van.der.linden@oracle.com 	uint32_t size;
315813050Sfrank.van.der.linden@oracle.com 	uint_t cnt;
315913050Sfrank.van.der.linden@oracle.com 	int physcontig;
316013050Sfrank.van.der.linden@oracle.com 
316113050Sfrank.van.der.linden@oracle.com 	ASSERT(dmar_object->dmao_type == DMA_OTYP_DVADDR);
316213050Sfrank.van.der.linden@oracle.com 
316313050Sfrank.van.der.linden@oracle.com 	/* shortcuts */
316413050Sfrank.van.der.linden@oracle.com 	maxseg = sglinfo->si_max_cookie_size;
316513050Sfrank.van.der.linden@oracle.com 	size = dmar_object->dmao_size;
316613050Sfrank.van.der.linden@oracle.com 
316713050Sfrank.van.der.linden@oracle.com 	cnt = 0;
316813050Sfrank.van.der.linden@oracle.com 	sglinfo->si_bounce_on_seg = B_FALSE;
316913050Sfrank.van.der.linden@oracle.com 
317013050Sfrank.van.der.linden@oracle.com 	dvs = dmar_object->dmao_obj.dvma_obj.dv_seg;
317113050Sfrank.van.der.linden@oracle.com 	offset = dmar_object->dmao_obj.dvma_obj.dv_off;
317213050Sfrank.van.der.linden@oracle.com 	ssize = dvs->dvs_len;
317313050Sfrank.van.der.linden@oracle.com 	paddr = dvs->dvs_start;
317413050Sfrank.van.der.linden@oracle.com 	paddr += offset;
317513050Sfrank.van.der.linden@oracle.com 	psize = MIN(ssize, (maxseg - offset));
317613050Sfrank.van.der.linden@oracle.com 	dvaddr = paddr + psize;
317713050Sfrank.van.der.linden@oracle.com 	ssize -= psize;
317813050Sfrank.van.der.linden@oracle.com 
317913050Sfrank.van.der.linden@oracle.com 	sgl[cnt].dmac_laddress = paddr;
318013050Sfrank.van.der.linden@oracle.com 	sgl[cnt].dmac_size = psize;
318113050Sfrank.van.der.linden@oracle.com 	sgl[cnt].dmac_type = 0;
318213050Sfrank.van.der.linden@oracle.com 
318313050Sfrank.van.der.linden@oracle.com 	size -= psize;
318413050Sfrank.van.der.linden@oracle.com 	while (size > 0) {
318513050Sfrank.van.der.linden@oracle.com 		if (ssize == 0) {
318613050Sfrank.van.der.linden@oracle.com 			dvs++;
318713050Sfrank.van.der.linden@oracle.com 			ssize = dvs->dvs_len;
318813050Sfrank.van.der.linden@oracle.com 			dvaddr = dvs->dvs_start;
318913050Sfrank.van.der.linden@oracle.com 			physcontig = 0;
319013050Sfrank.van.der.linden@oracle.com 		} else
319113050Sfrank.van.der.linden@oracle.com 			physcontig = 1;
319213050Sfrank.van.der.linden@oracle.com 
319313050Sfrank.van.der.linden@oracle.com 		paddr = dvaddr;
319413050Sfrank.van.der.linden@oracle.com 		psize = MIN(ssize, maxseg);
319513050Sfrank.van.der.linden@oracle.com 		dvaddr += psize;
319613050Sfrank.van.der.linden@oracle.com 		ssize -= psize;
319713050Sfrank.van.der.linden@oracle.com 
319813050Sfrank.van.der.linden@oracle.com 		if (!physcontig || !(paddr & sglinfo->si_segmask) ||
319913050Sfrank.van.der.linden@oracle.com 		    ((sgl[cnt].dmac_size + psize) > maxseg) ||
320013050Sfrank.van.der.linden@oracle.com 		    (sgl[cnt].dmac_size == 0)) {
320113050Sfrank.van.der.linden@oracle.com 			/*
320213050Sfrank.van.der.linden@oracle.com 			 * if we're not already in a new cookie, go to the next
320313050Sfrank.van.der.linden@oracle.com 			 * cookie.
320413050Sfrank.van.der.linden@oracle.com 			 */
320513050Sfrank.van.der.linden@oracle.com 			if (sgl[cnt].dmac_size != 0) {
320613050Sfrank.van.der.linden@oracle.com 				cnt++;
320713050Sfrank.van.der.linden@oracle.com 			}
320813050Sfrank.van.der.linden@oracle.com 
320913050Sfrank.van.der.linden@oracle.com 			/* save the cookie information */
321013050Sfrank.van.der.linden@oracle.com 			sgl[cnt].dmac_laddress = paddr;
321113050Sfrank.van.der.linden@oracle.com 			sgl[cnt].dmac_size = psize;
321213050Sfrank.van.der.linden@oracle.com 			sgl[cnt].dmac_type = 0;
321313050Sfrank.van.der.linden@oracle.com 		} else {
321413050Sfrank.van.der.linden@oracle.com 			sgl[cnt].dmac_size += psize;
321513050Sfrank.van.der.linden@oracle.com 
321613050Sfrank.van.der.linden@oracle.com 			/*
321713050Sfrank.van.der.linden@oracle.com 			 * if this exactly ==  the maximum cookie size, and
321813050Sfrank.van.der.linden@oracle.com 			 * it isn't the last cookie, go to the next cookie.
321913050Sfrank.van.der.linden@oracle.com 			 */
322013050Sfrank.van.der.linden@oracle.com 			if (((sgl[cnt].dmac_size + psize) == maxseg) &&
322113050Sfrank.van.der.linden@oracle.com 			    ((cnt + 1) < sglinfo->si_max_pages)) {
322213050Sfrank.van.der.linden@oracle.com 				cnt++;
322313050Sfrank.van.der.linden@oracle.com 				sgl[cnt].dmac_laddress = 0;
322413050Sfrank.van.der.linden@oracle.com 				sgl[cnt].dmac_size = 0;
322513050Sfrank.van.der.linden@oracle.com 				sgl[cnt].dmac_type = 0;
322613050Sfrank.van.der.linden@oracle.com 			}
322713050Sfrank.van.der.linden@oracle.com 		}
322813050Sfrank.van.der.linden@oracle.com 		size -= psize;
322913050Sfrank.van.der.linden@oracle.com 	}
323013050Sfrank.van.der.linden@oracle.com 
323113050Sfrank.van.der.linden@oracle.com 	/* we're done, save away how many cookies the sgl has */
323213050Sfrank.van.der.linden@oracle.com 	if (sgl[cnt].dmac_size == 0) {
323313050Sfrank.van.der.linden@oracle.com 		sglinfo->si_sgl_size = cnt;
323413050Sfrank.van.der.linden@oracle.com 	} else {
323513050Sfrank.van.der.linden@oracle.com 		sglinfo->si_sgl_size = cnt + 1;
323613050Sfrank.van.der.linden@oracle.com 	}
323713050Sfrank.van.der.linden@oracle.com }
323813050Sfrank.van.der.linden@oracle.com 
3239509Smrj /*
3240509Smrj  * rootnex_bind_slowpath()
3241509Smrj  *    Call in the bind path if the calling driver can't use the sgl without
3242509Smrj  *    modifying it. We either need to use the copy buffer and/or we will end up
3243509Smrj  *    with a partial bind.
3244509Smrj  */
3245509Smrj static int
rootnex_bind_slowpath(ddi_dma_impl_t * hp,struct ddi_dma_req * dmareq,rootnex_dma_t * dma,ddi_dma_attr_t * attr,ddi_dma_obj_t * dmao,int kmflag)3246509Smrj rootnex_bind_slowpath(ddi_dma_impl_t *hp, struct ddi_dma_req *dmareq,
324713050Sfrank.van.der.linden@oracle.com     rootnex_dma_t *dma, ddi_dma_attr_t *attr, ddi_dma_obj_t *dmao, int kmflag)
3248509Smrj {
3249509Smrj 	rootnex_sglinfo_t *sinfo;
3250509Smrj 	rootnex_window_t *window;
3251509Smrj 	ddi_dma_cookie_t *cookie;
3252509Smrj 	size_t copybuf_used;
3253509Smrj 	size_t dmac_size;
3254509Smrj 	boolean_t partial;
3255509Smrj 	off_t cur_offset;
3256509Smrj 	page_t *cur_pp;
3257509Smrj 	major_t mnum;
3258509Smrj 	int e;
3259509Smrj 	int i;
3260509Smrj 
3261509Smrj 
3262509Smrj 	sinfo = &dma->dp_sglinfo;
3263509Smrj 	copybuf_used = 0;
3264509Smrj 	partial = B_FALSE;
3265509Smrj 
3266509Smrj 	/*
3267509Smrj 	 * If we're using the copybuf, set the copybuf state in dma struct.
3268509Smrj 	 * Needs to be first since it sets the copy buffer size.
3269509Smrj 	 */
3270509Smrj 	if (sinfo->si_copybuf_req != 0) {
3271509Smrj 		e = rootnex_setup_copybuf(hp, dmareq, dma, attr);
3272509Smrj 		if (e != DDI_SUCCESS) {
3273509Smrj 			return (e);
3274509Smrj 		}
3275509Smrj 	} else {
3276509Smrj 		dma->dp_copybuf_size = 0;
3277509Smrj 	}
3278509Smrj 
3279509Smrj 	/*
3280509Smrj 	 * Figure out if we need to do a partial mapping. If so, figure out
3281509Smrj 	 * if we need to trim the buffers when we munge the sgl.
3282509Smrj 	 */
3283509Smrj 	if ((dma->dp_copybuf_size < sinfo->si_copybuf_req) ||
328413050Sfrank.van.der.linden@oracle.com 	    (dmao->dmao_size > dma->dp_maxxfer) ||
3285509Smrj 	    (attr->dma_attr_sgllen < sinfo->si_sgl_size)) {
3286509Smrj 		dma->dp_partial_required = B_TRUE;
3287509Smrj 		if (attr->dma_attr_granular != 1) {
3288509Smrj 			dma->dp_trim_required = B_TRUE;
3289509Smrj 		}
3290509Smrj 	} else {
3291509Smrj 		dma->dp_partial_required = B_FALSE;
3292509Smrj 		dma->dp_trim_required = B_FALSE;
3293509Smrj 	}
3294509Smrj 
3295509Smrj 	/* If we need to do a partial bind, make sure the driver supports it */
3296509Smrj 	if (dma->dp_partial_required &&
3297509Smrj 	    !(dmareq->dmar_flags & DDI_DMA_PARTIAL)) {
3298509Smrj 
3299509Smrj 		mnum = ddi_driver_major(dma->dp_dip);
3300509Smrj 		/*
3301509Smrj 		 * patchable which allows us to print one warning per major
3302509Smrj 		 * number.
3303509Smrj 		 */
3304509Smrj 		if ((rootnex_bind_warn) &&
3305509Smrj 		    ((rootnex_warn_list[mnum] & ROOTNEX_BIND_WARNING) == 0)) {
3306509Smrj 			rootnex_warn_list[mnum] |= ROOTNEX_BIND_WARNING;
3307509Smrj 			cmn_err(CE_WARN, "!%s: coding error detected, the "
3308509Smrj 			    "driver is using ddi_dma_attr(9S) incorrectly. "
3309509Smrj 			    "There is a small risk of data corruption in "
3310509Smrj 			    "particular with large I/Os. The driver should be "
3311509Smrj 			    "replaced with a corrected version for proper "
3312509Smrj 			    "system operation. To disable this warning, add "
3313509Smrj 			    "'set rootnex:rootnex_bind_warn=0' to "
3314509Smrj 			    "/etc/system(4).", ddi_driver_name(dma->dp_dip));
3315509Smrj 		}
3316509Smrj 		return (DDI_DMA_TOOBIG);
3317509Smrj 	}
3318509Smrj 
3319509Smrj 	/*
3320509Smrj 	 * we might need multiple windows, setup state to handle them. In this
3321509Smrj 	 * code path, we will have at least one window.
3322509Smrj 	 */
332313050Sfrank.van.der.linden@oracle.com 	e = rootnex_setup_windows(hp, dma, attr, dmao, kmflag);
3324509Smrj 	if (e != DDI_SUCCESS) {
3325509Smrj 		rootnex_teardown_copybuf(dma);
3326509Smrj 		return (e);
3327509Smrj 	}
3328509Smrj 
3329509Smrj 	window = &dma->dp_window[0];
3330509Smrj 	cookie = &dma->dp_cookies[0];
3331509Smrj 	cur_offset = 0;
3332509Smrj 	rootnex_init_win(hp, dma, window, cookie, cur_offset);
333313050Sfrank.van.der.linden@oracle.com 	if (dmao->dmao_type == DMA_OTYP_PAGES) {
3334509Smrj 		cur_pp = dmareq->dmar_object.dmao_obj.pp_obj.pp_pp;
3335509Smrj 	}
3336509Smrj 
3337509Smrj 	/* loop though all the cookies we got back from get_sgl() */
3338509Smrj 	for (i = 0; i < sinfo->si_sgl_size; i++) {
3339509Smrj 		/*
3340509Smrj 		 * If we're using the copy buffer, check this cookie and setup
3341509Smrj 		 * its associated copy buffer state. If this cookie uses the
3342509Smrj 		 * copy buffer, make sure we sync this window during dma_sync.
3343509Smrj 		 */
3344509Smrj 		if (dma->dp_copybuf_size > 0) {
334513050Sfrank.van.der.linden@oracle.com 			rootnex_setup_cookie(dmao, dma, cookie,
3346509Smrj 			    cur_offset, &copybuf_used, &cur_pp);
3347509Smrj 			if (cookie->dmac_type & ROOTNEX_USES_COPYBUF) {
3348509Smrj 				window->wd_dosync = B_TRUE;
3349509Smrj 			}
3350509Smrj 		}
3351509Smrj 
3352509Smrj 		/*
3353509Smrj 		 * save away the cookie size, since it could be modified in
3354509Smrj 		 * the windowing code.
3355509Smrj 		 */
3356509Smrj 		dmac_size = cookie->dmac_size;
3357509Smrj 
3358509Smrj 		/* if we went over max copybuf size */
3359509Smrj 		if (dma->dp_copybuf_size &&
3360509Smrj 		    (copybuf_used > dma->dp_copybuf_size)) {
3361509Smrj 			partial = B_TRUE;
3362509Smrj 			e = rootnex_copybuf_window_boundary(hp, dma, &window,
3363509Smrj 			    cookie, cur_offset, &copybuf_used);
3364509Smrj 			if (e != DDI_SUCCESS) {
3365509Smrj 				rootnex_teardown_copybuf(dma);
3366509Smrj 				rootnex_teardown_windows(dma);
3367509Smrj 				return (e);
3368509Smrj 			}
3369509Smrj 
3370509Smrj 			/*
3371509Smrj 			 * if the coookie uses the copy buffer, make sure the
3372509Smrj 			 * new window we just moved to is set to sync.
3373509Smrj 			 */
3374509Smrj 			if (cookie->dmac_type & ROOTNEX_USES_COPYBUF) {
3375509Smrj 				window->wd_dosync = B_TRUE;
3376509Smrj 			}
337712837Sfrank.van.der.linden@oracle.com 			ROOTNEX_DPROBE1(rootnex__copybuf__window, dev_info_t *,
3378509Smrj 			    dma->dp_dip);
3379509Smrj 
3380509Smrj 		/* if the cookie cnt == max sgllen, move to the next window */
3381509Smrj 		} else if (window->wd_cookie_cnt >= attr->dma_attr_sgllen) {
3382509Smrj 			partial = B_TRUE;
3383509Smrj 			ASSERT(window->wd_cookie_cnt == attr->dma_attr_sgllen);
3384509Smrj 			e = rootnex_sgllen_window_boundary(hp, dma, &window,
3385509Smrj 			    cookie, attr, cur_offset);
3386509Smrj 			if (e != DDI_SUCCESS) {
3387509Smrj 				rootnex_teardown_copybuf(dma);
3388509Smrj 				rootnex_teardown_windows(dma);
3389509Smrj 				return (e);
3390509Smrj 			}
3391509Smrj 
3392509Smrj 			/*
3393509Smrj 			 * if the coookie uses the copy buffer, make sure the
3394509Smrj 			 * new window we just moved to is set to sync.
3395509Smrj 			 */
3396509Smrj 			if (cookie->dmac_type & ROOTNEX_USES_COPYBUF) {
3397509Smrj 				window->wd_dosync = B_TRUE;
3398509Smrj 			}
339912837Sfrank.van.der.linden@oracle.com 			ROOTNEX_DPROBE1(rootnex__sgllen__window, dev_info_t *,
3400509Smrj 			    dma->dp_dip);
3401509Smrj 
3402509Smrj 		/* else if we will be over maxxfer */
3403509Smrj 		} else if ((window->wd_size + dmac_size) >
3404509Smrj 		    dma->dp_maxxfer) {
3405509Smrj 			partial = B_TRUE;
3406509Smrj 			e = rootnex_maxxfer_window_boundary(hp, dma, &window,
3407509Smrj 			    cookie);
3408509Smrj 			if (e != DDI_SUCCESS) {
3409509Smrj 				rootnex_teardown_copybuf(dma);
3410509Smrj 				rootnex_teardown_windows(dma);
3411509Smrj 				return (e);
3412509Smrj 			}
3413509Smrj 
3414509Smrj 			/*
3415509Smrj 			 * if the coookie uses the copy buffer, make sure the
3416509Smrj 			 * new window we just moved to is set to sync.
34170Sstevel@tonic-gate 			 */
3418509Smrj 			if (cookie->dmac_type & ROOTNEX_USES_COPYBUF) {
3419509Smrj 				window->wd_dosync = B_TRUE;
3420509Smrj 			}
342112837Sfrank.van.der.linden@oracle.com 			ROOTNEX_DPROBE1(rootnex__maxxfer__window, dev_info_t *,
3422509Smrj 			    dma->dp_dip);
3423509Smrj 
3424509Smrj 		/* else this cookie fits in the current window */
3425509Smrj 		} else {
3426509Smrj 			window->wd_cookie_cnt++;
3427509Smrj 			window->wd_size += dmac_size;
3428509Smrj 		}
3429509Smrj 
3430509Smrj 		/* track our offset into the buffer, go to the next cookie */
343113050Sfrank.van.der.linden@oracle.com 		ASSERT(dmac_size <= dmao->dmao_size);
3432509Smrj 		ASSERT(cookie->dmac_size <= dmac_size);
3433509Smrj 		cur_offset += dmac_size;
3434509Smrj 		cookie++;
3435509Smrj 	}
3436509Smrj 
3437509Smrj 	/* if we ended up with a zero sized window in the end, clean it up */
3438509Smrj 	if (window->wd_size == 0) {
3439509Smrj 		hp->dmai_nwin--;
3440509Smrj 		window--;
3441509Smrj 	}
3442509Smrj 
3443509Smrj 	ASSERT(window->wd_trim.tr_trim_last == B_FALSE);
3444509Smrj 
3445509Smrj 	if (!partial) {
3446509Smrj 		return (DDI_DMA_MAPPED);
3447509Smrj 	}
3448509Smrj 
3449509Smrj 	ASSERT(dma->dp_partial_required);
3450509Smrj 	return (DDI_DMA_PARTIAL_MAP);
3451509Smrj }
3452509Smrj 
3453509Smrj /*
3454509Smrj  * rootnex_setup_copybuf()
3455509Smrj  *    Called in bind slowpath. Figures out if we're going to use the copy
3456509Smrj  *    buffer, and if we do, sets up the basic state to handle it.
3457509Smrj  */
3458509Smrj static int
rootnex_setup_copybuf(ddi_dma_impl_t * hp,struct ddi_dma_req * dmareq,rootnex_dma_t * dma,ddi_dma_attr_t * attr)3459509Smrj rootnex_setup_copybuf(ddi_dma_impl_t *hp, struct ddi_dma_req *dmareq,
3460509Smrj     rootnex_dma_t *dma, ddi_dma_attr_t *attr)
3461509Smrj {
3462509Smrj 	rootnex_sglinfo_t *sinfo;
3463509Smrj 	ddi_dma_attr_t lattr;
3464509Smrj 	size_t max_copybuf;
3465509Smrj 	int cansleep;
3466509Smrj 	int e;
3467509Smrj #if !defined(__amd64)
3468509Smrj 	int vmflag;
3469509Smrj #endif
3470509Smrj 
347113050Sfrank.van.der.linden@oracle.com 	ASSERT(!dma->dp_dvma_used);
3472509Smrj 
3473509Smrj 	sinfo = &dma->dp_sglinfo;
3474509Smrj 
34755251Smrj 	/* read this first so it's consistent through the routine  */
34765251Smrj 	max_copybuf = i_ddi_copybuf_size() & MMU_PAGEMASK;
3477509Smrj 
3478509Smrj 	/* We need to call into the rootnex on ddi_dma_sync() */
3479509Smrj 	hp->dmai_rflags &= ~DMP_NOSYNC;
3480509Smrj 
3481509Smrj 	/* make sure the copybuf size <= the max size */
3482509Smrj 	dma->dp_copybuf_size = MIN(sinfo->si_copybuf_req, max_copybuf);
3483509Smrj 	ASSERT((dma->dp_copybuf_size & MMU_PAGEOFFSET) == 0);
3484509Smrj 
3485509Smrj #if !defined(__amd64)
3486509Smrj 	/*
3487509Smrj 	 * if we don't have kva space to copy to/from, allocate the KVA space
3488509Smrj 	 * now. We only do this for the 32-bit kernel. We use seg kpm space for
3489509Smrj 	 * the 64-bit kernel.
3490509Smrj 	 */
3491509Smrj 	if ((dmareq->dmar_object.dmao_type == DMA_OTYP_PAGES) ||
3492509Smrj 	    (dmareq->dmar_object.dmao_obj.virt_obj.v_as != NULL)) {
3493509Smrj 
3494509Smrj 		/* convert the sleep flags */
3495509Smrj 		if (dmareq->dmar_fp == DDI_DMA_SLEEP) {
3496509Smrj 			vmflag = VM_SLEEP;
3497509Smrj 		} else {
3498509Smrj 			vmflag = VM_NOSLEEP;
3499509Smrj 		}
3500509Smrj 
3501509Smrj 		/* allocate Kernel VA space that we can bcopy to/from */
3502509Smrj 		dma->dp_kva = vmem_alloc(heap_arena, dma->dp_copybuf_size,
3503509Smrj 		    vmflag);
3504509Smrj 		if (dma->dp_kva == NULL) {
3505509Smrj 			return (DDI_DMA_NORESOURCES);
3506509Smrj 		}
3507509Smrj 	}
3508509Smrj #endif
3509509Smrj 
3510509Smrj 	/* convert the sleep flags */
3511509Smrj 	if (dmareq->dmar_fp == DDI_DMA_SLEEP) {
3512509Smrj 		cansleep = 1;
3513509Smrj 	} else {
3514509Smrj 		cansleep = 0;
3515509Smrj 	}
3516509Smrj 
3517509Smrj 	/*
35187173Smrj 	 * Allocate the actual copy buffer. This needs to fit within the DMA
35197173Smrj 	 * engine limits, so we can't use kmem_alloc... We don't need
35207173Smrj 	 * contiguous memory (sgllen) since we will be forcing windows on
35217173Smrj 	 * sgllen anyway.
3522509Smrj 	 */
3523509Smrj 	lattr = *attr;
3524509Smrj 	lattr.dma_attr_align = MMU_PAGESIZE;
35257173Smrj 	/*
35267173Smrj 	 * this should be < 0 to indicate no limit, but due to a bug in
35277173Smrj 	 * the rootnex, we'll set it to the maximum positive int.
35287173Smrj 	 */
35297173Smrj 	lattr.dma_attr_sgllen = 0x7fffffff;
353011793SMark.Johnson@Sun.COM 	/*
353111793SMark.Johnson@Sun.COM 	 * if we're using the copy buffer because of seg, use that for our
353211793SMark.Johnson@Sun.COM 	 * upper address limit.
353311793SMark.Johnson@Sun.COM 	 */
353411793SMark.Johnson@Sun.COM 	if (sinfo->si_bounce_on_seg) {
353511793SMark.Johnson@Sun.COM 		lattr.dma_attr_addr_hi = lattr.dma_attr_seg;
353611793SMark.Johnson@Sun.COM 	}
3537509Smrj 	e = i_ddi_mem_alloc(dma->dp_dip, &lattr, dma->dp_copybuf_size, cansleep,
3538509Smrj 	    0, NULL, &dma->dp_cbaddr, &dma->dp_cbsize, NULL);
3539509Smrj 	if (e != DDI_SUCCESS) {
3540509Smrj #if !defined(__amd64)
3541509Smrj 		if (dma->dp_kva != NULL) {
3542509Smrj 			vmem_free(heap_arena, dma->dp_kva,
3543509Smrj 			    dma->dp_copybuf_size);
3544509Smrj 		}
3545509Smrj #endif
3546509Smrj 		return (DDI_DMA_NORESOURCES);
3547509Smrj 	}
3548509Smrj 
354912837Sfrank.van.der.linden@oracle.com 	ROOTNEX_DPROBE2(rootnex__alloc__copybuf, dev_info_t *, dma->dp_dip,
3550509Smrj 	    size_t, dma->dp_copybuf_size);
3551509Smrj 
3552509Smrj 	return (DDI_SUCCESS);
3553509Smrj }
3554509Smrj 
3555509Smrj 
3556509Smrj /*
3557509Smrj  * rootnex_setup_windows()
3558509Smrj  *    Called in bind slowpath to setup the window state. We always have windows
3559509Smrj  *    in the slowpath. Even if the window count = 1.
3560509Smrj  */
3561509Smrj static int
rootnex_setup_windows(ddi_dma_impl_t * hp,rootnex_dma_t * dma,ddi_dma_attr_t * attr,ddi_dma_obj_t * dmao,int kmflag)3562509Smrj rootnex_setup_windows(ddi_dma_impl_t *hp, rootnex_dma_t *dma,
356313050Sfrank.van.der.linden@oracle.com     ddi_dma_attr_t *attr, ddi_dma_obj_t *dmao, int kmflag)
3564509Smrj {
3565509Smrj 	rootnex_window_t *windowp;
3566509Smrj 	rootnex_sglinfo_t *sinfo;
3567509Smrj 	size_t copy_state_size;
3568509Smrj 	size_t win_state_size;
3569509Smrj 	size_t state_available;
3570509Smrj 	size_t space_needed;
3571509Smrj 	uint_t copybuf_win;
3572509Smrj 	uint_t maxxfer_win;
3573509Smrj 	size_t space_used;
3574509Smrj 	uint_t sglwin;
3575509Smrj 
3576509Smrj 
3577509Smrj 	sinfo = &dma->dp_sglinfo;
3578509Smrj 
3579509Smrj 	dma->dp_current_win = 0;
3580509Smrj 	hp->dmai_nwin = 0;
3581509Smrj 
3582509Smrj 	/* If we don't need to do a partial, we only have one window */
3583509Smrj 	if (!dma->dp_partial_required) {
3584509Smrj 		dma->dp_max_win = 1;
3585509Smrj 
3586509Smrj 	/*
3587509Smrj 	 * we need multiple windows, need to figure out the worse case number
3588509Smrj 	 * of windows.
3589509Smrj 	 */
3590509Smrj 	} else {
3591509Smrj 		/*
3592509Smrj 		 * if we need windows because we need more copy buffer that
3593509Smrj 		 * we allow, the worse case number of windows we could need
3594509Smrj 		 * here would be (copybuf space required / copybuf space that
3595509Smrj 		 * we have) plus one for remainder, and plus 2 to handle the
3596509Smrj 		 * extra pages on the trim for the first and last pages of the
3597509Smrj 		 * buffer (a page is the minimum window size so under the right
3598509Smrj 		 * attr settings, you could have a window for each page).
3599509Smrj 		 * The last page will only be hit here if the size is not a
3600509Smrj 		 * multiple of the granularity (which theoretically shouldn't
3601509Smrj 		 * be the case but never has been enforced, so we could have
3602509Smrj 		 * broken things without it).
3603509Smrj 		 */
3604509Smrj 		if (sinfo->si_copybuf_req > dma->dp_copybuf_size) {
3605509Smrj 			ASSERT(dma->dp_copybuf_size > 0);
3606509Smrj 			copybuf_win = (sinfo->si_copybuf_req /
3607509Smrj 			    dma->dp_copybuf_size) + 1 + 2;
3608509Smrj 		} else {
3609509Smrj 			copybuf_win = 0;
3610509Smrj 		}
3611509Smrj 
3612509Smrj 		/*
3613509Smrj 		 * if we need windows because we have more cookies than the H/W
3614509Smrj 		 * can handle, the number of windows we would need here would
361512118Smark.r.johnson@oracle.com 		 * be (cookie count / cookies count H/W supports minus 1[for
361612118Smark.r.johnson@oracle.com 		 * trim]) plus one for remainder.
3617509Smrj 		 */
3618509Smrj 		if (attr->dma_attr_sgllen < sinfo->si_sgl_size) {
361912118Smark.r.johnson@oracle.com 			sglwin = (sinfo->si_sgl_size /
362012118Smark.r.johnson@oracle.com 			    (attr->dma_attr_sgllen - 1)) + 1;
3621509Smrj 		} else {
3622509Smrj 			sglwin = 0;
3623509Smrj 		}
3624509Smrj 
3625509Smrj 		/*
3626509Smrj 		 * if we need windows because we're binding more memory than the
3627509Smrj 		 * H/W can transfer at once, the number of windows we would need
3628509Smrj 		 * here would be (xfer count / max xfer H/W supports) plus one
3629509Smrj 		 * for remainder, and plus 2 to handle the extra pages on the
3630509Smrj 		 * trim (see above comment about trim)
3631509Smrj 		 */
363213050Sfrank.van.der.linden@oracle.com 		if (dmao->dmao_size > dma->dp_maxxfer) {
363313050Sfrank.van.der.linden@oracle.com 			maxxfer_win = (dmao->dmao_size /
3634509Smrj 			    dma->dp_maxxfer) + 1 + 2;
3635509Smrj 		} else {
3636509Smrj 			maxxfer_win = 0;
3637509Smrj 		}
3638509Smrj 		dma->dp_max_win =  copybuf_win + sglwin + maxxfer_win;
3639509Smrj 		ASSERT(dma->dp_max_win > 0);
3640509Smrj 	}
3641509Smrj 	win_state_size = dma->dp_max_win * sizeof (rootnex_window_t);
3642509Smrj 
3643509Smrj 	/*
3644509Smrj 	 * Get space for window and potential copy buffer state. Before we
3645509Smrj 	 * go and allocate memory, see if we can get away with using what's
3646509Smrj 	 * left in the pre-allocted state or the dynamically allocated sgl.
3647509Smrj 	 */
3648509Smrj 	space_used = (uintptr_t)(sinfo->si_sgl_size *
3649509Smrj 	    sizeof (ddi_dma_cookie_t));
3650509Smrj 
3651509Smrj 	/* if we dynamically allocated space for the cookies */
3652509Smrj 	if (dma->dp_need_to_free_cookie) {
3653509Smrj 		/* if we have more space in the pre-allocted buffer, use it */
3654509Smrj 		ASSERT(space_used <= dma->dp_cookie_size);
3655509Smrj 		if ((dma->dp_cookie_size - space_used) <=
3656509Smrj 		    rootnex_state->r_prealloc_size) {
3657509Smrj 			state_available = rootnex_state->r_prealloc_size;
3658509Smrj 			windowp = (rootnex_window_t *)dma->dp_prealloc_buffer;
3659509Smrj 
3660509Smrj 		/*
3661509Smrj 		 * else, we have more free space in the dynamically allocated
3662509Smrj 		 * buffer, i.e. the buffer wasn't worse case fragmented so we
3663509Smrj 		 * didn't need a lot of cookies.
3664509Smrj 		 */
3665509Smrj 		} else {
3666509Smrj 			state_available = dma->dp_cookie_size - space_used;
3667509Smrj 			windowp = (rootnex_window_t *)
3668509Smrj 			    &dma->dp_cookies[sinfo->si_sgl_size];
3669509Smrj 		}
3670509Smrj 
3671509Smrj 	/* we used the pre-alloced buffer */
3672509Smrj 	} else {
3673509Smrj 		ASSERT(space_used <= rootnex_state->r_prealloc_size);
3674509Smrj 		state_available = rootnex_state->r_prealloc_size - space_used;
3675509Smrj 		windowp = (rootnex_window_t *)
3676509Smrj 		    &dma->dp_cookies[sinfo->si_sgl_size];
3677509Smrj 	}
3678509Smrj 
3679509Smrj 	/*
3680509Smrj 	 * figure out how much state we need to track the copy buffer. Add an
3681509Smrj 	 * addition 8 bytes for pointer alignemnt later.
3682509Smrj 	 */
3683509Smrj 	if (dma->dp_copybuf_size > 0) {
3684509Smrj 		copy_state_size = sinfo->si_max_pages *
3685509Smrj 		    sizeof (rootnex_pgmap_t);
3686509Smrj 	} else {
3687509Smrj 		copy_state_size = 0;
3688509Smrj 	}
3689509Smrj 	/* add an additional 8 bytes for pointer alignment */
3690509Smrj 	space_needed = win_state_size + copy_state_size + 0x8;
3691509Smrj 
3692509Smrj 	/* if we have enough space already, use it */
3693509Smrj 	if (state_available >= space_needed) {
3694509Smrj 		dma->dp_window = windowp;
3695509Smrj 		dma->dp_need_to_free_window = B_FALSE;
3696509Smrj 
3697509Smrj 	/* not enough space, need to allocate more. */
3698509Smrj 	} else {
3699509Smrj 		dma->dp_window = kmem_alloc(space_needed, kmflag);
3700509Smrj 		if (dma->dp_window == NULL) {
3701509Smrj 			return (DDI_DMA_NORESOURCES);
3702509Smrj 		}
3703509Smrj 		dma->dp_need_to_free_window = B_TRUE;
3704509Smrj 		dma->dp_window_size = space_needed;
370512837Sfrank.van.der.linden@oracle.com 		ROOTNEX_DPROBE2(rootnex__bind__sp__alloc, dev_info_t *,
3706509Smrj 		    dma->dp_dip, size_t, space_needed);
3707509Smrj 	}
3708509Smrj 
3709509Smrj 	/*
3710509Smrj 	 * we allocate copy buffer state and window state at the same time.
3711509Smrj 	 * setup our copy buffer state pointers. Make sure it's aligned.
3712509Smrj 	 */
3713509Smrj 	if (dma->dp_copybuf_size > 0) {
3714509Smrj 		dma->dp_pgmap = (rootnex_pgmap_t *)(((uintptr_t)
3715509Smrj 		    &dma->dp_window[dma->dp_max_win] + 0x7) & ~0x7);
3716509Smrj 
3717509Smrj #if !defined(__amd64)
3718509Smrj 		/*
3719509Smrj 		 * make sure all pm_mapped, pm_vaddr, and pm_pp are set to
3720509Smrj 		 * false/NULL. Should be quicker to bzero vs loop and set.
3721509Smrj 		 */
3722509Smrj 		bzero(dma->dp_pgmap, copy_state_size);
3723509Smrj #endif
3724509Smrj 	} else {
3725509Smrj 		dma->dp_pgmap = NULL;
3726509Smrj 	}
3727509Smrj 
3728509Smrj 	return (DDI_SUCCESS);
3729509Smrj }
3730509Smrj 
3731509Smrj 
3732509Smrj /*
3733509Smrj  * rootnex_teardown_copybuf()
3734509Smrj  *    cleans up after rootnex_setup_copybuf()
3735509Smrj  */
3736509Smrj static void
rootnex_teardown_copybuf(rootnex_dma_t * dma)3737509Smrj rootnex_teardown_copybuf(rootnex_dma_t *dma)
3738509Smrj {
3739509Smrj #if !defined(__amd64)
3740509Smrj 	int i;
3741509Smrj 
3742509Smrj 	/*
3743509Smrj 	 * if we allocated kernel heap VMEM space, go through all the pages and
3744509Smrj 	 * map out any of the ones that we're mapped into the kernel heap VMEM
3745509Smrj 	 * arena. Then free the VMEM space.
3746509Smrj 	 */
3747509Smrj 	if (dma->dp_kva != NULL) {
3748509Smrj 		for (i = 0; i < dma->dp_sglinfo.si_max_pages; i++) {
3749509Smrj 			if (dma->dp_pgmap[i].pm_mapped) {
3750509Smrj 				hat_unload(kas.a_hat, dma->dp_pgmap[i].pm_kaddr,
3751509Smrj 				    MMU_PAGESIZE, HAT_UNLOAD);
3752509Smrj 				dma->dp_pgmap[i].pm_mapped = B_FALSE;
3753509Smrj 			}
3754509Smrj 		}
3755509Smrj 
3756509Smrj 		vmem_free(heap_arena, dma->dp_kva, dma->dp_copybuf_size);
3757509Smrj 	}
3758509Smrj 
3759509Smrj #endif
3760509Smrj 
3761509Smrj 	/* if we allocated a copy buffer, free it */
3762509Smrj 	if (dma->dp_cbaddr != NULL) {
37631900Seota 		i_ddi_mem_free(dma->dp_cbaddr, NULL);
3764509Smrj 	}
3765509Smrj }
3766509Smrj 
3767509Smrj 
3768509Smrj /*
3769509Smrj  * rootnex_teardown_windows()
3770509Smrj  *    cleans up after rootnex_setup_windows()
3771509Smrj  */
3772509Smrj static void
rootnex_teardown_windows(rootnex_dma_t * dma)3773509Smrj rootnex_teardown_windows(rootnex_dma_t *dma)
3774509Smrj {
3775509Smrj 	/*
3776509Smrj 	 * if we had to allocate window state on the last bind (because we
3777509Smrj 	 * didn't have enough pre-allocated space in the handle), free it.
3778509Smrj 	 */
3779509Smrj 	if (dma->dp_need_to_free_window) {
3780509Smrj 		kmem_free(dma->dp_window, dma->dp_window_size);
3781509Smrj 	}
3782509Smrj }
3783509Smrj 
3784509Smrj 
3785509Smrj /*
3786509Smrj  * rootnex_init_win()
3787509Smrj  *    Called in bind slow path during creation of a new window. Initializes
3788509Smrj  *    window state to default values.
3789509Smrj  */
3790509Smrj /*ARGSUSED*/
3791509Smrj static void
rootnex_init_win(ddi_dma_impl_t * hp,rootnex_dma_t * dma,rootnex_window_t * window,ddi_dma_cookie_t * cookie,off_t cur_offset)3792509Smrj rootnex_init_win(ddi_dma_impl_t *hp, rootnex_dma_t *dma,
3793509Smrj     rootnex_window_t *window, ddi_dma_cookie_t *cookie, off_t cur_offset)
3794509Smrj {
3795509Smrj 	hp->dmai_nwin++;
3796509Smrj 	window->wd_dosync = B_FALSE;
3797509Smrj 	window->wd_offset = cur_offset;
3798509Smrj 	window->wd_size = 0;
3799509Smrj 	window->wd_first_cookie = cookie;
3800509Smrj 	window->wd_cookie_cnt = 0;
3801509Smrj 	window->wd_trim.tr_trim_first = B_FALSE;
3802509Smrj 	window->wd_trim.tr_trim_last = B_FALSE;
3803509Smrj 	window->wd_trim.tr_first_copybuf_win = B_FALSE;
3804509Smrj 	window->wd_trim.tr_last_copybuf_win = B_FALSE;
3805509Smrj #if !defined(__amd64)
3806509Smrj 	window->wd_remap_copybuf = dma->dp_cb_remaping;
3807509Smrj #endif
3808509Smrj }
3809509Smrj 
3810509Smrj 
3811509Smrj /*
3812509Smrj  * rootnex_setup_cookie()
3813509Smrj  *    Called in the bind slow path when the sgl uses the copy buffer. If any of
3814509Smrj  *    the sgl uses the copy buffer, we need to go through each cookie, figure
3815509Smrj  *    out if it uses the copy buffer, and if it does, save away everything we'll
3816509Smrj  *    need during sync.
3817509Smrj  */
3818509Smrj static void
rootnex_setup_cookie(ddi_dma_obj_t * dmar_object,rootnex_dma_t * dma,ddi_dma_cookie_t * cookie,off_t cur_offset,size_t * copybuf_used,page_t ** cur_pp)3819509Smrj rootnex_setup_cookie(ddi_dma_obj_t *dmar_object, rootnex_dma_t *dma,
3820509Smrj     ddi_dma_cookie_t *cookie, off_t cur_offset, size_t *copybuf_used,
3821509Smrj     page_t **cur_pp)
3822509Smrj {
3823509Smrj 	boolean_t copybuf_sz_power_2;
3824509Smrj 	rootnex_sglinfo_t *sinfo;
38255084Sjohnlev 	paddr_t paddr;
3826509Smrj 	uint_t pidx;
3827509Smrj 	uint_t pcnt;
3828509Smrj 	off_t poff;
3829509Smrj #if defined(__amd64)
3830509Smrj 	pfn_t pfn;
3831509Smrj #else
3832509Smrj 	page_t **pplist;
3833509Smrj #endif
3834509Smrj 
383513050Sfrank.van.der.linden@oracle.com 	ASSERT(dmar_object->dmao_type != DMA_OTYP_DVADDR);
383613050Sfrank.van.der.linden@oracle.com 
3837509Smrj 	sinfo = &dma->dp_sglinfo;
3838509Smrj 
3839509Smrj 	/*
3840509Smrj 	 * Calculate the page index relative to the start of the buffer. The
3841509Smrj 	 * index to the current page for our buffer is the offset into the
3842509Smrj 	 * first page of the buffer plus our current offset into the buffer
3843509Smrj 	 * itself, shifted of course...
3844509Smrj 	 */
3845509Smrj 	pidx = (sinfo->si_buf_offset + cur_offset) >> MMU_PAGESHIFT;
3846509Smrj 	ASSERT(pidx < sinfo->si_max_pages);
3847509Smrj 
3848509Smrj 	/* if this cookie uses the copy buffer */
3849509Smrj 	if (cookie->dmac_type & ROOTNEX_USES_COPYBUF) {
3850509Smrj 		/*
3851509Smrj 		 * NOTE: we know that since this cookie uses the copy buffer, it
3852509Smrj 		 * is <= MMU_PAGESIZE.
3853509Smrj 		 */
3854509Smrj 
3855509Smrj 		/*
3856509Smrj 		 * get the offset into the page. For the 64-bit kernel, get the
3857509Smrj 		 * pfn which we'll use with seg kpm.
3858509Smrj 		 */
38595084Sjohnlev 		poff = cookie->dmac_laddress & MMU_PAGEOFFSET;
3860509Smrj #if defined(__amd64)
38615084Sjohnlev 		/* mfn_to_pfn() is a NOP on i86pc */
38625084Sjohnlev 		pfn = mfn_to_pfn(cookie->dmac_laddress >> MMU_PAGESHIFT);
38635084Sjohnlev #endif /* __amd64 */
3864509Smrj 
3865509Smrj 		/* figure out if the copybuf size is a power of 2 */
3866509Smrj 		if (dma->dp_copybuf_size & (dma->dp_copybuf_size - 1)) {
3867509Smrj 			copybuf_sz_power_2 = B_FALSE;
3868509Smrj 		} else {
3869509Smrj 			copybuf_sz_power_2 = B_TRUE;
3870509Smrj 		}
3871509Smrj 
3872509Smrj 		/* This page uses the copy buffer */
3873509Smrj 		dma->dp_pgmap[pidx].pm_uses_copybuf = B_TRUE;
3874509Smrj 
3875509Smrj 		/*
3876509Smrj 		 * save the copy buffer KVA that we'll use with this page.
3877509Smrj 		 * if we still fit within the copybuf, it's a simple add.
3878509Smrj 		 * otherwise, we need to wrap over using & or % accordingly.
3879509Smrj 		 */
3880509Smrj 		if ((*copybuf_used + MMU_PAGESIZE) <= dma->dp_copybuf_size) {
3881509Smrj 			dma->dp_pgmap[pidx].pm_cbaddr = dma->dp_cbaddr +
3882509Smrj 			    *copybuf_used;
3883509Smrj 		} else {
3884509Smrj 			if (copybuf_sz_power_2) {
3885509Smrj 				dma->dp_pgmap[pidx].pm_cbaddr = (caddr_t)(
3886509Smrj 				    (uintptr_t)dma->dp_cbaddr +
3887509Smrj 				    (*copybuf_used &
3888509Smrj 				    (dma->dp_copybuf_size - 1)));
38890Sstevel@tonic-gate 			} else {
3890509Smrj 				dma->dp_pgmap[pidx].pm_cbaddr = (caddr_t)(
3891509Smrj 				    (uintptr_t)dma->dp_cbaddr +
3892509Smrj 				    (*copybuf_used % dma->dp_copybuf_size));
38930Sstevel@tonic-gate 			}
3894509Smrj 		}
3895509Smrj 
3896509Smrj 		/*
3897509Smrj 		 * over write the cookie physical address with the address of
3898509Smrj 		 * the physical address of the copy buffer page that we will
3899509Smrj 		 * use.
3900509Smrj 		 */
39015084Sjohnlev 		paddr = pfn_to_pa(hat_getpfnum(kas.a_hat,
3902509Smrj 		    dma->dp_pgmap[pidx].pm_cbaddr)) + poff;
3903509Smrj 
390413050Sfrank.van.der.linden@oracle.com 		cookie->dmac_laddress = ROOTNEX_PADDR_TO_RBASE(paddr);
39055084Sjohnlev 
3906509Smrj 		/* if we have a kernel VA, it's easy, just save that address */
3907509Smrj 		if ((dmar_object->dmao_type != DMA_OTYP_PAGES) &&
3908509Smrj 		    (sinfo->si_asp == &kas)) {
3909509Smrj 			/*
3910509Smrj 			 * save away the page aligned virtual address of the
3911509Smrj 			 * driver buffer. Offsets are handled in the sync code.
3912509Smrj 			 */
3913509Smrj 			dma->dp_pgmap[pidx].pm_kaddr = (caddr_t)(((uintptr_t)
3914509Smrj 			    dmar_object->dmao_obj.virt_obj.v_addr + cur_offset)
3915509Smrj 			    & MMU_PAGEMASK);
3916509Smrj #if !defined(__amd64)
3917509Smrj 			/*
3918509Smrj 			 * we didn't need to, and will never need to map this
3919509Smrj 			 * page.
3920509Smrj 			 */
3921509Smrj 			dma->dp_pgmap[pidx].pm_mapped = B_FALSE;
3922509Smrj #endif
3923509Smrj 
3924509Smrj 		/* we don't have a kernel VA. We need one for the bcopy. */
3925509Smrj 		} else {
3926509Smrj #if defined(__amd64)
3927509Smrj 			/*
3928509Smrj 			 * for the 64-bit kernel, it's easy. We use seg kpm to
3929509Smrj 			 * get a Kernel VA for the corresponding pfn.
3930509Smrj 			 */
3931509Smrj 			dma->dp_pgmap[pidx].pm_kaddr = hat_kpm_pfn2va(pfn);
3932509Smrj #else
3933509Smrj 			/*
3934509Smrj 			 * for the 32-bit kernel, this is a pain. First we'll
3935509Smrj 			 * save away the page_t or user VA for this page. This
3936509Smrj 			 * is needed in rootnex_dma_win() when we switch to a
3937509Smrj 			 * new window which requires us to re-map the copy
3938509Smrj 			 * buffer.
3939509Smrj 			 */
3940509Smrj 			pplist = dmar_object->dmao_obj.virt_obj.v_priv;
3941509Smrj 			if (dmar_object->dmao_type == DMA_OTYP_PAGES) {
3942509Smrj 				dma->dp_pgmap[pidx].pm_pp = *cur_pp;
3943509Smrj 				dma->dp_pgmap[pidx].pm_vaddr = NULL;
3944509Smrj 			} else if (pplist != NULL) {
3945509Smrj 				dma->dp_pgmap[pidx].pm_pp = pplist[pidx];
3946509Smrj 				dma->dp_pgmap[pidx].pm_vaddr = NULL;
3947509Smrj 			} else {
3948509Smrj 				dma->dp_pgmap[pidx].pm_pp = NULL;
3949509Smrj 				dma->dp_pgmap[pidx].pm_vaddr = (caddr_t)
3950509Smrj 				    (((uintptr_t)
3951509Smrj 				    dmar_object->dmao_obj.virt_obj.v_addr +
3952509Smrj 				    cur_offset) & MMU_PAGEMASK);
3953509Smrj 			}
3954509Smrj 
3955509Smrj 			/*
3956509Smrj 			 * save away the page aligned virtual address which was
3957509Smrj 			 * allocated from the kernel heap arena (taking into
3958509Smrj 			 * account if we need more copy buffer than we alloced
3959509Smrj 			 * and use multiple windows to handle this, i.e. &,%).
3960509Smrj 			 * NOTE: there isn't and physical memory backing up this
3961509Smrj 			 * virtual address space currently.
3962509Smrj 			 */
3963509Smrj 			if ((*copybuf_used + MMU_PAGESIZE) <=
3964509Smrj 			    dma->dp_copybuf_size) {
3965509Smrj 				dma->dp_pgmap[pidx].pm_kaddr = (caddr_t)
3966509Smrj 				    (((uintptr_t)dma->dp_kva + *copybuf_used) &
3967509Smrj 				    MMU_PAGEMASK);
3968509Smrj 			} else {
3969509Smrj 				if (copybuf_sz_power_2) {
3970509Smrj 					dma->dp_pgmap[pidx].pm_kaddr = (caddr_t)
3971509Smrj 					    (((uintptr_t)dma->dp_kva +
3972509Smrj 					    (*copybuf_used &
3973509Smrj 					    (dma->dp_copybuf_size - 1))) &
3974509Smrj 					    MMU_PAGEMASK);
3975509Smrj 				} else {
3976509Smrj 					dma->dp_pgmap[pidx].pm_kaddr = (caddr_t)
3977509Smrj 					    (((uintptr_t)dma->dp_kva +
3978509Smrj 					    (*copybuf_used %
3979509Smrj 					    dma->dp_copybuf_size)) &
3980509Smrj 					    MMU_PAGEMASK);
3981509Smrj 				}
3982509Smrj 			}
3983509Smrj 
3984509Smrj 			/*
3985509Smrj 			 * if we haven't used up the available copy buffer yet,
3986509Smrj 			 * map the kva to the physical page.
3987509Smrj 			 */
3988509Smrj 			if (!dma->dp_cb_remaping && ((*copybuf_used +
3989509Smrj 			    MMU_PAGESIZE) <= dma->dp_copybuf_size)) {
3990509Smrj 				dma->dp_pgmap[pidx].pm_mapped = B_TRUE;
3991509Smrj 				if (dma->dp_pgmap[pidx].pm_pp != NULL) {
3992509Smrj 					i86_pp_map(dma->dp_pgmap[pidx].pm_pp,
3993509Smrj 					    dma->dp_pgmap[pidx].pm_kaddr);
3994509Smrj 				} else {
3995509Smrj 					i86_va_map(dma->dp_pgmap[pidx].pm_vaddr,
3996509Smrj 					    sinfo->si_asp,
3997509Smrj 					    dma->dp_pgmap[pidx].pm_kaddr);
3998509Smrj 				}
3999509Smrj 
4000509Smrj 			/*
4001509Smrj 			 * we've used up the available copy buffer, this page
4002509Smrj 			 * will have to be mapped during rootnex_dma_win() when
4003509Smrj 			 * we switch to a new window which requires a re-map
4004509Smrj 			 * the copy buffer. (32-bit kernel only)
4005509Smrj 			 */
4006509Smrj 			} else {
4007509Smrj 				dma->dp_pgmap[pidx].pm_mapped = B_FALSE;
4008509Smrj 			}
4009509Smrj #endif
4010509Smrj 			/* go to the next page_t */
4011509Smrj 			if (dmar_object->dmao_type == DMA_OTYP_PAGES) {
4012509Smrj 				*cur_pp = (*cur_pp)->p_next;
4013509Smrj 			}
40140Sstevel@tonic-gate 		}
4015509Smrj 
4016509Smrj 		/* add to the copy buffer count */
4017509Smrj 		*copybuf_used += MMU_PAGESIZE;
4018509Smrj 
4019509Smrj 	/*
4020509Smrj 	 * This cookie doesn't use the copy buffer. Walk through the pages this
4021509Smrj 	 * cookie occupies to reflect this.
4022509Smrj 	 */
4023509Smrj 	} else {
4024509Smrj 		/*
4025509Smrj 		 * figure out how many pages the cookie occupies. We need to
4026509Smrj 		 * use the original page offset of the buffer and the cookies
4027509Smrj 		 * offset in the buffer to do this.
4028509Smrj 		 */
4029509Smrj 		poff = (sinfo->si_buf_offset + cur_offset) & MMU_PAGEOFFSET;
4030509Smrj 		pcnt = mmu_btopr(cookie->dmac_size + poff);
4031509Smrj 
4032509Smrj 		while (pcnt > 0) {
4033509Smrj #if !defined(__amd64)
4034509Smrj 			/*
4035509Smrj 			 * the 32-bit kernel doesn't have seg kpm, so we need
4036509Smrj 			 * to map in the driver buffer (if it didn't come down
4037509Smrj 			 * with a kernel VA) on the fly. Since this page doesn't
4038509Smrj 			 * use the copy buffer, it's not, or will it ever, have
4039509Smrj 			 * to be mapped in.
4040509Smrj 			 */
4041509Smrj 			dma->dp_pgmap[pidx].pm_mapped = B_FALSE;
4042509Smrj #endif
4043509Smrj 			dma->dp_pgmap[pidx].pm_uses_copybuf = B_FALSE;
4044509Smrj 
4045509Smrj 			/*
4046509Smrj 			 * we need to update pidx and cur_pp or we'll loose
4047509Smrj 			 * track of where we are.
4048509Smrj 			 */
4049509Smrj 			if (dmar_object->dmao_type == DMA_OTYP_PAGES) {
4050509Smrj 				*cur_pp = (*cur_pp)->p_next;
4051509Smrj 			}
4052509Smrj 			pidx++;
4053509Smrj 			pcnt--;
4054509Smrj 		}
4055509Smrj 	}
4056509Smrj }
4057509Smrj 
4058509Smrj 
4059509Smrj /*
4060509Smrj  * rootnex_sgllen_window_boundary()
4061509Smrj  *    Called in the bind slow path when the next cookie causes us to exceed (in
4062509Smrj  *    this case == since we start at 0 and sgllen starts at 1) the maximum sgl
4063509Smrj  *    length supported by the DMA H/W.
4064509Smrj  */
4065509Smrj static int
rootnex_sgllen_window_boundary(ddi_dma_impl_t * hp,rootnex_dma_t * dma,rootnex_window_t ** windowp,ddi_dma_cookie_t * cookie,ddi_dma_attr_t * attr,off_t cur_offset)4066509Smrj rootnex_sgllen_window_boundary(ddi_dma_impl_t *hp, rootnex_dma_t *dma,
4067509Smrj     rootnex_window_t **windowp, ddi_dma_cookie_t *cookie, ddi_dma_attr_t *attr,
4068509Smrj     off_t cur_offset)
4069509Smrj {
4070509Smrj 	off_t new_offset;
4071509Smrj 	size_t trim_sz;
4072509Smrj 	off_t coffset;
4073509Smrj 
4074509Smrj 
4075509Smrj 	/*
4076509Smrj 	 * if we know we'll never have to trim, it's pretty easy. Just move to
4077509Smrj 	 * the next window and init it. We're done.
4078509Smrj 	 */
4079509Smrj 	if (!dma->dp_trim_required) {
4080509Smrj 		(*windowp)++;
4081509Smrj 		rootnex_init_win(hp, dma, *windowp, cookie, cur_offset);
4082509Smrj 		(*windowp)->wd_cookie_cnt++;
4083509Smrj 		(*windowp)->wd_size = cookie->dmac_size;
4084509Smrj 		return (DDI_SUCCESS);
4085509Smrj 	}
4086509Smrj 
4087509Smrj 	/* figure out how much we need to trim from the window */
4088509Smrj 	ASSERT(attr->dma_attr_granular != 0);
4089509Smrj 	if (dma->dp_granularity_power_2) {
4090509Smrj 		trim_sz = (*windowp)->wd_size & (attr->dma_attr_granular - 1);
4091509Smrj 	} else {
4092509Smrj 		trim_sz = (*windowp)->wd_size % attr->dma_attr_granular;
4093509Smrj 	}
4094509Smrj 
4095509Smrj 	/* The window's a whole multiple of granularity. We're done */
4096509Smrj 	if (trim_sz == 0) {
4097509Smrj 		(*windowp)++;
4098509Smrj 		rootnex_init_win(hp, dma, *windowp, cookie, cur_offset);
4099509Smrj 		(*windowp)->wd_cookie_cnt++;
4100509Smrj 		(*windowp)->wd_size = cookie->dmac_size;
4101509Smrj 		return (DDI_SUCCESS);
4102509Smrj 	}
4103509Smrj 
4104509Smrj 	/*
4105509Smrj 	 * The window's not a whole multiple of granularity, since we know this
4106509Smrj 	 * is due to the sgllen, we need to go back to the last cookie and trim
4107509Smrj 	 * that one, add the left over part of the old cookie into the new
4108509Smrj 	 * window, and then add in the new cookie into the new window.
4109509Smrj 	 */
4110509Smrj 
4111509Smrj 	/*
4112509Smrj 	 * make sure the driver isn't making us do something bad... Trimming and
4113509Smrj 	 * sgllen == 1 don't go together.
4114509Smrj 	 */
4115509Smrj 	if (attr->dma_attr_sgllen == 1) {
4116509Smrj 		return (DDI_DMA_NOMAPPING);
4117509Smrj 	}
4118509Smrj 
4119509Smrj 	/*
4120509Smrj 	 * first, setup the current window to account for the trim. Need to go
4121509Smrj 	 * back to the last cookie for this.
4122509Smrj 	 */
4123509Smrj 	cookie--;
4124509Smrj 	(*windowp)->wd_trim.tr_trim_last = B_TRUE;
4125509Smrj 	(*windowp)->wd_trim.tr_last_cookie = cookie;
41265084Sjohnlev 	(*windowp)->wd_trim.tr_last_paddr = cookie->dmac_laddress;
4127509Smrj 	ASSERT(cookie->dmac_size > trim_sz);
4128509Smrj 	(*windowp)->wd_trim.tr_last_size = cookie->dmac_size - trim_sz;
4129509Smrj 	(*windowp)->wd_size -= trim_sz;
4130509Smrj 
4131509Smrj 	/* save the buffer offsets for the next window */
4132509Smrj 	coffset = cookie->dmac_size - trim_sz;
4133509Smrj 	new_offset = (*windowp)->wd_offset + (*windowp)->wd_size;
4134509Smrj 
4135509Smrj 	/*
4136509Smrj 	 * set this now in case this is the first window. all other cases are
4137509Smrj 	 * set in dma_win()
4138509Smrj 	 */
4139509Smrj 	cookie->dmac_size = (*windowp)->wd_trim.tr_last_size;
4140509Smrj 
4141509Smrj 	/*
4142509Smrj 	 * initialize the next window using what's left over in the previous
4143509Smrj 	 * cookie.
4144509Smrj 	 */
4145509Smrj 	(*windowp)++;
4146509Smrj 	rootnex_init_win(hp, dma, *windowp, cookie, new_offset);
4147509Smrj 	(*windowp)->wd_cookie_cnt++;
4148509Smrj 	(*windowp)->wd_trim.tr_trim_first = B_TRUE;
41495084Sjohnlev 	(*windowp)->wd_trim.tr_first_paddr = cookie->dmac_laddress + coffset;
4150509Smrj 	(*windowp)->wd_trim.tr_first_size = trim_sz;
4151509Smrj 	if (cookie->dmac_type & ROOTNEX_USES_COPYBUF) {
4152509Smrj 		(*windowp)->wd_dosync = B_TRUE;
4153509Smrj 	}
4154509Smrj 
4155509Smrj 	/*
4156509Smrj 	 * now go back to the current cookie and add it to the new window. set
4157509Smrj 	 * the new window size to the what was left over from the previous
4158509Smrj 	 * cookie and what's in the current cookie.
4159509Smrj 	 */
4160509Smrj 	cookie++;
4161509Smrj 	(*windowp)->wd_cookie_cnt++;
4162509Smrj 	(*windowp)->wd_size = trim_sz + cookie->dmac_size;
4163509Smrj 
4164509Smrj 	/*
4165509Smrj 	 * trim plus the next cookie could put us over maxxfer (a cookie can be
4166509Smrj 	 * a max size of maxxfer). Handle that case.
4167509Smrj 	 */
4168509Smrj 	if ((*windowp)->wd_size > dma->dp_maxxfer) {
4169509Smrj 		/*
4170509Smrj 		 * maxxfer is already a whole multiple of granularity, and this
4171509Smrj 		 * trim will be <= the previous trim (since a cookie can't be
4172509Smrj 		 * larger than maxxfer). Make things simple here.
4173509Smrj 		 */
4174509Smrj 		trim_sz = (*windowp)->wd_size - dma->dp_maxxfer;
4175509Smrj 		(*windowp)->wd_trim.tr_trim_last = B_TRUE;
4176509Smrj 		(*windowp)->wd_trim.tr_last_cookie = cookie;
41775084Sjohnlev 		(*windowp)->wd_trim.tr_last_paddr = cookie->dmac_laddress;
4178509Smrj 		(*windowp)->wd_trim.tr_last_size = cookie->dmac_size - trim_sz;
4179509Smrj 		(*windowp)->wd_size -= trim_sz;
4180509Smrj 		ASSERT((*windowp)->wd_size == dma->dp_maxxfer);
4181509Smrj 
4182509Smrj 		/* save the buffer offsets for the next window */
4183509Smrj 		coffset = cookie->dmac_size - trim_sz;
4184509Smrj 		new_offset = (*windowp)->wd_offset + (*windowp)->wd_size;
4185509Smrj 
4186509Smrj 		/* setup the next window */
4187509Smrj 		(*windowp)++;
4188509Smrj 		rootnex_init_win(hp, dma, *windowp, cookie, new_offset);
4189509Smrj 		(*windowp)->wd_cookie_cnt++;
4190509Smrj 		(*windowp)->wd_trim.tr_trim_first = B_TRUE;
41915084Sjohnlev 		(*windowp)->wd_trim.tr_first_paddr = cookie->dmac_laddress +
4192509Smrj 		    coffset;
4193509Smrj 		(*windowp)->wd_trim.tr_first_size = trim_sz;
4194509Smrj 	}
4195509Smrj 
4196509Smrj 	return (DDI_SUCCESS);
4197509Smrj }
4198509Smrj 
4199509Smrj 
4200509Smrj /*
4201509Smrj  * rootnex_copybuf_window_boundary()
4202509Smrj  *    Called in bind slowpath when we get to a window boundary because we used
4203509Smrj  *    up all the copy buffer that we have.
4204509Smrj  */
4205509Smrj static int
rootnex_copybuf_window_boundary(ddi_dma_impl_t * hp,rootnex_dma_t * dma,rootnex_window_t ** windowp,ddi_dma_cookie_t * cookie,off_t cur_offset,size_t * copybuf_used)4206509Smrj rootnex_copybuf_window_boundary(ddi_dma_impl_t *hp, rootnex_dma_t *dma,
4207509Smrj     rootnex_window_t **windowp, ddi_dma_cookie_t *cookie, off_t cur_offset,
4208509Smrj     size_t *copybuf_used)
4209509Smrj {
4210509Smrj 	rootnex_sglinfo_t *sinfo;
4211509Smrj 	off_t new_offset;
4212509Smrj 	size_t trim_sz;
42135084Sjohnlev 	paddr_t paddr;
4214509Smrj 	off_t coffset;
4215509Smrj 	uint_t pidx;
4216509Smrj 	off_t poff;
4217509Smrj 
4218509Smrj 
4219509Smrj 	sinfo = &dma->dp_sglinfo;
4220509Smrj 
4221509Smrj 	/*
4222509Smrj 	 * the copy buffer should be a whole multiple of page size. We know that
4223509Smrj 	 * this cookie is <= MMU_PAGESIZE.
4224509Smrj 	 */
4225509Smrj 	ASSERT(cookie->dmac_size <= MMU_PAGESIZE);
4226509Smrj 
4227509Smrj 	/*
4228509Smrj 	 * from now on, all new windows in this bind need to be re-mapped during
4229509Smrj 	 * ddi_dma_getwin() (32-bit kernel only). i.e. we ran out out copybuf
4230509Smrj 	 * space...
4231509Smrj 	 */
4232509Smrj #if !defined(__amd64)
4233509Smrj 	dma->dp_cb_remaping = B_TRUE;
4234509Smrj #endif
4235509Smrj 
4236509Smrj 	/* reset copybuf used */
4237509Smrj 	*copybuf_used = 0;
4238509Smrj 
4239509Smrj 	/*
4240509Smrj 	 * if we don't have to trim (since granularity is set to 1), go to the
4241509Smrj 	 * next window and add the current cookie to it. We know the current
4242509Smrj 	 * cookie uses the copy buffer since we're in this code path.
4243509Smrj 	 */
4244509Smrj 	if (!dma->dp_trim_required) {
4245509Smrj 		(*windowp)++;
4246509Smrj 		rootnex_init_win(hp, dma, *windowp, cookie, cur_offset);
4247509Smrj 
4248509Smrj 		/* Add this cookie to the new window */
4249509Smrj 		(*windowp)->wd_cookie_cnt++;
4250509Smrj 		(*windowp)->wd_size += cookie->dmac_size;
4251509Smrj 		*copybuf_used += MMU_PAGESIZE;
4252509Smrj 		return (DDI_SUCCESS);
4253509Smrj 	}
4254509Smrj 
4255509Smrj 	/*
4256509Smrj 	 * *** may need to trim, figure it out.
4257509Smrj 	 */
4258509Smrj 
4259509Smrj 	/* figure out how much we need to trim from the window */
4260509Smrj 	if (dma->dp_granularity_power_2) {
4261509Smrj 		trim_sz = (*windowp)->wd_size &
4262509Smrj 		    (hp->dmai_attr.dma_attr_granular - 1);
4263509Smrj 	} else {
4264509Smrj 		trim_sz = (*windowp)->wd_size % hp->dmai_attr.dma_attr_granular;
4265509Smrj 	}
4266509Smrj 
4267509Smrj 	/*
4268509Smrj 	 * if the window's a whole multiple of granularity, go to the next
4269509Smrj 	 * window, init it, then add in the current cookie. We know the current
4270509Smrj 	 * cookie uses the copy buffer since we're in this code path.
4271509Smrj 	 */
4272509Smrj 	if (trim_sz == 0) {
4273509Smrj 		(*windowp)++;
4274509Smrj 		rootnex_init_win(hp, dma, *windowp, cookie, cur_offset);
4275509Smrj 
4276509Smrj 		/* Add this cookie to the new window */
4277509Smrj 		(*windowp)->wd_cookie_cnt++;
4278509Smrj 		(*windowp)->wd_size += cookie->dmac_size;
4279509Smrj 		*copybuf_used += MMU_PAGESIZE;
4280509Smrj 		return (DDI_SUCCESS);
4281509Smrj 	}
4282509Smrj 
4283509Smrj 	/*
4284509Smrj 	 * *** We figured it out, we definitly need to trim
4285509Smrj 	 */
4286509Smrj 
4287509Smrj 	/*
4288509Smrj 	 * make sure the driver isn't making us do something bad...
4289509Smrj 	 * Trimming and sgllen == 1 don't go together.
4290509Smrj 	 */
4291509Smrj 	if (hp->dmai_attr.dma_attr_sgllen == 1) {
4292509Smrj 		return (DDI_DMA_NOMAPPING);
4293509Smrj 	}
4294509Smrj 
4295509Smrj 	/*
4296509Smrj 	 * first, setup the current window to account for the trim. Need to go
4297509Smrj 	 * back to the last cookie for this. Some of the last cookie will be in
4298509Smrj 	 * the current window, and some of the last cookie will be in the new
4299509Smrj 	 * window. All of the current cookie will be in the new window.
4300509Smrj 	 */
4301509Smrj 	cookie--;
4302509Smrj 	(*windowp)->wd_trim.tr_trim_last = B_TRUE;
4303509Smrj 	(*windowp)->wd_trim.tr_last_cookie = cookie;
43045084Sjohnlev 	(*windowp)->wd_trim.tr_last_paddr = cookie->dmac_laddress;
4305509Smrj 	ASSERT(cookie->dmac_size > trim_sz);
4306509Smrj 	(*windowp)->wd_trim.tr_last_size = cookie->dmac_size - trim_sz;
4307509Smrj 	(*windowp)->wd_size -= trim_sz;
4308509Smrj 
4309509Smrj 	/*
4310509Smrj 	 * we're trimming the last cookie (not the current cookie). So that
4311509Smrj 	 * last cookie may have or may not have been using the copy buffer (
4312509Smrj 	 * we know the cookie passed in uses the copy buffer since we're in
4313509Smrj 	 * this code path).
4314509Smrj 	 *
4315509Smrj 	 * If the last cookie doesn't use the copy buffer, nothing special to
4316509Smrj 	 * do. However, if it does uses the copy buffer, it will be both the
4317509Smrj 	 * last page in the current window and the first page in the next
4318509Smrj 	 * window. Since we are reusing the copy buffer (and KVA space on the
4319509Smrj 	 * 32-bit kernel), this page will use the end of the copy buffer in the
4320509Smrj 	 * current window, and the start of the copy buffer in the next window.
4321509Smrj 	 * Track that info... The cookie physical address was already set to
4322509Smrj 	 * the copy buffer physical address in setup_cookie..
4323509Smrj 	 */
4324509Smrj 	if (cookie->dmac_type & ROOTNEX_USES_COPYBUF) {
4325509Smrj 		pidx = (sinfo->si_buf_offset + (*windowp)->wd_offset +
4326509Smrj 		    (*windowp)->wd_size) >> MMU_PAGESHIFT;
4327509Smrj 		(*windowp)->wd_trim.tr_last_copybuf_win = B_TRUE;
4328509Smrj 		(*windowp)->wd_trim.tr_last_pidx = pidx;
4329509Smrj 		(*windowp)->wd_trim.tr_last_cbaddr =
4330509Smrj 		    dma->dp_pgmap[pidx].pm_cbaddr;
4331509Smrj #if !defined(__amd64)
4332509Smrj 		(*windowp)->wd_trim.tr_last_kaddr =
4333509Smrj 		    dma->dp_pgmap[pidx].pm_kaddr;
4334509Smrj #endif
4335509Smrj 	}
4336509Smrj 
4337509Smrj 	/* save the buffer offsets for the next window */
4338509Smrj 	coffset = cookie->dmac_size - trim_sz;
4339509Smrj 	new_offset = (*windowp)->wd_offset + (*windowp)->wd_size;
4340509Smrj 
4341509Smrj 	/*
4342509Smrj 	 * set this now in case this is the first window. all other cases are
4343509Smrj 	 * set in dma_win()
4344509Smrj 	 */
4345509Smrj 	cookie->dmac_size = (*windowp)->wd_trim.tr_last_size;
4346509Smrj 
4347509Smrj 	/*
4348509Smrj 	 * initialize the next window using what's left over in the previous
4349509Smrj 	 * cookie.
4350509Smrj 	 */
4351509Smrj 	(*windowp)++;
4352509Smrj 	rootnex_init_win(hp, dma, *windowp, cookie, new_offset);
4353509Smrj 	(*windowp)->wd_cookie_cnt++;
4354509Smrj 	(*windowp)->wd_trim.tr_trim_first = B_TRUE;
43555084Sjohnlev 	(*windowp)->wd_trim.tr_first_paddr = cookie->dmac_laddress + coffset;
4356509Smrj 	(*windowp)->wd_trim.tr_first_size = trim_sz;
4357509Smrj 
4358509Smrj 	/*
4359509Smrj 	 * again, we're tracking if the last cookie uses the copy buffer.
4360509Smrj 	 * read the comment above for more info on why we need to track
4361509Smrj 	 * additional state.
4362509Smrj 	 *
4363509Smrj 	 * For the first cookie in the new window, we need reset the physical
4364509Smrj 	 * address to DMA into to the start of the copy buffer plus any
4365509Smrj 	 * initial page offset which may be present.
4366509Smrj 	 */
4367509Smrj 	if (cookie->dmac_type & ROOTNEX_USES_COPYBUF) {
4368509Smrj 		(*windowp)->wd_dosync = B_TRUE;
4369509Smrj 		(*windowp)->wd_trim.tr_first_copybuf_win = B_TRUE;
4370509Smrj 		(*windowp)->wd_trim.tr_first_pidx = pidx;
4371509Smrj 		(*windowp)->wd_trim.tr_first_cbaddr = dma->dp_cbaddr;
4372509Smrj 		poff = (*windowp)->wd_trim.tr_first_paddr & MMU_PAGEOFFSET;
43735084Sjohnlev 
43745084Sjohnlev 		paddr = pfn_to_pa(hat_getpfnum(kas.a_hat, dma->dp_cbaddr)) +
43755084Sjohnlev 		    poff;
43765084Sjohnlev 		(*windowp)->wd_trim.tr_first_paddr =
437713050Sfrank.van.der.linden@oracle.com 		    ROOTNEX_PADDR_TO_RBASE(paddr);
43785084Sjohnlev 
4379509Smrj #if !defined(__amd64)
4380509Smrj 		(*windowp)->wd_trim.tr_first_kaddr = dma->dp_kva;
4381509Smrj #endif
4382509Smrj 		/* account for the cookie copybuf usage in the new window */
4383509Smrj 		*copybuf_used += MMU_PAGESIZE;
4384509Smrj 
4385509Smrj 		/*
4386509Smrj 		 * every piece of code has to have a hack, and here is this
4387509Smrj 		 * ones :-)
4388509Smrj 		 *
4389509Smrj 		 * There is a complex interaction between setup_cookie and the
4390509Smrj 		 * copybuf window boundary. The complexity had to be in either
4391509Smrj 		 * the maxxfer window, or the copybuf window, and I chose the
4392509Smrj 		 * copybuf code.
4393509Smrj 		 *
4394509Smrj 		 * So in this code path, we have taken the last cookie,
4395509Smrj 		 * virtually broken it in half due to the trim, and it happens
4396509Smrj 		 * to use the copybuf which further complicates life. At the
4397509Smrj 		 * same time, we have already setup the current cookie, which
4398509Smrj 		 * is now wrong. More background info: the current cookie uses
4399509Smrj 		 * the copybuf, so it is only a page long max. So we need to
4400509Smrj 		 * fix the current cookies copy buffer address, physical
4401509Smrj 		 * address, and kva for the 32-bit kernel. We due this by
4402509Smrj 		 * bumping them by page size (of course, we can't due this on
4403509Smrj 		 * the physical address since the copy buffer may not be
4404509Smrj 		 * physically contiguous).
4405509Smrj 		 */
4406509Smrj 		cookie++;
4407509Smrj 		dma->dp_pgmap[pidx + 1].pm_cbaddr += MMU_PAGESIZE;
44085084Sjohnlev 		poff = cookie->dmac_laddress & MMU_PAGEOFFSET;
44095084Sjohnlev 
44105084Sjohnlev 		paddr = pfn_to_pa(hat_getpfnum(kas.a_hat,
4411509Smrj 		    dma->dp_pgmap[pidx + 1].pm_cbaddr)) + poff;
441213050Sfrank.van.der.linden@oracle.com 		cookie->dmac_laddress = ROOTNEX_PADDR_TO_RBASE(paddr);
44135084Sjohnlev 
4414509Smrj #if !defined(__amd64)
4415509Smrj 		ASSERT(dma->dp_pgmap[pidx + 1].pm_mapped == B_FALSE);
4416509Smrj 		dma->dp_pgmap[pidx + 1].pm_kaddr += MMU_PAGESIZE;
4417509Smrj #endif
4418509Smrj 	} else {
4419509Smrj 		/* go back to the current cookie */
4420509Smrj 		cookie++;
4421509Smrj 	}
4422509Smrj 
4423509Smrj 	/*
4424509Smrj 	 * add the current cookie to the new window. set the new window size to
4425509Smrj 	 * the what was left over from the previous cookie and what's in the
4426509Smrj 	 * current cookie.
4427509Smrj 	 */
4428509Smrj 	(*windowp)->wd_cookie_cnt++;
4429509Smrj 	(*windowp)->wd_size = trim_sz + cookie->dmac_size;
4430509Smrj 	ASSERT((*windowp)->wd_size < dma->dp_maxxfer);
4431509Smrj 
4432509Smrj 	/*
4433509Smrj 	 * we know that the cookie passed in always uses the copy buffer. We
4434509Smrj 	 * wouldn't be here if it didn't.
4435509Smrj 	 */
4436509Smrj 	*copybuf_used += MMU_PAGESIZE;
4437509Smrj 
4438509Smrj 	return (DDI_SUCCESS);
4439509Smrj }
4440509Smrj 
4441509Smrj 
4442509Smrj /*
4443509Smrj  * rootnex_maxxfer_window_boundary()
4444509Smrj  *    Called in bind slowpath when we get to a window boundary because we will
4445509Smrj  *    go over maxxfer.
4446509Smrj  */
4447509Smrj static int
rootnex_maxxfer_window_boundary(ddi_dma_impl_t * hp,rootnex_dma_t * dma,rootnex_window_t ** windowp,ddi_dma_cookie_t * cookie)4448509Smrj rootnex_maxxfer_window_boundary(ddi_dma_impl_t *hp, rootnex_dma_t *dma,
4449509Smrj     rootnex_window_t **windowp, ddi_dma_cookie_t *cookie)
4450509Smrj {
4451509Smrj 	size_t dmac_size;
4452509Smrj 	off_t new_offset;
4453509Smrj 	size_t trim_sz;
4454509Smrj 	off_t coffset;
4455509Smrj 
4456509Smrj 
4457509Smrj 	/*
4458509Smrj 	 * calculate how much we have to trim off of the current cookie to equal
4459509Smrj 	 * maxxfer. We don't have to account for granularity here since our
4460509Smrj 	 * maxxfer already takes that into account.
4461509Smrj 	 */
4462509Smrj 	trim_sz = ((*windowp)->wd_size + cookie->dmac_size) - dma->dp_maxxfer;
4463509Smrj 	ASSERT(trim_sz <= cookie->dmac_size);
4464509Smrj 	ASSERT(trim_sz <= dma->dp_maxxfer);
4465509Smrj 
4466509Smrj 	/* save cookie size since we need it later and we might change it */
4467509Smrj 	dmac_size = cookie->dmac_size;
4468509Smrj 
4469509Smrj 	/*
4470509Smrj 	 * if we're not trimming the entire cookie, setup the current window to
4471509Smrj 	 * account for the trim.
4472509Smrj 	 */
4473509Smrj 	if (trim_sz < cookie->dmac_size) {
4474509Smrj 		(*windowp)->wd_cookie_cnt++;
4475509Smrj 		(*windowp)->wd_trim.tr_trim_last = B_TRUE;
4476509Smrj 		(*windowp)->wd_trim.tr_last_cookie = cookie;
44775084Sjohnlev 		(*windowp)->wd_trim.tr_last_paddr = cookie->dmac_laddress;
4478509Smrj 		(*windowp)->wd_trim.tr_last_size = cookie->dmac_size - trim_sz;
4479509Smrj 		(*windowp)->wd_size = dma->dp_maxxfer;
4480509Smrj 
4481509Smrj 		/*
4482509Smrj 		 * set the adjusted cookie size now in case this is the first
4483509Smrj 		 * window. All other windows are taken care of in get win
4484509Smrj 		 */
4485509Smrj 		cookie->dmac_size = (*windowp)->wd_trim.tr_last_size;
4486509Smrj 	}
4487509Smrj 
4488509Smrj 	/*
4489509Smrj 	 * coffset is the current offset within the cookie, new_offset is the
4490509Smrj 	 * current offset with the entire buffer.
4491509Smrj 	 */
4492509Smrj 	coffset = dmac_size - trim_sz;
4493509Smrj 	new_offset = (*windowp)->wd_offset + (*windowp)->wd_size;
4494509Smrj 
4495509Smrj 	/* initialize the next window */
4496509Smrj 	(*windowp)++;
4497509Smrj 	rootnex_init_win(hp, dma, *windowp, cookie, new_offset);
4498509Smrj 	(*windowp)->wd_cookie_cnt++;
4499509Smrj 	(*windowp)->wd_size = trim_sz;
4500509Smrj 	if (trim_sz < dmac_size) {
4501509Smrj 		(*windowp)->wd_trim.tr_trim_first = B_TRUE;
45025084Sjohnlev 		(*windowp)->wd_trim.tr_first_paddr = cookie->dmac_laddress +
4503509Smrj 		    coffset;
4504509Smrj 		(*windowp)->wd_trim.tr_first_size = trim_sz;
4505509Smrj 	}
4506509Smrj 
4507509Smrj 	return (DDI_SUCCESS);
4508509Smrj }
4509509Smrj 
4510509Smrj 
4511509Smrj /*ARGSUSED*/
4512509Smrj static int
rootnex_coredma_sync(dev_info_t * dip,dev_info_t * rdip,ddi_dma_handle_t handle,off_t off,size_t len,uint_t cache_flags)45137613SVikram.Hegde@Sun.COM rootnex_coredma_sync(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle,
4514509Smrj     off_t off, size_t len, uint_t cache_flags)
4515509Smrj {
4516509Smrj 	rootnex_sglinfo_t *sinfo;
4517509Smrj 	rootnex_pgmap_t *cbpage;
4518509Smrj 	rootnex_window_t *win;
4519509Smrj 	ddi_dma_impl_t *hp;
4520509Smrj 	rootnex_dma_t *dma;
4521509Smrj 	caddr_t fromaddr;
4522509Smrj 	caddr_t toaddr;
4523509Smrj 	uint_t psize;
4524509Smrj 	off_t offset;
4525509Smrj 	uint_t pidx;
4526509Smrj 	size_t size;
4527509Smrj 	off_t poff;
4528509Smrj 	int e;
4529509Smrj 
4530509Smrj 
4531509Smrj 	hp = (ddi_dma_impl_t *)handle;
4532509Smrj 	dma = (rootnex_dma_t *)hp->dmai_private;
4533509Smrj 	sinfo = &dma->dp_sglinfo;
4534509Smrj 
4535509Smrj 	/*
4536509Smrj 	 * if we don't have any windows, we don't need to sync. A copybuf
4537509Smrj 	 * will cause us to have at least one window.
4538509Smrj 	 */
4539509Smrj 	if (dma->dp_window == NULL) {
4540509Smrj 		return (DDI_SUCCESS);
4541509Smrj 	}
4542509Smrj 
4543509Smrj 	/* This window may not need to be sync'd */
4544509Smrj 	win = &dma->dp_window[dma->dp_current_win];
4545509Smrj 	if (!win->wd_dosync) {
4546509Smrj 		return (DDI_SUCCESS);
4547509Smrj 	}
4548509Smrj 
4549509Smrj 	/* handle off and len special cases */
4550509Smrj 	if ((off == 0) || (rootnex_sync_ignore_params)) {
4551509Smrj 		offset = win->wd_offset;
4552509Smrj 	} else {
4553509Smrj 		offset = off;
4554509Smrj 	}
4555509Smrj 	if ((len == 0) || (rootnex_sync_ignore_params)) {
4556509Smrj 		size = win->wd_size;
4557509Smrj 	} else {
4558509Smrj 		size = len;
4559509Smrj 	}
4560509Smrj 
4561509Smrj 	/* check the sync args to make sure they make a little sense */
4562509Smrj 	if (rootnex_sync_check_parms) {
4563509Smrj 		e = rootnex_valid_sync_parms(hp, win, offset, size,
4564509Smrj 		    cache_flags);
4565509Smrj 		if (e != DDI_SUCCESS) {
456612837Sfrank.van.der.linden@oracle.com 			ROOTNEX_DPROF_INC(&rootnex_cnt[ROOTNEX_CNT_SYNC_FAIL]);
4567509Smrj 			return (DDI_FAILURE);
4568509Smrj 		}
4569509Smrj 	}
4570509Smrj 
4571509Smrj 	/*
4572509Smrj 	 * special case the first page to handle the offset into the page. The
4573509Smrj 	 * offset to the current page for our buffer is the offset into the
4574509Smrj 	 * first page of the buffer plus our current offset into the buffer
4575509Smrj 	 * itself, masked of course.
4576509Smrj 	 */
4577509Smrj 	poff = (sinfo->si_buf_offset + offset) & MMU_PAGEOFFSET;
4578509Smrj 	psize = MIN((MMU_PAGESIZE - poff), size);
4579509Smrj 
4580509Smrj 	/* go through all the pages that we want to sync */
4581509Smrj 	while (size > 0) {
4582509Smrj 		/*
4583509Smrj 		 * Calculate the page index relative to the start of the buffer.
4584509Smrj 		 * The index to the current page for our buffer is the offset
4585509Smrj 		 * into the first page of the buffer plus our current offset
4586509Smrj 		 * into the buffer itself, shifted of course...
4587509Smrj 		 */
4588509Smrj 		pidx = (sinfo->si_buf_offset + offset) >> MMU_PAGESHIFT;
4589509Smrj 		ASSERT(pidx < sinfo->si_max_pages);
4590509Smrj 
4591509Smrj 		/*
4592509Smrj 		 * if this page uses the copy buffer, we need to sync it,
4593509Smrj 		 * otherwise, go on to the next page.
4594509Smrj 		 */
4595509Smrj 		cbpage = &dma->dp_pgmap[pidx];
4596509Smrj 		ASSERT((cbpage->pm_uses_copybuf == B_TRUE) ||
4597509Smrj 		    (cbpage->pm_uses_copybuf == B_FALSE));
4598509Smrj 		if (cbpage->pm_uses_copybuf) {
4599509Smrj 			/* cbaddr and kaddr should be page aligned */
4600509Smrj 			ASSERT(((uintptr_t)cbpage->pm_cbaddr &
4601509Smrj 			    MMU_PAGEOFFSET) == 0);
4602509Smrj 			ASSERT(((uintptr_t)cbpage->pm_kaddr &
4603509Smrj 			    MMU_PAGEOFFSET) == 0);
4604509Smrj 
4605509Smrj 			/*
4606509Smrj 			 * if we're copying for the device, we are going to
4607509Smrj 			 * copy from the drivers buffer and to the rootnex
4608509Smrj 			 * allocated copy buffer.
4609509Smrj 			 */
4610509Smrj 			if (cache_flags == DDI_DMA_SYNC_FORDEV) {
4611509Smrj 				fromaddr = cbpage->pm_kaddr + poff;
4612509Smrj 				toaddr = cbpage->pm_cbaddr + poff;
461312837Sfrank.van.der.linden@oracle.com 				ROOTNEX_DPROBE2(rootnex__sync__dev,
4614509Smrj 				    dev_info_t *, dma->dp_dip, size_t, psize);
4615509Smrj 
4616509Smrj 			/*
4617509Smrj 			 * if we're copying for the cpu/kernel, we are going to
4618509Smrj 			 * copy from the rootnex allocated copy buffer to the
4619509Smrj 			 * drivers buffer.
4620509Smrj 			 */
4621509Smrj 			} else {
4622509Smrj 				fromaddr = cbpage->pm_cbaddr + poff;
4623509Smrj 				toaddr = cbpage->pm_kaddr + poff;
462412837Sfrank.van.der.linden@oracle.com 				ROOTNEX_DPROBE2(rootnex__sync__cpu,
4625509Smrj 				    dev_info_t *, dma->dp_dip, size_t, psize);
4626509Smrj 			}
4627509Smrj 
4628509Smrj 			bcopy(fromaddr, toaddr, psize);
4629509Smrj 		}
4630509Smrj 
4631509Smrj 		/*
4632509Smrj 		 * decrement size until we're done, update our offset into the
4633509Smrj 		 * buffer, and get the next page size.
4634509Smrj 		 */
4635509Smrj 		size -= psize;
4636509Smrj 		offset += psize;
4637509Smrj 		psize = MIN(MMU_PAGESIZE, size);
4638509Smrj 
4639509Smrj 		/* page offset is zero for the rest of this loop */
4640509Smrj 		poff = 0;
4641509Smrj 	}
4642509Smrj 
4643509Smrj 	return (DDI_SUCCESS);
4644509Smrj }
4645509Smrj 
46467613SVikram.Hegde@Sun.COM /*
46477613SVikram.Hegde@Sun.COM  * rootnex_dma_sync()
46487613SVikram.Hegde@Sun.COM  *    called from ddi_dma_sync() if DMP_NOSYNC is not set in hp->dmai_rflags.
46497613SVikram.Hegde@Sun.COM  *    We set DMP_NOSYNC if we're not using the copy buffer. If DMP_NOSYNC
46507613SVikram.Hegde@Sun.COM  *    is set, ddi_dma_sync() returns immediately passing back success.
46517613SVikram.Hegde@Sun.COM  */
46527613SVikram.Hegde@Sun.COM /*ARGSUSED*/
46537613SVikram.Hegde@Sun.COM static int
rootnex_dma_sync(dev_info_t * dip,dev_info_t * rdip,ddi_dma_handle_t handle,off_t off,size_t len,uint_t cache_flags)46547613SVikram.Hegde@Sun.COM rootnex_dma_sync(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle,
46557613SVikram.Hegde@Sun.COM     off_t off, size_t len, uint_t cache_flags)
46567613SVikram.Hegde@Sun.COM {
465711600SVikram.Hegde@Sun.COM #if defined(__amd64) && !defined(__xpv)
465810216SVikram.Hegde@Sun.COM 	if (IOMMU_USED(rdip)) {
46597613SVikram.Hegde@Sun.COM 		return (iommulib_nexdma_sync(dip, rdip, handle, off, len,
46607613SVikram.Hegde@Sun.COM 		    cache_flags));
46617613SVikram.Hegde@Sun.COM 	}
46627613SVikram.Hegde@Sun.COM #endif
46637613SVikram.Hegde@Sun.COM 	return (rootnex_coredma_sync(dip, rdip, handle, off, len,
46647613SVikram.Hegde@Sun.COM 	    cache_flags));
46657613SVikram.Hegde@Sun.COM }
4666509Smrj 
4667509Smrj /*
4668509Smrj  * rootnex_valid_sync_parms()
4669509Smrj  *    checks the parameters passed to sync to verify they are correct.
4670509Smrj  */
4671509Smrj static int
rootnex_valid_sync_parms(ddi_dma_impl_t * hp,rootnex_window_t * win,off_t offset,size_t size,uint_t cache_flags)4672509Smrj rootnex_valid_sync_parms(ddi_dma_impl_t *hp, rootnex_window_t *win,
4673509Smrj     off_t offset, size_t size, uint_t cache_flags)
4674509Smrj {
4675509Smrj 	off_t woffset;
4676509Smrj 
4677509Smrj 
4678509Smrj 	/*
4679509Smrj 	 * the first part of the test to make sure the offset passed in is
4680509Smrj 	 * within the window.
4681509Smrj 	 */
4682509Smrj 	if (offset < win->wd_offset) {
4683509Smrj 		return (DDI_FAILURE);
4684509Smrj 	}
4685509Smrj 
4686509Smrj 	/*
4687509Smrj 	 * second and last part of the test to make sure the offset and length
4688509Smrj 	 * passed in is within the window.
4689509Smrj 	 */
4690509Smrj 	woffset = offset - win->wd_offset;
4691509Smrj 	if ((woffset + size) > win->wd_size) {
4692509Smrj 		return (DDI_FAILURE);
4693509Smrj 	}
4694509Smrj 
4695509Smrj 	/*
4696509Smrj 	 * if we are sync'ing for the device, the DDI_DMA_WRITE flag should
4697509Smrj 	 * be set too.
4698509Smrj 	 */
4699509Smrj 	if ((cache_flags == DDI_DMA_SYNC_FORDEV) &&
4700509Smrj 	    (hp->dmai_rflags & DDI_DMA_WRITE)) {
4701509Smrj 		return (DDI_SUCCESS);
4702509Smrj 	}
4703509Smrj 
4704509Smrj 	/*
4705509Smrj 	 * at this point, either DDI_DMA_SYNC_FORCPU or DDI_DMA_SYNC_FORKERNEL
4706509Smrj 	 * should be set. Also DDI_DMA_READ should be set in the flags.
4707509Smrj 	 */
4708509Smrj 	if (((cache_flags == DDI_DMA_SYNC_FORCPU) ||
4709509Smrj 	    (cache_flags == DDI_DMA_SYNC_FORKERNEL)) &&
4710509Smrj 	    (hp->dmai_rflags & DDI_DMA_READ)) {
4711509Smrj 		return (DDI_SUCCESS);
4712509Smrj 	}
4713509Smrj 
4714509Smrj 	return (DDI_FAILURE);
4715509Smrj }
4716509Smrj 
4717509Smrj 
4718509Smrj /*ARGSUSED*/
4719509Smrj static int
rootnex_coredma_win(dev_info_t * dip,dev_info_t * rdip,ddi_dma_handle_t handle,uint_t win,off_t * offp,size_t * lenp,ddi_dma_cookie_t * cookiep,uint_t * ccountp)47207613SVikram.Hegde@Sun.COM rootnex_coredma_win(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle,
4721509Smrj     uint_t win, off_t *offp, size_t *lenp, ddi_dma_cookie_t *cookiep,
4722509Smrj     uint_t *ccountp)
4723509Smrj {
4724509Smrj 	rootnex_window_t *window;
4725509Smrj 	rootnex_trim_t *trim;
4726509Smrj 	ddi_dma_impl_t *hp;
4727509Smrj 	rootnex_dma_t *dma;
472813050Sfrank.van.der.linden@oracle.com 	ddi_dma_obj_t *dmao;
4729509Smrj #if !defined(__amd64)
4730509Smrj 	rootnex_sglinfo_t *sinfo;
4731509Smrj 	rootnex_pgmap_t *pmap;
4732509Smrj 	uint_t pidx;
4733509Smrj 	uint_t pcnt;
4734509Smrj 	off_t poff;
4735509Smrj 	int i;
4736509Smrj #endif
4737509Smrj 
4738509Smrj 
4739509Smrj 	hp = (ddi_dma_impl_t *)handle;
4740509Smrj 	dma = (rootnex_dma_t *)hp->dmai_private;
4741509Smrj #if !defined(__amd64)
4742509Smrj 	sinfo = &dma->dp_sglinfo;
4743509Smrj #endif
4744509Smrj 
4745509Smrj 	/* If we try and get a window which doesn't exist, return failure */
4746509Smrj 	if (win >= hp->dmai_nwin) {
474712837Sfrank.van.der.linden@oracle.com 		ROOTNEX_DPROF_INC(&rootnex_cnt[ROOTNEX_CNT_GETWIN_FAIL]);
4748509Smrj 		return (DDI_FAILURE);
4749509Smrj 	}
4750509Smrj 
475113050Sfrank.van.der.linden@oracle.com 	dmao = dma->dp_dvma_used ? &dma->dp_dma : &dma->dp_dvma;
475213050Sfrank.van.der.linden@oracle.com 
4753509Smrj 	/*
4754509Smrj 	 * if we don't have any windows, and they're asking for the first
4755509Smrj 	 * window, setup the cookie pointer to the first cookie in the bind.
4756509Smrj 	 * setup our return values, then increment the cookie since we return
4757509Smrj 	 * the first cookie on the stack.
4758509Smrj 	 */
4759509Smrj 	if (dma->dp_window == NULL) {
4760509Smrj 		if (win != 0) {
476112837Sfrank.van.der.linden@oracle.com 			ROOTNEX_DPROF_INC(
476212837Sfrank.van.der.linden@oracle.com 			    &rootnex_cnt[ROOTNEX_CNT_GETWIN_FAIL]);
4763509Smrj 			return (DDI_FAILURE);
4764509Smrj 		}
4765509Smrj 		hp->dmai_cookie = dma->dp_cookies;
4766509Smrj 		*offp = 0;
476713050Sfrank.van.der.linden@oracle.com 		*lenp = dmao->dmao_size;
4768509Smrj 		*ccountp = dma->dp_sglinfo.si_sgl_size;
4769509Smrj 		*cookiep = hp->dmai_cookie[0];
4770509Smrj 		hp->dmai_cookie++;
4771509Smrj 		return (DDI_SUCCESS);
4772509Smrj 	}
4773509Smrj 
4774509Smrj 	/* sync the old window before moving on to the new one */
4775509Smrj 	window = &dma->dp_window[dma->dp_current_win];
4776509Smrj 	if ((window->wd_dosync) && (hp->dmai_rflags & DDI_DMA_READ)) {
47778215SVikram.Hegde@Sun.COM 		(void) rootnex_coredma_sync(dip, rdip, handle, 0, 0,
4778509Smrj 		    DDI_DMA_SYNC_FORCPU);
4779509Smrj 	}
4780509Smrj 
4781509Smrj #if !defined(__amd64)
4782509Smrj 	/*
4783509Smrj 	 * before we move to the next window, if we need to re-map, unmap all
4784509Smrj 	 * the pages in this window.
4785509Smrj 	 */
4786509Smrj 	if (dma->dp_cb_remaping) {
4787509Smrj 		/*
4788509Smrj 		 * If we switch to this window again, we'll need to map in
4789509Smrj 		 * on the fly next time.
4790509Smrj 		 */
4791509Smrj 		window->wd_remap_copybuf = B_TRUE;
4792509Smrj 
4793509Smrj 		/*
4794509Smrj 		 * calculate the page index into the buffer where this window
4795509Smrj 		 * starts, and the number of pages this window takes up.
4796509Smrj 		 */
4797509Smrj 		pidx = (sinfo->si_buf_offset + window->wd_offset) >>
4798509Smrj 		    MMU_PAGESHIFT;
4799509Smrj 		poff = (sinfo->si_buf_offset + window->wd_offset) &
4800509Smrj 		    MMU_PAGEOFFSET;
4801509Smrj 		pcnt = mmu_btopr(window->wd_size + poff);
4802509Smrj 		ASSERT((pidx + pcnt) <= sinfo->si_max_pages);
4803509Smrj 
4804509Smrj 		/* unmap pages which are currently mapped in this window */
4805509Smrj 		for (i = 0; i < pcnt; i++) {
4806509Smrj 			if (dma->dp_pgmap[pidx].pm_mapped) {
4807509Smrj 				hat_unload(kas.a_hat,
4808509Smrj 				    dma->dp_pgmap[pidx].pm_kaddr, MMU_PAGESIZE,
4809509Smrj 				    HAT_UNLOAD);
4810509Smrj 				dma->dp_pgmap[pidx].pm_mapped = B_FALSE;
4811509Smrj 			}
4812509Smrj 			pidx++;
4813509Smrj 		}
4814509Smrj 	}
4815509Smrj #endif
4816509Smrj 
4817509Smrj 	/*
4818509Smrj 	 * Move to the new window.
4819509Smrj 	 * NOTE: current_win must be set for sync to work right
4820509Smrj 	 */
4821509Smrj 	dma->dp_current_win = win;
4822509Smrj 	window = &dma->dp_window[win];
4823509Smrj 
4824509Smrj 	/* if needed, adjust the first and/or last cookies for trim */
4825509Smrj 	trim = &window->wd_trim;
4826509Smrj 	if (trim->tr_trim_first) {
48275084Sjohnlev 		window->wd_first_cookie->dmac_laddress = trim->tr_first_paddr;
4828509Smrj 		window->wd_first_cookie->dmac_size = trim->tr_first_size;
4829509Smrj #if !defined(__amd64)
4830509Smrj 		window->wd_first_cookie->dmac_type =
4831509Smrj 		    (window->wd_first_cookie->dmac_type &
4832509Smrj 		    ROOTNEX_USES_COPYBUF) + window->wd_offset;
4833509Smrj #endif
4834509Smrj 		if (trim->tr_first_copybuf_win) {
4835509Smrj 			dma->dp_pgmap[trim->tr_first_pidx].pm_cbaddr =
4836509Smrj 			    trim->tr_first_cbaddr;
4837509Smrj #if !defined(__amd64)
4838509Smrj 			dma->dp_pgmap[trim->tr_first_pidx].pm_kaddr =
4839509Smrj 			    trim->tr_first_kaddr;
4840509Smrj #endif
4841509Smrj 		}
4842509Smrj 	}
4843509Smrj 	if (trim->tr_trim_last) {
48445084Sjohnlev 		trim->tr_last_cookie->dmac_laddress = trim->tr_last_paddr;
4845509Smrj 		trim->tr_last_cookie->dmac_size = trim->tr_last_size;
4846509Smrj 		if (trim->tr_last_copybuf_win) {
4847509Smrj 			dma->dp_pgmap[trim->tr_last_pidx].pm_cbaddr =
4848509Smrj 			    trim->tr_last_cbaddr;
4849509Smrj #if !defined(__amd64)
4850509Smrj 			dma->dp_pgmap[trim->tr_last_pidx].pm_kaddr =
4851509Smrj 			    trim->tr_last_kaddr;
4852509Smrj #endif
4853509Smrj 		}
4854509Smrj 	}
4855509Smrj 
4856509Smrj 	/*
4857509Smrj 	 * setup the cookie pointer to the first cookie in the window. setup
4858509Smrj 	 * our return values, then increment the cookie since we return the
4859509Smrj 	 * first cookie on the stack.
4860509Smrj 	 */
4861509Smrj 	hp->dmai_cookie = window->wd_first_cookie;
4862509Smrj 	*offp = window->wd_offset;
4863509Smrj 	*lenp = window->wd_size;
4864509Smrj 	*ccountp = window->wd_cookie_cnt;
4865509Smrj 	*cookiep = hp->dmai_cookie[0];
4866509Smrj 	hp->dmai_cookie++;
4867509Smrj 
4868509Smrj #if !defined(__amd64)
4869509Smrj 	/* re-map copybuf if required for this window */
4870509Smrj 	if (dma->dp_cb_remaping) {
4871509Smrj 		/*
4872509Smrj 		 * calculate the page index into the buffer where this
4873509Smrj 		 * window starts.
4874509Smrj 		 */
4875509Smrj 		pidx = (sinfo->si_buf_offset + window->wd_offset) >>
4876509Smrj 		    MMU_PAGESHIFT;
4877509Smrj 		ASSERT(pidx < sinfo->si_max_pages);
4878509Smrj 
4879509Smrj 		/*
4880509Smrj 		 * the first page can get unmapped if it's shared with the
4881509Smrj 		 * previous window. Even if the rest of this window is already
4882509Smrj 		 * mapped in, we need to still check this one.
4883509Smrj 		 */
4884509Smrj 		pmap = &dma->dp_pgmap[pidx];
4885509Smrj 		if ((pmap->pm_uses_copybuf) && (pmap->pm_mapped == B_FALSE)) {
4886509Smrj 			if (pmap->pm_pp != NULL) {
4887509Smrj 				pmap->pm_mapped = B_TRUE;
4888509Smrj 				i86_pp_map(pmap->pm_pp, pmap->pm_kaddr);
4889509Smrj 			} else if (pmap->pm_vaddr != NULL) {
4890509Smrj 				pmap->pm_mapped = B_TRUE;
4891509Smrj 				i86_va_map(pmap->pm_vaddr, sinfo->si_asp,
4892509Smrj 				    pmap->pm_kaddr);
4893509Smrj 			}
4894509Smrj 		}
4895509Smrj 		pidx++;
4896509Smrj 
4897509Smrj 		/* map in the rest of the pages if required */
4898509Smrj 		if (window->wd_remap_copybuf) {
4899509Smrj 			window->wd_remap_copybuf = B_FALSE;
4900509Smrj 
4901509Smrj 			/* figure out many pages this window takes up */
4902509Smrj 			poff = (sinfo->si_buf_offset + window->wd_offset) &
4903509Smrj 			    MMU_PAGEOFFSET;
4904509Smrj 			pcnt = mmu_btopr(window->wd_size + poff);
4905509Smrj 			ASSERT(((pidx - 1) + pcnt) <= sinfo->si_max_pages);
4906509Smrj 
4907509Smrj 			/* map pages which require it */
4908509Smrj 			for (i = 1; i < pcnt; i++) {
4909509Smrj 				pmap = &dma->dp_pgmap[pidx];
4910509Smrj 				if (pmap->pm_uses_copybuf) {
4911509Smrj 					ASSERT(pmap->pm_mapped == B_FALSE);
4912509Smrj 					if (pmap->pm_pp != NULL) {
4913509Smrj 						pmap->pm_mapped = B_TRUE;
4914509Smrj 						i86_pp_map(pmap->pm_pp,
4915509Smrj 						    pmap->pm_kaddr);
4916509Smrj 					} else if (pmap->pm_vaddr != NULL) {
4917509Smrj 						pmap->pm_mapped = B_TRUE;
4918509Smrj 						i86_va_map(pmap->pm_vaddr,
4919509Smrj 						    sinfo->si_asp,
4920509Smrj 						    pmap->pm_kaddr);
4921509Smrj 					}
4922509Smrj 				}
4923509Smrj 				pidx++;
4924509Smrj 			}
4925509Smrj 		}
4926509Smrj 	}
4927509Smrj #endif
4928509Smrj 
4929509Smrj 	/* if the new window uses the copy buffer, sync it for the device */
4930509Smrj 	if ((window->wd_dosync) && (hp->dmai_rflags & DDI_DMA_WRITE)) {
49318215SVikram.Hegde@Sun.COM 		(void) rootnex_coredma_sync(dip, rdip, handle, 0, 0,
4932509Smrj 		    DDI_DMA_SYNC_FORDEV);
4933509Smrj 	}
4934509Smrj 
4935509Smrj 	return (DDI_SUCCESS);
4936509Smrj }
4937509Smrj 
49387613SVikram.Hegde@Sun.COM /*
49397613SVikram.Hegde@Sun.COM  * rootnex_dma_win()
49407613SVikram.Hegde@Sun.COM  *    called from ddi_dma_getwin()
49417613SVikram.Hegde@Sun.COM  */
49427613SVikram.Hegde@Sun.COM /*ARGSUSED*/
49437613SVikram.Hegde@Sun.COM static int
rootnex_dma_win(dev_info_t * dip,dev_info_t * rdip,ddi_dma_handle_t handle,uint_t win,off_t * offp,size_t * lenp,ddi_dma_cookie_t * cookiep,uint_t * ccountp)49447613SVikram.Hegde@Sun.COM rootnex_dma_win(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle,
49457613SVikram.Hegde@Sun.COM     uint_t win, off_t *offp, size_t *lenp, ddi_dma_cookie_t *cookiep,
49467613SVikram.Hegde@Sun.COM     uint_t *ccountp)
49477613SVikram.Hegde@Sun.COM {
494811600SVikram.Hegde@Sun.COM #if defined(__amd64) && !defined(__xpv)
494910216SVikram.Hegde@Sun.COM 	if (IOMMU_USED(rdip)) {
49507613SVikram.Hegde@Sun.COM 		return (iommulib_nexdma_win(dip, rdip, handle, win, offp, lenp,
49517613SVikram.Hegde@Sun.COM 		    cookiep, ccountp));
49527613SVikram.Hegde@Sun.COM 	}
49537613SVikram.Hegde@Sun.COM #endif
49547613SVikram.Hegde@Sun.COM 
49557613SVikram.Hegde@Sun.COM 	return (rootnex_coredma_win(dip, rdip, handle, win, offp, lenp,
49567613SVikram.Hegde@Sun.COM 	    cookiep, ccountp));
49577613SVikram.Hegde@Sun.COM }
4958509Smrj 
495913053Sfrank.van.der.linden@oracle.com #if defined(__amd64) && !defined(__xpv)
496013050Sfrank.van.der.linden@oracle.com /*ARGSUSED*/
496113050Sfrank.van.der.linden@oracle.com static int
rootnex_coredma_hdl_setprivate(dev_info_t * dip,dev_info_t * rdip,ddi_dma_handle_t handle,void * v)496213050Sfrank.van.der.linden@oracle.com rootnex_coredma_hdl_setprivate(dev_info_t *dip, dev_info_t *rdip,
496313050Sfrank.van.der.linden@oracle.com     ddi_dma_handle_t handle, void *v)
496413050Sfrank.van.der.linden@oracle.com {
496513050Sfrank.van.der.linden@oracle.com 	ddi_dma_impl_t *hp;
496613050Sfrank.van.der.linden@oracle.com 	rootnex_dma_t *dma;
496713050Sfrank.van.der.linden@oracle.com 
496813050Sfrank.van.der.linden@oracle.com 	hp = (ddi_dma_impl_t *)handle;
496913050Sfrank.van.der.linden@oracle.com 	dma = (rootnex_dma_t *)hp->dmai_private;
497013050Sfrank.van.der.linden@oracle.com 	dma->dp_iommu_private = v;
497113050Sfrank.van.der.linden@oracle.com 
497213050Sfrank.van.der.linden@oracle.com 	return (DDI_SUCCESS);
497313050Sfrank.van.der.linden@oracle.com }
497413050Sfrank.van.der.linden@oracle.com 
497513050Sfrank.van.der.linden@oracle.com /*ARGSUSED*/
497613050Sfrank.van.der.linden@oracle.com static void *
rootnex_coredma_hdl_getprivate(dev_info_t * dip,dev_info_t * rdip,ddi_dma_handle_t handle)497713050Sfrank.van.der.linden@oracle.com rootnex_coredma_hdl_getprivate(dev_info_t *dip, dev_info_t *rdip,
497813050Sfrank.van.der.linden@oracle.com     ddi_dma_handle_t handle)
497913050Sfrank.van.der.linden@oracle.com {
498013050Sfrank.van.der.linden@oracle.com 	ddi_dma_impl_t *hp;
498113050Sfrank.van.der.linden@oracle.com 	rootnex_dma_t *dma;
498213050Sfrank.van.der.linden@oracle.com 
498313050Sfrank.van.der.linden@oracle.com 	hp = (ddi_dma_impl_t *)handle;
498413050Sfrank.van.der.linden@oracle.com 	dma = (rootnex_dma_t *)hp->dmai_private;
498513050Sfrank.van.der.linden@oracle.com 
498613050Sfrank.van.der.linden@oracle.com 	return (dma->dp_iommu_private);
498713050Sfrank.van.der.linden@oracle.com }
498813053Sfrank.van.der.linden@oracle.com #endif
498913050Sfrank.van.der.linden@oracle.com 
4990509Smrj /*
4991509Smrj  * ************************
4992509Smrj  *  obsoleted dma routines
4993509Smrj  * ************************
4994509Smrj  */
4995509Smrj 
499610216SVikram.Hegde@Sun.COM /*
499710216SVikram.Hegde@Sun.COM  * rootnex_dma_map()
499810216SVikram.Hegde@Sun.COM  *    called from ddi_dma_setup()
499910216SVikram.Hegde@Sun.COM  * NO IOMMU in 32 bit mode. The below routines doesn't work in 64 bit mode.
500010216SVikram.Hegde@Sun.COM  */
5001509Smrj /* ARGSUSED */
5002509Smrj static int
rootnex_dma_map(dev_info_t * dip,dev_info_t * rdip,struct ddi_dma_req * dmareq,ddi_dma_handle_t * handlep)500310216SVikram.Hegde@Sun.COM rootnex_dma_map(dev_info_t *dip, dev_info_t *rdip,
50047613SVikram.Hegde@Sun.COM     struct ddi_dma_req *dmareq, ddi_dma_handle_t *handlep)
5005509Smrj {
5006509Smrj #if defined(__amd64)
5007509Smrj 	/*
5008509Smrj 	 * this interface is not supported in 64-bit x86 kernel. See comment in
5009509Smrj 	 * rootnex_dma_mctl()
5010509Smrj 	 */
5011509Smrj 	return (DDI_DMA_NORESOURCES);
5012509Smrj 
5013509Smrj #else /* 32-bit x86 kernel */
5014509Smrj 	ddi_dma_handle_t *lhandlep;
5015509Smrj 	ddi_dma_handle_t lhandle;
5016509Smrj 	ddi_dma_cookie_t cookie;
5017509Smrj 	ddi_dma_attr_t dma_attr;
5018509Smrj 	ddi_dma_lim_t *dma_lim;
5019509Smrj 	uint_t ccnt;
5020509Smrj 	int e;
5021509Smrj 
5022509Smrj 
5023509Smrj 	/*
5024509Smrj 	 * if the driver is just testing to see if it's possible to do the bind,
5025509Smrj 	 * we'll use local state. Otherwise, use the handle pointer passed in.
5026509Smrj 	 */
5027509Smrj 	if (handlep == NULL) {
5028509Smrj 		lhandlep = &lhandle;
5029509Smrj 	} else {
5030509Smrj 		lhandlep = handlep;
5031509Smrj 	}
5032509Smrj 
5033509Smrj 	/* convert the limit structure to a dma_attr one */
5034509Smrj 	dma_lim = dmareq->dmar_limits;
5035509Smrj 	dma_attr.dma_attr_version = DMA_ATTR_V0;
5036509Smrj 	dma_attr.dma_attr_addr_lo = dma_lim->dlim_addr_lo;
5037509Smrj 	dma_attr.dma_attr_addr_hi = dma_lim->dlim_addr_hi;
5038509Smrj 	dma_attr.dma_attr_minxfer = dma_lim->dlim_minxfer;
5039509Smrj 	dma_attr.dma_attr_seg = dma_lim->dlim_adreg_max;
5040509Smrj 	dma_attr.dma_attr_count_max = dma_lim->dlim_ctreg_max;
5041509Smrj 	dma_attr.dma_attr_granular = dma_lim->dlim_granular;
5042509Smrj 	dma_attr.dma_attr_sgllen = dma_lim->dlim_sgllen;
5043509Smrj 	dma_attr.dma_attr_maxxfer = dma_lim->dlim_reqsize;
5044509Smrj 	dma_attr.dma_attr_burstsizes = dma_lim->dlim_burstsizes;
5045509Smrj 	dma_attr.dma_attr_align = MMU_PAGESIZE;
5046509Smrj 	dma_attr.dma_attr_flags = 0;
5047509Smrj 
5048509Smrj 	e = rootnex_dma_allochdl(dip, rdip, &dma_attr, dmareq->dmar_fp,
5049509Smrj 	    dmareq->dmar_arg, lhandlep);
5050509Smrj 	if (e != DDI_SUCCESS) {
5051509Smrj 		return (e);
5052509Smrj 	}
5053509Smrj 
5054509Smrj 	e = rootnex_dma_bindhdl(dip, rdip, *lhandlep, dmareq, &cookie, &ccnt);
5055509Smrj 	if ((e != DDI_DMA_MAPPED) && (e != DDI_DMA_PARTIAL_MAP)) {
5056509Smrj 		(void) rootnex_dma_freehdl(dip, rdip, *lhandlep);
5057509Smrj 		return (e);
5058509Smrj 	}
5059509Smrj 
5060509Smrj 	/*
5061509Smrj 	 * if the driver is just testing to see if it's possible to do the bind,
5062509Smrj 	 * free up the local state and return the result.
5063509Smrj 	 */
5064509Smrj 	if (handlep == NULL) {
5065509Smrj 		(void) rootnex_dma_unbindhdl(dip, rdip, *lhandlep);
5066509Smrj 		(void) rootnex_dma_freehdl(dip, rdip, *lhandlep);
5067509Smrj 		if (e == DDI_DMA_MAPPED) {
5068509Smrj 			return (DDI_DMA_MAPOK);
50690Sstevel@tonic-gate 		} else {
5070509Smrj 			return (DDI_DMA_NOMAPPING);
5071509Smrj 		}
5072509Smrj 	}
5073509Smrj 
5074509Smrj 	return (e);
5075509Smrj #endif /* defined(__amd64) */
5076509Smrj }
5077509Smrj 
50787613SVikram.Hegde@Sun.COM /*
507910216SVikram.Hegde@Sun.COM  * rootnex_dma_mctl()
508010216SVikram.Hegde@Sun.COM  *
508110216SVikram.Hegde@Sun.COM  * No IOMMU in 32 bit mode. The below routine doesn't work in 64 bit mode.
50827613SVikram.Hegde@Sun.COM  */
50837613SVikram.Hegde@Sun.COM /* ARGSUSED */
50847613SVikram.Hegde@Sun.COM static int
rootnex_dma_mctl(dev_info_t * dip,dev_info_t * rdip,ddi_dma_handle_t handle,enum ddi_dma_ctlops request,off_t * offp,size_t * lenp,caddr_t * objpp,uint_t cache_flags)508510216SVikram.Hegde@Sun.COM rootnex_dma_mctl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle,
5086509Smrj     enum ddi_dma_ctlops request, off_t *offp, size_t *lenp, caddr_t *objpp,
5087509Smrj     uint_t cache_flags)
5088509Smrj {
5089509Smrj #if defined(__amd64)
5090509Smrj 	/*
5091509Smrj 	 * DDI_DMA_SMEM_ALLOC & DDI_DMA_IOPB_ALLOC we're changed to have a
5092509Smrj 	 * common implementation in genunix, so they no longer have x86
5093509Smrj 	 * specific functionality which called into dma_ctl.
5094509Smrj 	 *
5095509Smrj 	 * The rest of the obsoleted interfaces were never supported in the
5096509Smrj 	 * 64-bit x86 kernel. For s10, the obsoleted DDI_DMA_SEGTOC interface
5097509Smrj 	 * was not ported to the x86 64-bit kernel do to serious x86 rootnex
5098509Smrj 	 * implementation issues.
5099509Smrj 	 *
5100509Smrj 	 * If you can't use DDI_DMA_SEGTOC; DDI_DMA_NEXTSEG, DDI_DMA_FREE, and
5101509Smrj 	 * DDI_DMA_NEXTWIN are useless since you can get to the cookie, so we
5102509Smrj 	 * reflect that now too...
5103509Smrj 	 *
5104509Smrj 	 * Even though we fixed the pointer problem in DDI_DMA_SEGTOC, we are
5105509Smrj 	 * not going to put this functionality into the 64-bit x86 kernel now.
5106509Smrj 	 * It wasn't ported to the 64-bit kernel for s10, no reason to change
5107509Smrj 	 * that in a future release.
5108509Smrj 	 */
5109509Smrj 	return (DDI_FAILURE);
5110509Smrj 
5111509Smrj #else /* 32-bit x86 kernel */
5112509Smrj 	ddi_dma_cookie_t lcookie;
5113509Smrj 	ddi_dma_cookie_t *cookie;
5114509Smrj 	rootnex_window_t *window;
5115509Smrj 	ddi_dma_impl_t *hp;
5116509Smrj 	rootnex_dma_t *dma;
5117509Smrj 	uint_t nwin;
5118509Smrj 	uint_t ccnt;
5119509Smrj 	size_t len;
5120509Smrj 	off_t off;
5121509Smrj 	int e;
5122509Smrj 
5123509Smrj 
5124509Smrj 	/*
5125509Smrj 	 * DDI_DMA_SEGTOC, DDI_DMA_NEXTSEG, and DDI_DMA_NEXTWIN are a little
5126509Smrj 	 * hacky since were optimizing for the current interfaces and so we can
5127509Smrj 	 * cleanup the mess in genunix. Hopefully we will remove the this
5128509Smrj 	 * obsoleted routines someday soon.
5129509Smrj 	 */
5130509Smrj 
5131509Smrj 	switch (request) {
5132509Smrj 
5133509Smrj 	case DDI_DMA_SEGTOC: /* ddi_dma_segtocookie() */
5134509Smrj 		hp = (ddi_dma_impl_t *)handle;
5135509Smrj 		cookie = (ddi_dma_cookie_t *)objpp;
5136509Smrj 
5137509Smrj 		/*
5138509Smrj 		 * convert segment to cookie. We don't distinguish between the
5139509Smrj 		 * two :-)
5140509Smrj 		 */
5141509Smrj 		*cookie = *hp->dmai_cookie;
5142509Smrj 		*lenp = cookie->dmac_size;
5143509Smrj 		*offp = cookie->dmac_type & ~ROOTNEX_USES_COPYBUF;
5144509Smrj 		return (DDI_SUCCESS);
5145509Smrj 
5146509Smrj 	case DDI_DMA_NEXTSEG: /* ddi_dma_nextseg() */
5147509Smrj 		hp = (ddi_dma_impl_t *)handle;
5148509Smrj 		dma = (rootnex_dma_t *)hp->dmai_private;
5149509Smrj 
5150509Smrj 		if ((*lenp != NULL) && ((uintptr_t)*lenp != (uintptr_t)hp)) {
5151509Smrj 			return (DDI_DMA_STALE);
51520Sstevel@tonic-gate 		}
5153509Smrj 
5154509Smrj 		/* handle the case where we don't have any windows */
5155509Smrj 		if (dma->dp_window == NULL) {
5156509Smrj 			/*
5157509Smrj 			 * if seg == NULL, and we don't have any windows,
5158509Smrj 			 * return the first cookie in the sgl.
5159509Smrj 			 */
5160509Smrj 			if (*lenp == NULL) {
5161509Smrj 				dma->dp_current_cookie = 0;
5162509Smrj 				hp->dmai_cookie = dma->dp_cookies;
5163509Smrj 				*objpp = (caddr_t)handle;
5164509Smrj 				return (DDI_SUCCESS);
5165509Smrj 
5166509Smrj 			/* if we have more cookies, go to the next cookie */
5167509Smrj 			} else {
5168509Smrj 				if ((dma->dp_current_cookie + 1) >=
5169509Smrj 				    dma->dp_sglinfo.si_sgl_size) {
5170509Smrj 					return (DDI_DMA_DONE);
5171509Smrj 				}
5172509Smrj 				dma->dp_current_cookie++;
5173509Smrj 				hp->dmai_cookie++;
5174509Smrj 				return (DDI_SUCCESS);
5175509Smrj 			}
5176509Smrj 		}
5177509Smrj 
5178509Smrj 		/* We have one or more windows */
5179509Smrj 		window = &dma->dp_window[dma->dp_current_win];
5180509Smrj 
5181509Smrj 		/*
5182509Smrj 		 * if seg == NULL, return the first cookie in the current
5183509Smrj 		 * window
5184509Smrj 		 */
5185509Smrj 		if (*lenp == NULL) {
5186509Smrj 			dma->dp_current_cookie = 0;
5187683Smrj 			hp->dmai_cookie = window->wd_first_cookie;
5188509Smrj 
5189509Smrj 		/*
5190509Smrj 		 * go to the next cookie in the window then see if we done with
5191509Smrj 		 * this window.
5192509Smrj 		 */
5193509Smrj 		} else {
5194509Smrj 			if ((dma->dp_current_cookie + 1) >=
5195509Smrj 			    window->wd_cookie_cnt) {
5196509Smrj 				return (DDI_DMA_DONE);
5197509Smrj 			}
5198509Smrj 			dma->dp_current_cookie++;
5199509Smrj 			hp->dmai_cookie++;
5200509Smrj 		}
5201509Smrj 		*objpp = (caddr_t)handle;
5202509Smrj 		return (DDI_SUCCESS);
5203509Smrj 
5204509Smrj 	case DDI_DMA_NEXTWIN: /* ddi_dma_nextwin() */
5205509Smrj 		hp = (ddi_dma_impl_t *)handle;
5206509Smrj 		dma = (rootnex_dma_t *)hp->dmai_private;
5207509Smrj 
5208509Smrj 		if ((*offp != NULL) && ((uintptr_t)*offp != (uintptr_t)hp)) {
5209509Smrj 			return (DDI_DMA_STALE);
5210509Smrj 		}
5211509Smrj 
5212509Smrj 		/* if win == NULL, return the first window in the bind */
5213509Smrj 		if (*offp == NULL) {
5214509Smrj 			nwin = 0;
5215509Smrj 
5216509Smrj 		/*
5217509Smrj 		 * else, go to the next window then see if we're done with all
5218509Smrj 		 * the windows.
5219509Smrj 		 */
5220509Smrj 		} else {
5221509Smrj 			nwin = dma->dp_current_win + 1;
5222509Smrj 			if (nwin >= hp->dmai_nwin) {
5223509Smrj 				return (DDI_DMA_DONE);
5224509Smrj 			}
5225509Smrj 		}
5226509Smrj 
5227509Smrj 		/* switch to the next window */
5228509Smrj 		e = rootnex_dma_win(dip, rdip, handle, nwin, &off, &len,
5229509Smrj 		    &lcookie, &ccnt);
5230509Smrj 		ASSERT(e == DDI_SUCCESS);
5231509Smrj 		if (e != DDI_SUCCESS) {
5232509Smrj 			return (DDI_DMA_STALE);
5233509Smrj 		}
5234509Smrj 
5235509Smrj 		/* reset the cookie back to the first cookie in the window */
5236509Smrj 		if (dma->dp_window != NULL) {
5237509Smrj 			window = &dma->dp_window[dma->dp_current_win];
5238509Smrj 			hp->dmai_cookie = window->wd_first_cookie;
5239509Smrj 		} else {
5240509Smrj 			hp->dmai_cookie = dma->dp_cookies;
5241509Smrj 		}
5242509Smrj 
5243509Smrj 		*objpp = (caddr_t)handle;
5244509Smrj 		return (DDI_SUCCESS);
5245509Smrj 
5246509Smrj 	case DDI_DMA_FREE: /* ddi_dma_free() */
5247509Smrj 		(void) rootnex_dma_unbindhdl(dip, rdip, handle);
5248509Smrj 		(void) rootnex_dma_freehdl(dip, rdip, handle);
5249509Smrj 		if (rootnex_state->r_dvma_call_list_id) {
5250509Smrj 			ddi_run_callback(&rootnex_state->r_dvma_call_list_id);
5251509Smrj 		}
5252509Smrj 		return (DDI_SUCCESS);
5253509Smrj 
5254509Smrj 	case DDI_DMA_IOPB_ALLOC:	/* get contiguous DMA-able memory */
5255509Smrj 	case DDI_DMA_SMEM_ALLOC:	/* get contiguous DMA-able memory */
5256509Smrj 		/* should never get here, handled in genunix */
5257509Smrj 		ASSERT(0);
5258509Smrj 		return (DDI_FAILURE);
5259509Smrj 
5260509Smrj 	case DDI_DMA_KVADDR:
5261509Smrj 	case DDI_DMA_GETERR:
5262509Smrj 	case DDI_DMA_COFF:
5263509Smrj 		return (DDI_FAILURE);
52640Sstevel@tonic-gate 	}
5265509Smrj 
5266509Smrj 	return (DDI_FAILURE);
5267509Smrj #endif /* defined(__amd64) */
52680Sstevel@tonic-gate }
52691414Scindi 
52707613SVikram.Hegde@Sun.COM /*
52711865Sdilpreet  * *********
52721865Sdilpreet  *  FMA Code
52731865Sdilpreet  * *********
52741865Sdilpreet  */
52751865Sdilpreet 
52761865Sdilpreet /*
52771865Sdilpreet  * rootnex_fm_init()
52781865Sdilpreet  *    FMA init busop
52791865Sdilpreet  */
52801865Sdilpreet /* ARGSUSED */
52811865Sdilpreet static int
rootnex_fm_init(dev_info_t * dip,dev_info_t * tdip,int tcap,ddi_iblock_cookie_t * ibc)52821865Sdilpreet rootnex_fm_init(dev_info_t *dip, dev_info_t *tdip, int tcap,
52831865Sdilpreet     ddi_iblock_cookie_t *ibc)
52841865Sdilpreet {
52851865Sdilpreet 	*ibc = rootnex_state->r_err_ibc;
52861865Sdilpreet 
52871865Sdilpreet 	return (ddi_system_fmcap);
52881865Sdilpreet }
52891865Sdilpreet 
52901865Sdilpreet /*
52911865Sdilpreet  * rootnex_dma_check()
52921865Sdilpreet  *    Function called after a dma fault occurred to find out whether the
52931865Sdilpreet  *    fault address is associated with a driver that is able to handle faults
52941865Sdilpreet  *    and recover from faults.
52951865Sdilpreet  */
52961865Sdilpreet /* ARGSUSED */
52971414Scindi static int
rootnex_dma_check(dev_info_t * dip,const void * handle,const void * addr,const void * not_used)52981865Sdilpreet rootnex_dma_check(dev_info_t *dip, const void *handle, const void *addr,
52991865Sdilpreet     const void *not_used)
53001414Scindi {
53011865Sdilpreet 	rootnex_window_t *window;
53021865Sdilpreet 	uint64_t start_addr;
53031865Sdilpreet 	uint64_t fault_addr;
53041865Sdilpreet 	ddi_dma_impl_t *hp;
53051865Sdilpreet 	rootnex_dma_t *dma;
53061865Sdilpreet 	uint64_t end_addr;
53071865Sdilpreet 	size_t csize;
53081865Sdilpreet 	int i;
53091865Sdilpreet 	int j;
53101865Sdilpreet 
53111865Sdilpreet 
53121865Sdilpreet 	/* The driver has to set DDI_DMA_FLAGERR to recover from dma faults */
53131865Sdilpreet 	hp = (ddi_dma_impl_t *)handle;
53141865Sdilpreet 	ASSERT(hp);
53151865Sdilpreet 
53161865Sdilpreet 	dma = (rootnex_dma_t *)hp->dmai_private;
53171865Sdilpreet 
53181865Sdilpreet 	/* Get the address that we need to search for */
53191865Sdilpreet 	fault_addr = *(uint64_t *)addr;
53201865Sdilpreet 
53211865Sdilpreet 	/*
53221865Sdilpreet 	 * if we don't have any windows, we can just walk through all the
53231865Sdilpreet 	 * cookies.
53241865Sdilpreet 	 */
53251865Sdilpreet 	if (dma->dp_window == NULL) {
53261865Sdilpreet 		/* for each cookie */
53271865Sdilpreet 		for (i = 0; i < dma->dp_sglinfo.si_sgl_size; i++) {
53281865Sdilpreet 			/*
53291865Sdilpreet 			 * if the faulted address is within the physical address
53301865Sdilpreet 			 * range of the cookie, return DDI_FM_NONFATAL.
53311865Sdilpreet 			 */
53321865Sdilpreet 			if ((fault_addr >= dma->dp_cookies[i].dmac_laddress) &&
53331865Sdilpreet 			    (fault_addr <= (dma->dp_cookies[i].dmac_laddress +
53341865Sdilpreet 			    dma->dp_cookies[i].dmac_size))) {
53351865Sdilpreet 				return (DDI_FM_NONFATAL);
53361865Sdilpreet 			}
53371865Sdilpreet 		}
53381865Sdilpreet 
53391865Sdilpreet 		/* fault_addr not within this DMA handle */
53401865Sdilpreet 		return (DDI_FM_UNKNOWN);
53411865Sdilpreet 	}
53421865Sdilpreet 
53431865Sdilpreet 	/* we have mutiple windows, walk through each window */
53441865Sdilpreet 	for (i = 0; i < hp->dmai_nwin; i++) {
53451865Sdilpreet 		window = &dma->dp_window[i];
53461865Sdilpreet 
53471865Sdilpreet 		/* Go through all the cookies in the window */
53481865Sdilpreet 		for (j = 0; j < window->wd_cookie_cnt; j++) {
53491865Sdilpreet 
53501865Sdilpreet 			start_addr = window->wd_first_cookie[j].dmac_laddress;
53511865Sdilpreet 			csize = window->wd_first_cookie[j].dmac_size;
53521865Sdilpreet 
53531865Sdilpreet 			/*
53541865Sdilpreet 			 * if we are trimming the first cookie in the window,
53551865Sdilpreet 			 * and this is the first cookie, adjust the start
53561865Sdilpreet 			 * address and size of the cookie to account for the
53571865Sdilpreet 			 * trim.
53581865Sdilpreet 			 */
53591865Sdilpreet 			if (window->wd_trim.tr_trim_first && (j == 0)) {
53601865Sdilpreet 				start_addr = window->wd_trim.tr_first_paddr;
53611865Sdilpreet 				csize = window->wd_trim.tr_first_size;
53621865Sdilpreet 			}
53631865Sdilpreet 
53641865Sdilpreet 			/*
53651865Sdilpreet 			 * if we are trimming the last cookie in the window,
53661865Sdilpreet 			 * and this is the last cookie, adjust the start
53671865Sdilpreet 			 * address and size of the cookie to account for the
53681865Sdilpreet 			 * trim.
53691865Sdilpreet 			 */
53701865Sdilpreet 			if (window->wd_trim.tr_trim_last &&
53711865Sdilpreet 			    (j == (window->wd_cookie_cnt - 1))) {
53721865Sdilpreet 				start_addr = window->wd_trim.tr_last_paddr;
53731865Sdilpreet 				csize = window->wd_trim.tr_last_size;
53741865Sdilpreet 			}
53751865Sdilpreet 
53761865Sdilpreet 			end_addr = start_addr + csize;
53771865Sdilpreet 
53781865Sdilpreet 			/*
537911600SVikram.Hegde@Sun.COM 			 * if the faulted address is within the physical
538011600SVikram.Hegde@Sun.COM 			 * address of the cookie, return DDI_FM_NONFATAL.
53811865Sdilpreet 			 */
53821865Sdilpreet 			if ((fault_addr >= start_addr) &&
53831865Sdilpreet 			    (fault_addr <= end_addr)) {
53841865Sdilpreet 				return (DDI_FM_NONFATAL);
53851865Sdilpreet 			}
53861865Sdilpreet 		}
53871865Sdilpreet 	}
53881865Sdilpreet 
53891865Sdilpreet 	/* fault_addr not within this DMA handle */
53901865Sdilpreet 	return (DDI_FM_UNKNOWN);
53911414Scindi }
539211600SVikram.Hegde@Sun.COM 
539311600SVikram.Hegde@Sun.COM /*ARGSUSED*/
539411600SVikram.Hegde@Sun.COM static int
rootnex_quiesce(dev_info_t * dip)539511600SVikram.Hegde@Sun.COM rootnex_quiesce(dev_info_t *dip)
539611600SVikram.Hegde@Sun.COM {
539711600SVikram.Hegde@Sun.COM #if defined(__amd64) && !defined(__xpv)
539811600SVikram.Hegde@Sun.COM 	return (immu_quiesce());
539911600SVikram.Hegde@Sun.COM #else
540011600SVikram.Hegde@Sun.COM 	return (DDI_SUCCESS);
540111600SVikram.Hegde@Sun.COM #endif
540211600SVikram.Hegde@Sun.COM }
540311600SVikram.Hegde@Sun.COM 
540411600SVikram.Hegde@Sun.COM #if defined(__xpv)
540511600SVikram.Hegde@Sun.COM void
immu_init(void)540611600SVikram.Hegde@Sun.COM immu_init(void)
540711600SVikram.Hegde@Sun.COM {
540811600SVikram.Hegde@Sun.COM 	;
540911600SVikram.Hegde@Sun.COM }
541011600SVikram.Hegde@Sun.COM 
541111600SVikram.Hegde@Sun.COM void
immu_startup(void)541211600SVikram.Hegde@Sun.COM immu_startup(void)
541311600SVikram.Hegde@Sun.COM {
541411600SVikram.Hegde@Sun.COM 	;
541511600SVikram.Hegde@Sun.COM }
541611600SVikram.Hegde@Sun.COM /*ARGSUSED*/
541711600SVikram.Hegde@Sun.COM void
immu_physmem_update(uint64_t addr,uint64_t size)541811600SVikram.Hegde@Sun.COM immu_physmem_update(uint64_t addr, uint64_t size)
541911600SVikram.Hegde@Sun.COM {
542011600SVikram.Hegde@Sun.COM 	;
542111600SVikram.Hegde@Sun.COM }
542211600SVikram.Hegde@Sun.COM #endif
5423