xref: /titanic_53/usr/src/uts/i86pc/io/rootnex.c (revision ef4ab52f39b58254bab6dfdaa5df32aa10af2e6c)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
500d0963fSdilpreet  * Common Development and Distribution License (the "License").
600d0963fSdilpreet  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22b57cd2d3SMark Johnson  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
2612f080e7Smrj  * x86 root nexus driver
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
307c478bd9Sstevel@tonic-gate #include <sys/conf.h>
317c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
327c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
337c478bd9Sstevel@tonic-gate #include <sys/debug.h>
347c478bd9Sstevel@tonic-gate #include <sys/psw.h>
357c478bd9Sstevel@tonic-gate #include <sys/ddidmareq.h>
367c478bd9Sstevel@tonic-gate #include <sys/promif.h>
377c478bd9Sstevel@tonic-gate #include <sys/devops.h>
387c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
397c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
407c478bd9Sstevel@tonic-gate #include <vm/seg.h>
417c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
427c478bd9Sstevel@tonic-gate #include <vm/seg_dev.h>
437c478bd9Sstevel@tonic-gate #include <sys/vmem.h>
447c478bd9Sstevel@tonic-gate #include <sys/mman.h>
457c478bd9Sstevel@tonic-gate #include <vm/hat.h>
467c478bd9Sstevel@tonic-gate #include <vm/as.h>
477c478bd9Sstevel@tonic-gate #include <vm/page.h>
487c478bd9Sstevel@tonic-gate #include <sys/avintr.h>
497c478bd9Sstevel@tonic-gate #include <sys/errno.h>
507c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
517c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
527c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
537c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
547a364d25Sschwartz #include <sys/mach_intr.h>
557c478bd9Sstevel@tonic-gate #include <sys/psm.h>
567c478bd9Sstevel@tonic-gate #include <sys/ontrap.h>
5712f080e7Smrj #include <sys/atomic.h>
5812f080e7Smrj #include <sys/sdt.h>
5912f080e7Smrj #include <sys/rootnex.h>
6012f080e7Smrj #include <vm/hat_i86.h>
6100d0963fSdilpreet #include <sys/ddifm.h>
6236945f79Smrj #include <sys/ddi_isa.h>
637ff178cdSJimmy Vetayases #include <sys/apic.h>
647c478bd9Sstevel@tonic-gate 
65843e1988Sjohnlev #ifdef __xpv
66843e1988Sjohnlev #include <sys/bootinfo.h>
67843e1988Sjohnlev #include <sys/hypervisor.h>
68843e1988Sjohnlev #include <sys/bootconf.h>
69843e1988Sjohnlev #include <vm/kboot_mmu.h>
703a634bfcSVikram Hegde #endif
713a634bfcSVikram Hegde 
723a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
733a634bfcSVikram Hegde #include <sys/immu.h>
74843e1988Sjohnlev #endif
75843e1988Sjohnlev 
7686c1f4dcSVikram Hegde 
7712f080e7Smrj /*
7812f080e7Smrj  * enable/disable extra checking of function parameters. Useful for debugging
7912f080e7Smrj  * drivers.
8012f080e7Smrj  */
8112f080e7Smrj #ifdef	DEBUG
8212f080e7Smrj int rootnex_alloc_check_parms = 1;
8312f080e7Smrj int rootnex_bind_check_parms = 1;
8412f080e7Smrj int rootnex_bind_check_inuse = 1;
8512f080e7Smrj int rootnex_unbind_verify_buffer = 0;
8612f080e7Smrj int rootnex_sync_check_parms = 1;
8712f080e7Smrj #else
8812f080e7Smrj int rootnex_alloc_check_parms = 0;
8912f080e7Smrj int rootnex_bind_check_parms = 0;
9012f080e7Smrj int rootnex_bind_check_inuse = 0;
9112f080e7Smrj int rootnex_unbind_verify_buffer = 0;
9212f080e7Smrj int rootnex_sync_check_parms = 0;
9312f080e7Smrj #endif
947c478bd9Sstevel@tonic-gate 
953a634bfcSVikram Hegde boolean_t rootnex_dmar_not_setup;
963a634bfcSVikram Hegde 
977aec1d6eScindi /* Master Abort and Target Abort panic flag */
987aec1d6eScindi int rootnex_fm_ma_ta_panic_flag = 0;
997aec1d6eScindi 
10012f080e7Smrj /* Semi-temporary patchables to phase in bug fixes, test drivers, etc. */
1017c478bd9Sstevel@tonic-gate int rootnex_bind_fail = 1;
1027c478bd9Sstevel@tonic-gate int rootnex_bind_warn = 1;
1037c478bd9Sstevel@tonic-gate uint8_t *rootnex_warn_list;
1047c478bd9Sstevel@tonic-gate /* bitmasks for rootnex_warn_list. Up to 8 different warnings with uint8_t */
1057c478bd9Sstevel@tonic-gate #define	ROOTNEX_BIND_WARNING	(0x1 << 0)
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /*
10812f080e7Smrj  * revert back to old broken behavior of always sync'ing entire copy buffer.
10912f080e7Smrj  * This is useful if be have a buggy driver which doesn't correctly pass in
11012f080e7Smrj  * the offset and size into ddi_dma_sync().
1117c478bd9Sstevel@tonic-gate  */
11212f080e7Smrj int rootnex_sync_ignore_params = 0;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /*
11512f080e7Smrj  * For the 64-bit kernel, pre-alloc enough cookies for a 256K buffer plus 1
11612f080e7Smrj  * page for alignment. For the 32-bit kernel, pre-alloc enough cookies for a
11712f080e7Smrj  * 64K buffer plus 1 page for alignment (we have less kernel space in a 32-bit
11812f080e7Smrj  * kernel). Allocate enough windows to handle a 256K buffer w/ at least 65
11912f080e7Smrj  * sgllen DMA engine, and enough copybuf buffer state pages to handle 2 pages
12012f080e7Smrj  * (< 8K). We will still need to allocate the copy buffer during bind though
12112f080e7Smrj  * (if we need one). These can only be modified in /etc/system before rootnex
12212f080e7Smrj  * attach.
1237c478bd9Sstevel@tonic-gate  */
12412f080e7Smrj #if defined(__amd64)
12512f080e7Smrj int rootnex_prealloc_cookies = 65;
12612f080e7Smrj int rootnex_prealloc_windows = 4;
12712f080e7Smrj int rootnex_prealloc_copybuf = 2;
12812f080e7Smrj #else
12912f080e7Smrj int rootnex_prealloc_cookies = 33;
13012f080e7Smrj int rootnex_prealloc_windows = 4;
13112f080e7Smrj int rootnex_prealloc_copybuf = 2;
13212f080e7Smrj #endif
1337c478bd9Sstevel@tonic-gate 
13412f080e7Smrj /* driver global state */
13512f080e7Smrj static rootnex_state_t *rootnex_state;
13612f080e7Smrj 
137*ef4ab52fSFrank Van Der Linden #ifdef DEBUG
13812f080e7Smrj /* shortcut to rootnex counters */
13912f080e7Smrj static uint64_t *rootnex_cnt;
140*ef4ab52fSFrank Van Der Linden #endif
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate /*
14312f080e7Smrj  * XXX - does x86 even need these or are they left over from the SPARC days?
1447c478bd9Sstevel@tonic-gate  */
14512f080e7Smrj /* statically defined integer/boolean properties for the root node */
14612f080e7Smrj static rootnex_intprop_t rootnex_intprp[] = {
14712f080e7Smrj 	{ "PAGESIZE",			PAGESIZE },
14812f080e7Smrj 	{ "MMU_PAGESIZE",		MMU_PAGESIZE },
14912f080e7Smrj 	{ "MMU_PAGEOFFSET",		MMU_PAGEOFFSET },
15012f080e7Smrj 	{ DDI_RELATIVE_ADDRESSING,	1 },
15112f080e7Smrj };
15212f080e7Smrj #define	NROOT_INTPROPS	(sizeof (rootnex_intprp) / sizeof (rootnex_intprop_t))
1537c478bd9Sstevel@tonic-gate 
154843e1988Sjohnlev #ifdef __xpv
155843e1988Sjohnlev typedef maddr_t rootnex_addr_t;
156843e1988Sjohnlev #define	ROOTNEX_PADDR_TO_RBASE(xinfo, pa)	\
157843e1988Sjohnlev 	(DOMAIN_IS_INITDOMAIN(xinfo) ? pa_to_ma(pa) : (pa))
158843e1988Sjohnlev #else
159843e1988Sjohnlev typedef paddr_t rootnex_addr_t;
160843e1988Sjohnlev #endif
161843e1988Sjohnlev 
16220906b23SVikram Hegde #if !defined(__xpv)
1637ff178cdSJimmy Vetayases char _depends_on[] = "misc/iommulib misc/acpica";
16420906b23SVikram Hegde #endif
1657c478bd9Sstevel@tonic-gate 
16612f080e7Smrj static struct cb_ops rootnex_cb_ops = {
16712f080e7Smrj 	nodev,		/* open */
16812f080e7Smrj 	nodev,		/* close */
16912f080e7Smrj 	nodev,		/* strategy */
17012f080e7Smrj 	nodev,		/* print */
17112f080e7Smrj 	nodev,		/* dump */
17212f080e7Smrj 	nodev,		/* read */
17312f080e7Smrj 	nodev,		/* write */
17412f080e7Smrj 	nodev,		/* ioctl */
17512f080e7Smrj 	nodev,		/* devmap */
17612f080e7Smrj 	nodev,		/* mmap */
17712f080e7Smrj 	nodev,		/* segmap */
17812f080e7Smrj 	nochpoll,	/* chpoll */
17912f080e7Smrj 	ddi_prop_op,	/* cb_prop_op */
18012f080e7Smrj 	NULL,		/* struct streamtab */
18112f080e7Smrj 	D_NEW | D_MP | D_HOTPLUG, /* compatibility flags */
18212f080e7Smrj 	CB_REV,		/* Rev */
18312f080e7Smrj 	nodev,		/* cb_aread */
18412f080e7Smrj 	nodev		/* cb_awrite */
18512f080e7Smrj };
1867c478bd9Sstevel@tonic-gate 
18712f080e7Smrj static int rootnex_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
1887c478bd9Sstevel@tonic-gate     off_t offset, off_t len, caddr_t *vaddrp);
18912f080e7Smrj static int rootnex_map_fault(dev_info_t *dip, dev_info_t *rdip,
1907c478bd9Sstevel@tonic-gate     struct hat *hat, struct seg *seg, caddr_t addr,
1917c478bd9Sstevel@tonic-gate     struct devpage *dp, pfn_t pfn, uint_t prot, uint_t lock);
19212f080e7Smrj static int rootnex_dma_map(dev_info_t *dip, dev_info_t *rdip,
1937c478bd9Sstevel@tonic-gate     struct ddi_dma_req *dmareq, ddi_dma_handle_t *handlep);
19412f080e7Smrj static int rootnex_dma_allochdl(dev_info_t *dip, dev_info_t *rdip,
19512f080e7Smrj     ddi_dma_attr_t *attr, int (*waitfp)(caddr_t), caddr_t arg,
19612f080e7Smrj     ddi_dma_handle_t *handlep);
19712f080e7Smrj static int rootnex_dma_freehdl(dev_info_t *dip, dev_info_t *rdip,
19812f080e7Smrj     ddi_dma_handle_t handle);
19912f080e7Smrj static int rootnex_dma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
20012f080e7Smrj     ddi_dma_handle_t handle, struct ddi_dma_req *dmareq,
20112f080e7Smrj     ddi_dma_cookie_t *cookiep, uint_t *ccountp);
20212f080e7Smrj static int rootnex_dma_unbindhdl(dev_info_t *dip, dev_info_t *rdip,
20312f080e7Smrj     ddi_dma_handle_t handle);
20412f080e7Smrj static int rootnex_dma_sync(dev_info_t *dip, dev_info_t *rdip,
20512f080e7Smrj     ddi_dma_handle_t handle, off_t off, size_t len, uint_t cache_flags);
20612f080e7Smrj static int rootnex_dma_win(dev_info_t *dip, dev_info_t *rdip,
20712f080e7Smrj     ddi_dma_handle_t handle, uint_t win, off_t *offp, size_t *lenp,
20812f080e7Smrj     ddi_dma_cookie_t *cookiep, uint_t *ccountp);
20912f080e7Smrj static int rootnex_dma_mctl(dev_info_t *dip, dev_info_t *rdip,
2107c478bd9Sstevel@tonic-gate     ddi_dma_handle_t handle, enum ddi_dma_ctlops request,
2117c478bd9Sstevel@tonic-gate     off_t *offp, size_t *lenp, caddr_t *objp, uint_t cache_flags);
21212f080e7Smrj static int rootnex_ctlops(dev_info_t *dip, dev_info_t *rdip,
21312f080e7Smrj     ddi_ctl_enum_t ctlop, void *arg, void *result);
21400d0963fSdilpreet static int rootnex_fm_init(dev_info_t *dip, dev_info_t *tdip, int tcap,
21500d0963fSdilpreet     ddi_iblock_cookie_t *ibc);
21612f080e7Smrj static int rootnex_intr_ops(dev_info_t *pdip, dev_info_t *rdip,
21712f080e7Smrj     ddi_intr_op_t intr_op, ddi_intr_handle_impl_t *hdlp, void *result);
2187ff178cdSJimmy Vetayases static int rootnex_alloc_intr_fixed(dev_info_t *, ddi_intr_handle_impl_t *,
2197ff178cdSJimmy Vetayases     void *);
2207ff178cdSJimmy Vetayases static int rootnex_free_intr_fixed(dev_info_t *, ddi_intr_handle_impl_t *);
2217c478bd9Sstevel@tonic-gate 
22220906b23SVikram Hegde static int rootnex_coredma_allochdl(dev_info_t *dip, dev_info_t *rdip,
22320906b23SVikram Hegde     ddi_dma_attr_t *attr, int (*waitfp)(caddr_t), caddr_t arg,
22420906b23SVikram Hegde     ddi_dma_handle_t *handlep);
22520906b23SVikram Hegde static int rootnex_coredma_freehdl(dev_info_t *dip, dev_info_t *rdip,
22620906b23SVikram Hegde     ddi_dma_handle_t handle);
22720906b23SVikram Hegde static int rootnex_coredma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
22820906b23SVikram Hegde     ddi_dma_handle_t handle, struct ddi_dma_req *dmareq,
22920906b23SVikram Hegde     ddi_dma_cookie_t *cookiep, uint_t *ccountp);
23020906b23SVikram Hegde static int rootnex_coredma_unbindhdl(dev_info_t *dip, dev_info_t *rdip,
23120906b23SVikram Hegde     ddi_dma_handle_t handle);
2323a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
23320906b23SVikram Hegde static void rootnex_coredma_reset_cookies(dev_info_t *dip,
23420906b23SVikram Hegde     ddi_dma_handle_t handle);
23520906b23SVikram Hegde static int rootnex_coredma_get_cookies(dev_info_t *dip, ddi_dma_handle_t handle,
23694f1124eSVikram Hegde     ddi_dma_cookie_t **cookiepp, uint_t *ccountp);
23794f1124eSVikram Hegde static int rootnex_coredma_set_cookies(dev_info_t *dip, ddi_dma_handle_t handle,
23894f1124eSVikram Hegde     ddi_dma_cookie_t *cookiep, uint_t ccount);
23994f1124eSVikram Hegde static int rootnex_coredma_clear_cookies(dev_info_t *dip,
24094f1124eSVikram Hegde     ddi_dma_handle_t handle);
24194f1124eSVikram Hegde static int rootnex_coredma_get_sleep_flags(ddi_dma_handle_t handle);
2425dfdb46bSVikram Hegde #endif
24320906b23SVikram Hegde static int rootnex_coredma_sync(dev_info_t *dip, dev_info_t *rdip,
24420906b23SVikram Hegde     ddi_dma_handle_t handle, off_t off, size_t len, uint_t cache_flags);
24520906b23SVikram Hegde static int rootnex_coredma_win(dev_info_t *dip, dev_info_t *rdip,
24620906b23SVikram Hegde     ddi_dma_handle_t handle, uint_t win, off_t *offp, size_t *lenp,
24720906b23SVikram Hegde     ddi_dma_cookie_t *cookiep, uint_t *ccountp);
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate static struct bus_ops rootnex_bus_ops = {
2507c478bd9Sstevel@tonic-gate 	BUSO_REV,
2517c478bd9Sstevel@tonic-gate 	rootnex_map,
2527c478bd9Sstevel@tonic-gate 	NULL,
2537c478bd9Sstevel@tonic-gate 	NULL,
2547c478bd9Sstevel@tonic-gate 	NULL,
2557c478bd9Sstevel@tonic-gate 	rootnex_map_fault,
2567c478bd9Sstevel@tonic-gate 	rootnex_dma_map,
2577c478bd9Sstevel@tonic-gate 	rootnex_dma_allochdl,
2587c478bd9Sstevel@tonic-gate 	rootnex_dma_freehdl,
2597c478bd9Sstevel@tonic-gate 	rootnex_dma_bindhdl,
2607c478bd9Sstevel@tonic-gate 	rootnex_dma_unbindhdl,
26112f080e7Smrj 	rootnex_dma_sync,
2627c478bd9Sstevel@tonic-gate 	rootnex_dma_win,
2637c478bd9Sstevel@tonic-gate 	rootnex_dma_mctl,
2647c478bd9Sstevel@tonic-gate 	rootnex_ctlops,
2657c478bd9Sstevel@tonic-gate 	ddi_bus_prop_op,
2667c478bd9Sstevel@tonic-gate 	i_ddi_rootnex_get_eventcookie,
2677c478bd9Sstevel@tonic-gate 	i_ddi_rootnex_add_eventcall,
2687c478bd9Sstevel@tonic-gate 	i_ddi_rootnex_remove_eventcall,
2697c478bd9Sstevel@tonic-gate 	i_ddi_rootnex_post_event,
2707c478bd9Sstevel@tonic-gate 	0,			/* bus_intr_ctl */
2717c478bd9Sstevel@tonic-gate 	0,			/* bus_config */
2727c478bd9Sstevel@tonic-gate 	0,			/* bus_unconfig */
27300d0963fSdilpreet 	rootnex_fm_init,	/* bus_fm_init */
2747c478bd9Sstevel@tonic-gate 	NULL,			/* bus_fm_fini */
2757c478bd9Sstevel@tonic-gate 	NULL,			/* bus_fm_access_enter */
2767c478bd9Sstevel@tonic-gate 	NULL,			/* bus_fm_access_exit */
2777c478bd9Sstevel@tonic-gate 	NULL,			/* bus_powr */
2787c478bd9Sstevel@tonic-gate 	rootnex_intr_ops	/* bus_intr_op */
2797c478bd9Sstevel@tonic-gate };
2807c478bd9Sstevel@tonic-gate 
28112f080e7Smrj static int rootnex_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
28212f080e7Smrj static int rootnex_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
2833a634bfcSVikram Hegde static int rootnex_quiesce(dev_info_t *dip);
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate static struct dev_ops rootnex_ops = {
2867c478bd9Sstevel@tonic-gate 	DEVO_REV,
28712f080e7Smrj 	0,
28812f080e7Smrj 	ddi_no_info,
2897c478bd9Sstevel@tonic-gate 	nulldev,
29012f080e7Smrj 	nulldev,
2917c478bd9Sstevel@tonic-gate 	rootnex_attach,
29212f080e7Smrj 	rootnex_detach,
29312f080e7Smrj 	nulldev,
29412f080e7Smrj 	&rootnex_cb_ops,
29519397407SSherry Moore 	&rootnex_bus_ops,
29619397407SSherry Moore 	NULL,
2973a634bfcSVikram Hegde 	rootnex_quiesce,		/* quiesce */
2987c478bd9Sstevel@tonic-gate };
2997c478bd9Sstevel@tonic-gate 
30012f080e7Smrj static struct modldrv rootnex_modldrv = {
30112f080e7Smrj 	&mod_driverops,
302613b2871SRichard Bean 	"i86pc root nexus",
30312f080e7Smrj 	&rootnex_ops
3047c478bd9Sstevel@tonic-gate };
3057c478bd9Sstevel@tonic-gate 
30612f080e7Smrj static struct modlinkage rootnex_modlinkage = {
30712f080e7Smrj 	MODREV_1,
30812f080e7Smrj 	(void *)&rootnex_modldrv,
30912f080e7Smrj 	NULL
3107c478bd9Sstevel@tonic-gate };
3117c478bd9Sstevel@tonic-gate 
3123a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
31320906b23SVikram Hegde static iommulib_nexops_t iommulib_nexops = {
31420906b23SVikram Hegde 	IOMMU_NEXOPS_VERSION,
31520906b23SVikram Hegde 	"Rootnex IOMMU ops Vers 1.1",
31620906b23SVikram Hegde 	NULL,
31720906b23SVikram Hegde 	rootnex_coredma_allochdl,
31820906b23SVikram Hegde 	rootnex_coredma_freehdl,
31920906b23SVikram Hegde 	rootnex_coredma_bindhdl,
32020906b23SVikram Hegde 	rootnex_coredma_unbindhdl,
32120906b23SVikram Hegde 	rootnex_coredma_reset_cookies,
32220906b23SVikram Hegde 	rootnex_coredma_get_cookies,
32394f1124eSVikram Hegde 	rootnex_coredma_set_cookies,
32494f1124eSVikram Hegde 	rootnex_coredma_clear_cookies,
32594f1124eSVikram Hegde 	rootnex_coredma_get_sleep_flags,
32620906b23SVikram Hegde 	rootnex_coredma_sync,
32720906b23SVikram Hegde 	rootnex_coredma_win,
328b51bbbf5SVikram Hegde 	rootnex_dma_map,
329b51bbbf5SVikram Hegde 	rootnex_dma_mctl
33020906b23SVikram Hegde };
3315dfdb46bSVikram Hegde #endif
3327c478bd9Sstevel@tonic-gate 
33312f080e7Smrj /*
33412f080e7Smrj  *  extern hacks
33512f080e7Smrj  */
33612f080e7Smrj extern struct seg_ops segdev_ops;
33712f080e7Smrj extern int ignore_hardware_nodes;	/* force flag from ddi_impl.c */
33812f080e7Smrj #ifdef	DDI_MAP_DEBUG
33912f080e7Smrj extern int ddi_map_debug_flag;
34012f080e7Smrj #define	ddi_map_debug	if (ddi_map_debug_flag) prom_printf
34112f080e7Smrj #endif
34212f080e7Smrj extern void i86_pp_map(page_t *pp, caddr_t kaddr);
34312f080e7Smrj extern void i86_va_map(caddr_t vaddr, struct as *asp, caddr_t kaddr);
34412f080e7Smrj extern int (*psm_intr_ops)(dev_info_t *, ddi_intr_handle_impl_t *,
34512f080e7Smrj     psm_intr_op_t, int *);
34612f080e7Smrj extern int impl_ddi_sunbus_initchild(dev_info_t *dip);
34712f080e7Smrj extern void impl_ddi_sunbus_removechild(dev_info_t *dip);
34836945f79Smrj 
34912f080e7Smrj /*
35012f080e7Smrj  * Use device arena to use for device control register mappings.
35112f080e7Smrj  * Various kernel memory walkers (debugger, dtrace) need to know
35212f080e7Smrj  * to avoid this address range to prevent undesired device activity.
35312f080e7Smrj  */
35412f080e7Smrj extern void *device_arena_alloc(size_t size, int vm_flag);
35512f080e7Smrj extern void device_arena_free(void * vaddr, size_t size);
35612f080e7Smrj 
35712f080e7Smrj 
35812f080e7Smrj /*
35912f080e7Smrj  *  Internal functions
36012f080e7Smrj  */
36112f080e7Smrj static int rootnex_dma_init();
36212f080e7Smrj static void rootnex_add_props(dev_info_t *);
36312f080e7Smrj static int rootnex_ctl_reportdev(dev_info_t *dip);
36412f080e7Smrj static struct intrspec *rootnex_get_ispec(dev_info_t *rdip, int inum);
36512f080e7Smrj static int rootnex_map_regspec(ddi_map_req_t *mp, caddr_t *vaddrp);
36612f080e7Smrj static int rootnex_unmap_regspec(ddi_map_req_t *mp, caddr_t *vaddrp);
36712f080e7Smrj static int rootnex_map_handle(ddi_map_req_t *mp);
36812f080e7Smrj static void rootnex_clean_dmahdl(ddi_dma_impl_t *hp);
36912f080e7Smrj static int rootnex_valid_alloc_parms(ddi_dma_attr_t *attr, uint_t maxsegsize);
37012f080e7Smrj static int rootnex_valid_bind_parms(ddi_dma_req_t *dmareq,
37112f080e7Smrj     ddi_dma_attr_t *attr);
37212f080e7Smrj static void rootnex_get_sgl(ddi_dma_obj_t *dmar_object, ddi_dma_cookie_t *sgl,
37312f080e7Smrj     rootnex_sglinfo_t *sglinfo);
37412f080e7Smrj static int rootnex_bind_slowpath(ddi_dma_impl_t *hp, struct ddi_dma_req *dmareq,
37512f080e7Smrj     rootnex_dma_t *dma, ddi_dma_attr_t *attr, int kmflag);
37612f080e7Smrj static int rootnex_setup_copybuf(ddi_dma_impl_t *hp, struct ddi_dma_req *dmareq,
37712f080e7Smrj     rootnex_dma_t *dma, ddi_dma_attr_t *attr);
37812f080e7Smrj static void rootnex_teardown_copybuf(rootnex_dma_t *dma);
37912f080e7Smrj static int rootnex_setup_windows(ddi_dma_impl_t *hp, rootnex_dma_t *dma,
38012f080e7Smrj     ddi_dma_attr_t *attr, int kmflag);
38112f080e7Smrj static void rootnex_teardown_windows(rootnex_dma_t *dma);
38212f080e7Smrj static void rootnex_init_win(ddi_dma_impl_t *hp, rootnex_dma_t *dma,
38312f080e7Smrj     rootnex_window_t *window, ddi_dma_cookie_t *cookie, off_t cur_offset);
38412f080e7Smrj static void rootnex_setup_cookie(ddi_dma_obj_t *dmar_object,
38512f080e7Smrj     rootnex_dma_t *dma, ddi_dma_cookie_t *cookie, off_t cur_offset,
38612f080e7Smrj     size_t *copybuf_used, page_t **cur_pp);
38712f080e7Smrj static int rootnex_sgllen_window_boundary(ddi_dma_impl_t *hp,
38812f080e7Smrj     rootnex_dma_t *dma, rootnex_window_t **windowp, ddi_dma_cookie_t *cookie,
38912f080e7Smrj     ddi_dma_attr_t *attr, off_t cur_offset);
39012f080e7Smrj static int rootnex_copybuf_window_boundary(ddi_dma_impl_t *hp,
39112f080e7Smrj     rootnex_dma_t *dma, rootnex_window_t **windowp,
39212f080e7Smrj     ddi_dma_cookie_t *cookie, off_t cur_offset, size_t *copybuf_used);
39312f080e7Smrj static int rootnex_maxxfer_window_boundary(ddi_dma_impl_t *hp,
39412f080e7Smrj     rootnex_dma_t *dma, rootnex_window_t **windowp, ddi_dma_cookie_t *cookie);
39512f080e7Smrj static int rootnex_valid_sync_parms(ddi_dma_impl_t *hp, rootnex_window_t *win,
39612f080e7Smrj     off_t offset, size_t size, uint_t cache_flags);
39712f080e7Smrj static int rootnex_verify_buffer(rootnex_dma_t *dma);
39800d0963fSdilpreet static int rootnex_dma_check(dev_info_t *dip, const void *handle,
39900d0963fSdilpreet     const void *comp_addr, const void *not_used);
40007c6692fSMark Johnson static boolean_t rootnex_need_bounce_seg(ddi_dma_obj_t *dmar_object,
40107c6692fSMark Johnson     rootnex_sglinfo_t *sglinfo);
40212f080e7Smrj 
40312f080e7Smrj /*
40412f080e7Smrj  * _init()
40512f080e7Smrj  *
40612f080e7Smrj  */
4077c478bd9Sstevel@tonic-gate int
4087c478bd9Sstevel@tonic-gate _init(void)
4097c478bd9Sstevel@tonic-gate {
41012f080e7Smrj 
41112f080e7Smrj 	rootnex_state = NULL;
41212f080e7Smrj 	return (mod_install(&rootnex_modlinkage));
4137c478bd9Sstevel@tonic-gate }
4147c478bd9Sstevel@tonic-gate 
41512f080e7Smrj 
41612f080e7Smrj /*
41712f080e7Smrj  * _info()
41812f080e7Smrj  *
41912f080e7Smrj  */
42012f080e7Smrj int
42112f080e7Smrj _info(struct modinfo *modinfop)
42212f080e7Smrj {
42312f080e7Smrj 	return (mod_info(&rootnex_modlinkage, modinfop));
42412f080e7Smrj }
42512f080e7Smrj 
42612f080e7Smrj 
42712f080e7Smrj /*
42812f080e7Smrj  * _fini()
42912f080e7Smrj  *
43012f080e7Smrj  */
4317c478bd9Sstevel@tonic-gate int
4327c478bd9Sstevel@tonic-gate _fini(void)
4337c478bd9Sstevel@tonic-gate {
4347c478bd9Sstevel@tonic-gate 	return (EBUSY);
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate 
43712f080e7Smrj 
43812f080e7Smrj /*
43912f080e7Smrj  * rootnex_attach()
44012f080e7Smrj  *
44112f080e7Smrj  */
44212f080e7Smrj static int
44312f080e7Smrj rootnex_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
4447c478bd9Sstevel@tonic-gate {
4457aec1d6eScindi 	int fmcap;
44612f080e7Smrj 	int e;
44712f080e7Smrj 
44812f080e7Smrj 	switch (cmd) {
44912f080e7Smrj 	case DDI_ATTACH:
45012f080e7Smrj 		break;
45112f080e7Smrj 	case DDI_RESUME:
4523a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
4533a634bfcSVikram Hegde 		return (immu_unquiesce());
4543a634bfcSVikram Hegde #else
45512f080e7Smrj 		return (DDI_SUCCESS);
4563a634bfcSVikram Hegde #endif
45712f080e7Smrj 	default:
45812f080e7Smrj 		return (DDI_FAILURE);
4597c478bd9Sstevel@tonic-gate 	}
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	/*
46212f080e7Smrj 	 * We should only have one instance of rootnex. Save it away since we
46312f080e7Smrj 	 * don't have an easy way to get it back later.
4647c478bd9Sstevel@tonic-gate 	 */
46512f080e7Smrj 	ASSERT(rootnex_state == NULL);
46612f080e7Smrj 	rootnex_state = kmem_zalloc(sizeof (rootnex_state_t), KM_SLEEP);
4677c478bd9Sstevel@tonic-gate 
46812f080e7Smrj 	rootnex_state->r_dip = dip;
4697aec1d6eScindi 	rootnex_state->r_err_ibc = (ddi_iblock_cookie_t)ipltospl(15);
47012f080e7Smrj 	rootnex_state->r_reserved_msg_printed = B_FALSE;
471*ef4ab52fSFrank Van Der Linden #ifdef DEBUG
47212f080e7Smrj 	rootnex_cnt = &rootnex_state->r_counters[0];
473*ef4ab52fSFrank Van Der Linden #endif
4747c478bd9Sstevel@tonic-gate 
4757aec1d6eScindi 	/*
4767aec1d6eScindi 	 * Set minimum fm capability level for i86pc platforms and then
4777aec1d6eScindi 	 * initialize error handling. Since we're the rootnex, we don't
4787aec1d6eScindi 	 * care what's returned in the fmcap field.
4797aec1d6eScindi 	 */
48000d0963fSdilpreet 	ddi_system_fmcap = DDI_FM_EREPORT_CAPABLE | DDI_FM_ERRCB_CAPABLE |
48100d0963fSdilpreet 	    DDI_FM_ACCCHK_CAPABLE | DDI_FM_DMACHK_CAPABLE;
4827aec1d6eScindi 	fmcap = ddi_system_fmcap;
4837aec1d6eScindi 	ddi_fm_init(dip, &fmcap, &rootnex_state->r_err_ibc);
4847aec1d6eScindi 
48512f080e7Smrj 	/* initialize DMA related state */
48612f080e7Smrj 	e = rootnex_dma_init();
48712f080e7Smrj 	if (e != DDI_SUCCESS) {
48812f080e7Smrj 		kmem_free(rootnex_state, sizeof (rootnex_state_t));
48912f080e7Smrj 		return (DDI_FAILURE);
49012f080e7Smrj 	}
49112f080e7Smrj 
49212f080e7Smrj 	/* Add static root node properties */
49312f080e7Smrj 	rootnex_add_props(dip);
49412f080e7Smrj 
49512f080e7Smrj 	/* since we can't call ddi_report_dev() */
49612f080e7Smrj 	cmn_err(CE_CONT, "?root nexus = %s\n", ddi_get_name(dip));
49712f080e7Smrj 
49812f080e7Smrj 	/* Initialize rootnex event handle */
49912f080e7Smrj 	i_ddi_rootnex_init_events(dip);
50012f080e7Smrj 
5013a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
50220906b23SVikram Hegde 	e = iommulib_nexus_register(dip, &iommulib_nexops,
50320906b23SVikram Hegde 	    &rootnex_state->r_iommulib_handle);
50420906b23SVikram Hegde 
50520906b23SVikram Hegde 	ASSERT(e == DDI_SUCCESS);
50620906b23SVikram Hegde #endif
50720906b23SVikram Hegde 
50812f080e7Smrj 	return (DDI_SUCCESS);
50912f080e7Smrj }
51012f080e7Smrj 
51112f080e7Smrj 
51212f080e7Smrj /*
51312f080e7Smrj  * rootnex_detach()
51412f080e7Smrj  *
51512f080e7Smrj  */
5167c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5177c478bd9Sstevel@tonic-gate static int
51812f080e7Smrj rootnex_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
5197c478bd9Sstevel@tonic-gate {
52012f080e7Smrj 	switch (cmd) {
52112f080e7Smrj 	case DDI_SUSPEND:
5223a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
5233a634bfcSVikram Hegde 		return (immu_quiesce());
5243a634bfcSVikram Hegde #else
5253a634bfcSVikram Hegde 		return (DDI_SUCCESS);
5263a634bfcSVikram Hegde #endif
52712f080e7Smrj 	default:
52812f080e7Smrj 		return (DDI_FAILURE);
52912f080e7Smrj 	}
5303a634bfcSVikram Hegde 	/*NOTREACHED*/
5317c478bd9Sstevel@tonic-gate 
53212f080e7Smrj }
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 
53512f080e7Smrj /*
53612f080e7Smrj  * rootnex_dma_init()
53712f080e7Smrj  *
53812f080e7Smrj  */
53912f080e7Smrj /*ARGSUSED*/
54012f080e7Smrj static int
54112f080e7Smrj rootnex_dma_init()
54212f080e7Smrj {
54312f080e7Smrj 	size_t bufsize;
54412f080e7Smrj 
54512f080e7Smrj 
54612f080e7Smrj 	/*
54712f080e7Smrj 	 * size of our cookie/window/copybuf state needed in dma bind that we
54812f080e7Smrj 	 * pre-alloc in dma_alloc_handle
54912f080e7Smrj 	 */
55012f080e7Smrj 	rootnex_state->r_prealloc_cookies = rootnex_prealloc_cookies;
55112f080e7Smrj 	rootnex_state->r_prealloc_size =
55212f080e7Smrj 	    (rootnex_state->r_prealloc_cookies * sizeof (ddi_dma_cookie_t)) +
55312f080e7Smrj 	    (rootnex_prealloc_windows * sizeof (rootnex_window_t)) +
55412f080e7Smrj 	    (rootnex_prealloc_copybuf * sizeof (rootnex_pgmap_t));
55512f080e7Smrj 
55612f080e7Smrj 	/*
55712f080e7Smrj 	 * setup DDI DMA handle kmem cache, align each handle on 64 bytes,
55812f080e7Smrj 	 * allocate 16 extra bytes for struct pointer alignment
55912f080e7Smrj 	 * (p->dmai_private & dma->dp_prealloc_buffer)
56012f080e7Smrj 	 */
56112f080e7Smrj 	bufsize = sizeof (ddi_dma_impl_t) + sizeof (rootnex_dma_t) +
56212f080e7Smrj 	    rootnex_state->r_prealloc_size + 0x10;
56312f080e7Smrj 	rootnex_state->r_dmahdl_cache = kmem_cache_create("rootnex_dmahdl",
56412f080e7Smrj 	    bufsize, 64, NULL, NULL, NULL, NULL, NULL, 0);
56512f080e7Smrj 	if (rootnex_state->r_dmahdl_cache == NULL) {
56612f080e7Smrj 		return (DDI_FAILURE);
56712f080e7Smrj 	}
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	/*
5707c478bd9Sstevel@tonic-gate 	 * allocate array to track which major numbers we have printed warnings
5717c478bd9Sstevel@tonic-gate 	 * for.
5727c478bd9Sstevel@tonic-gate 	 */
5737c478bd9Sstevel@tonic-gate 	rootnex_warn_list = kmem_zalloc(devcnt * sizeof (*rootnex_warn_list),
5747c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
5777c478bd9Sstevel@tonic-gate }
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate /*
58112f080e7Smrj  * rootnex_add_props()
58212f080e7Smrj  *
5837c478bd9Sstevel@tonic-gate  */
5847c478bd9Sstevel@tonic-gate static void
58512f080e7Smrj rootnex_add_props(dev_info_t *dip)
5867c478bd9Sstevel@tonic-gate {
58712f080e7Smrj 	rootnex_intprop_t *rpp;
5887c478bd9Sstevel@tonic-gate 	int i;
5897c478bd9Sstevel@tonic-gate 
59012f080e7Smrj 	/* Add static integer/boolean properties to the root node */
59112f080e7Smrj 	rpp = rootnex_intprp;
59212f080e7Smrj 	for (i = 0; i < NROOT_INTPROPS; i++) {
59312f080e7Smrj 		(void) e_ddi_prop_update_int(DDI_DEV_T_NONE, dip,
59412f080e7Smrj 		    rpp[i].prop_name, rpp[i].prop_value);
59512f080e7Smrj 	}
5967c478bd9Sstevel@tonic-gate }
5977c478bd9Sstevel@tonic-gate 
59812f080e7Smrj 
59912f080e7Smrj 
6007c478bd9Sstevel@tonic-gate /*
60112f080e7Smrj  * *************************
60212f080e7Smrj  *  ctlops related routines
60312f080e7Smrj  * *************************
60412f080e7Smrj  */
60512f080e7Smrj 
60612f080e7Smrj /*
60712f080e7Smrj  * rootnex_ctlops()
6087c478bd9Sstevel@tonic-gate  *
6097c478bd9Sstevel@tonic-gate  */
610a195726fSgovinda /*ARGSUSED*/
6117c478bd9Sstevel@tonic-gate static int
61212f080e7Smrj rootnex_ctlops(dev_info_t *dip, dev_info_t *rdip, ddi_ctl_enum_t ctlop,
61312f080e7Smrj     void *arg, void *result)
6147c478bd9Sstevel@tonic-gate {
61512f080e7Smrj 	int n, *ptr;
61612f080e7Smrj 	struct ddi_parent_private_data *pdp;
6177c478bd9Sstevel@tonic-gate 
61812f080e7Smrj 	switch (ctlop) {
61912f080e7Smrj 	case DDI_CTLOPS_DMAPMAPC:
6207c478bd9Sstevel@tonic-gate 		/*
62112f080e7Smrj 		 * Return 'partial' to indicate that dma mapping
62212f080e7Smrj 		 * has to be done in the main MMU.
6237c478bd9Sstevel@tonic-gate 		 */
62412f080e7Smrj 		return (DDI_DMA_PARTIAL);
6257c478bd9Sstevel@tonic-gate 
62612f080e7Smrj 	case DDI_CTLOPS_BTOP:
6277c478bd9Sstevel@tonic-gate 		/*
62812f080e7Smrj 		 * Convert byte count input to physical page units.
62912f080e7Smrj 		 * (byte counts that are not a page-size multiple
63012f080e7Smrj 		 * are rounded down)
6317c478bd9Sstevel@tonic-gate 		 */
63212f080e7Smrj 		*(ulong_t *)result = btop(*(ulong_t *)arg);
6337c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
6347c478bd9Sstevel@tonic-gate 
63512f080e7Smrj 	case DDI_CTLOPS_PTOB:
6367c478bd9Sstevel@tonic-gate 		/*
63712f080e7Smrj 		 * Convert size in physical pages to bytes
6387c478bd9Sstevel@tonic-gate 		 */
63912f080e7Smrj 		*(ulong_t *)result = ptob(*(ulong_t *)arg);
6407c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
6417c478bd9Sstevel@tonic-gate 
64212f080e7Smrj 	case DDI_CTLOPS_BTOPR:
6437c478bd9Sstevel@tonic-gate 		/*
64412f080e7Smrj 		 * Convert byte count input to physical page units
64512f080e7Smrj 		 * (byte counts that are not a page-size multiple
64612f080e7Smrj 		 * are rounded up)
6477c478bd9Sstevel@tonic-gate 		 */
64812f080e7Smrj 		*(ulong_t *)result = btopr(*(ulong_t *)arg);
64912f080e7Smrj 		return (DDI_SUCCESS);
65012f080e7Smrj 
65112f080e7Smrj 	case DDI_CTLOPS_INITCHILD:
65212f080e7Smrj 		return (impl_ddi_sunbus_initchild(arg));
65312f080e7Smrj 
65412f080e7Smrj 	case DDI_CTLOPS_UNINITCHILD:
65512f080e7Smrj 		impl_ddi_sunbus_removechild(arg);
65612f080e7Smrj 		return (DDI_SUCCESS);
65712f080e7Smrj 
65812f080e7Smrj 	case DDI_CTLOPS_REPORTDEV:
65912f080e7Smrj 		return (rootnex_ctl_reportdev(rdip));
66012f080e7Smrj 
66112f080e7Smrj 	case DDI_CTLOPS_IOMIN:
6627c478bd9Sstevel@tonic-gate 		/*
66312f080e7Smrj 		 * Nothing to do here but reflect back..
6647c478bd9Sstevel@tonic-gate 		 */
6657c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
6667c478bd9Sstevel@tonic-gate 
66712f080e7Smrj 	case DDI_CTLOPS_REGSIZE:
66812f080e7Smrj 	case DDI_CTLOPS_NREGS:
66912f080e7Smrj 		break;
6707c478bd9Sstevel@tonic-gate 
67112f080e7Smrj 	case DDI_CTLOPS_SIDDEV:
67212f080e7Smrj 		if (ndi_dev_is_prom_node(rdip))
6737c478bd9Sstevel@tonic-gate 			return (DDI_SUCCESS);
67412f080e7Smrj 		if (ndi_dev_is_persistent_node(rdip))
67512f080e7Smrj 			return (DDI_SUCCESS);
6767c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6777c478bd9Sstevel@tonic-gate 
67812f080e7Smrj 	case DDI_CTLOPS_POWER:
67912f080e7Smrj 		return ((*pm_platform_power)((power_req_t *)arg));
68012f080e7Smrj 
681a195726fSgovinda 	case DDI_CTLOPS_RESERVED0: /* Was DDI_CTLOPS_NINTRS, obsolete */
68212f080e7Smrj 	case DDI_CTLOPS_RESERVED1: /* Was DDI_CTLOPS_POKE_INIT, obsolete */
68312f080e7Smrj 	case DDI_CTLOPS_RESERVED2: /* Was DDI_CTLOPS_POKE_FLUSH, obsolete */
68412f080e7Smrj 	case DDI_CTLOPS_RESERVED3: /* Was DDI_CTLOPS_POKE_FINI, obsolete */
685a195726fSgovinda 	case DDI_CTLOPS_RESERVED4: /* Was DDI_CTLOPS_INTR_HILEVEL, obsolete */
686a195726fSgovinda 	case DDI_CTLOPS_RESERVED5: /* Was DDI_CTLOPS_XLATE_INTRS, obsolete */
68712f080e7Smrj 		if (!rootnex_state->r_reserved_msg_printed) {
68812f080e7Smrj 			rootnex_state->r_reserved_msg_printed = B_TRUE;
68912f080e7Smrj 			cmn_err(CE_WARN, "Failing ddi_ctlops call(s) for "
69012f080e7Smrj 			    "1 or more reserved/obsolete operations.");
6917c478bd9Sstevel@tonic-gate 		}
69212f080e7Smrj 		return (DDI_FAILURE);
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	default:
6957c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6967c478bd9Sstevel@tonic-gate 	}
69712f080e7Smrj 	/*
69812f080e7Smrj 	 * The rest are for "hardware" properties
69912f080e7Smrj 	 */
70012f080e7Smrj 	if ((pdp = ddi_get_parent_data(rdip)) == NULL)
70112f080e7Smrj 		return (DDI_FAILURE);
7027c478bd9Sstevel@tonic-gate 
70312f080e7Smrj 	if (ctlop == DDI_CTLOPS_NREGS) {
70412f080e7Smrj 		ptr = (int *)result;
70512f080e7Smrj 		*ptr = pdp->par_nreg;
70612f080e7Smrj 	} else {
70712f080e7Smrj 		off_t *size = (off_t *)result;
7087c478bd9Sstevel@tonic-gate 
70912f080e7Smrj 		ptr = (int *)arg;
71012f080e7Smrj 		n = *ptr;
71112f080e7Smrj 		if (n >= pdp->par_nreg) {
71212f080e7Smrj 			return (DDI_FAILURE);
71312f080e7Smrj 		}
71412f080e7Smrj 		*size = (off_t)pdp->par_reg[n].regspec_size;
71512f080e7Smrj 	}
7167c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
7177c478bd9Sstevel@tonic-gate }
7187c478bd9Sstevel@tonic-gate 
71912f080e7Smrj 
72012f080e7Smrj /*
72112f080e7Smrj  * rootnex_ctl_reportdev()
72212f080e7Smrj  *
72312f080e7Smrj  */
7247c478bd9Sstevel@tonic-gate static int
72512f080e7Smrj rootnex_ctl_reportdev(dev_info_t *dev)
72612f080e7Smrj {
72712f080e7Smrj 	int i, n, len, f_len = 0;
72812f080e7Smrj 	char *buf;
72912f080e7Smrj 
73012f080e7Smrj 	buf = kmem_alloc(REPORTDEV_BUFSIZE, KM_SLEEP);
73112f080e7Smrj 	f_len += snprintf(buf, REPORTDEV_BUFSIZE,
73212f080e7Smrj 	    "%s%d at root", ddi_driver_name(dev), ddi_get_instance(dev));
73312f080e7Smrj 	len = strlen(buf);
73412f080e7Smrj 
73512f080e7Smrj 	for (i = 0; i < sparc_pd_getnreg(dev); i++) {
73612f080e7Smrj 
73712f080e7Smrj 		struct regspec *rp = sparc_pd_getreg(dev, i);
73812f080e7Smrj 
73912f080e7Smrj 		if (i == 0)
74012f080e7Smrj 			f_len += snprintf(buf + len, REPORTDEV_BUFSIZE - len,
74112f080e7Smrj 			    ": ");
74212f080e7Smrj 		else
74312f080e7Smrj 			f_len += snprintf(buf + len, REPORTDEV_BUFSIZE - len,
74412f080e7Smrj 			    " and ");
74512f080e7Smrj 		len = strlen(buf);
74612f080e7Smrj 
74712f080e7Smrj 		switch (rp->regspec_bustype) {
74812f080e7Smrj 
74912f080e7Smrj 		case BTEISA:
75012f080e7Smrj 			f_len += snprintf(buf + len, REPORTDEV_BUFSIZE - len,
75112f080e7Smrj 			    "%s 0x%x", DEVI_EISA_NEXNAME, rp->regspec_addr);
75212f080e7Smrj 			break;
75312f080e7Smrj 
75412f080e7Smrj 		case BTISA:
75512f080e7Smrj 			f_len += snprintf(buf + len, REPORTDEV_BUFSIZE - len,
75612f080e7Smrj 			    "%s 0x%x", DEVI_ISA_NEXNAME, rp->regspec_addr);
75712f080e7Smrj 			break;
75812f080e7Smrj 
75912f080e7Smrj 		default:
76012f080e7Smrj 			f_len += snprintf(buf + len, REPORTDEV_BUFSIZE - len,
76112f080e7Smrj 			    "space %x offset %x",
76212f080e7Smrj 			    rp->regspec_bustype, rp->regspec_addr);
76312f080e7Smrj 			break;
76412f080e7Smrj 		}
76512f080e7Smrj 		len = strlen(buf);
76612f080e7Smrj 	}
76712f080e7Smrj 	for (i = 0, n = sparc_pd_getnintr(dev); i < n; i++) {
76812f080e7Smrj 		int pri;
76912f080e7Smrj 
77012f080e7Smrj 		if (i != 0) {
77112f080e7Smrj 			f_len += snprintf(buf + len, REPORTDEV_BUFSIZE - len,
77212f080e7Smrj 			    ",");
77312f080e7Smrj 			len = strlen(buf);
77412f080e7Smrj 		}
77512f080e7Smrj 		pri = INT_IPL(sparc_pd_getintr(dev, i)->intrspec_pri);
77612f080e7Smrj 		f_len += snprintf(buf + len, REPORTDEV_BUFSIZE - len,
77712f080e7Smrj 		    " sparc ipl %d", pri);
77812f080e7Smrj 		len = strlen(buf);
77912f080e7Smrj 	}
78012f080e7Smrj #ifdef DEBUG
78112f080e7Smrj 	if (f_len + 1 >= REPORTDEV_BUFSIZE) {
78212f080e7Smrj 		cmn_err(CE_NOTE, "next message is truncated: "
78312f080e7Smrj 		    "printed length 1024, real length %d", f_len);
78412f080e7Smrj 	}
78512f080e7Smrj #endif /* DEBUG */
78612f080e7Smrj 	cmn_err(CE_CONT, "?%s\n", buf);
78712f080e7Smrj 	kmem_free(buf, REPORTDEV_BUFSIZE);
78812f080e7Smrj 	return (DDI_SUCCESS);
78912f080e7Smrj }
79012f080e7Smrj 
79112f080e7Smrj 
79212f080e7Smrj /*
79312f080e7Smrj  * ******************
79412f080e7Smrj  *  map related code
79512f080e7Smrj  * ******************
79612f080e7Smrj  */
79712f080e7Smrj 
79812f080e7Smrj /*
79912f080e7Smrj  * rootnex_map()
80012f080e7Smrj  *
80112f080e7Smrj  */
80212f080e7Smrj static int
80312f080e7Smrj rootnex_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp, off_t offset,
80412f080e7Smrj     off_t len, caddr_t *vaddrp)
8057c478bd9Sstevel@tonic-gate {
8067c478bd9Sstevel@tonic-gate 	struct regspec *rp, tmp_reg;
8077c478bd9Sstevel@tonic-gate 	ddi_map_req_t mr = *mp;		/* Get private copy of request */
8087c478bd9Sstevel@tonic-gate 	int error;
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 	mp = &mr;
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 	switch (mp->map_op)  {
8137c478bd9Sstevel@tonic-gate 	case DDI_MO_MAP_LOCKED:
8147c478bd9Sstevel@tonic-gate 	case DDI_MO_UNMAP:
8157c478bd9Sstevel@tonic-gate 	case DDI_MO_MAP_HANDLE:
8167c478bd9Sstevel@tonic-gate 		break;
8177c478bd9Sstevel@tonic-gate 	default:
8187c478bd9Sstevel@tonic-gate #ifdef	DDI_MAP_DEBUG
8197c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "rootnex_map: unimplemented map op %d.",
8207c478bd9Sstevel@tonic-gate 		    mp->map_op);
8217c478bd9Sstevel@tonic-gate #endif	/* DDI_MAP_DEBUG */
8227c478bd9Sstevel@tonic-gate 		return (DDI_ME_UNIMPLEMENTED);
8237c478bd9Sstevel@tonic-gate 	}
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	if (mp->map_flags & DDI_MF_USER_MAPPING)  {
8267c478bd9Sstevel@tonic-gate #ifdef	DDI_MAP_DEBUG
8277c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "rootnex_map: unimplemented map type: user.");
8287c478bd9Sstevel@tonic-gate #endif	/* DDI_MAP_DEBUG */
8297c478bd9Sstevel@tonic-gate 		return (DDI_ME_UNIMPLEMENTED);
8307c478bd9Sstevel@tonic-gate 	}
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	/*
8337c478bd9Sstevel@tonic-gate 	 * First, if given an rnumber, convert it to a regspec...
8347c478bd9Sstevel@tonic-gate 	 * (Presumably, this is on behalf of a child of the root node?)
8357c478bd9Sstevel@tonic-gate 	 */
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 	if (mp->map_type == DDI_MT_RNUMBER)  {
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 		int rnumber = mp->map_obj.rnumber;
8407c478bd9Sstevel@tonic-gate #ifdef	DDI_MAP_DEBUG
8417c478bd9Sstevel@tonic-gate 		static char *out_of_range =
8427c478bd9Sstevel@tonic-gate 		    "rootnex_map: Out of range rnumber <%d>, device <%s>";
8437c478bd9Sstevel@tonic-gate #endif	/* DDI_MAP_DEBUG */
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 		rp = i_ddi_rnumber_to_regspec(rdip, rnumber);
8467c478bd9Sstevel@tonic-gate 		if (rp == NULL)  {
8477c478bd9Sstevel@tonic-gate #ifdef	DDI_MAP_DEBUG
8487c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, out_of_range, rnumber,
8497c478bd9Sstevel@tonic-gate 			    ddi_get_name(rdip));
8507c478bd9Sstevel@tonic-gate #endif	/* DDI_MAP_DEBUG */
8517c478bd9Sstevel@tonic-gate 			return (DDI_ME_RNUMBER_RANGE);
8527c478bd9Sstevel@tonic-gate 		}
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 		/*
8557c478bd9Sstevel@tonic-gate 		 * Convert the given ddi_map_req_t from rnumber to regspec...
8567c478bd9Sstevel@tonic-gate 		 */
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 		mp->map_type = DDI_MT_REGSPEC;
8597c478bd9Sstevel@tonic-gate 		mp->map_obj.rp = rp;
8607c478bd9Sstevel@tonic-gate 	}
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	/*
8637c478bd9Sstevel@tonic-gate 	 * Adjust offset and length correspnding to called values...
8647c478bd9Sstevel@tonic-gate 	 * XXX: A non-zero length means override the one in the regspec
8657c478bd9Sstevel@tonic-gate 	 * XXX: (regardless of what's in the parent's range?)
8667c478bd9Sstevel@tonic-gate 	 */
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	tmp_reg = *(mp->map_obj.rp);		/* Preserve underlying data */
8697c478bd9Sstevel@tonic-gate 	rp = mp->map_obj.rp = &tmp_reg;		/* Use tmp_reg in request */
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate #ifdef	DDI_MAP_DEBUG
872843e1988Sjohnlev 	cmn_err(CE_CONT, "rootnex: <%s,%s> <0x%x, 0x%x, 0x%d> offset %d len %d "
873843e1988Sjohnlev 	    "handle 0x%x\n", ddi_get_name(dip), ddi_get_name(rdip),
874843e1988Sjohnlev 	    rp->regspec_bustype, rp->regspec_addr, rp->regspec_size, offset,
875843e1988Sjohnlev 	    len, mp->map_handlep);
8767c478bd9Sstevel@tonic-gate #endif	/* DDI_MAP_DEBUG */
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 	/*
8797c478bd9Sstevel@tonic-gate 	 * I/O or memory mapping:
8807c478bd9Sstevel@tonic-gate 	 *
8817c478bd9Sstevel@tonic-gate 	 *	<bustype=0, addr=x, len=x>: memory
8827c478bd9Sstevel@tonic-gate 	 *	<bustype=1, addr=x, len=x>: i/o
8837c478bd9Sstevel@tonic-gate 	 *	<bustype>1, addr=0, len=x>: x86-compatibility i/o
8847c478bd9Sstevel@tonic-gate 	 */
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	if (rp->regspec_bustype > 1 && rp->regspec_addr != 0) {
8877c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "<%s,%s> invalid register spec"
8887c478bd9Sstevel@tonic-gate 		    " <0x%x, 0x%x, 0x%x>", ddi_get_name(dip),
8897c478bd9Sstevel@tonic-gate 		    ddi_get_name(rdip), rp->regspec_bustype,
8907c478bd9Sstevel@tonic-gate 		    rp->regspec_addr, rp->regspec_size);
8917c478bd9Sstevel@tonic-gate 		return (DDI_ME_INVAL);
8927c478bd9Sstevel@tonic-gate 	}
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 	if (rp->regspec_bustype > 1 && rp->regspec_addr == 0) {
8957c478bd9Sstevel@tonic-gate 		/*
8967c478bd9Sstevel@tonic-gate 		 * compatibility i/o mapping
8977c478bd9Sstevel@tonic-gate 		 */
8987c478bd9Sstevel@tonic-gate 		rp->regspec_bustype += (uint_t)offset;
8997c478bd9Sstevel@tonic-gate 	} else {
9007c478bd9Sstevel@tonic-gate 		/*
9017c478bd9Sstevel@tonic-gate 		 * Normal memory or i/o mapping
9027c478bd9Sstevel@tonic-gate 		 */
9037c478bd9Sstevel@tonic-gate 		rp->regspec_addr += (uint_t)offset;
9047c478bd9Sstevel@tonic-gate 	}
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	if (len != 0)
9077c478bd9Sstevel@tonic-gate 		rp->regspec_size = (uint_t)len;
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate #ifdef	DDI_MAP_DEBUG
910843e1988Sjohnlev 	cmn_err(CE_CONT, "             <%s,%s> <0x%x, 0x%x, 0x%d> offset %d "
911843e1988Sjohnlev 	    "len %d handle 0x%x\n", ddi_get_name(dip), ddi_get_name(rdip),
9127c478bd9Sstevel@tonic-gate 	    rp->regspec_bustype, rp->regspec_addr, rp->regspec_size,
9137c478bd9Sstevel@tonic-gate 	    offset, len, mp->map_handlep);
9147c478bd9Sstevel@tonic-gate #endif	/* DDI_MAP_DEBUG */
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 	/*
9177c478bd9Sstevel@tonic-gate 	 * Apply any parent ranges at this level, if applicable.
9187c478bd9Sstevel@tonic-gate 	 * (This is where nexus specific regspec translation takes place.
9197c478bd9Sstevel@tonic-gate 	 * Use of this function is implicit agreement that translation is
9207c478bd9Sstevel@tonic-gate 	 * provided via ddi_apply_range.)
9217c478bd9Sstevel@tonic-gate 	 */
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate #ifdef	DDI_MAP_DEBUG
9247c478bd9Sstevel@tonic-gate 	ddi_map_debug("applying range of parent <%s> to child <%s>...\n",
9257c478bd9Sstevel@tonic-gate 	    ddi_get_name(dip), ddi_get_name(rdip));
9267c478bd9Sstevel@tonic-gate #endif	/* DDI_MAP_DEBUG */
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	if ((error = i_ddi_apply_range(dip, rdip, mp->map_obj.rp)) != 0)
9297c478bd9Sstevel@tonic-gate 		return (error);
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 	switch (mp->map_op)  {
9327c478bd9Sstevel@tonic-gate 	case DDI_MO_MAP_LOCKED:
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 		/*
9357c478bd9Sstevel@tonic-gate 		 * Set up the locked down kernel mapping to the regspec...
9367c478bd9Sstevel@tonic-gate 		 */
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 		return (rootnex_map_regspec(mp, vaddrp));
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 	case DDI_MO_UNMAP:
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 		/*
9437c478bd9Sstevel@tonic-gate 		 * Release mapping...
9447c478bd9Sstevel@tonic-gate 		 */
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 		return (rootnex_unmap_regspec(mp, vaddrp));
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 	case DDI_MO_MAP_HANDLE:
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 		return (rootnex_map_handle(mp));
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 	default:
9537c478bd9Sstevel@tonic-gate 		return (DDI_ME_UNIMPLEMENTED);
9547c478bd9Sstevel@tonic-gate 	}
9557c478bd9Sstevel@tonic-gate }
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate /*
95912f080e7Smrj  * rootnex_map_fault()
9607c478bd9Sstevel@tonic-gate  *
9617c478bd9Sstevel@tonic-gate  *	fault in mappings for requestors
9627c478bd9Sstevel@tonic-gate  */
9637c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9647c478bd9Sstevel@tonic-gate static int
96512f080e7Smrj rootnex_map_fault(dev_info_t *dip, dev_info_t *rdip, struct hat *hat,
96612f080e7Smrj     struct seg *seg, caddr_t addr, struct devpage *dp, pfn_t pfn, uint_t prot,
96712f080e7Smrj     uint_t lock)
9687c478bd9Sstevel@tonic-gate {
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate #ifdef	DDI_MAP_DEBUG
9717c478bd9Sstevel@tonic-gate 	ddi_map_debug("rootnex_map_fault: address <%x> pfn <%x>", addr, pfn);
9727c478bd9Sstevel@tonic-gate 	ddi_map_debug(" Seg <%s>\n",
9737c478bd9Sstevel@tonic-gate 	    seg->s_ops == &segdev_ops ? "segdev" :
9747c478bd9Sstevel@tonic-gate 	    seg == &kvseg ? "segkmem" : "NONE!");
9757c478bd9Sstevel@tonic-gate #endif	/* DDI_MAP_DEBUG */
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 	/*
9787c478bd9Sstevel@tonic-gate 	 * This is all terribly broken, but it is a start
9797c478bd9Sstevel@tonic-gate 	 *
9807c478bd9Sstevel@tonic-gate 	 * XXX	Note that this test means that segdev_ops
9817c478bd9Sstevel@tonic-gate 	 *	must be exported from seg_dev.c.
9827c478bd9Sstevel@tonic-gate 	 * XXX	What about devices with their own segment drivers?
9837c478bd9Sstevel@tonic-gate 	 */
9847c478bd9Sstevel@tonic-gate 	if (seg->s_ops == &segdev_ops) {
985843e1988Sjohnlev 		struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 		if (hat == NULL) {
9887c478bd9Sstevel@tonic-gate 			/*
9897c478bd9Sstevel@tonic-gate 			 * This is one plausible interpretation of
9907c478bd9Sstevel@tonic-gate 			 * a null hat i.e. use the first hat on the
9917c478bd9Sstevel@tonic-gate 			 * address space hat list which by convention is
9927c478bd9Sstevel@tonic-gate 			 * the hat of the system MMU.  At alternative
9937c478bd9Sstevel@tonic-gate 			 * would be to panic .. this might well be better ..
9947c478bd9Sstevel@tonic-gate 			 */
9957c478bd9Sstevel@tonic-gate 			ASSERT(AS_READ_HELD(seg->s_as, &seg->s_as->a_lock));
9967c478bd9Sstevel@tonic-gate 			hat = seg->s_as->a_hat;
9977c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "rootnex_map_fault: nil hat");
9987c478bd9Sstevel@tonic-gate 		}
9997c478bd9Sstevel@tonic-gate 		hat_devload(hat, addr, MMU_PAGESIZE, pfn, prot | sdp->hat_attr,
10007c478bd9Sstevel@tonic-gate 		    (lock ? HAT_LOAD_LOCK : HAT_LOAD));
10017c478bd9Sstevel@tonic-gate 	} else if (seg == &kvseg && dp == NULL) {
10027c478bd9Sstevel@tonic-gate 		hat_devload(kas.a_hat, addr, MMU_PAGESIZE, pfn, prot,
10037c478bd9Sstevel@tonic-gate 		    HAT_LOAD_LOCK);
10047c478bd9Sstevel@tonic-gate 	} else
10057c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
10067c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
10077c478bd9Sstevel@tonic-gate }
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate /*
101112f080e7Smrj  * rootnex_map_regspec()
101212f080e7Smrj  *     we don't support mapping of I/O cards above 4Gb
10137c478bd9Sstevel@tonic-gate  */
10147c478bd9Sstevel@tonic-gate static int
101512f080e7Smrj rootnex_map_regspec(ddi_map_req_t *mp, caddr_t *vaddrp)
10167c478bd9Sstevel@tonic-gate {
1017843e1988Sjohnlev 	rootnex_addr_t rbase;
101812f080e7Smrj 	void *cvaddr;
101912f080e7Smrj 	uint_t npages, pgoffset;
102012f080e7Smrj 	struct regspec *rp;
102112f080e7Smrj 	ddi_acc_hdl_t *hp;
102212f080e7Smrj 	ddi_acc_impl_t *ap;
102312f080e7Smrj 	uint_t	hat_acc_flags;
1024843e1988Sjohnlev 	paddr_t pbase;
10257c478bd9Sstevel@tonic-gate 
102612f080e7Smrj 	rp = mp->map_obj.rp;
102712f080e7Smrj 	hp = mp->map_handlep;
102812f080e7Smrj 
102912f080e7Smrj #ifdef	DDI_MAP_DEBUG
103012f080e7Smrj 	ddi_map_debug(
103112f080e7Smrj 	    "rootnex_map_regspec: <0x%x 0x%x 0x%x> handle 0x%x\n",
103212f080e7Smrj 	    rp->regspec_bustype, rp->regspec_addr,
103312f080e7Smrj 	    rp->regspec_size, mp->map_handlep);
103412f080e7Smrj #endif	/* DDI_MAP_DEBUG */
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 	/*
103712f080e7Smrj 	 * I/O or memory mapping
103812f080e7Smrj 	 *
103912f080e7Smrj 	 *	<bustype=0, addr=x, len=x>: memory
104012f080e7Smrj 	 *	<bustype=1, addr=x, len=x>: i/o
104112f080e7Smrj 	 *	<bustype>1, addr=0, len=x>: x86-compatibility i/o
10427c478bd9Sstevel@tonic-gate 	 */
104312f080e7Smrj 
104412f080e7Smrj 	if (rp->regspec_bustype > 1 && rp->regspec_addr != 0) {
104512f080e7Smrj 		cmn_err(CE_WARN, "rootnex: invalid register spec"
104612f080e7Smrj 		    " <0x%x, 0x%x, 0x%x>", rp->regspec_bustype,
104712f080e7Smrj 		    rp->regspec_addr, rp->regspec_size);
104812f080e7Smrj 		return (DDI_FAILURE);
10497c478bd9Sstevel@tonic-gate 	}
105012f080e7Smrj 
105112f080e7Smrj 	if (rp->regspec_bustype != 0) {
10527c478bd9Sstevel@tonic-gate 		/*
105312f080e7Smrj 		 * I/O space - needs a handle.
10547c478bd9Sstevel@tonic-gate 		 */
10557c478bd9Sstevel@tonic-gate 		if (hp == NULL) {
105612f080e7Smrj 			return (DDI_FAILURE);
10577c478bd9Sstevel@tonic-gate 		}
105812f080e7Smrj 		ap = (ddi_acc_impl_t *)hp->ah_platform_private;
105912f080e7Smrj 		ap->ahi_acc_attr |= DDI_ACCATTR_IO_SPACE;
106012f080e7Smrj 		impl_acc_hdl_init(hp);
10617c478bd9Sstevel@tonic-gate 
106212f080e7Smrj 		if (mp->map_flags & DDI_MF_DEVICE_MAPPING) {
106312f080e7Smrj #ifdef  DDI_MAP_DEBUG
1064843e1988Sjohnlev 			ddi_map_debug("rootnex_map_regspec: mmap() "
1065843e1988Sjohnlev 			    "to I/O space is not supported.\n");
106612f080e7Smrj #endif  /* DDI_MAP_DEBUG */
106712f080e7Smrj 			return (DDI_ME_INVAL);
10687c478bd9Sstevel@tonic-gate 		} else {
10697c478bd9Sstevel@tonic-gate 			/*
107012f080e7Smrj 			 * 1275-compliant vs. compatibility i/o mapping
10717c478bd9Sstevel@tonic-gate 			 */
107212f080e7Smrj 			*vaddrp =
107312f080e7Smrj 			    (rp->regspec_bustype > 1 && rp->regspec_addr == 0) ?
107412f080e7Smrj 			    ((caddr_t)(uintptr_t)rp->regspec_bustype) :
107512f080e7Smrj 			    ((caddr_t)(uintptr_t)rp->regspec_addr);
1076843e1988Sjohnlev #ifdef __xpv
1077843e1988Sjohnlev 			if (DOMAIN_IS_INITDOMAIN(xen_info)) {
1078843e1988Sjohnlev 				hp->ah_pfn = xen_assign_pfn(
1079843e1988Sjohnlev 				    mmu_btop((ulong_t)rp->regspec_addr &
1080843e1988Sjohnlev 				    MMU_PAGEMASK));
1081843e1988Sjohnlev 			} else {
1082843e1988Sjohnlev 				hp->ah_pfn = mmu_btop(
1083843e1988Sjohnlev 				    (ulong_t)rp->regspec_addr & MMU_PAGEMASK);
1084843e1988Sjohnlev 			}
1085843e1988Sjohnlev #else
108600d0963fSdilpreet 			hp->ah_pfn = mmu_btop((ulong_t)rp->regspec_addr &
1087843e1988Sjohnlev 			    MMU_PAGEMASK);
1088843e1988Sjohnlev #endif
108900d0963fSdilpreet 			hp->ah_pnum = mmu_btopr(rp->regspec_size +
109000d0963fSdilpreet 			    (ulong_t)rp->regspec_addr & MMU_PAGEOFFSET);
10917c478bd9Sstevel@tonic-gate 		}
10927c478bd9Sstevel@tonic-gate 
109312f080e7Smrj #ifdef	DDI_MAP_DEBUG
109412f080e7Smrj 		ddi_map_debug(
109512f080e7Smrj 	    "rootnex_map_regspec: \"Mapping\" %d bytes I/O space at 0x%x\n",
109612f080e7Smrj 		    rp->regspec_size, *vaddrp);
109712f080e7Smrj #endif	/* DDI_MAP_DEBUG */
109812f080e7Smrj 		return (DDI_SUCCESS);
10997c478bd9Sstevel@tonic-gate 	}
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 	/*
110212f080e7Smrj 	 * Memory space
110312f080e7Smrj 	 */
110412f080e7Smrj 
110512f080e7Smrj 	if (hp != NULL) {
110612f080e7Smrj 		/*
110712f080e7Smrj 		 * hat layer ignores
110812f080e7Smrj 		 * hp->ah_acc.devacc_attr_endian_flags.
110912f080e7Smrj 		 */
111012f080e7Smrj 		switch (hp->ah_acc.devacc_attr_dataorder) {
111112f080e7Smrj 		case DDI_STRICTORDER_ACC:
111212f080e7Smrj 			hat_acc_flags = HAT_STRICTORDER;
111312f080e7Smrj 			break;
111412f080e7Smrj 		case DDI_UNORDERED_OK_ACC:
111512f080e7Smrj 			hat_acc_flags = HAT_UNORDERED_OK;
111612f080e7Smrj 			break;
111712f080e7Smrj 		case DDI_MERGING_OK_ACC:
111812f080e7Smrj 			hat_acc_flags = HAT_MERGING_OK;
111912f080e7Smrj 			break;
112012f080e7Smrj 		case DDI_LOADCACHING_OK_ACC:
112112f080e7Smrj 			hat_acc_flags = HAT_LOADCACHING_OK;
112212f080e7Smrj 			break;
112312f080e7Smrj 		case DDI_STORECACHING_OK_ACC:
112412f080e7Smrj 			hat_acc_flags = HAT_STORECACHING_OK;
112512f080e7Smrj 			break;
112612f080e7Smrj 		}
112712f080e7Smrj 		ap = (ddi_acc_impl_t *)hp->ah_platform_private;
112812f080e7Smrj 		ap->ahi_acc_attr |= DDI_ACCATTR_CPU_VADDR;
112912f080e7Smrj 		impl_acc_hdl_init(hp);
113012f080e7Smrj 		hp->ah_hat_flags = hat_acc_flags;
113112f080e7Smrj 	} else {
113212f080e7Smrj 		hat_acc_flags = HAT_STRICTORDER;
113312f080e7Smrj 	}
113412f080e7Smrj 
1135843e1988Sjohnlev 	rbase = (rootnex_addr_t)(rp->regspec_addr & MMU_PAGEMASK);
1136843e1988Sjohnlev #ifdef __xpv
1137843e1988Sjohnlev 	/*
1138843e1988Sjohnlev 	 * If we're dom0, we're using a real device so we need to translate
1139843e1988Sjohnlev 	 * the MA to a PA.
1140843e1988Sjohnlev 	 */
1141843e1988Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
1142843e1988Sjohnlev 		pbase = pfn_to_pa(xen_assign_pfn(mmu_btop(rbase)));
1143843e1988Sjohnlev 	} else {
1144843e1988Sjohnlev 		pbase = rbase;
1145843e1988Sjohnlev 	}
1146843e1988Sjohnlev #else
1147843e1988Sjohnlev 	pbase = rbase;
1148843e1988Sjohnlev #endif
1149843e1988Sjohnlev 	pgoffset = (ulong_t)rp->regspec_addr & MMU_PAGEOFFSET;
115012f080e7Smrj 
115112f080e7Smrj 	if (rp->regspec_size == 0) {
115212f080e7Smrj #ifdef  DDI_MAP_DEBUG
115312f080e7Smrj 		ddi_map_debug("rootnex_map_regspec: zero regspec_size\n");
115412f080e7Smrj #endif  /* DDI_MAP_DEBUG */
115512f080e7Smrj 		return (DDI_ME_INVAL);
115612f080e7Smrj 	}
115712f080e7Smrj 
115812f080e7Smrj 	if (mp->map_flags & DDI_MF_DEVICE_MAPPING) {
1159843e1988Sjohnlev 		/* extra cast to make gcc happy */
1160843e1988Sjohnlev 		*vaddrp = (caddr_t)((uintptr_t)mmu_btop(pbase));
116112f080e7Smrj 	} else {
116212f080e7Smrj 		npages = mmu_btopr(rp->regspec_size + pgoffset);
116312f080e7Smrj 
116412f080e7Smrj #ifdef	DDI_MAP_DEBUG
1165843e1988Sjohnlev 		ddi_map_debug("rootnex_map_regspec: Mapping %d pages "
1166843e1988Sjohnlev 		    "physical %llx", npages, pbase);
116712f080e7Smrj #endif	/* DDI_MAP_DEBUG */
116812f080e7Smrj 
116912f080e7Smrj 		cvaddr = device_arena_alloc(ptob(npages), VM_NOSLEEP);
117012f080e7Smrj 		if (cvaddr == NULL)
117112f080e7Smrj 			return (DDI_ME_NORESOURCES);
117212f080e7Smrj 
117312f080e7Smrj 		/*
117412f080e7Smrj 		 * Now map in the pages we've allocated...
117512f080e7Smrj 		 */
1176843e1988Sjohnlev 		hat_devload(kas.a_hat, cvaddr, mmu_ptob(npages),
1177843e1988Sjohnlev 		    mmu_btop(pbase), mp->map_prot | hat_acc_flags,
1178843e1988Sjohnlev 		    HAT_LOAD_LOCK);
117912f080e7Smrj 		*vaddrp = (caddr_t)cvaddr + pgoffset;
118000d0963fSdilpreet 
118100d0963fSdilpreet 		/* save away pfn and npages for FMA */
118200d0963fSdilpreet 		hp = mp->map_handlep;
118300d0963fSdilpreet 		if (hp) {
1184843e1988Sjohnlev 			hp->ah_pfn = mmu_btop(pbase);
118500d0963fSdilpreet 			hp->ah_pnum = npages;
118600d0963fSdilpreet 		}
118712f080e7Smrj 	}
118812f080e7Smrj 
118912f080e7Smrj #ifdef	DDI_MAP_DEBUG
119012f080e7Smrj 	ddi_map_debug("at virtual 0x%x\n", *vaddrp);
119112f080e7Smrj #endif	/* DDI_MAP_DEBUG */
119212f080e7Smrj 	return (DDI_SUCCESS);
119312f080e7Smrj }
119412f080e7Smrj 
119512f080e7Smrj 
119612f080e7Smrj /*
119712f080e7Smrj  * rootnex_unmap_regspec()
11987c478bd9Sstevel@tonic-gate  *
11997c478bd9Sstevel@tonic-gate  */
12007c478bd9Sstevel@tonic-gate static int
120112f080e7Smrj rootnex_unmap_regspec(ddi_map_req_t *mp, caddr_t *vaddrp)
12027c478bd9Sstevel@tonic-gate {
120312f080e7Smrj 	caddr_t addr = (caddr_t)*vaddrp;
120412f080e7Smrj 	uint_t npages, pgoffset;
120512f080e7Smrj 	struct regspec *rp;
12067c478bd9Sstevel@tonic-gate 
120712f080e7Smrj 	if (mp->map_flags & DDI_MF_DEVICE_MAPPING)
120812f080e7Smrj 		return (0);
12097c478bd9Sstevel@tonic-gate 
121012f080e7Smrj 	rp = mp->map_obj.rp;
12117c478bd9Sstevel@tonic-gate 
121212f080e7Smrj 	if (rp->regspec_size == 0) {
121312f080e7Smrj #ifdef  DDI_MAP_DEBUG
121412f080e7Smrj 		ddi_map_debug("rootnex_unmap_regspec: zero regspec_size\n");
121512f080e7Smrj #endif  /* DDI_MAP_DEBUG */
121612f080e7Smrj 		return (DDI_ME_INVAL);
12177c478bd9Sstevel@tonic-gate 	}
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 	/*
122012f080e7Smrj 	 * I/O or memory mapping:
12217c478bd9Sstevel@tonic-gate 	 *
122212f080e7Smrj 	 *	<bustype=0, addr=x, len=x>: memory
122312f080e7Smrj 	 *	<bustype=1, addr=x, len=x>: i/o
122412f080e7Smrj 	 *	<bustype>1, addr=0, len=x>: x86-compatibility i/o
12257c478bd9Sstevel@tonic-gate 	 */
122612f080e7Smrj 	if (rp->regspec_bustype != 0) {
12277c478bd9Sstevel@tonic-gate 		/*
122812f080e7Smrj 		 * This is I/O space, which requires no particular
122912f080e7Smrj 		 * processing on unmap since it isn't mapped in the
123012f080e7Smrj 		 * first place.
12317c478bd9Sstevel@tonic-gate 		 */
12327c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
12337c478bd9Sstevel@tonic-gate 	}
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 	/*
123612f080e7Smrj 	 * Memory space
12377c478bd9Sstevel@tonic-gate 	 */
123812f080e7Smrj 	pgoffset = (uintptr_t)addr & MMU_PAGEOFFSET;
123912f080e7Smrj 	npages = mmu_btopr(rp->regspec_size + pgoffset);
124012f080e7Smrj 	hat_unload(kas.a_hat, addr - pgoffset, ptob(npages), HAT_UNLOAD_UNLOCK);
124112f080e7Smrj 	device_arena_free(addr - pgoffset, ptob(npages));
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 	/*
124412f080e7Smrj 	 * Destroy the pointer - the mapping has logically gone
12457c478bd9Sstevel@tonic-gate 	 */
124612f080e7Smrj 	*vaddrp = NULL;
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
12497c478bd9Sstevel@tonic-gate }
12507c478bd9Sstevel@tonic-gate 
125112f080e7Smrj 
125212f080e7Smrj /*
125312f080e7Smrj  * rootnex_map_handle()
125412f080e7Smrj  *
125512f080e7Smrj  */
12567c478bd9Sstevel@tonic-gate static int
125712f080e7Smrj rootnex_map_handle(ddi_map_req_t *mp)
12587c478bd9Sstevel@tonic-gate {
1259843e1988Sjohnlev 	rootnex_addr_t rbase;
126012f080e7Smrj 	ddi_acc_hdl_t *hp;
126112f080e7Smrj 	uint_t pgoffset;
126212f080e7Smrj 	struct regspec *rp;
1263843e1988Sjohnlev 	paddr_t pbase;
12647c478bd9Sstevel@tonic-gate 
126512f080e7Smrj 	rp = mp->map_obj.rp;
12667c478bd9Sstevel@tonic-gate 
126712f080e7Smrj #ifdef	DDI_MAP_DEBUG
126812f080e7Smrj 	ddi_map_debug(
126912f080e7Smrj 	    "rootnex_map_handle: <0x%x 0x%x 0x%x> handle 0x%x\n",
127012f080e7Smrj 	    rp->regspec_bustype, rp->regspec_addr,
127112f080e7Smrj 	    rp->regspec_size, mp->map_handlep);
127212f080e7Smrj #endif	/* DDI_MAP_DEBUG */
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 	/*
127512f080e7Smrj 	 * I/O or memory mapping:
127612f080e7Smrj 	 *
127712f080e7Smrj 	 *	<bustype=0, addr=x, len=x>: memory
127812f080e7Smrj 	 *	<bustype=1, addr=x, len=x>: i/o
127912f080e7Smrj 	 *	<bustype>1, addr=0, len=x>: x86-compatibility i/o
12807c478bd9Sstevel@tonic-gate 	 */
128112f080e7Smrj 	if (rp->regspec_bustype != 0) {
128212f080e7Smrj 		/*
128312f080e7Smrj 		 * This refers to I/O space, and we don't support "mapping"
128412f080e7Smrj 		 * I/O space to a user.
128512f080e7Smrj 		 */
12867c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
12877c478bd9Sstevel@tonic-gate 	}
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 	/*
129012f080e7Smrj 	 * Set up the hat_flags for the mapping.
12917c478bd9Sstevel@tonic-gate 	 */
129212f080e7Smrj 	hp = mp->map_handlep;
12937c478bd9Sstevel@tonic-gate 
129412f080e7Smrj 	switch (hp->ah_acc.devacc_attr_endian_flags) {
129512f080e7Smrj 	case DDI_NEVERSWAP_ACC:
129612f080e7Smrj 		hp->ah_hat_flags = HAT_NEVERSWAP | HAT_STRICTORDER;
12977c478bd9Sstevel@tonic-gate 		break;
129812f080e7Smrj 	case DDI_STRUCTURE_LE_ACC:
129912f080e7Smrj 		hp->ah_hat_flags = HAT_STRUCTURE_LE;
13007c478bd9Sstevel@tonic-gate 		break;
130112f080e7Smrj 	case DDI_STRUCTURE_BE_ACC:
13027c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
13037c478bd9Sstevel@tonic-gate 	default:
130412f080e7Smrj 		return (DDI_REGS_ACC_CONFLICT);
13057c478bd9Sstevel@tonic-gate 	}
13067c478bd9Sstevel@tonic-gate 
130712f080e7Smrj 	switch (hp->ah_acc.devacc_attr_dataorder) {
130812f080e7Smrj 	case DDI_STRICTORDER_ACC:
13097c478bd9Sstevel@tonic-gate 		break;
131012f080e7Smrj 	case DDI_UNORDERED_OK_ACC:
131112f080e7Smrj 		hp->ah_hat_flags |= HAT_UNORDERED_OK;
13127c478bd9Sstevel@tonic-gate 		break;
131312f080e7Smrj 	case DDI_MERGING_OK_ACC:
131412f080e7Smrj 		hp->ah_hat_flags |= HAT_MERGING_OK;
13157c478bd9Sstevel@tonic-gate 		break;
131612f080e7Smrj 	case DDI_LOADCACHING_OK_ACC:
131712f080e7Smrj 		hp->ah_hat_flags |= HAT_LOADCACHING_OK;
131812f080e7Smrj 		break;
131912f080e7Smrj 	case DDI_STORECACHING_OK_ACC:
132012f080e7Smrj 		hp->ah_hat_flags |= HAT_STORECACHING_OK;
132112f080e7Smrj 		break;
13227c478bd9Sstevel@tonic-gate 	default:
13237c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
13247c478bd9Sstevel@tonic-gate 	}
13257c478bd9Sstevel@tonic-gate 
1326843e1988Sjohnlev 	rbase = (rootnex_addr_t)rp->regspec_addr &
1327843e1988Sjohnlev 	    (~(rootnex_addr_t)MMU_PAGEOFFSET);
1328843e1988Sjohnlev 	pgoffset = (ulong_t)rp->regspec_addr & MMU_PAGEOFFSET;
13297c478bd9Sstevel@tonic-gate 
133012f080e7Smrj 	if (rp->regspec_size == 0)
133112f080e7Smrj 		return (DDI_ME_INVAL);
13327c478bd9Sstevel@tonic-gate 
1333843e1988Sjohnlev #ifdef __xpv
1334843e1988Sjohnlev 	/*
1335843e1988Sjohnlev 	 * If we're dom0, we're using a real device so we need to translate
1336843e1988Sjohnlev 	 * the MA to a PA.
1337843e1988Sjohnlev 	 */
1338843e1988Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
1339843e1988Sjohnlev 		pbase = pfn_to_pa(xen_assign_pfn(mmu_btop(rbase))) |
1340843e1988Sjohnlev 		    (rbase & MMU_PAGEOFFSET);
1341843e1988Sjohnlev 	} else {
1342843e1988Sjohnlev 		pbase = rbase;
1343843e1988Sjohnlev 	}
1344843e1988Sjohnlev #else
1345843e1988Sjohnlev 	pbase = rbase;
1346843e1988Sjohnlev #endif
1347843e1988Sjohnlev 
1348843e1988Sjohnlev 	hp->ah_pfn = mmu_btop(pbase);
134912f080e7Smrj 	hp->ah_pnum = mmu_btopr(rp->regspec_size + pgoffset);
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
13527c478bd9Sstevel@tonic-gate }
13537c478bd9Sstevel@tonic-gate 
135412f080e7Smrj 
135512f080e7Smrj 
13567c478bd9Sstevel@tonic-gate /*
135712f080e7Smrj  * ************************
135812f080e7Smrj  *  interrupt related code
135912f080e7Smrj  * ************************
13607c478bd9Sstevel@tonic-gate  */
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate /*
136312f080e7Smrj  * rootnex_intr_ops()
13647c478bd9Sstevel@tonic-gate  *	bus_intr_op() function for interrupt support
13657c478bd9Sstevel@tonic-gate  */
13667c478bd9Sstevel@tonic-gate /* ARGSUSED */
13677c478bd9Sstevel@tonic-gate static int
13687c478bd9Sstevel@tonic-gate rootnex_intr_ops(dev_info_t *pdip, dev_info_t *rdip, ddi_intr_op_t intr_op,
13697c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, void *result)
13707c478bd9Sstevel@tonic-gate {
13717c478bd9Sstevel@tonic-gate 	struct intrspec			*ispec;
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate 	DDI_INTR_NEXDBG((CE_CONT,
13747c478bd9Sstevel@tonic-gate 	    "rootnex_intr_ops: pdip = %p, rdip = %p, intr_op = %x, hdlp = %p\n",
13757c478bd9Sstevel@tonic-gate 	    (void *)pdip, (void *)rdip, intr_op, (void *)hdlp));
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate 	/* Process the interrupt operation */
13787c478bd9Sstevel@tonic-gate 	switch (intr_op) {
13797c478bd9Sstevel@tonic-gate 	case DDI_INTROP_GETCAP:
13807c478bd9Sstevel@tonic-gate 		/* First check with pcplusmp */
13817c478bd9Sstevel@tonic-gate 		if (psm_intr_ops == NULL)
13827c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate 		if ((*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_GET_CAP, result)) {
13857c478bd9Sstevel@tonic-gate 			*(int *)result = 0;
13867c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
13877c478bd9Sstevel@tonic-gate 		}
13887c478bd9Sstevel@tonic-gate 		break;
13897c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SETCAP:
13907c478bd9Sstevel@tonic-gate 		if (psm_intr_ops == NULL)
13917c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
13927c478bd9Sstevel@tonic-gate 
13937c478bd9Sstevel@tonic-gate 		if ((*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_SET_CAP, result))
13947c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
13957c478bd9Sstevel@tonic-gate 		break;
13967c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ALLOC:
13977ff178cdSJimmy Vetayases 		ASSERT(hdlp->ih_type == DDI_INTR_TYPE_FIXED);
13987ff178cdSJimmy Vetayases 		return (rootnex_alloc_intr_fixed(rdip, hdlp, result));
13997c478bd9Sstevel@tonic-gate 	case DDI_INTROP_FREE:
14007ff178cdSJimmy Vetayases 		ASSERT(hdlp->ih_type == DDI_INTR_TYPE_FIXED);
14017ff178cdSJimmy Vetayases 		return (rootnex_free_intr_fixed(rdip, hdlp));
14027c478bd9Sstevel@tonic-gate 	case DDI_INTROP_GETPRI:
14037c478bd9Sstevel@tonic-gate 		if ((ispec = rootnex_get_ispec(rdip, hdlp->ih_inum)) == NULL)
14047c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14057c478bd9Sstevel@tonic-gate 		*(int *)result = ispec->intrspec_pri;
14067c478bd9Sstevel@tonic-gate 		break;
14077c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SETPRI:
14087c478bd9Sstevel@tonic-gate 		/* Validate the interrupt priority passed to us */
14097c478bd9Sstevel@tonic-gate 		if (*(int *)result > LOCK_LEVEL)
14107c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 		/* Ensure that PSM is all initialized and ispec is ok */
14137c478bd9Sstevel@tonic-gate 		if ((psm_intr_ops == NULL) ||
14147c478bd9Sstevel@tonic-gate 		    ((ispec = rootnex_get_ispec(rdip, hdlp->ih_inum)) == NULL))
14157c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate 		/* Change the priority */
14187c478bd9Sstevel@tonic-gate 		if ((*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_SET_PRI, result) ==
14197c478bd9Sstevel@tonic-gate 		    PSM_FAILURE)
14207c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate 		/* update the ispec with the new priority */
14237c478bd9Sstevel@tonic-gate 		ispec->intrspec_pri =  *(int *)result;
14247c478bd9Sstevel@tonic-gate 		break;
14257c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ADDISR:
14267c478bd9Sstevel@tonic-gate 		if ((ispec = rootnex_get_ispec(rdip, hdlp->ih_inum)) == NULL)
14277c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14287c478bd9Sstevel@tonic-gate 		ispec->intrspec_func = hdlp->ih_cb_func;
14297c478bd9Sstevel@tonic-gate 		break;
14307c478bd9Sstevel@tonic-gate 	case DDI_INTROP_REMISR:
14317c478bd9Sstevel@tonic-gate 		if ((ispec = rootnex_get_ispec(rdip, hdlp->ih_inum)) == NULL)
14327c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14337c478bd9Sstevel@tonic-gate 		ispec->intrspec_func = (uint_t (*)()) 0;
14347c478bd9Sstevel@tonic-gate 		break;
14357c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ENABLE:
14367c478bd9Sstevel@tonic-gate 		if ((ispec = rootnex_get_ispec(rdip, hdlp->ih_inum)) == NULL)
14377c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 		/* Call psmi to translate irq with the dip */
14407c478bd9Sstevel@tonic-gate 		if (psm_intr_ops == NULL)
14417c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14427c478bd9Sstevel@tonic-gate 
14437a364d25Sschwartz 		((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp = ispec;
144486a9c507SGuoli Shu 		if ((*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_XLATE_VECTOR,
144586a9c507SGuoli Shu 		    (int *)&hdlp->ih_vector) == PSM_FAILURE)
144686a9c507SGuoli Shu 			return (DDI_FAILURE);
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 		/* Add the interrupt handler */
14497c478bd9Sstevel@tonic-gate 		if (!add_avintr((void *)hdlp, ispec->intrspec_pri,
14507c478bd9Sstevel@tonic-gate 		    hdlp->ih_cb_func, DEVI(rdip)->devi_name, hdlp->ih_vector,
14517a364d25Sschwartz 		    hdlp->ih_cb_arg1, hdlp->ih_cb_arg2, NULL, rdip))
14527c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14537c478bd9Sstevel@tonic-gate 		break;
14547c478bd9Sstevel@tonic-gate 	case DDI_INTROP_DISABLE:
14557c478bd9Sstevel@tonic-gate 		if ((ispec = rootnex_get_ispec(rdip, hdlp->ih_inum)) == NULL)
14567c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate 		/* Call psm_ops() to translate irq with the dip */
14597c478bd9Sstevel@tonic-gate 		if (psm_intr_ops == NULL)
14607c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14617c478bd9Sstevel@tonic-gate 
14627a364d25Sschwartz 		((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp = ispec;
14637c478bd9Sstevel@tonic-gate 		(void) (*psm_intr_ops)(rdip, hdlp,
14647c478bd9Sstevel@tonic-gate 		    PSM_INTR_OP_XLATE_VECTOR, (int *)&hdlp->ih_vector);
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate 		/* Remove the interrupt handler */
14677c478bd9Sstevel@tonic-gate 		rem_avintr((void *)hdlp, ispec->intrspec_pri,
14687c478bd9Sstevel@tonic-gate 		    hdlp->ih_cb_func, hdlp->ih_vector);
14697c478bd9Sstevel@tonic-gate 		break;
14707c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SETMASK:
14717c478bd9Sstevel@tonic-gate 		if (psm_intr_ops == NULL)
14727c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate 		if ((*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_SET_MASK, NULL))
14757c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14767c478bd9Sstevel@tonic-gate 		break;
14777c478bd9Sstevel@tonic-gate 	case DDI_INTROP_CLRMASK:
14787c478bd9Sstevel@tonic-gate 		if (psm_intr_ops == NULL)
14797c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14807c478bd9Sstevel@tonic-gate 
14817c478bd9Sstevel@tonic-gate 		if ((*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_CLEAR_MASK, NULL))
14827c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14837c478bd9Sstevel@tonic-gate 		break;
14847c478bd9Sstevel@tonic-gate 	case DDI_INTROP_GETPENDING:
14857c478bd9Sstevel@tonic-gate 		if (psm_intr_ops == NULL)
14867c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14877c478bd9Sstevel@tonic-gate 
14887c478bd9Sstevel@tonic-gate 		if ((*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_GET_PENDING,
14897c478bd9Sstevel@tonic-gate 		    result)) {
14907c478bd9Sstevel@tonic-gate 			*(int *)result = 0;
14917c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14927c478bd9Sstevel@tonic-gate 		}
14937c478bd9Sstevel@tonic-gate 		break;
1494a54f81fbSanish 	case DDI_INTROP_NAVAIL:
14957c478bd9Sstevel@tonic-gate 	case DDI_INTROP_NINTRS:
1496a54f81fbSanish 		*(int *)result = i_ddi_get_intx_nintrs(rdip);
1497a54f81fbSanish 		if (*(int *)result == 0) {
14987c478bd9Sstevel@tonic-gate 			/*
14997c478bd9Sstevel@tonic-gate 			 * Special case for 'pcic' driver' only. This driver
15007c478bd9Sstevel@tonic-gate 			 * driver is a child of 'isa' and 'rootnex' drivers.
15017c478bd9Sstevel@tonic-gate 			 *
15027c478bd9Sstevel@tonic-gate 			 * See detailed comments on this in the function
15037c478bd9Sstevel@tonic-gate 			 * rootnex_get_ispec().
15047c478bd9Sstevel@tonic-gate 			 *
15057c478bd9Sstevel@tonic-gate 			 * Children of 'pcic' send 'NINITR' request all the
15067c478bd9Sstevel@tonic-gate 			 * way to rootnex driver. But, the 'pdp->par_nintr'
15077c478bd9Sstevel@tonic-gate 			 * field may not initialized. So, we fake it here
15087c478bd9Sstevel@tonic-gate 			 * to return 1 (a la what PCMCIA nexus does).
15097c478bd9Sstevel@tonic-gate 			 */
15107c478bd9Sstevel@tonic-gate 			if (strcmp(ddi_get_name(rdip), "pcic") == 0)
15117c478bd9Sstevel@tonic-gate 				*(int *)result = 1;
1512a54f81fbSanish 			else
1513a54f81fbSanish 				return (DDI_FAILURE);
15147c478bd9Sstevel@tonic-gate 		}
15157c478bd9Sstevel@tonic-gate 		break;
15167c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SUPPORTED_TYPES:
1517a54f81fbSanish 		*(int *)result = DDI_INTR_TYPE_FIXED;	/* Always ... */
15187c478bd9Sstevel@tonic-gate 		break;
15197c478bd9Sstevel@tonic-gate 	default:
15207c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
15217c478bd9Sstevel@tonic-gate 	}
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
15247c478bd9Sstevel@tonic-gate }
15257c478bd9Sstevel@tonic-gate 
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate /*
152812f080e7Smrj  * rootnex_get_ispec()
152912f080e7Smrj  *	convert an interrupt number to an interrupt specification.
153012f080e7Smrj  *	The interrupt number determines which interrupt spec will be
153112f080e7Smrj  *	returned if more than one exists.
153212f080e7Smrj  *
153312f080e7Smrj  *	Look into the parent private data area of the 'rdip' to find out
153412f080e7Smrj  *	the interrupt specification.  First check to make sure there is
153512f080e7Smrj  *	one that matchs "inumber" and then return a pointer to it.
153612f080e7Smrj  *
153712f080e7Smrj  *	Return NULL if one could not be found.
153812f080e7Smrj  *
153912f080e7Smrj  *	NOTE: This is needed for rootnex_intr_ops()
15407c478bd9Sstevel@tonic-gate  */
154112f080e7Smrj static struct intrspec *
154212f080e7Smrj rootnex_get_ispec(dev_info_t *rdip, int inum)
15437c478bd9Sstevel@tonic-gate {
154412f080e7Smrj 	struct ddi_parent_private_data *pdp = ddi_get_parent_data(rdip);
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate 	/*
154712f080e7Smrj 	 * Special case handling for drivers that provide their own
154812f080e7Smrj 	 * intrspec structures instead of relying on the DDI framework.
154912f080e7Smrj 	 *
155012f080e7Smrj 	 * A broken hardware driver in ON could potentially provide its
155112f080e7Smrj 	 * own intrspec structure, instead of relying on the hardware.
155212f080e7Smrj 	 * If these drivers are children of 'rootnex' then we need to
155312f080e7Smrj 	 * continue to provide backward compatibility to them here.
155412f080e7Smrj 	 *
155512f080e7Smrj 	 * Following check is a special case for 'pcic' driver which
155612f080e7Smrj 	 * was found to have broken hardwre andby provides its own intrspec.
155712f080e7Smrj 	 *
155812f080e7Smrj 	 * Verbatim comments from this driver are shown here:
155912f080e7Smrj 	 * "Don't use the ddi_add_intr since we don't have a
156012f080e7Smrj 	 * default intrspec in all cases."
156112f080e7Smrj 	 *
156212f080e7Smrj 	 * Since an 'ispec' may not be always created for it,
156312f080e7Smrj 	 * check for that and create one if so.
156412f080e7Smrj 	 *
156512f080e7Smrj 	 * NOTE: Currently 'pcic' is the only driver found to do this.
15667c478bd9Sstevel@tonic-gate 	 */
156712f080e7Smrj 	if (!pdp->par_intr && strcmp(ddi_get_name(rdip), "pcic") == 0) {
156812f080e7Smrj 		pdp->par_nintr = 1;
156912f080e7Smrj 		pdp->par_intr = kmem_zalloc(sizeof (struct intrspec) *
157012f080e7Smrj 		    pdp->par_nintr, KM_SLEEP);
157112f080e7Smrj 	}
157212f080e7Smrj 
157312f080e7Smrj 	/* Validate the interrupt number */
157412f080e7Smrj 	if (inum >= pdp->par_nintr)
157512f080e7Smrj 		return (NULL);
157612f080e7Smrj 
157712f080e7Smrj 	/* Get the interrupt structure pointer and return that */
157812f080e7Smrj 	return ((struct intrspec *)&pdp->par_intr[inum]);
157912f080e7Smrj }
158012f080e7Smrj 
15817ff178cdSJimmy Vetayases /*
15827ff178cdSJimmy Vetayases  * Allocate interrupt vector for FIXED (legacy) type.
15837ff178cdSJimmy Vetayases  */
15847ff178cdSJimmy Vetayases static int
15857ff178cdSJimmy Vetayases rootnex_alloc_intr_fixed(dev_info_t *rdip, ddi_intr_handle_impl_t *hdlp,
15867ff178cdSJimmy Vetayases     void *result)
15877ff178cdSJimmy Vetayases {
15887ff178cdSJimmy Vetayases 	struct intrspec		*ispec;
15897ff178cdSJimmy Vetayases 	ddi_intr_handle_impl_t	info_hdl;
15907ff178cdSJimmy Vetayases 	int			ret;
15917ff178cdSJimmy Vetayases 	int			free_phdl = 0;
15927ff178cdSJimmy Vetayases 	apic_get_type_t		type_info;
15937ff178cdSJimmy Vetayases 
15947ff178cdSJimmy Vetayases 	if (psm_intr_ops == NULL)
15957ff178cdSJimmy Vetayases 		return (DDI_FAILURE);
15967ff178cdSJimmy Vetayases 
15977ff178cdSJimmy Vetayases 	if ((ispec = rootnex_get_ispec(rdip, hdlp->ih_inum)) == NULL)
15987ff178cdSJimmy Vetayases 		return (DDI_FAILURE);
15997ff178cdSJimmy Vetayases 
16007ff178cdSJimmy Vetayases 	/*
16017ff178cdSJimmy Vetayases 	 * If the PSM module is "APIX" then pass the request for it
16027ff178cdSJimmy Vetayases 	 * to allocate the vector now.
16037ff178cdSJimmy Vetayases 	 */
16047ff178cdSJimmy Vetayases 	bzero(&info_hdl, sizeof (ddi_intr_handle_impl_t));
16057ff178cdSJimmy Vetayases 	info_hdl.ih_private = &type_info;
16067ff178cdSJimmy Vetayases 	if ((*psm_intr_ops)(NULL, &info_hdl, PSM_INTR_OP_APIC_TYPE, NULL) ==
16077ff178cdSJimmy Vetayases 	    PSM_SUCCESS && strcmp(type_info.avgi_type, APIC_APIX_NAME) == 0) {
16087ff178cdSJimmy Vetayases 		if (hdlp->ih_private == NULL) { /* allocate phdl structure */
16097ff178cdSJimmy Vetayases 			free_phdl = 1;
16107ff178cdSJimmy Vetayases 			i_ddi_alloc_intr_phdl(hdlp);
16117ff178cdSJimmy Vetayases 		}
16127ff178cdSJimmy Vetayases 		((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp = ispec;
16137ff178cdSJimmy Vetayases 		ret = (*psm_intr_ops)(rdip, hdlp,
16147ff178cdSJimmy Vetayases 		    PSM_INTR_OP_ALLOC_VECTORS, result);
16157ff178cdSJimmy Vetayases 		if (free_phdl) { /* free up the phdl structure */
16167ff178cdSJimmy Vetayases 			free_phdl = 0;
16177ff178cdSJimmy Vetayases 			i_ddi_free_intr_phdl(hdlp);
16187ff178cdSJimmy Vetayases 			hdlp->ih_private = NULL;
16197ff178cdSJimmy Vetayases 		}
16207ff178cdSJimmy Vetayases 	} else {
16217ff178cdSJimmy Vetayases 		/*
16227ff178cdSJimmy Vetayases 		 * No APIX module; fall back to the old scheme where the
16237ff178cdSJimmy Vetayases 		 * interrupt vector is allocated during ddi_enable_intr() call.
16247ff178cdSJimmy Vetayases 		 */
16257ff178cdSJimmy Vetayases 		hdlp->ih_pri = ispec->intrspec_pri;
16267ff178cdSJimmy Vetayases 		*(int *)result = hdlp->ih_scratch1;
16277ff178cdSJimmy Vetayases 		ret = DDI_SUCCESS;
16287ff178cdSJimmy Vetayases 	}
16297ff178cdSJimmy Vetayases 
16307ff178cdSJimmy Vetayases 	return (ret);
16317ff178cdSJimmy Vetayases }
16327ff178cdSJimmy Vetayases 
16337ff178cdSJimmy Vetayases /*
16347ff178cdSJimmy Vetayases  * Free up interrupt vector for FIXED (legacy) type.
16357ff178cdSJimmy Vetayases  */
16367ff178cdSJimmy Vetayases static int
16377ff178cdSJimmy Vetayases rootnex_free_intr_fixed(dev_info_t *rdip, ddi_intr_handle_impl_t *hdlp)
16387ff178cdSJimmy Vetayases {
16397ff178cdSJimmy Vetayases 	struct intrspec			*ispec;
16407ff178cdSJimmy Vetayases 	struct ddi_parent_private_data	*pdp;
16417ff178cdSJimmy Vetayases 	ddi_intr_handle_impl_t		info_hdl;
16427ff178cdSJimmy Vetayases 	int				ret;
16437ff178cdSJimmy Vetayases 	apic_get_type_t			type_info;
16447ff178cdSJimmy Vetayases 
16457ff178cdSJimmy Vetayases 	if (psm_intr_ops == NULL)
16467ff178cdSJimmy Vetayases 		return (DDI_FAILURE);
16477ff178cdSJimmy Vetayases 
16487ff178cdSJimmy Vetayases 	/*
16497ff178cdSJimmy Vetayases 	 * If the PSM module is "APIX" then pass the request for it
16507ff178cdSJimmy Vetayases 	 * to free up the vector now.
16517ff178cdSJimmy Vetayases 	 */
16527ff178cdSJimmy Vetayases 	bzero(&info_hdl, sizeof (ddi_intr_handle_impl_t));
16537ff178cdSJimmy Vetayases 	info_hdl.ih_private = &type_info;
16547ff178cdSJimmy Vetayases 	if ((*psm_intr_ops)(NULL, &info_hdl, PSM_INTR_OP_APIC_TYPE, NULL) ==
16557ff178cdSJimmy Vetayases 	    PSM_SUCCESS && strcmp(type_info.avgi_type, APIC_APIX_NAME) == 0) {
16567ff178cdSJimmy Vetayases 		if ((ispec = rootnex_get_ispec(rdip, hdlp->ih_inum)) == NULL)
16577ff178cdSJimmy Vetayases 			return (DDI_FAILURE);
16587ff178cdSJimmy Vetayases 		((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp = ispec;
16597ff178cdSJimmy Vetayases 		ret = (*psm_intr_ops)(rdip, hdlp,
16607ff178cdSJimmy Vetayases 		    PSM_INTR_OP_FREE_VECTORS, NULL);
16617ff178cdSJimmy Vetayases 	} else {
16627ff178cdSJimmy Vetayases 		/*
16637ff178cdSJimmy Vetayases 		 * No APIX module; fall back to the old scheme where
16647ff178cdSJimmy Vetayases 		 * the interrupt vector was already freed during
16657ff178cdSJimmy Vetayases 		 * ddi_disable_intr() call.
16667ff178cdSJimmy Vetayases 		 */
16677ff178cdSJimmy Vetayases 		ret = DDI_SUCCESS;
16687ff178cdSJimmy Vetayases 	}
16697ff178cdSJimmy Vetayases 
16707ff178cdSJimmy Vetayases 	pdp = ddi_get_parent_data(rdip);
16717ff178cdSJimmy Vetayases 
16727ff178cdSJimmy Vetayases 	/*
16737ff178cdSJimmy Vetayases 	 * Special case for 'pcic' driver' only.
16747ff178cdSJimmy Vetayases 	 * If an intrspec was created for it, clean it up here
16757ff178cdSJimmy Vetayases 	 * See detailed comments on this in the function
16767ff178cdSJimmy Vetayases 	 * rootnex_get_ispec().
16777ff178cdSJimmy Vetayases 	 */
16787ff178cdSJimmy Vetayases 	if (pdp->par_intr && strcmp(ddi_get_name(rdip), "pcic") == 0) {
16797ff178cdSJimmy Vetayases 		kmem_free(pdp->par_intr, sizeof (struct intrspec) *
16807ff178cdSJimmy Vetayases 		    pdp->par_nintr);
16817ff178cdSJimmy Vetayases 		/*
16827ff178cdSJimmy Vetayases 		 * Set it to zero; so that
16837ff178cdSJimmy Vetayases 		 * DDI framework doesn't free it again
16847ff178cdSJimmy Vetayases 		 */
16857ff178cdSJimmy Vetayases 		pdp->par_intr = NULL;
16867ff178cdSJimmy Vetayases 		pdp->par_nintr = 0;
16877ff178cdSJimmy Vetayases 	}
16887ff178cdSJimmy Vetayases 
16897ff178cdSJimmy Vetayases 	return (ret);
16907ff178cdSJimmy Vetayases }
16917ff178cdSJimmy Vetayases 
169212f080e7Smrj 
169312f080e7Smrj /*
169412f080e7Smrj  * ******************
169512f080e7Smrj  *  dma related code
169612f080e7Smrj  * ******************
169712f080e7Smrj  */
169812f080e7Smrj 
169912f080e7Smrj /*ARGSUSED*/
170012f080e7Smrj static int
170120906b23SVikram Hegde rootnex_coredma_allochdl(dev_info_t *dip, dev_info_t *rdip,
170220906b23SVikram Hegde     ddi_dma_attr_t *attr, int (*waitfp)(caddr_t), caddr_t arg,
170320906b23SVikram Hegde     ddi_dma_handle_t *handlep)
170412f080e7Smrj {
170512f080e7Smrj 	uint64_t maxsegmentsize_ll;
170612f080e7Smrj 	uint_t maxsegmentsize;
170712f080e7Smrj 	ddi_dma_impl_t *hp;
170812f080e7Smrj 	rootnex_dma_t *dma;
170912f080e7Smrj 	uint64_t count_max;
171012f080e7Smrj 	uint64_t seg;
171112f080e7Smrj 	int kmflag;
171212f080e7Smrj 	int e;
171312f080e7Smrj 
171412f080e7Smrj 
171512f080e7Smrj 	/* convert our sleep flags */
171612f080e7Smrj 	if (waitfp == DDI_DMA_SLEEP) {
171712f080e7Smrj 		kmflag = KM_SLEEP;
171812f080e7Smrj 	} else {
171912f080e7Smrj 		kmflag = KM_NOSLEEP;
172012f080e7Smrj 	}
172112f080e7Smrj 
172212f080e7Smrj 	/*
172312f080e7Smrj 	 * We try to do only one memory allocation here. We'll do a little
172412f080e7Smrj 	 * pointer manipulation later. If the bind ends up taking more than
172512f080e7Smrj 	 * our prealloc's space, we'll have to allocate more memory in the
172612f080e7Smrj 	 * bind operation. Not great, but much better than before and the
172712f080e7Smrj 	 * best we can do with the current bind interfaces.
172812f080e7Smrj 	 */
172912f080e7Smrj 	hp = kmem_cache_alloc(rootnex_state->r_dmahdl_cache, kmflag);
173012f080e7Smrj 	if (hp == NULL) {
173112f080e7Smrj 		if (waitfp != DDI_DMA_DONTWAIT) {
173212f080e7Smrj 			ddi_set_callback(waitfp, arg,
173312f080e7Smrj 			    &rootnex_state->r_dvma_call_list_id);
173412f080e7Smrj 		}
173512f080e7Smrj 		return (DDI_DMA_NORESOURCES);
173612f080e7Smrj 	}
173712f080e7Smrj 
173812f080e7Smrj 	/* Do our pointer manipulation now, align the structures */
173912f080e7Smrj 	hp->dmai_private = (void *)(((uintptr_t)hp +
174012f080e7Smrj 	    (uintptr_t)sizeof (ddi_dma_impl_t) + 0x7) & ~0x7);
174112f080e7Smrj 	dma = (rootnex_dma_t *)hp->dmai_private;
174212f080e7Smrj 	dma->dp_prealloc_buffer = (uchar_t *)(((uintptr_t)dma +
174312f080e7Smrj 	    sizeof (rootnex_dma_t) + 0x7) & ~0x7);
174412f080e7Smrj 
174512f080e7Smrj 	/* setup the handle */
174612f080e7Smrj 	rootnex_clean_dmahdl(hp);
1747567c0b92SStephen Hanson 	hp->dmai_error.err_fep = NULL;
1748567c0b92SStephen Hanson 	hp->dmai_error.err_cf = NULL;
174912f080e7Smrj 	dma->dp_dip = rdip;
175012f080e7Smrj 	dma->dp_sglinfo.si_min_addr = attr->dma_attr_addr_lo;
175112f080e7Smrj 	dma->dp_sglinfo.si_max_addr = attr->dma_attr_addr_hi;
175212f080e7Smrj 	hp->dmai_minxfer = attr->dma_attr_minxfer;
175312f080e7Smrj 	hp->dmai_burstsizes = attr->dma_attr_burstsizes;
175412f080e7Smrj 	hp->dmai_rdip = rdip;
175512f080e7Smrj 	hp->dmai_attr = *attr;
175612f080e7Smrj 
175712f080e7Smrj 	/* we don't need to worry about the SPL since we do a tryenter */
175812f080e7Smrj 	mutex_init(&dma->dp_mutex, NULL, MUTEX_DRIVER, NULL);
175912f080e7Smrj 
176012f080e7Smrj 	/*
176112f080e7Smrj 	 * Figure out our maximum segment size. If the segment size is greater
176212f080e7Smrj 	 * than 4G, we will limit it to (4G - 1) since the max size of a dma
176312f080e7Smrj 	 * object (ddi_dma_obj_t.dmao_size) is 32 bits. dma_attr_seg and
176412f080e7Smrj 	 * dma_attr_count_max are size-1 type values.
176512f080e7Smrj 	 *
176612f080e7Smrj 	 * Maximum segment size is the largest physically contiguous chunk of
176712f080e7Smrj 	 * memory that we can return from a bind (i.e. the maximum size of a
176812f080e7Smrj 	 * single cookie).
176912f080e7Smrj 	 */
177012f080e7Smrj 
177112f080e7Smrj 	/* handle the rollover cases */
177212f080e7Smrj 	seg = attr->dma_attr_seg + 1;
177312f080e7Smrj 	if (seg < attr->dma_attr_seg) {
177412f080e7Smrj 		seg = attr->dma_attr_seg;
177512f080e7Smrj 	}
177612f080e7Smrj 	count_max = attr->dma_attr_count_max + 1;
177712f080e7Smrj 	if (count_max < attr->dma_attr_count_max) {
177812f080e7Smrj 		count_max = attr->dma_attr_count_max;
177912f080e7Smrj 	}
178012f080e7Smrj 
178112f080e7Smrj 	/*
178212f080e7Smrj 	 * granularity may or may not be a power of two. If it isn't, we can't
178312f080e7Smrj 	 * use a simple mask.
178412f080e7Smrj 	 */
178512f080e7Smrj 	if (attr->dma_attr_granular & (attr->dma_attr_granular - 1)) {
178612f080e7Smrj 		dma->dp_granularity_power_2 = B_FALSE;
178712f080e7Smrj 	} else {
178812f080e7Smrj 		dma->dp_granularity_power_2 = B_TRUE;
178912f080e7Smrj 	}
179012f080e7Smrj 
179112f080e7Smrj 	/*
179212f080e7Smrj 	 * maxxfer should be a whole multiple of granularity. If we're going to
179312f080e7Smrj 	 * break up a window because we're greater than maxxfer, we might as
179412f080e7Smrj 	 * well make sure it's maxxfer is a whole multiple so we don't have to
179512f080e7Smrj 	 * worry about triming the window later on for this case.
179612f080e7Smrj 	 */
179712f080e7Smrj 	if (attr->dma_attr_granular > 1) {
179812f080e7Smrj 		if (dma->dp_granularity_power_2) {
179912f080e7Smrj 			dma->dp_maxxfer = attr->dma_attr_maxxfer -
180012f080e7Smrj 			    (attr->dma_attr_maxxfer &
180112f080e7Smrj 			    (attr->dma_attr_granular - 1));
180212f080e7Smrj 		} else {
180312f080e7Smrj 			dma->dp_maxxfer = attr->dma_attr_maxxfer -
180412f080e7Smrj 			    (attr->dma_attr_maxxfer % attr->dma_attr_granular);
180512f080e7Smrj 		}
180612f080e7Smrj 	} else {
180712f080e7Smrj 		dma->dp_maxxfer = attr->dma_attr_maxxfer;
180812f080e7Smrj 	}
180912f080e7Smrj 
181012f080e7Smrj 	maxsegmentsize_ll = MIN(seg, dma->dp_maxxfer);
181112f080e7Smrj 	maxsegmentsize_ll = MIN(maxsegmentsize_ll, count_max);
181212f080e7Smrj 	if (maxsegmentsize_ll == 0 || (maxsegmentsize_ll > 0xFFFFFFFF)) {
181312f080e7Smrj 		maxsegmentsize = 0xFFFFFFFF;
181412f080e7Smrj 	} else {
181512f080e7Smrj 		maxsegmentsize = maxsegmentsize_ll;
181612f080e7Smrj 	}
181712f080e7Smrj 	dma->dp_sglinfo.si_max_cookie_size = maxsegmentsize;
181812f080e7Smrj 	dma->dp_sglinfo.si_segmask = attr->dma_attr_seg;
181907c6692fSMark Johnson 	dma->dp_sglinfo.si_flags = attr->dma_attr_flags;
182012f080e7Smrj 
182112f080e7Smrj 	/* check the ddi_dma_attr arg to make sure it makes a little sense */
182212f080e7Smrj 	if (rootnex_alloc_check_parms) {
182312f080e7Smrj 		e = rootnex_valid_alloc_parms(attr, maxsegmentsize);
182412f080e7Smrj 		if (e != DDI_SUCCESS) {
1825*ef4ab52fSFrank Van Der Linden 			ROOTNEX_DPROF_INC(&rootnex_cnt[ROOTNEX_CNT_ALLOC_FAIL]);
182612f080e7Smrj 			(void) rootnex_dma_freehdl(dip, rdip,
182712f080e7Smrj 			    (ddi_dma_handle_t)hp);
182812f080e7Smrj 			return (e);
182912f080e7Smrj 		}
183012f080e7Smrj 	}
183112f080e7Smrj 
183212f080e7Smrj 	*handlep = (ddi_dma_handle_t)hp;
183312f080e7Smrj 
18340b7ba611SMark Johnson 	ROOTNEX_DPROF_INC(&rootnex_cnt[ROOTNEX_CNT_ACTIVE_HDLS]);
18350b7ba611SMark Johnson 	ROOTNEX_DPROBE1(rootnex__alloc__handle, uint64_t,
183612f080e7Smrj 	    rootnex_cnt[ROOTNEX_CNT_ACTIVE_HDLS]);
183712f080e7Smrj 
183812f080e7Smrj 	return (DDI_SUCCESS);
183912f080e7Smrj }
184012f080e7Smrj 
184112f080e7Smrj 
184212f080e7Smrj /*
184320906b23SVikram Hegde  * rootnex_dma_allochdl()
184420906b23SVikram Hegde  *    called from ddi_dma_alloc_handle().
184512f080e7Smrj  */
184620906b23SVikram Hegde static int
184720906b23SVikram Hegde rootnex_dma_allochdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_attr_t *attr,
184820906b23SVikram Hegde     int (*waitfp)(caddr_t), caddr_t arg, ddi_dma_handle_t *handlep)
184920906b23SVikram Hegde {
1850567c0b92SStephen Hanson 	int retval;
18513a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
185220906b23SVikram Hegde 	uint_t error = ENOTSUP;
185320906b23SVikram Hegde 
185420906b23SVikram Hegde 	retval = iommulib_nex_open(rdip, &error);
185520906b23SVikram Hegde 
185620906b23SVikram Hegde 	if (retval != DDI_SUCCESS && error == ENOTSUP) {
185720906b23SVikram Hegde 		/* No IOMMU */
185820906b23SVikram Hegde 		return (rootnex_coredma_allochdl(dip, rdip, attr, waitfp, arg,
185920906b23SVikram Hegde 		    handlep));
186020906b23SVikram Hegde 	} else if (retval != DDI_SUCCESS) {
186120906b23SVikram Hegde 		return (DDI_FAILURE);
186220906b23SVikram Hegde 	}
186320906b23SVikram Hegde 
1864b51bbbf5SVikram Hegde 	ASSERT(IOMMU_USED(rdip));
186520906b23SVikram Hegde 
186620906b23SVikram Hegde 	/* has an IOMMU */
1867567c0b92SStephen Hanson 	retval = iommulib_nexdma_allochdl(dip, rdip, attr,
1868567c0b92SStephen Hanson 	    waitfp, arg, handlep);
186920906b23SVikram Hegde #else
1870567c0b92SStephen Hanson 	retval = rootnex_coredma_allochdl(dip, rdip, attr, waitfp, arg,
1871567c0b92SStephen Hanson 	    handlep);
187220906b23SVikram Hegde #endif
1873567c0b92SStephen Hanson 	if (retval == DDI_SUCCESS)
1874567c0b92SStephen Hanson 		ndi_fmc_insert(rdip, DMA_HANDLE, *handlep, NULL);
1875567c0b92SStephen Hanson 	return (retval);
187620906b23SVikram Hegde }
187720906b23SVikram Hegde 
187812f080e7Smrj /*ARGSUSED*/
187912f080e7Smrj static int
188020906b23SVikram Hegde rootnex_coredma_freehdl(dev_info_t *dip, dev_info_t *rdip,
188120906b23SVikram Hegde     ddi_dma_handle_t handle)
188212f080e7Smrj {
188312f080e7Smrj 	ddi_dma_impl_t *hp;
188412f080e7Smrj 	rootnex_dma_t *dma;
188512f080e7Smrj 
188612f080e7Smrj 
188712f080e7Smrj 	hp = (ddi_dma_impl_t *)handle;
188812f080e7Smrj 	dma = (rootnex_dma_t *)hp->dmai_private;
188912f080e7Smrj 
189012f080e7Smrj 	/* unbind should have been called first */
189112f080e7Smrj 	ASSERT(!dma->dp_inuse);
189212f080e7Smrj 
189312f080e7Smrj 	mutex_destroy(&dma->dp_mutex);
189412f080e7Smrj 	kmem_cache_free(rootnex_state->r_dmahdl_cache, hp);
189512f080e7Smrj 
18960b7ba611SMark Johnson 	ROOTNEX_DPROF_DEC(&rootnex_cnt[ROOTNEX_CNT_ACTIVE_HDLS]);
18970b7ba611SMark Johnson 	ROOTNEX_DPROBE1(rootnex__free__handle, uint64_t,
189812f080e7Smrj 	    rootnex_cnt[ROOTNEX_CNT_ACTIVE_HDLS]);
189912f080e7Smrj 
190012f080e7Smrj 	if (rootnex_state->r_dvma_call_list_id)
190112f080e7Smrj 		ddi_run_callback(&rootnex_state->r_dvma_call_list_id);
190212f080e7Smrj 
190312f080e7Smrj 	return (DDI_SUCCESS);
190412f080e7Smrj }
190512f080e7Smrj 
190612f080e7Smrj /*
190720906b23SVikram Hegde  * rootnex_dma_freehdl()
190820906b23SVikram Hegde  *    called from ddi_dma_free_handle().
190912f080e7Smrj  */
191020906b23SVikram Hegde static int
191120906b23SVikram Hegde rootnex_dma_freehdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle)
191220906b23SVikram Hegde {
1913567c0b92SStephen Hanson 	ndi_fmc_remove(rdip, DMA_HANDLE, handle);
19143a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
1915b51bbbf5SVikram Hegde 	if (IOMMU_USED(rdip)) {
191620906b23SVikram Hegde 		return (iommulib_nexdma_freehdl(dip, rdip, handle));
191720906b23SVikram Hegde 	}
191820906b23SVikram Hegde #endif
191920906b23SVikram Hegde 	return (rootnex_coredma_freehdl(dip, rdip, handle));
192020906b23SVikram Hegde }
192120906b23SVikram Hegde 
192212f080e7Smrj /*ARGSUSED*/
192312f080e7Smrj static int
192420906b23SVikram Hegde rootnex_coredma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
192520906b23SVikram Hegde     ddi_dma_handle_t handle, struct ddi_dma_req *dmareq,
192620906b23SVikram Hegde     ddi_dma_cookie_t *cookiep, uint_t *ccountp)
192712f080e7Smrj {
192812f080e7Smrj 	rootnex_sglinfo_t *sinfo;
192912f080e7Smrj 	ddi_dma_attr_t *attr;
193012f080e7Smrj 	ddi_dma_impl_t *hp;
193112f080e7Smrj 	rootnex_dma_t *dma;
193212f080e7Smrj 	int kmflag;
193312f080e7Smrj 	int e;
193412f080e7Smrj 
193512f080e7Smrj 	hp = (ddi_dma_impl_t *)handle;
193612f080e7Smrj 	dma = (rootnex_dma_t *)hp->dmai_private;
193712f080e7Smrj 	sinfo = &dma->dp_sglinfo;
193812f080e7Smrj 	attr = &hp->dmai_attr;
193912f080e7Smrj 
194094f1124eSVikram Hegde 	if (dmareq->dmar_fp == DDI_DMA_SLEEP) {
194194f1124eSVikram Hegde 		dma->dp_sleep_flags = KM_SLEEP;
194294f1124eSVikram Hegde 	} else {
194394f1124eSVikram Hegde 		dma->dp_sleep_flags = KM_NOSLEEP;
194494f1124eSVikram Hegde 	}
194594f1124eSVikram Hegde 
194612f080e7Smrj 	hp->dmai_rflags = dmareq->dmar_flags & DMP_DDIFLAGS;
194712f080e7Smrj 
194812f080e7Smrj 	/*
194912f080e7Smrj 	 * This is useful for debugging a driver. Not as useful in a production
195012f080e7Smrj 	 * system. The only time this will fail is if you have a driver bug.
195112f080e7Smrj 	 */
195212f080e7Smrj 	if (rootnex_bind_check_inuse) {
195312f080e7Smrj 		/*
195412f080e7Smrj 		 * No one else should ever have this lock unless someone else
195512f080e7Smrj 		 * is trying to use this handle. So contention on the lock
195612f080e7Smrj 		 * is the same as inuse being set.
195712f080e7Smrj 		 */
195812f080e7Smrj 		e = mutex_tryenter(&dma->dp_mutex);
195912f080e7Smrj 		if (e == 0) {
1960*ef4ab52fSFrank Van Der Linden 			ROOTNEX_DPROF_INC(&rootnex_cnt[ROOTNEX_CNT_BIND_FAIL]);
196112f080e7Smrj 			return (DDI_DMA_INUSE);
196212f080e7Smrj 		}
196312f080e7Smrj 		if (dma->dp_inuse) {
196412f080e7Smrj 			mutex_exit(&dma->dp_mutex);
1965*ef4ab52fSFrank Van Der Linden 			ROOTNEX_DPROF_INC(&rootnex_cnt[ROOTNEX_CNT_BIND_FAIL]);
196612f080e7Smrj 			return (DDI_DMA_INUSE);
196712f080e7Smrj 		}
196812f080e7Smrj 		dma->dp_inuse = B_TRUE;
196912f080e7Smrj 		mutex_exit(&dma->dp_mutex);
197012f080e7Smrj 	}
197112f080e7Smrj 
197212f080e7Smrj 	/* check the ddi_dma_attr arg to make sure it makes a little sense */
197312f080e7Smrj 	if (rootnex_bind_check_parms) {
197412f080e7Smrj 		e = rootnex_valid_bind_parms(dmareq, attr);
197512f080e7Smrj 		if (e != DDI_SUCCESS) {
1976*ef4ab52fSFrank Van Der Linden 			ROOTNEX_DPROF_INC(&rootnex_cnt[ROOTNEX_CNT_BIND_FAIL]);
197712f080e7Smrj 			rootnex_clean_dmahdl(hp);
197812f080e7Smrj 			return (e);
197912f080e7Smrj 		}
198012f080e7Smrj 	}
198112f080e7Smrj 
198212f080e7Smrj 	/* save away the original bind info */
198312f080e7Smrj 	dma->dp_dma = dmareq->dmar_object;
198412f080e7Smrj 
19853a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
19863a634bfcSVikram Hegde 	e = immu_map_sgl(hp, dmareq, rootnex_prealloc_cookies, rdip);
198786c1f4dcSVikram Hegde 	switch (e) {
19883a634bfcSVikram Hegde 	case DDI_DMA_MAPPED:
19893a634bfcSVikram Hegde 		goto out;
19903a634bfcSVikram Hegde 	case DDI_DMA_USE_PHYSICAL:
19913a634bfcSVikram Hegde 		break;
19923a634bfcSVikram Hegde 	case DDI_DMA_PARTIAL:
19933a634bfcSVikram Hegde 		ddi_err(DER_PANIC, rdip, "Partial DVMA map");
19943a634bfcSVikram Hegde 		e = DDI_DMA_NORESOURCES;
19953a634bfcSVikram Hegde 		/*FALLTHROUGH*/
199686c1f4dcSVikram Hegde 	default:
19973a634bfcSVikram Hegde 		ddi_err(DER_MODE, rdip, "DVMA map failed");
1998*ef4ab52fSFrank Van Der Linden 		ROOTNEX_DPROF_INC(&rootnex_cnt[ROOTNEX_CNT_BIND_FAIL]);
199986c1f4dcSVikram Hegde 		rootnex_clean_dmahdl(hp);
20003a634bfcSVikram Hegde 		return (e);
200186c1f4dcSVikram Hegde 	}
200220906b23SVikram Hegde #endif
200386c1f4dcSVikram Hegde 
200412f080e7Smrj 	/*
200512f080e7Smrj 	 * Figure out a rough estimate of what maximum number of pages this
200612f080e7Smrj 	 * buffer could use (a high estimate of course).
200712f080e7Smrj 	 */
200812f080e7Smrj 	sinfo->si_max_pages = mmu_btopr(dma->dp_dma.dmao_size) + 1;
200912f080e7Smrj 
201012f080e7Smrj 	/*
201112f080e7Smrj 	 * We'll use the pre-allocated cookies for any bind that will *always*
201212f080e7Smrj 	 * fit (more important to be consistent, we don't want to create
201312f080e7Smrj 	 * additional degenerate cases).
201412f080e7Smrj 	 */
201512f080e7Smrj 	if (sinfo->si_max_pages <= rootnex_state->r_prealloc_cookies) {
201612f080e7Smrj 		dma->dp_cookies = (ddi_dma_cookie_t *)dma->dp_prealloc_buffer;
201712f080e7Smrj 		dma->dp_need_to_free_cookie = B_FALSE;
2018*ef4ab52fSFrank Van Der Linden 		ROOTNEX_DPROBE2(rootnex__bind__prealloc, dev_info_t *, rdip,
201912f080e7Smrj 		    uint_t, sinfo->si_max_pages);
202012f080e7Smrj 
202112f080e7Smrj 	/*
202212f080e7Smrj 	 * For anything larger than that, we'll go ahead and allocate the
202312f080e7Smrj 	 * maximum number of pages we expect to see. Hopefuly, we won't be
202412f080e7Smrj 	 * seeing this path in the fast path for high performance devices very
202512f080e7Smrj 	 * frequently.
202612f080e7Smrj 	 *
202712f080e7Smrj 	 * a ddi bind interface that allowed the driver to provide storage to
202812f080e7Smrj 	 * the bind interface would speed this case up.
202912f080e7Smrj 	 */
203012f080e7Smrj 	} else {
203112f080e7Smrj 		/* convert the sleep flags */
203212f080e7Smrj 		if (dmareq->dmar_fp == DDI_DMA_SLEEP) {
203312f080e7Smrj 			kmflag =  KM_SLEEP;
203412f080e7Smrj 		} else {
203512f080e7Smrj 			kmflag =  KM_NOSLEEP;
203612f080e7Smrj 		}
203712f080e7Smrj 
203812f080e7Smrj 		/*
203912f080e7Smrj 		 * Save away how much memory we allocated. If we're doing a
204012f080e7Smrj 		 * nosleep, the alloc could fail...
204112f080e7Smrj 		 */
204212f080e7Smrj 		dma->dp_cookie_size = sinfo->si_max_pages *
204312f080e7Smrj 		    sizeof (ddi_dma_cookie_t);
204412f080e7Smrj 		dma->dp_cookies = kmem_alloc(dma->dp_cookie_size, kmflag);
204512f080e7Smrj 		if (dma->dp_cookies == NULL) {
2046*ef4ab52fSFrank Van Der Linden 			ROOTNEX_DPROF_INC(&rootnex_cnt[ROOTNEX_CNT_BIND_FAIL]);
204712f080e7Smrj 			rootnex_clean_dmahdl(hp);
204812f080e7Smrj 			return (DDI_DMA_NORESOURCES);
204912f080e7Smrj 		}
205012f080e7Smrj 		dma->dp_need_to_free_cookie = B_TRUE;
2051*ef4ab52fSFrank Van Der Linden 		ROOTNEX_DPROBE2(rootnex__bind__alloc, dev_info_t *, rdip,
2052*ef4ab52fSFrank Van Der Linden 		    uint_t, sinfo->si_max_pages);
205312f080e7Smrj 	}
205412f080e7Smrj 	hp->dmai_cookie = dma->dp_cookies;
205512f080e7Smrj 
205612f080e7Smrj 	/*
205712f080e7Smrj 	 * Get the real sgl. rootnex_get_sgl will fill in cookie array while
20583a634bfcSVikram Hegde 	 * looking at the constraints in the dma structure. It will then put
20593a634bfcSVikram Hegde 	 * some additional state about the sgl in the dma struct (i.e. is
20603a634bfcSVikram Hegde 	 * the sgl clean, or do we need to do some munging; how many pages
20613a634bfcSVikram Hegde 	 * need to be copied, etc.)
206212f080e7Smrj 	 */
206312f080e7Smrj 	rootnex_get_sgl(&dmareq->dmar_object, dma->dp_cookies,
206412f080e7Smrj 	    &dma->dp_sglinfo);
206512f080e7Smrj 
20663a634bfcSVikram Hegde out:
206786c1f4dcSVikram Hegde 	ASSERT(sinfo->si_sgl_size <= sinfo->si_max_pages);
206812f080e7Smrj 	/* if we don't need a copy buffer, we don't need to sync */
206912f080e7Smrj 	if (sinfo->si_copybuf_req == 0) {
207012f080e7Smrj 		hp->dmai_rflags |= DMP_NOSYNC;
207112f080e7Smrj 	}
207212f080e7Smrj 
207312f080e7Smrj 	/*
207412f080e7Smrj 	 * if we don't need the copybuf and we don't need to do a partial,  we
207512f080e7Smrj 	 * hit the fast path. All the high performance devices should be trying
207612f080e7Smrj 	 * to hit this path. To hit this path, a device should be able to reach
207712f080e7Smrj 	 * all of memory, shouldn't try to bind more than it can transfer, and
207812f080e7Smrj 	 * the buffer shouldn't require more cookies than the driver/device can
207912f080e7Smrj 	 * handle [sgllen]).
208012f080e7Smrj 	 */
208112f080e7Smrj 	if ((sinfo->si_copybuf_req == 0) &&
208212f080e7Smrj 	    (sinfo->si_sgl_size <= attr->dma_attr_sgllen) &&
208312f080e7Smrj 	    (dma->dp_dma.dmao_size < dma->dp_maxxfer)) {
208412f080e7Smrj 		/*
208585c8e0e8Sstephh 		 * If the driver supports FMA, insert the handle in the FMA DMA
208685c8e0e8Sstephh 		 * handle cache.
208785c8e0e8Sstephh 		 */
2088567c0b92SStephen Hanson 		if (attr->dma_attr_flags & DDI_DMA_FLAGERR)
208985c8e0e8Sstephh 			hp->dmai_error.err_cf = rootnex_dma_check;
209085c8e0e8Sstephh 
209185c8e0e8Sstephh 		/*
209212f080e7Smrj 		 * copy out the first cookie and ccountp, set the cookie
209312f080e7Smrj 		 * pointer to the second cookie. The first cookie is passed
209412f080e7Smrj 		 * back on the stack. Additional cookies are accessed via
209512f080e7Smrj 		 * ddi_dma_nextcookie()
209612f080e7Smrj 		 */
209712f080e7Smrj 		*cookiep = dma->dp_cookies[0];
209812f080e7Smrj 		*ccountp = sinfo->si_sgl_size;
209912f080e7Smrj 		hp->dmai_cookie++;
210012f080e7Smrj 		hp->dmai_rflags &= ~DDI_DMA_PARTIAL;
2101*ef4ab52fSFrank Van Der Linden 		ROOTNEX_DPROF_INC(&rootnex_cnt[ROOTNEX_CNT_ACTIVE_BINDS]);
2102*ef4ab52fSFrank Van Der Linden 		ROOTNEX_DPROBE3(rootnex__bind__fast, dev_info_t *, rdip,
21033a634bfcSVikram Hegde 		    uint64_t, rootnex_cnt[ROOTNEX_CNT_ACTIVE_BINDS],
21043a634bfcSVikram Hegde 		    uint_t, dma->dp_dma.dmao_size);
21053a634bfcSVikram Hegde 
21063a634bfcSVikram Hegde 
210712f080e7Smrj 		return (DDI_DMA_MAPPED);
210812f080e7Smrj 	}
210912f080e7Smrj 
211012f080e7Smrj 	/*
211112f080e7Smrj 	 * go to the slow path, we may need to alloc more memory, create
211212f080e7Smrj 	 * multiple windows, and munge up a sgl to make the device happy.
211312f080e7Smrj 	 */
211412f080e7Smrj 	e = rootnex_bind_slowpath(hp, dmareq, dma, attr, kmflag);
211512f080e7Smrj 	if ((e != DDI_DMA_MAPPED) && (e != DDI_DMA_PARTIAL_MAP)) {
211612f080e7Smrj 		if (dma->dp_need_to_free_cookie) {
211712f080e7Smrj 			kmem_free(dma->dp_cookies, dma->dp_cookie_size);
211812f080e7Smrj 		}
2119*ef4ab52fSFrank Van Der Linden 		ROOTNEX_DPROF_INC(&rootnex_cnt[ROOTNEX_CNT_BIND_FAIL]);
212012f080e7Smrj 		rootnex_clean_dmahdl(hp); /* must be after free cookie */
212112f080e7Smrj 		return (e);
212212f080e7Smrj 	}
212312f080e7Smrj 
212485c8e0e8Sstephh 	/*
212585c8e0e8Sstephh 	 * If the driver supports FMA, insert the handle in the FMA DMA handle
212685c8e0e8Sstephh 	 * cache.
212785c8e0e8Sstephh 	 */
2128567c0b92SStephen Hanson 	if (attr->dma_attr_flags & DDI_DMA_FLAGERR)
212985c8e0e8Sstephh 		hp->dmai_error.err_cf = rootnex_dma_check;
213085c8e0e8Sstephh 
213112f080e7Smrj 	/* if the first window uses the copy buffer, sync it for the device */
213212f080e7Smrj 	if ((dma->dp_window[dma->dp_current_win].wd_dosync) &&
213312f080e7Smrj 	    (hp->dmai_rflags & DDI_DMA_WRITE)) {
213494f1124eSVikram Hegde 		(void) rootnex_coredma_sync(dip, rdip, handle, 0, 0,
213512f080e7Smrj 		    DDI_DMA_SYNC_FORDEV);
213612f080e7Smrj 	}
213712f080e7Smrj 
213812f080e7Smrj 	/*
213912f080e7Smrj 	 * copy out the first cookie and ccountp, set the cookie pointer to the
214012f080e7Smrj 	 * second cookie. Make sure the partial flag is set/cleared correctly.
214112f080e7Smrj 	 * If we have a partial map (i.e. multiple windows), the number of
214212f080e7Smrj 	 * cookies we return is the number of cookies in the first window.
214312f080e7Smrj 	 */
214412f080e7Smrj 	if (e == DDI_DMA_MAPPED) {
214512f080e7Smrj 		hp->dmai_rflags &= ~DDI_DMA_PARTIAL;
214612f080e7Smrj 		*ccountp = sinfo->si_sgl_size;
21473a634bfcSVikram Hegde 		hp->dmai_nwin = 1;
214812f080e7Smrj 	} else {
214912f080e7Smrj 		hp->dmai_rflags |= DDI_DMA_PARTIAL;
215012f080e7Smrj 		*ccountp = dma->dp_window[dma->dp_current_win].wd_cookie_cnt;
215112f080e7Smrj 		ASSERT(hp->dmai_nwin <= dma->dp_max_win);
215212f080e7Smrj 	}
215312f080e7Smrj 	*cookiep = dma->dp_cookies[0];
215412f080e7Smrj 	hp->dmai_cookie++;
215512f080e7Smrj 
21560b7ba611SMark Johnson 	ROOTNEX_DPROF_INC(&rootnex_cnt[ROOTNEX_CNT_ACTIVE_BINDS]);
21570b7ba611SMark Johnson 	ROOTNEX_DPROBE3(rootnex__bind__slow, dev_info_t *, rdip, uint64_t,
215812f080e7Smrj 	    rootnex_cnt[ROOTNEX_CNT_ACTIVE_BINDS], uint_t,
215912f080e7Smrj 	    dma->dp_dma.dmao_size);
216012f080e7Smrj 	return (e);
216112f080e7Smrj }
216212f080e7Smrj 
216312f080e7Smrj /*
216420906b23SVikram Hegde  * rootnex_dma_bindhdl()
216520906b23SVikram Hegde  *    called from ddi_dma_addr_bind_handle() and ddi_dma_buf_bind_handle().
216612f080e7Smrj  */
216720906b23SVikram Hegde static int
216820906b23SVikram Hegde rootnex_dma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
216920906b23SVikram Hegde     ddi_dma_handle_t handle, struct ddi_dma_req *dmareq,
217020906b23SVikram Hegde     ddi_dma_cookie_t *cookiep, uint_t *ccountp)
217120906b23SVikram Hegde {
21723a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
2173b51bbbf5SVikram Hegde 	if (IOMMU_USED(rdip)) {
217420906b23SVikram Hegde 		return (iommulib_nexdma_bindhdl(dip, rdip, handle, dmareq,
217520906b23SVikram Hegde 		    cookiep, ccountp));
217620906b23SVikram Hegde 	}
217720906b23SVikram Hegde #endif
217820906b23SVikram Hegde 	return (rootnex_coredma_bindhdl(dip, rdip, handle, dmareq,
217920906b23SVikram Hegde 	    cookiep, ccountp));
218020906b23SVikram Hegde }
218120906b23SVikram Hegde 
21823a634bfcSVikram Hegde 
21833a634bfcSVikram Hegde 
218412f080e7Smrj /*ARGSUSED*/
218512f080e7Smrj static int
218620906b23SVikram Hegde rootnex_coredma_unbindhdl(dev_info_t *dip, dev_info_t *rdip,
218712f080e7Smrj     ddi_dma_handle_t handle)
218812f080e7Smrj {
218912f080e7Smrj 	ddi_dma_impl_t *hp;
219012f080e7Smrj 	rootnex_dma_t *dma;
219112f080e7Smrj 	int e;
219212f080e7Smrj 
219312f080e7Smrj 	hp = (ddi_dma_impl_t *)handle;
219412f080e7Smrj 	dma = (rootnex_dma_t *)hp->dmai_private;
219512f080e7Smrj 
219612f080e7Smrj 	/* make sure the buffer wasn't free'd before calling unbind */
219712f080e7Smrj 	if (rootnex_unbind_verify_buffer) {
219812f080e7Smrj 		e = rootnex_verify_buffer(dma);
219912f080e7Smrj 		if (e != DDI_SUCCESS) {
220012f080e7Smrj 			ASSERT(0);
220112f080e7Smrj 			return (DDI_FAILURE);
220212f080e7Smrj 		}
220312f080e7Smrj 	}
220412f080e7Smrj 
220512f080e7Smrj 	/* sync the current window before unbinding the buffer */
220612f080e7Smrj 	if (dma->dp_window && dma->dp_window[dma->dp_current_win].wd_dosync &&
220712f080e7Smrj 	    (hp->dmai_rflags & DDI_DMA_READ)) {
220894f1124eSVikram Hegde 		(void) rootnex_coredma_sync(dip, rdip, handle, 0, 0,
220912f080e7Smrj 		    DDI_DMA_SYNC_FORCPU);
221012f080e7Smrj 	}
221112f080e7Smrj 
221212f080e7Smrj 	/*
221312f080e7Smrj 	 * cleanup and copy buffer or window state. if we didn't use the copy
221412f080e7Smrj 	 * buffer or windows, there won't be much to do :-)
221512f080e7Smrj 	 */
221612f080e7Smrj 	rootnex_teardown_copybuf(dma);
221712f080e7Smrj 	rootnex_teardown_windows(dma);
221812f080e7Smrj 
22193a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
222012f080e7Smrj 	/*
22213a634bfcSVikram Hegde 	 * Clean up the page tables and free the dvma
222286c1f4dcSVikram Hegde 	 */
22233a634bfcSVikram Hegde 	e = immu_unmap_sgl(hp, rdip);
22243a634bfcSVikram Hegde 	if (e != DDI_DMA_USE_PHYSICAL && e != DDI_SUCCESS) {
22253a634bfcSVikram Hegde 		return (e);
222686c1f4dcSVikram Hegde 	}
222720906b23SVikram Hegde #endif
222886c1f4dcSVikram Hegde 
222986c1f4dcSVikram Hegde 	/*
223012f080e7Smrj 	 * If we had to allocate space to for the worse case sgl (it didn't
223112f080e7Smrj 	 * fit into our pre-allocate buffer), free that up now
223212f080e7Smrj 	 */
223312f080e7Smrj 	if (dma->dp_need_to_free_cookie) {
223412f080e7Smrj 		kmem_free(dma->dp_cookies, dma->dp_cookie_size);
223512f080e7Smrj 	}
223612f080e7Smrj 
223712f080e7Smrj 	/*
223812f080e7Smrj 	 * clean up the handle so it's ready for the next bind (i.e. if the
223912f080e7Smrj 	 * handle is reused).
224012f080e7Smrj 	 */
224112f080e7Smrj 	rootnex_clean_dmahdl(hp);
2242567c0b92SStephen Hanson 	hp->dmai_error.err_cf = NULL;
224312f080e7Smrj 
224412f080e7Smrj 	if (rootnex_state->r_dvma_call_list_id)
224512f080e7Smrj 		ddi_run_callback(&rootnex_state->r_dvma_call_list_id);
224612f080e7Smrj 
22470b7ba611SMark Johnson 	ROOTNEX_DPROF_DEC(&rootnex_cnt[ROOTNEX_CNT_ACTIVE_BINDS]);
22480b7ba611SMark Johnson 	ROOTNEX_DPROBE1(rootnex__unbind, uint64_t,
224912f080e7Smrj 	    rootnex_cnt[ROOTNEX_CNT_ACTIVE_BINDS]);
225012f080e7Smrj 
225112f080e7Smrj 	return (DDI_SUCCESS);
225212f080e7Smrj }
225312f080e7Smrj 
225420906b23SVikram Hegde /*
225520906b23SVikram Hegde  * rootnex_dma_unbindhdl()
225620906b23SVikram Hegde  *    called from ddi_dma_unbind_handle()
225720906b23SVikram Hegde  */
225820906b23SVikram Hegde /*ARGSUSED*/
225920906b23SVikram Hegde static int
226020906b23SVikram Hegde rootnex_dma_unbindhdl(dev_info_t *dip, dev_info_t *rdip,
226120906b23SVikram Hegde     ddi_dma_handle_t handle)
226220906b23SVikram Hegde {
22633a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
2264b51bbbf5SVikram Hegde 	if (IOMMU_USED(rdip)) {
226520906b23SVikram Hegde 		return (iommulib_nexdma_unbindhdl(dip, rdip, handle));
226620906b23SVikram Hegde 	}
226720906b23SVikram Hegde #endif
226820906b23SVikram Hegde 	return (rootnex_coredma_unbindhdl(dip, rdip, handle));
226920906b23SVikram Hegde }
227020906b23SVikram Hegde 
22713a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
227294f1124eSVikram Hegde 
227394f1124eSVikram Hegde static int
227494f1124eSVikram Hegde rootnex_coredma_get_sleep_flags(ddi_dma_handle_t handle)
227594f1124eSVikram Hegde {
227694f1124eSVikram Hegde 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
227794f1124eSVikram Hegde 	rootnex_dma_t *dma = (rootnex_dma_t *)hp->dmai_private;
227894f1124eSVikram Hegde 
227994f1124eSVikram Hegde 	if (dma->dp_sleep_flags != KM_SLEEP &&
228094f1124eSVikram Hegde 	    dma->dp_sleep_flags != KM_NOSLEEP)
228194f1124eSVikram Hegde 		cmn_err(CE_PANIC, "kmem sleep flags not set in DMA handle");
228294f1124eSVikram Hegde 	return (dma->dp_sleep_flags);
228394f1124eSVikram Hegde }
228420906b23SVikram Hegde /*ARGSUSED*/
228520906b23SVikram Hegde static void
228620906b23SVikram Hegde rootnex_coredma_reset_cookies(dev_info_t *dip, ddi_dma_handle_t handle)
228720906b23SVikram Hegde {
228820906b23SVikram Hegde 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
228920906b23SVikram Hegde 	rootnex_dma_t *dma = (rootnex_dma_t *)hp->dmai_private;
229094f1124eSVikram Hegde 	rootnex_window_t *window;
229120906b23SVikram Hegde 
229294f1124eSVikram Hegde 	if (dma->dp_window) {
229394f1124eSVikram Hegde 		window = &dma->dp_window[dma->dp_current_win];
229494f1124eSVikram Hegde 		hp->dmai_cookie = window->wd_first_cookie;
229594f1124eSVikram Hegde 	} else {
229694f1124eSVikram Hegde 		hp->dmai_cookie = dma->dp_cookies;
229794f1124eSVikram Hegde 	}
229820906b23SVikram Hegde 	hp->dmai_cookie++;
229920906b23SVikram Hegde }
230020906b23SVikram Hegde 
230120906b23SVikram Hegde /*ARGSUSED*/
230220906b23SVikram Hegde static int
230320906b23SVikram Hegde rootnex_coredma_get_cookies(dev_info_t *dip, ddi_dma_handle_t handle,
230494f1124eSVikram Hegde     ddi_dma_cookie_t **cookiepp, uint_t *ccountp)
230520906b23SVikram Hegde {
230694f1124eSVikram Hegde 	int i;
230794f1124eSVikram Hegde 	int km_flags;
230820906b23SVikram Hegde 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
230920906b23SVikram Hegde 	rootnex_dma_t *dma = (rootnex_dma_t *)hp->dmai_private;
231094f1124eSVikram Hegde 	rootnex_window_t *window;
231194f1124eSVikram Hegde 	ddi_dma_cookie_t *cp;
231294f1124eSVikram Hegde 	ddi_dma_cookie_t *cookie;
231320906b23SVikram Hegde 
231494f1124eSVikram Hegde 	ASSERT(*cookiepp == NULL);
231594f1124eSVikram Hegde 	ASSERT(*ccountp == 0);
231620906b23SVikram Hegde 
231794f1124eSVikram Hegde 	if (dma->dp_window) {
231894f1124eSVikram Hegde 		window = &dma->dp_window[dma->dp_current_win];
231994f1124eSVikram Hegde 		cp = window->wd_first_cookie;
232094f1124eSVikram Hegde 		*ccountp = window->wd_cookie_cnt;
232120906b23SVikram Hegde 	} else {
232294f1124eSVikram Hegde 		cp = dma->dp_cookies;
232320906b23SVikram Hegde 		*ccountp = dma->dp_sglinfo.si_sgl_size;
232420906b23SVikram Hegde 	}
232520906b23SVikram Hegde 
232694f1124eSVikram Hegde 	km_flags = rootnex_coredma_get_sleep_flags(handle);
232794f1124eSVikram Hegde 	cookie = kmem_zalloc(sizeof (ddi_dma_cookie_t) * (*ccountp), km_flags);
232894f1124eSVikram Hegde 	if (cookie == NULL) {
232994f1124eSVikram Hegde 		return (DDI_DMA_NORESOURCES);
233094f1124eSVikram Hegde 	}
233194f1124eSVikram Hegde 
233294f1124eSVikram Hegde 	for (i = 0; i < *ccountp; i++) {
233394f1124eSVikram Hegde 		cookie[i].dmac_notused = cp[i].dmac_notused;
233494f1124eSVikram Hegde 		cookie[i].dmac_type = cp[i].dmac_type;
233594f1124eSVikram Hegde 		cookie[i].dmac_address = cp[i].dmac_address;
233694f1124eSVikram Hegde 		cookie[i].dmac_size = cp[i].dmac_size;
233794f1124eSVikram Hegde 	}
233894f1124eSVikram Hegde 
233994f1124eSVikram Hegde 	*cookiepp = cookie;
234020906b23SVikram Hegde 
234120906b23SVikram Hegde 	return (DDI_SUCCESS);
234220906b23SVikram Hegde }
234394f1124eSVikram Hegde 
234494f1124eSVikram Hegde /*ARGSUSED*/
234594f1124eSVikram Hegde static int
234694f1124eSVikram Hegde rootnex_coredma_set_cookies(dev_info_t *dip, ddi_dma_handle_t handle,
234794f1124eSVikram Hegde     ddi_dma_cookie_t *cookiep, uint_t ccount)
234894f1124eSVikram Hegde {
234994f1124eSVikram Hegde 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
235094f1124eSVikram Hegde 	rootnex_dma_t *dma = (rootnex_dma_t *)hp->dmai_private;
235194f1124eSVikram Hegde 	rootnex_window_t *window;
235294f1124eSVikram Hegde 	ddi_dma_cookie_t *cur_cookiep;
235394f1124eSVikram Hegde 
235494f1124eSVikram Hegde 	ASSERT(cookiep);
235594f1124eSVikram Hegde 	ASSERT(ccount != 0);
235694f1124eSVikram Hegde 	ASSERT(dma->dp_need_to_switch_cookies == B_FALSE);
235794f1124eSVikram Hegde 
235894f1124eSVikram Hegde 	if (dma->dp_window) {
235994f1124eSVikram Hegde 		window = &dma->dp_window[dma->dp_current_win];
236094f1124eSVikram Hegde 		dma->dp_saved_cookies = window->wd_first_cookie;
236194f1124eSVikram Hegde 		window->wd_first_cookie = cookiep;
236294f1124eSVikram Hegde 		ASSERT(ccount == window->wd_cookie_cnt);
236394f1124eSVikram Hegde 		cur_cookiep = (hp->dmai_cookie - dma->dp_saved_cookies)
236494f1124eSVikram Hegde 		    + window->wd_first_cookie;
236594f1124eSVikram Hegde 	} else {
236694f1124eSVikram Hegde 		dma->dp_saved_cookies = dma->dp_cookies;
236794f1124eSVikram Hegde 		dma->dp_cookies = cookiep;
236894f1124eSVikram Hegde 		ASSERT(ccount == dma->dp_sglinfo.si_sgl_size);
236994f1124eSVikram Hegde 		cur_cookiep = (hp->dmai_cookie - dma->dp_saved_cookies)
237094f1124eSVikram Hegde 		    + dma->dp_cookies;
237194f1124eSVikram Hegde 	}
237294f1124eSVikram Hegde 
237394f1124eSVikram Hegde 	dma->dp_need_to_switch_cookies = B_TRUE;
237494f1124eSVikram Hegde 	hp->dmai_cookie = cur_cookiep;
237594f1124eSVikram Hegde 
237694f1124eSVikram Hegde 	return (DDI_SUCCESS);
237794f1124eSVikram Hegde }
237894f1124eSVikram Hegde 
237994f1124eSVikram Hegde /*ARGSUSED*/
238094f1124eSVikram Hegde static int
238194f1124eSVikram Hegde rootnex_coredma_clear_cookies(dev_info_t *dip, ddi_dma_handle_t handle)
238294f1124eSVikram Hegde {
238394f1124eSVikram Hegde 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
238494f1124eSVikram Hegde 	rootnex_dma_t *dma = (rootnex_dma_t *)hp->dmai_private;
238594f1124eSVikram Hegde 	rootnex_window_t *window;
238694f1124eSVikram Hegde 	ddi_dma_cookie_t *cur_cookiep;
238794f1124eSVikram Hegde 	ddi_dma_cookie_t *cookie_array;
238894f1124eSVikram Hegde 	uint_t ccount;
238994f1124eSVikram Hegde 
239094f1124eSVikram Hegde 	/* check if cookies have not been switched */
239194f1124eSVikram Hegde 	if (dma->dp_need_to_switch_cookies == B_FALSE)
239294f1124eSVikram Hegde 		return (DDI_SUCCESS);
239394f1124eSVikram Hegde 
239494f1124eSVikram Hegde 	ASSERT(dma->dp_saved_cookies);
239594f1124eSVikram Hegde 
239694f1124eSVikram Hegde 	if (dma->dp_window) {
239794f1124eSVikram Hegde 		window = &dma->dp_window[dma->dp_current_win];
239894f1124eSVikram Hegde 		cookie_array = window->wd_first_cookie;
239994f1124eSVikram Hegde 		window->wd_first_cookie = dma->dp_saved_cookies;
240094f1124eSVikram Hegde 		dma->dp_saved_cookies = NULL;
240194f1124eSVikram Hegde 		ccount = window->wd_cookie_cnt;
240294f1124eSVikram Hegde 		cur_cookiep = (hp->dmai_cookie - cookie_array)
240394f1124eSVikram Hegde 		    + window->wd_first_cookie;
240494f1124eSVikram Hegde 	} else {
240594f1124eSVikram Hegde 		cookie_array = dma->dp_cookies;
240694f1124eSVikram Hegde 		dma->dp_cookies = dma->dp_saved_cookies;
240794f1124eSVikram Hegde 		dma->dp_saved_cookies = NULL;
240894f1124eSVikram Hegde 		ccount = dma->dp_sglinfo.si_sgl_size;
240994f1124eSVikram Hegde 		cur_cookiep = (hp->dmai_cookie - cookie_array)
241094f1124eSVikram Hegde 		    + dma->dp_cookies;
241194f1124eSVikram Hegde 	}
241294f1124eSVikram Hegde 
241394f1124eSVikram Hegde 	kmem_free(cookie_array, sizeof (ddi_dma_cookie_t) * ccount);
241494f1124eSVikram Hegde 
241594f1124eSVikram Hegde 	hp->dmai_cookie = cur_cookiep;
241694f1124eSVikram Hegde 
241794f1124eSVikram Hegde 	dma->dp_need_to_switch_cookies = B_FALSE;
241894f1124eSVikram Hegde 
241994f1124eSVikram Hegde 	return (DDI_SUCCESS);
242094f1124eSVikram Hegde }
242194f1124eSVikram Hegde 
24225dfdb46bSVikram Hegde #endif
242312f080e7Smrj 
242412f080e7Smrj /*
242512f080e7Smrj  * rootnex_verify_buffer()
242612f080e7Smrj  *   verify buffer wasn't free'd
242712f080e7Smrj  */
242812f080e7Smrj static int
242912f080e7Smrj rootnex_verify_buffer(rootnex_dma_t *dma)
243012f080e7Smrj {
243112f080e7Smrj 	page_t **pplist;
243212f080e7Smrj 	caddr_t vaddr;
243312f080e7Smrj 	uint_t pcnt;
243412f080e7Smrj 	uint_t poff;
243512f080e7Smrj 	page_t *pp;
243600d0963fSdilpreet 	char b;
243712f080e7Smrj 	int i;
243812f080e7Smrj 
243912f080e7Smrj 	/* Figure out how many pages this buffer occupies */
244012f080e7Smrj 	if (dma->dp_dma.dmao_type == DMA_OTYP_PAGES) {
244112f080e7Smrj 		poff = dma->dp_dma.dmao_obj.pp_obj.pp_offset & MMU_PAGEOFFSET;
244212f080e7Smrj 	} else {
244312f080e7Smrj 		vaddr = dma->dp_dma.dmao_obj.virt_obj.v_addr;
244412f080e7Smrj 		poff = (uintptr_t)vaddr & MMU_PAGEOFFSET;
244512f080e7Smrj 	}
244612f080e7Smrj 	pcnt = mmu_btopr(dma->dp_dma.dmao_size + poff);
244712f080e7Smrj 
244812f080e7Smrj 	switch (dma->dp_dma.dmao_type) {
244912f080e7Smrj 	case DMA_OTYP_PAGES:
245012f080e7Smrj 		/*
245112f080e7Smrj 		 * for a linked list of pp's walk through them to make sure
245212f080e7Smrj 		 * they're locked and not free.
245312f080e7Smrj 		 */
245412f080e7Smrj 		pp = dma->dp_dma.dmao_obj.pp_obj.pp_pp;
245512f080e7Smrj 		for (i = 0; i < pcnt; i++) {
245612f080e7Smrj 			if (PP_ISFREE(pp) || !PAGE_LOCKED(pp)) {
245712f080e7Smrj 				return (DDI_FAILURE);
245812f080e7Smrj 			}
24597c478bd9Sstevel@tonic-gate 			pp = pp->p_next;
24607c478bd9Sstevel@tonic-gate 		}
24617c478bd9Sstevel@tonic-gate 		break;
246212f080e7Smrj 
24637c478bd9Sstevel@tonic-gate 	case DMA_OTYP_VADDR:
24647c478bd9Sstevel@tonic-gate 	case DMA_OTYP_BUFVADDR:
246512f080e7Smrj 		pplist = dma->dp_dma.dmao_obj.virt_obj.v_priv;
246612f080e7Smrj 		/*
246712f080e7Smrj 		 * for an array of pp's walk through them to make sure they're
246812f080e7Smrj 		 * not free. It's possible that they may not be locked.
246912f080e7Smrj 		 */
247012f080e7Smrj 		if (pplist) {
247112f080e7Smrj 			for (i = 0; i < pcnt; i++) {
247212f080e7Smrj 				if (PP_ISFREE(pplist[i])) {
247312f080e7Smrj 					return (DDI_FAILURE);
247412f080e7Smrj 				}
247512f080e7Smrj 			}
247612f080e7Smrj 
247712f080e7Smrj 		/* For a virtual address, try to peek at each page */
247812f080e7Smrj 		} else {
247912f080e7Smrj 			if (dma->dp_sglinfo.si_asp == &kas) {
248012f080e7Smrj 				for (i = 0; i < pcnt; i++) {
248100d0963fSdilpreet 					if (ddi_peek8(NULL, vaddr, &b) ==
248200d0963fSdilpreet 					    DDI_FAILURE)
248312f080e7Smrj 						return (DDI_FAILURE);
248400d0963fSdilpreet 					vaddr += MMU_PAGESIZE;
248512f080e7Smrj 				}
248612f080e7Smrj 			}
248712f080e7Smrj 		}
248812f080e7Smrj 		break;
248912f080e7Smrj 
249012f080e7Smrj 	default:
249112f080e7Smrj 		ASSERT(0);
249212f080e7Smrj 		break;
249312f080e7Smrj 	}
249412f080e7Smrj 
249512f080e7Smrj 	return (DDI_SUCCESS);
249612f080e7Smrj }
249712f080e7Smrj 
249812f080e7Smrj 
249912f080e7Smrj /*
250012f080e7Smrj  * rootnex_clean_dmahdl()
250112f080e7Smrj  *    Clean the dma handle. This should be called on a handle alloc and an
250212f080e7Smrj  *    unbind handle. Set the handle state to the default settings.
250312f080e7Smrj  */
250412f080e7Smrj static void
250512f080e7Smrj rootnex_clean_dmahdl(ddi_dma_impl_t *hp)
250612f080e7Smrj {
250712f080e7Smrj 	rootnex_dma_t *dma;
250812f080e7Smrj 
250912f080e7Smrj 
251012f080e7Smrj 	dma = (rootnex_dma_t *)hp->dmai_private;
251112f080e7Smrj 
251212f080e7Smrj 	hp->dmai_nwin = 0;
251312f080e7Smrj 	dma->dp_current_cookie = 0;
251412f080e7Smrj 	dma->dp_copybuf_size = 0;
251512f080e7Smrj 	dma->dp_window = NULL;
251612f080e7Smrj 	dma->dp_cbaddr = NULL;
251712f080e7Smrj 	dma->dp_inuse = B_FALSE;
251812f080e7Smrj 	dma->dp_need_to_free_cookie = B_FALSE;
251994f1124eSVikram Hegde 	dma->dp_need_to_switch_cookies = B_FALSE;
252094f1124eSVikram Hegde 	dma->dp_saved_cookies = NULL;
252194f1124eSVikram Hegde 	dma->dp_sleep_flags = KM_PANIC;
252212f080e7Smrj 	dma->dp_need_to_free_window = B_FALSE;
252312f080e7Smrj 	dma->dp_partial_required = B_FALSE;
252412f080e7Smrj 	dma->dp_trim_required = B_FALSE;
252512f080e7Smrj 	dma->dp_sglinfo.si_copybuf_req = 0;
252612f080e7Smrj #if !defined(__amd64)
252712f080e7Smrj 	dma->dp_cb_remaping = B_FALSE;
252812f080e7Smrj 	dma->dp_kva = NULL;
252912f080e7Smrj #endif
253012f080e7Smrj 
253112f080e7Smrj 	/* FMA related initialization */
253212f080e7Smrj 	hp->dmai_fault = 0;
253312f080e7Smrj 	hp->dmai_fault_check = NULL;
253412f080e7Smrj 	hp->dmai_fault_notify = NULL;
253512f080e7Smrj 	hp->dmai_error.err_ena = 0;
253612f080e7Smrj 	hp->dmai_error.err_status = DDI_FM_OK;
253712f080e7Smrj 	hp->dmai_error.err_expected = DDI_FM_ERR_UNEXPECTED;
253812f080e7Smrj 	hp->dmai_error.err_ontrap = NULL;
253912f080e7Smrj }
254012f080e7Smrj 
254112f080e7Smrj 
254212f080e7Smrj /*
254312f080e7Smrj  * rootnex_valid_alloc_parms()
254412f080e7Smrj  *    Called in ddi_dma_alloc_handle path to validate its parameters.
254512f080e7Smrj  */
254612f080e7Smrj static int
254712f080e7Smrj rootnex_valid_alloc_parms(ddi_dma_attr_t *attr, uint_t maxsegmentsize)
254812f080e7Smrj {
254912f080e7Smrj 	if ((attr->dma_attr_seg < MMU_PAGEOFFSET) ||
255012f080e7Smrj 	    (attr->dma_attr_count_max < MMU_PAGEOFFSET) ||
255112f080e7Smrj 	    (attr->dma_attr_granular > MMU_PAGESIZE) ||
255212f080e7Smrj 	    (attr->dma_attr_maxxfer < MMU_PAGESIZE)) {
255312f080e7Smrj 		return (DDI_DMA_BADATTR);
255412f080e7Smrj 	}
255512f080e7Smrj 
255612f080e7Smrj 	if (attr->dma_attr_addr_hi <= attr->dma_attr_addr_lo) {
255712f080e7Smrj 		return (DDI_DMA_BADATTR);
255812f080e7Smrj 	}
255912f080e7Smrj 
256012f080e7Smrj 	if ((attr->dma_attr_seg & MMU_PAGEOFFSET) != MMU_PAGEOFFSET ||
256112f080e7Smrj 	    MMU_PAGESIZE & (attr->dma_attr_granular - 1) ||
256212f080e7Smrj 	    attr->dma_attr_sgllen <= 0) {
256312f080e7Smrj 		return (DDI_DMA_BADATTR);
256412f080e7Smrj 	}
256512f080e7Smrj 
256612f080e7Smrj 	/* We should be able to DMA into every byte offset in a page */
256712f080e7Smrj 	if (maxsegmentsize < MMU_PAGESIZE) {
256812f080e7Smrj 		return (DDI_DMA_BADATTR);
256912f080e7Smrj 	}
257012f080e7Smrj 
257107c6692fSMark Johnson 	/* if we're bouncing on seg, seg must be <= addr_hi */
257207c6692fSMark Johnson 	if ((attr->dma_attr_flags & _DDI_DMA_BOUNCE_ON_SEG) &&
257307c6692fSMark Johnson 	    (attr->dma_attr_seg > attr->dma_attr_addr_hi)) {
257407c6692fSMark Johnson 		return (DDI_DMA_BADATTR);
257507c6692fSMark Johnson 	}
257612f080e7Smrj 	return (DDI_SUCCESS);
257712f080e7Smrj }
257812f080e7Smrj 
257912f080e7Smrj /*
258012f080e7Smrj  * rootnex_valid_bind_parms()
258112f080e7Smrj  *    Called in ddi_dma_*_bind_handle path to validate its parameters.
258212f080e7Smrj  */
258312f080e7Smrj /* ARGSUSED */
258412f080e7Smrj static int
258512f080e7Smrj rootnex_valid_bind_parms(ddi_dma_req_t *dmareq, ddi_dma_attr_t *attr)
258612f080e7Smrj {
258712f080e7Smrj #if !defined(__amd64)
258812f080e7Smrj 	/*
258912f080e7Smrj 	 * we only support up to a 2G-1 transfer size on 32-bit kernels so
259012f080e7Smrj 	 * we can track the offset for the obsoleted interfaces.
259112f080e7Smrj 	 */
259212f080e7Smrj 	if (dmareq->dmar_object.dmao_size > 0x7FFFFFFF) {
259312f080e7Smrj 		return (DDI_DMA_TOOBIG);
259412f080e7Smrj 	}
259512f080e7Smrj #endif
259612f080e7Smrj 
259712f080e7Smrj 	return (DDI_SUCCESS);
259812f080e7Smrj }
259912f080e7Smrj 
260012f080e7Smrj 
260112f080e7Smrj /*
260207c6692fSMark Johnson  * rootnex_need_bounce_seg()
260307c6692fSMark Johnson  *    check to see if the buffer lives on both side of the seg.
260407c6692fSMark Johnson  */
260507c6692fSMark Johnson static boolean_t
260607c6692fSMark Johnson rootnex_need_bounce_seg(ddi_dma_obj_t *dmar_object, rootnex_sglinfo_t *sglinfo)
260707c6692fSMark Johnson {
260807c6692fSMark Johnson 	ddi_dma_atyp_t buftype;
260907c6692fSMark Johnson 	rootnex_addr_t raddr;
261007c6692fSMark Johnson 	boolean_t lower_addr;
261107c6692fSMark Johnson 	boolean_t upper_addr;
261207c6692fSMark Johnson 	uint64_t offset;
261307c6692fSMark Johnson 	page_t **pplist;
261407c6692fSMark Johnson 	uint64_t paddr;
261507c6692fSMark Johnson 	uint32_t psize;
261607c6692fSMark Johnson 	uint32_t size;
261707c6692fSMark Johnson 	caddr_t vaddr;
261807c6692fSMark Johnson 	uint_t pcnt;
261907c6692fSMark Johnson 	page_t *pp;
262007c6692fSMark Johnson 
262107c6692fSMark Johnson 
262207c6692fSMark Johnson 	/* shortcuts */
262307c6692fSMark Johnson 	pplist = dmar_object->dmao_obj.virt_obj.v_priv;
262407c6692fSMark Johnson 	vaddr = dmar_object->dmao_obj.virt_obj.v_addr;
262507c6692fSMark Johnson 	buftype = dmar_object->dmao_type;
262607c6692fSMark Johnson 	size = dmar_object->dmao_size;
262707c6692fSMark Johnson 
262807c6692fSMark Johnson 	lower_addr = B_FALSE;
262907c6692fSMark Johnson 	upper_addr = B_FALSE;
263007c6692fSMark Johnson 	pcnt = 0;
263107c6692fSMark Johnson 
263207c6692fSMark Johnson 	/*
263307c6692fSMark Johnson 	 * Process the first page to handle the initial offset of the buffer.
263407c6692fSMark Johnson 	 * We'll use the base address we get later when we loop through all
263507c6692fSMark Johnson 	 * the pages.
263607c6692fSMark Johnson 	 */
263707c6692fSMark Johnson 	if (buftype == DMA_OTYP_PAGES) {
263807c6692fSMark Johnson 		pp = dmar_object->dmao_obj.pp_obj.pp_pp;
263907c6692fSMark Johnson 		offset =  dmar_object->dmao_obj.pp_obj.pp_offset &
264007c6692fSMark Johnson 		    MMU_PAGEOFFSET;
264107c6692fSMark Johnson 		paddr = pfn_to_pa(pp->p_pagenum) + offset;
264207c6692fSMark Johnson 		psize = MIN(size, (MMU_PAGESIZE - offset));
264307c6692fSMark Johnson 		pp = pp->p_next;
264407c6692fSMark Johnson 		sglinfo->si_asp = NULL;
264507c6692fSMark Johnson 	} else if (pplist != NULL) {
264607c6692fSMark Johnson 		offset = (uintptr_t)vaddr & MMU_PAGEOFFSET;
264707c6692fSMark Johnson 		sglinfo->si_asp = dmar_object->dmao_obj.virt_obj.v_as;
264807c6692fSMark Johnson 		if (sglinfo->si_asp == NULL) {
264907c6692fSMark Johnson 			sglinfo->si_asp = &kas;
265007c6692fSMark Johnson 		}
265107c6692fSMark Johnson 		paddr = pfn_to_pa(pplist[pcnt]->p_pagenum);
265207c6692fSMark Johnson 		paddr += offset;
265307c6692fSMark Johnson 		psize = MIN(size, (MMU_PAGESIZE - offset));
265407c6692fSMark Johnson 		pcnt++;
265507c6692fSMark Johnson 	} else {
265607c6692fSMark Johnson 		offset = (uintptr_t)vaddr & MMU_PAGEOFFSET;
265707c6692fSMark Johnson 		sglinfo->si_asp = dmar_object->dmao_obj.virt_obj.v_as;
265807c6692fSMark Johnson 		if (sglinfo->si_asp == NULL) {
265907c6692fSMark Johnson 			sglinfo->si_asp = &kas;
266007c6692fSMark Johnson 		}
266107c6692fSMark Johnson 		paddr = pfn_to_pa(hat_getpfnum(sglinfo->si_asp->a_hat, vaddr));
266207c6692fSMark Johnson 		paddr += offset;
266307c6692fSMark Johnson 		psize = MIN(size, (MMU_PAGESIZE - offset));
266407c6692fSMark Johnson 		vaddr += psize;
266507c6692fSMark Johnson 	}
266607c6692fSMark Johnson 
266707c6692fSMark Johnson #ifdef __xpv
266807c6692fSMark Johnson 	/*
266907c6692fSMark Johnson 	 * If we're dom0, we're using a real device so we need to load
267007c6692fSMark Johnson 	 * the cookies with MFNs instead of PFNs.
267107c6692fSMark Johnson 	 */
267207c6692fSMark Johnson 	raddr = ROOTNEX_PADDR_TO_RBASE(xen_info, paddr);
267307c6692fSMark Johnson #else
267407c6692fSMark Johnson 	raddr = paddr;
267507c6692fSMark Johnson #endif
267607c6692fSMark Johnson 
267707c6692fSMark Johnson 	if ((raddr + psize) > sglinfo->si_segmask) {
267807c6692fSMark Johnson 		upper_addr = B_TRUE;
267907c6692fSMark Johnson 	} else {
268007c6692fSMark Johnson 		lower_addr = B_TRUE;
268107c6692fSMark Johnson 	}
268207c6692fSMark Johnson 	size -= psize;
268307c6692fSMark Johnson 
268407c6692fSMark Johnson 	/*
268507c6692fSMark Johnson 	 * Walk through the rest of the pages in the buffer. Track to see
268607c6692fSMark Johnson 	 * if we have pages on both sides of the segment boundary.
268707c6692fSMark Johnson 	 */
268807c6692fSMark Johnson 	while (size > 0) {
268907c6692fSMark Johnson 		/* partial or full page */
269007c6692fSMark Johnson 		psize = MIN(size, MMU_PAGESIZE);
269107c6692fSMark Johnson 
269207c6692fSMark Johnson 		if (buftype == DMA_OTYP_PAGES) {
269307c6692fSMark Johnson 			/* get the paddr from the page_t */
269407c6692fSMark Johnson 			ASSERT(!PP_ISFREE(pp) && PAGE_LOCKED(pp));
269507c6692fSMark Johnson 			paddr = pfn_to_pa(pp->p_pagenum);
269607c6692fSMark Johnson 			pp = pp->p_next;
269707c6692fSMark Johnson 		} else if (pplist != NULL) {
269807c6692fSMark Johnson 			/* index into the array of page_t's to get the paddr */
269907c6692fSMark Johnson 			ASSERT(!PP_ISFREE(pplist[pcnt]));
270007c6692fSMark Johnson 			paddr = pfn_to_pa(pplist[pcnt]->p_pagenum);
270107c6692fSMark Johnson 			pcnt++;
270207c6692fSMark Johnson 		} else {
270307c6692fSMark Johnson 			/* call into the VM to get the paddr */
270407c6692fSMark Johnson 			paddr =  pfn_to_pa(hat_getpfnum(sglinfo->si_asp->a_hat,
270507c6692fSMark Johnson 			    vaddr));
270607c6692fSMark Johnson 			vaddr += psize;
270707c6692fSMark Johnson 		}
270807c6692fSMark Johnson 
270907c6692fSMark Johnson #ifdef __xpv
271007c6692fSMark Johnson 		/*
271107c6692fSMark Johnson 		 * If we're dom0, we're using a real device so we need to load
271207c6692fSMark Johnson 		 * the cookies with MFNs instead of PFNs.
271307c6692fSMark Johnson 		 */
271407c6692fSMark Johnson 		raddr = ROOTNEX_PADDR_TO_RBASE(xen_info, paddr);
271507c6692fSMark Johnson #else
271607c6692fSMark Johnson 		raddr = paddr;
271707c6692fSMark Johnson #endif
271807c6692fSMark Johnson 
271907c6692fSMark Johnson 		if ((raddr + psize) > sglinfo->si_segmask) {
272007c6692fSMark Johnson 			upper_addr = B_TRUE;
272107c6692fSMark Johnson 		} else {
272207c6692fSMark Johnson 			lower_addr = B_TRUE;
272307c6692fSMark Johnson 		}
272407c6692fSMark Johnson 		/*
272507c6692fSMark Johnson 		 * if the buffer lives both above and below the segment
272607c6692fSMark Johnson 		 * boundary, or the current page is the page immediately
272707c6692fSMark Johnson 		 * after the segment, we will use a copy/bounce buffer for
272807c6692fSMark Johnson 		 * all pages > seg.
272907c6692fSMark Johnson 		 */
273007c6692fSMark Johnson 		if ((lower_addr && upper_addr) ||
273107c6692fSMark Johnson 		    (raddr == (sglinfo->si_segmask + 1))) {
273207c6692fSMark Johnson 			return (B_TRUE);
273307c6692fSMark Johnson 		}
273407c6692fSMark Johnson 
273507c6692fSMark Johnson 		size -= psize;
273607c6692fSMark Johnson 	}
273707c6692fSMark Johnson 
273807c6692fSMark Johnson 	return (B_FALSE);
273907c6692fSMark Johnson }
274007c6692fSMark Johnson 
274107c6692fSMark Johnson 
274207c6692fSMark Johnson /*
274312f080e7Smrj  * rootnex_get_sgl()
274412f080e7Smrj  *    Called in bind fastpath to get the sgl. Most of this will be replaced
274512f080e7Smrj  *    with a call to the vm layer when vm2.0 comes around...
274612f080e7Smrj  */
274712f080e7Smrj static void
274812f080e7Smrj rootnex_get_sgl(ddi_dma_obj_t *dmar_object, ddi_dma_cookie_t *sgl,
274912f080e7Smrj     rootnex_sglinfo_t *sglinfo)
275012f080e7Smrj {
275112f080e7Smrj 	ddi_dma_atyp_t buftype;
2752843e1988Sjohnlev 	rootnex_addr_t raddr;
275312f080e7Smrj 	uint64_t last_page;
275412f080e7Smrj 	uint64_t offset;
275512f080e7Smrj 	uint64_t addrhi;
275612f080e7Smrj 	uint64_t addrlo;
275712f080e7Smrj 	uint64_t maxseg;
275812f080e7Smrj 	page_t **pplist;
275912f080e7Smrj 	uint64_t paddr;
276012f080e7Smrj 	uint32_t psize;
276112f080e7Smrj 	uint32_t size;
276212f080e7Smrj 	caddr_t vaddr;
276312f080e7Smrj 	uint_t pcnt;
276412f080e7Smrj 	page_t *pp;
276512f080e7Smrj 	uint_t cnt;
276612f080e7Smrj 
276712f080e7Smrj 
276812f080e7Smrj 	/* shortcuts */
276912f080e7Smrj 	pplist = dmar_object->dmao_obj.virt_obj.v_priv;
277012f080e7Smrj 	vaddr = dmar_object->dmao_obj.virt_obj.v_addr;
277112f080e7Smrj 	maxseg = sglinfo->si_max_cookie_size;
277212f080e7Smrj 	buftype = dmar_object->dmao_type;
277312f080e7Smrj 	addrhi = sglinfo->si_max_addr;
277412f080e7Smrj 	addrlo = sglinfo->si_min_addr;
277512f080e7Smrj 	size = dmar_object->dmao_size;
277612f080e7Smrj 
277712f080e7Smrj 	pcnt = 0;
277812f080e7Smrj 	cnt = 0;
277912f080e7Smrj 
278007c6692fSMark Johnson 
278107c6692fSMark Johnson 	/*
278207c6692fSMark Johnson 	 * check to see if we need to use the copy buffer for pages over
278307c6692fSMark Johnson 	 * the segment attr.
278407c6692fSMark Johnson 	 */
278507c6692fSMark Johnson 	sglinfo->si_bounce_on_seg = B_FALSE;
278607c6692fSMark Johnson 	if (sglinfo->si_flags & _DDI_DMA_BOUNCE_ON_SEG) {
278707c6692fSMark Johnson 		sglinfo->si_bounce_on_seg = rootnex_need_bounce_seg(
278807c6692fSMark Johnson 		    dmar_object, sglinfo);
278907c6692fSMark Johnson 	}
279007c6692fSMark Johnson 
279112f080e7Smrj 	/*
279212f080e7Smrj 	 * if we were passed down a linked list of pages, i.e. pointer to
279312f080e7Smrj 	 * page_t, use this to get our physical address and buf offset.
279412f080e7Smrj 	 */
279512f080e7Smrj 	if (buftype == DMA_OTYP_PAGES) {
279612f080e7Smrj 		pp = dmar_object->dmao_obj.pp_obj.pp_pp;
279712f080e7Smrj 		ASSERT(!PP_ISFREE(pp) && PAGE_LOCKED(pp));
279812f080e7Smrj 		offset =  dmar_object->dmao_obj.pp_obj.pp_offset &
279912f080e7Smrj 		    MMU_PAGEOFFSET;
2800843e1988Sjohnlev 		paddr = pfn_to_pa(pp->p_pagenum) + offset;
280112f080e7Smrj 		psize = MIN(size, (MMU_PAGESIZE - offset));
280212f080e7Smrj 		pp = pp->p_next;
280312f080e7Smrj 		sglinfo->si_asp = NULL;
280412f080e7Smrj 
280512f080e7Smrj 	/*
280612f080e7Smrj 	 * We weren't passed down a linked list of pages, but if we were passed
280712f080e7Smrj 	 * down an array of pages, use this to get our physical address and buf
280812f080e7Smrj 	 * offset.
280912f080e7Smrj 	 */
281012f080e7Smrj 	} else if (pplist != NULL) {
281112f080e7Smrj 		ASSERT((buftype == DMA_OTYP_VADDR) ||
281212f080e7Smrj 		    (buftype == DMA_OTYP_BUFVADDR));
281312f080e7Smrj 
281412f080e7Smrj 		offset = (uintptr_t)vaddr & MMU_PAGEOFFSET;
281512f080e7Smrj 		sglinfo->si_asp = dmar_object->dmao_obj.virt_obj.v_as;
281612f080e7Smrj 		if (sglinfo->si_asp == NULL) {
281712f080e7Smrj 			sglinfo->si_asp = &kas;
281812f080e7Smrj 		}
281912f080e7Smrj 
282012f080e7Smrj 		ASSERT(!PP_ISFREE(pplist[pcnt]));
2821843e1988Sjohnlev 		paddr = pfn_to_pa(pplist[pcnt]->p_pagenum);
282212f080e7Smrj 		paddr += offset;
282312f080e7Smrj 		psize = MIN(size, (MMU_PAGESIZE - offset));
282412f080e7Smrj 		pcnt++;
282512f080e7Smrj 
282612f080e7Smrj 	/*
282712f080e7Smrj 	 * All we have is a virtual address, we'll need to call into the VM
282812f080e7Smrj 	 * to get the physical address.
282912f080e7Smrj 	 */
283012f080e7Smrj 	} else {
283112f080e7Smrj 		ASSERT((buftype == DMA_OTYP_VADDR) ||
283212f080e7Smrj 		    (buftype == DMA_OTYP_BUFVADDR));
283312f080e7Smrj 
283412f080e7Smrj 		offset = (uintptr_t)vaddr & MMU_PAGEOFFSET;
283512f080e7Smrj 		sglinfo->si_asp = dmar_object->dmao_obj.virt_obj.v_as;
283612f080e7Smrj 		if (sglinfo->si_asp == NULL) {
283712f080e7Smrj 			sglinfo->si_asp = &kas;
283812f080e7Smrj 		}
283912f080e7Smrj 
2840843e1988Sjohnlev 		paddr = pfn_to_pa(hat_getpfnum(sglinfo->si_asp->a_hat, vaddr));
284112f080e7Smrj 		paddr += offset;
284212f080e7Smrj 		psize = MIN(size, (MMU_PAGESIZE - offset));
284312f080e7Smrj 		vaddr += psize;
284412f080e7Smrj 	}
284512f080e7Smrj 
2846843e1988Sjohnlev #ifdef __xpv
2847843e1988Sjohnlev 	/*
2848843e1988Sjohnlev 	 * If we're dom0, we're using a real device so we need to load
2849843e1988Sjohnlev 	 * the cookies with MFNs instead of PFNs.
2850843e1988Sjohnlev 	 */
2851843e1988Sjohnlev 	raddr = ROOTNEX_PADDR_TO_RBASE(xen_info, paddr);
2852843e1988Sjohnlev #else
2853843e1988Sjohnlev 	raddr = paddr;
2854843e1988Sjohnlev #endif
2855843e1988Sjohnlev 
285612f080e7Smrj 	/*
285712f080e7Smrj 	 * Setup the first cookie with the physical address of the page and the
285812f080e7Smrj 	 * size of the page (which takes into account the initial offset into
285912f080e7Smrj 	 * the page.
286012f080e7Smrj 	 */
2861843e1988Sjohnlev 	sgl[cnt].dmac_laddress = raddr;
286212f080e7Smrj 	sgl[cnt].dmac_size = psize;
286312f080e7Smrj 	sgl[cnt].dmac_type = 0;
286412f080e7Smrj 
286512f080e7Smrj 	/*
286612f080e7Smrj 	 * Save away the buffer offset into the page. We'll need this later in
286712f080e7Smrj 	 * the copy buffer code to help figure out the page index within the
286812f080e7Smrj 	 * buffer and the offset into the current page.
286912f080e7Smrj 	 */
287012f080e7Smrj 	sglinfo->si_buf_offset = offset;
287112f080e7Smrj 
287212f080e7Smrj 	/*
287307c6692fSMark Johnson 	 * If we are using the copy buffer for anything over the segment
287407c6692fSMark Johnson 	 * boundary, and this page is over the segment boundary.
287507c6692fSMark Johnson 	 *   OR
287607c6692fSMark Johnson 	 * if the DMA engine can't reach the physical address.
287712f080e7Smrj 	 */
287807c6692fSMark Johnson 	if (((sglinfo->si_bounce_on_seg) &&
287907c6692fSMark Johnson 	    ((raddr + psize) > sglinfo->si_segmask)) ||
288007c6692fSMark Johnson 	    ((raddr < addrlo) || ((raddr + psize) > addrhi))) {
288107c6692fSMark Johnson 		/*
288207c6692fSMark Johnson 		 * Increase how much copy buffer we use. We always increase by
288307c6692fSMark Johnson 		 * pagesize so we don't have to worry about converting offsets.
288407c6692fSMark Johnson 		 * Set a flag in the cookies dmac_type to indicate that it uses
288507c6692fSMark Johnson 		 * the copy buffer. If this isn't the last cookie, go to the
288607c6692fSMark Johnson 		 * next cookie (since we separate each page which uses the copy
288707c6692fSMark Johnson 		 * buffer in case the copy buffer is not physically contiguous.
288807c6692fSMark Johnson 		 */
288912f080e7Smrj 		sglinfo->si_copybuf_req += MMU_PAGESIZE;
289012f080e7Smrj 		sgl[cnt].dmac_type = ROOTNEX_USES_COPYBUF;
289112f080e7Smrj 		if ((cnt + 1) < sglinfo->si_max_pages) {
289212f080e7Smrj 			cnt++;
289312f080e7Smrj 			sgl[cnt].dmac_laddress = 0;
289412f080e7Smrj 			sgl[cnt].dmac_size = 0;
289512f080e7Smrj 			sgl[cnt].dmac_type = 0;
289612f080e7Smrj 		}
289712f080e7Smrj 	}
289812f080e7Smrj 
289912f080e7Smrj 	/*
290012f080e7Smrj 	 * save this page's physical address so we can figure out if the next
290112f080e7Smrj 	 * page is physically contiguous. Keep decrementing size until we are
290212f080e7Smrj 	 * done with the buffer.
290312f080e7Smrj 	 */
2904843e1988Sjohnlev 	last_page = raddr & MMU_PAGEMASK;
290512f080e7Smrj 	size -= psize;
290612f080e7Smrj 
290712f080e7Smrj 	while (size > 0) {
290812f080e7Smrj 		/* Get the size for this page (i.e. partial or full page) */
290912f080e7Smrj 		psize = MIN(size, MMU_PAGESIZE);
291012f080e7Smrj 
291112f080e7Smrj 		if (buftype == DMA_OTYP_PAGES) {
291212f080e7Smrj 			/* get the paddr from the page_t */
291312f080e7Smrj 			ASSERT(!PP_ISFREE(pp) && PAGE_LOCKED(pp));
2914843e1988Sjohnlev 			paddr = pfn_to_pa(pp->p_pagenum);
291512f080e7Smrj 			pp = pp->p_next;
291612f080e7Smrj 		} else if (pplist != NULL) {
291712f080e7Smrj 			/* index into the array of page_t's to get the paddr */
291812f080e7Smrj 			ASSERT(!PP_ISFREE(pplist[pcnt]));
2919843e1988Sjohnlev 			paddr = pfn_to_pa(pplist[pcnt]->p_pagenum);
292012f080e7Smrj 			pcnt++;
292112f080e7Smrj 		} else {
292212f080e7Smrj 			/* call into the VM to get the paddr */
2923843e1988Sjohnlev 			paddr =  pfn_to_pa(hat_getpfnum(sglinfo->si_asp->a_hat,
292412f080e7Smrj 			    vaddr));
292512f080e7Smrj 			vaddr += psize;
292612f080e7Smrj 		}
292712f080e7Smrj 
2928843e1988Sjohnlev #ifdef __xpv
2929843e1988Sjohnlev 		/*
2930843e1988Sjohnlev 		 * If we're dom0, we're using a real device so we need to load
2931843e1988Sjohnlev 		 * the cookies with MFNs instead of PFNs.
2932843e1988Sjohnlev 		 */
2933843e1988Sjohnlev 		raddr = ROOTNEX_PADDR_TO_RBASE(xen_info, paddr);
2934843e1988Sjohnlev #else
2935843e1988Sjohnlev 		raddr = paddr;
2936843e1988Sjohnlev #endif
293707c6692fSMark Johnson 
293807c6692fSMark Johnson 		/*
293907c6692fSMark Johnson 		 * If we are using the copy buffer for anything over the
294007c6692fSMark Johnson 		 * segment boundary, and this page is over the segment
294107c6692fSMark Johnson 		 * boundary.
294207c6692fSMark Johnson 		 *   OR
294307c6692fSMark Johnson 		 * if the DMA engine can't reach the physical address.
294407c6692fSMark Johnson 		 */
294507c6692fSMark Johnson 		if (((sglinfo->si_bounce_on_seg) &&
294607c6692fSMark Johnson 		    ((raddr + psize) > sglinfo->si_segmask)) ||
294707c6692fSMark Johnson 		    ((raddr < addrlo) || ((raddr + psize) > addrhi))) {
294807c6692fSMark Johnson 
294912f080e7Smrj 			sglinfo->si_copybuf_req += MMU_PAGESIZE;
295012f080e7Smrj 
295112f080e7Smrj 			/*
295212f080e7Smrj 			 * if there is something in the current cookie, go to
295312f080e7Smrj 			 * the next one. We only want one page in a cookie which
295412f080e7Smrj 			 * uses the copybuf since the copybuf doesn't have to
295512f080e7Smrj 			 * be physically contiguous.
295612f080e7Smrj 			 */
295712f080e7Smrj 			if (sgl[cnt].dmac_size != 0) {
295812f080e7Smrj 				cnt++;
295912f080e7Smrj 			}
2960843e1988Sjohnlev 			sgl[cnt].dmac_laddress = raddr;
296112f080e7Smrj 			sgl[cnt].dmac_size = psize;
296212f080e7Smrj #if defined(__amd64)
296312f080e7Smrj 			sgl[cnt].dmac_type = ROOTNEX_USES_COPYBUF;
296412f080e7Smrj #else
296512f080e7Smrj 			/*
296612f080e7Smrj 			 * save the buf offset for 32-bit kernel. used in the
296712f080e7Smrj 			 * obsoleted interfaces.
296812f080e7Smrj 			 */
296912f080e7Smrj 			sgl[cnt].dmac_type = ROOTNEX_USES_COPYBUF |
297012f080e7Smrj 			    (dmar_object->dmao_size - size);
297112f080e7Smrj #endif
297212f080e7Smrj 			/* if this isn't the last cookie, go to the next one */
297312f080e7Smrj 			if ((cnt + 1) < sglinfo->si_max_pages) {
297412f080e7Smrj 				cnt++;
297512f080e7Smrj 				sgl[cnt].dmac_laddress = 0;
297612f080e7Smrj 				sgl[cnt].dmac_size = 0;
297712f080e7Smrj 				sgl[cnt].dmac_type = 0;
297812f080e7Smrj 			}
297912f080e7Smrj 
298012f080e7Smrj 		/*
298112f080e7Smrj 		 * this page didn't need the copy buffer, if it's not physically
298212f080e7Smrj 		 * contiguous, or it would put us over a segment boundary, or it
298312f080e7Smrj 		 * puts us over the max cookie size, or the current sgl doesn't
298412f080e7Smrj 		 * have anything in it.
298512f080e7Smrj 		 */
2986843e1988Sjohnlev 		} else if (((last_page + MMU_PAGESIZE) != raddr) ||
2987843e1988Sjohnlev 		    !(raddr & sglinfo->si_segmask) ||
298812f080e7Smrj 		    ((sgl[cnt].dmac_size + psize) > maxseg) ||
298912f080e7Smrj 		    (sgl[cnt].dmac_size == 0)) {
299012f080e7Smrj 			/*
299112f080e7Smrj 			 * if we're not already in a new cookie, go to the next
299212f080e7Smrj 			 * cookie.
299312f080e7Smrj 			 */
299412f080e7Smrj 			if (sgl[cnt].dmac_size != 0) {
299512f080e7Smrj 				cnt++;
299612f080e7Smrj 			}
299712f080e7Smrj 
299812f080e7Smrj 			/* save the cookie information */
2999843e1988Sjohnlev 			sgl[cnt].dmac_laddress = raddr;
300012f080e7Smrj 			sgl[cnt].dmac_size = psize;
300112f080e7Smrj #if defined(__amd64)
300212f080e7Smrj 			sgl[cnt].dmac_type = 0;
300312f080e7Smrj #else
300412f080e7Smrj 			/*
300512f080e7Smrj 			 * save the buf offset for 32-bit kernel. used in the
300612f080e7Smrj 			 * obsoleted interfaces.
300712f080e7Smrj 			 */
300812f080e7Smrj 			sgl[cnt].dmac_type = dmar_object->dmao_size - size;
300912f080e7Smrj #endif
301012f080e7Smrj 
301112f080e7Smrj 		/*
301212f080e7Smrj 		 * this page didn't need the copy buffer, it is physically
301312f080e7Smrj 		 * contiguous with the last page, and it's <= the max cookie
301412f080e7Smrj 		 * size.
301512f080e7Smrj 		 */
301612f080e7Smrj 		} else {
301712f080e7Smrj 			sgl[cnt].dmac_size += psize;
301812f080e7Smrj 
301912f080e7Smrj 			/*
302012f080e7Smrj 			 * if this exactly ==  the maximum cookie size, and
302112f080e7Smrj 			 * it isn't the last cookie, go to the next cookie.
302212f080e7Smrj 			 */
302312f080e7Smrj 			if (((sgl[cnt].dmac_size + psize) == maxseg) &&
302412f080e7Smrj 			    ((cnt + 1) < sglinfo->si_max_pages)) {
302512f080e7Smrj 				cnt++;
302612f080e7Smrj 				sgl[cnt].dmac_laddress = 0;
302712f080e7Smrj 				sgl[cnt].dmac_size = 0;
302812f080e7Smrj 				sgl[cnt].dmac_type = 0;
302912f080e7Smrj 			}
303012f080e7Smrj 		}
303112f080e7Smrj 
303212f080e7Smrj 		/*
303312f080e7Smrj 		 * save this page's physical address so we can figure out if the
303412f080e7Smrj 		 * next page is physically contiguous. Keep decrementing size
303512f080e7Smrj 		 * until we are done with the buffer.
303612f080e7Smrj 		 */
3037843e1988Sjohnlev 		last_page = raddr;
303812f080e7Smrj 		size -= psize;
303912f080e7Smrj 	}
304012f080e7Smrj 
304112f080e7Smrj 	/* we're done, save away how many cookies the sgl has */
304212f080e7Smrj 	if (sgl[cnt].dmac_size == 0) {
304312f080e7Smrj 		ASSERT(cnt < sglinfo->si_max_pages);
304412f080e7Smrj 		sglinfo->si_sgl_size = cnt;
304512f080e7Smrj 	} else {
304612f080e7Smrj 		sglinfo->si_sgl_size = cnt + 1;
304712f080e7Smrj 	}
304812f080e7Smrj }
304912f080e7Smrj 
305012f080e7Smrj /*
305112f080e7Smrj  * rootnex_bind_slowpath()
305212f080e7Smrj  *    Call in the bind path if the calling driver can't use the sgl without
305312f080e7Smrj  *    modifying it. We either need to use the copy buffer and/or we will end up
305412f080e7Smrj  *    with a partial bind.
305512f080e7Smrj  */
305612f080e7Smrj static int
305712f080e7Smrj rootnex_bind_slowpath(ddi_dma_impl_t *hp, struct ddi_dma_req *dmareq,
305812f080e7Smrj     rootnex_dma_t *dma, ddi_dma_attr_t *attr, int kmflag)
305912f080e7Smrj {
306012f080e7Smrj 	rootnex_sglinfo_t *sinfo;
306112f080e7Smrj 	rootnex_window_t *window;
306212f080e7Smrj 	ddi_dma_cookie_t *cookie;
306312f080e7Smrj 	size_t copybuf_used;
306412f080e7Smrj 	size_t dmac_size;
306512f080e7Smrj 	boolean_t partial;
306612f080e7Smrj 	off_t cur_offset;
306712f080e7Smrj 	page_t *cur_pp;
306812f080e7Smrj 	major_t mnum;
306912f080e7Smrj 	int e;
307012f080e7Smrj 	int i;
307112f080e7Smrj 
307212f080e7Smrj 
307312f080e7Smrj 	sinfo = &dma->dp_sglinfo;
307412f080e7Smrj 	copybuf_used = 0;
307512f080e7Smrj 	partial = B_FALSE;
307612f080e7Smrj 
307712f080e7Smrj 	/*
307812f080e7Smrj 	 * If we're using the copybuf, set the copybuf state in dma struct.
307912f080e7Smrj 	 * Needs to be first since it sets the copy buffer size.
308012f080e7Smrj 	 */
308112f080e7Smrj 	if (sinfo->si_copybuf_req != 0) {
308212f080e7Smrj 		e = rootnex_setup_copybuf(hp, dmareq, dma, attr);
308312f080e7Smrj 		if (e != DDI_SUCCESS) {
308412f080e7Smrj 			return (e);
308512f080e7Smrj 		}
308612f080e7Smrj 	} else {
308712f080e7Smrj 		dma->dp_copybuf_size = 0;
308812f080e7Smrj 	}
308912f080e7Smrj 
309012f080e7Smrj 	/*
309112f080e7Smrj 	 * Figure out if we need to do a partial mapping. If so, figure out
309212f080e7Smrj 	 * if we need to trim the buffers when we munge the sgl.
309312f080e7Smrj 	 */
309412f080e7Smrj 	if ((dma->dp_copybuf_size < sinfo->si_copybuf_req) ||
309512f080e7Smrj 	    (dma->dp_dma.dmao_size > dma->dp_maxxfer) ||
309612f080e7Smrj 	    (attr->dma_attr_sgllen < sinfo->si_sgl_size)) {
309712f080e7Smrj 		dma->dp_partial_required = B_TRUE;
309812f080e7Smrj 		if (attr->dma_attr_granular != 1) {
309912f080e7Smrj 			dma->dp_trim_required = B_TRUE;
310012f080e7Smrj 		}
310112f080e7Smrj 	} else {
310212f080e7Smrj 		dma->dp_partial_required = B_FALSE;
310312f080e7Smrj 		dma->dp_trim_required = B_FALSE;
310412f080e7Smrj 	}
310512f080e7Smrj 
310612f080e7Smrj 	/* If we need to do a partial bind, make sure the driver supports it */
310712f080e7Smrj 	if (dma->dp_partial_required &&
310812f080e7Smrj 	    !(dmareq->dmar_flags & DDI_DMA_PARTIAL)) {
310912f080e7Smrj 
311012f080e7Smrj 		mnum = ddi_driver_major(dma->dp_dip);
311112f080e7Smrj 		/*
311212f080e7Smrj 		 * patchable which allows us to print one warning per major
311312f080e7Smrj 		 * number.
311412f080e7Smrj 		 */
311512f080e7Smrj 		if ((rootnex_bind_warn) &&
311612f080e7Smrj 		    ((rootnex_warn_list[mnum] & ROOTNEX_BIND_WARNING) == 0)) {
311712f080e7Smrj 			rootnex_warn_list[mnum] |= ROOTNEX_BIND_WARNING;
311812f080e7Smrj 			cmn_err(CE_WARN, "!%s: coding error detected, the "
311912f080e7Smrj 			    "driver is using ddi_dma_attr(9S) incorrectly. "
312012f080e7Smrj 			    "There is a small risk of data corruption in "
312112f080e7Smrj 			    "particular with large I/Os. The driver should be "
312212f080e7Smrj 			    "replaced with a corrected version for proper "
312312f080e7Smrj 			    "system operation. To disable this warning, add "
312412f080e7Smrj 			    "'set rootnex:rootnex_bind_warn=0' to "
312512f080e7Smrj 			    "/etc/system(4).", ddi_driver_name(dma->dp_dip));
312612f080e7Smrj 		}
312712f080e7Smrj 		return (DDI_DMA_TOOBIG);
312812f080e7Smrj 	}
312912f080e7Smrj 
313012f080e7Smrj 	/*
313112f080e7Smrj 	 * we might need multiple windows, setup state to handle them. In this
313212f080e7Smrj 	 * code path, we will have at least one window.
313312f080e7Smrj 	 */
313412f080e7Smrj 	e = rootnex_setup_windows(hp, dma, attr, kmflag);
313512f080e7Smrj 	if (e != DDI_SUCCESS) {
313612f080e7Smrj 		rootnex_teardown_copybuf(dma);
313712f080e7Smrj 		return (e);
313812f080e7Smrj 	}
313912f080e7Smrj 
314012f080e7Smrj 	window = &dma->dp_window[0];
314112f080e7Smrj 	cookie = &dma->dp_cookies[0];
314212f080e7Smrj 	cur_offset = 0;
314312f080e7Smrj 	rootnex_init_win(hp, dma, window, cookie, cur_offset);
314412f080e7Smrj 	if (dmareq->dmar_object.dmao_type == DMA_OTYP_PAGES) {
314512f080e7Smrj 		cur_pp = dmareq->dmar_object.dmao_obj.pp_obj.pp_pp;
314612f080e7Smrj 	}
314712f080e7Smrj 
314812f080e7Smrj 	/* loop though all the cookies we got back from get_sgl() */
314912f080e7Smrj 	for (i = 0; i < sinfo->si_sgl_size; i++) {
315012f080e7Smrj 		/*
315112f080e7Smrj 		 * If we're using the copy buffer, check this cookie and setup
315212f080e7Smrj 		 * its associated copy buffer state. If this cookie uses the
315312f080e7Smrj 		 * copy buffer, make sure we sync this window during dma_sync.
315412f080e7Smrj 		 */
315512f080e7Smrj 		if (dma->dp_copybuf_size > 0) {
315612f080e7Smrj 			rootnex_setup_cookie(&dmareq->dmar_object, dma, cookie,
315712f080e7Smrj 			    cur_offset, &copybuf_used, &cur_pp);
315812f080e7Smrj 			if (cookie->dmac_type & ROOTNEX_USES_COPYBUF) {
315912f080e7Smrj 				window->wd_dosync = B_TRUE;
316012f080e7Smrj 			}
316112f080e7Smrj 		}
316212f080e7Smrj 
316312f080e7Smrj 		/*
316412f080e7Smrj 		 * save away the cookie size, since it could be modified in
316512f080e7Smrj 		 * the windowing code.
316612f080e7Smrj 		 */
316712f080e7Smrj 		dmac_size = cookie->dmac_size;
316812f080e7Smrj 
316912f080e7Smrj 		/* if we went over max copybuf size */
317012f080e7Smrj 		if (dma->dp_copybuf_size &&
317112f080e7Smrj 		    (copybuf_used > dma->dp_copybuf_size)) {
317212f080e7Smrj 			partial = B_TRUE;
317312f080e7Smrj 			e = rootnex_copybuf_window_boundary(hp, dma, &window,
317412f080e7Smrj 			    cookie, cur_offset, &copybuf_used);
317512f080e7Smrj 			if (e != DDI_SUCCESS) {
317612f080e7Smrj 				rootnex_teardown_copybuf(dma);
317712f080e7Smrj 				rootnex_teardown_windows(dma);
317812f080e7Smrj 				return (e);
317912f080e7Smrj 			}
318012f080e7Smrj 
318112f080e7Smrj 			/*
318212f080e7Smrj 			 * if the coookie uses the copy buffer, make sure the
318312f080e7Smrj 			 * new window we just moved to is set to sync.
318412f080e7Smrj 			 */
318512f080e7Smrj 			if (cookie->dmac_type & ROOTNEX_USES_COPYBUF) {
318612f080e7Smrj 				window->wd_dosync = B_TRUE;
318712f080e7Smrj 			}
3188*ef4ab52fSFrank Van Der Linden 			ROOTNEX_DPROBE1(rootnex__copybuf__window, dev_info_t *,
318912f080e7Smrj 			    dma->dp_dip);
319012f080e7Smrj 
319112f080e7Smrj 		/* if the cookie cnt == max sgllen, move to the next window */
319212f080e7Smrj 		} else if (window->wd_cookie_cnt >= attr->dma_attr_sgllen) {
319312f080e7Smrj 			partial = B_TRUE;
319412f080e7Smrj 			ASSERT(window->wd_cookie_cnt == attr->dma_attr_sgllen);
319512f080e7Smrj 			e = rootnex_sgllen_window_boundary(hp, dma, &window,
319612f080e7Smrj 			    cookie, attr, cur_offset);
319712f080e7Smrj 			if (e != DDI_SUCCESS) {
319812f080e7Smrj 				rootnex_teardown_copybuf(dma);
319912f080e7Smrj 				rootnex_teardown_windows(dma);
320012f080e7Smrj 				return (e);
320112f080e7Smrj 			}
320212f080e7Smrj 
320312f080e7Smrj 			/*
320412f080e7Smrj 			 * if the coookie uses the copy buffer, make sure the
320512f080e7Smrj 			 * new window we just moved to is set to sync.
320612f080e7Smrj 			 */
320712f080e7Smrj 			if (cookie->dmac_type & ROOTNEX_USES_COPYBUF) {
320812f080e7Smrj 				window->wd_dosync = B_TRUE;
320912f080e7Smrj 			}
3210*ef4ab52fSFrank Van Der Linden 			ROOTNEX_DPROBE1(rootnex__sgllen__window, dev_info_t *,
321112f080e7Smrj 			    dma->dp_dip);
321212f080e7Smrj 
321312f080e7Smrj 		/* else if we will be over maxxfer */
321412f080e7Smrj 		} else if ((window->wd_size + dmac_size) >
321512f080e7Smrj 		    dma->dp_maxxfer) {
321612f080e7Smrj 			partial = B_TRUE;
321712f080e7Smrj 			e = rootnex_maxxfer_window_boundary(hp, dma, &window,
321812f080e7Smrj 			    cookie);
321912f080e7Smrj 			if (e != DDI_SUCCESS) {
322012f080e7Smrj 				rootnex_teardown_copybuf(dma);
322112f080e7Smrj 				rootnex_teardown_windows(dma);
322212f080e7Smrj 				return (e);
322312f080e7Smrj 			}
322412f080e7Smrj 
322512f080e7Smrj 			/*
322612f080e7Smrj 			 * if the coookie uses the copy buffer, make sure the
322712f080e7Smrj 			 * new window we just moved to is set to sync.
322812f080e7Smrj 			 */
322912f080e7Smrj 			if (cookie->dmac_type & ROOTNEX_USES_COPYBUF) {
323012f080e7Smrj 				window->wd_dosync = B_TRUE;
323112f080e7Smrj 			}
3232*ef4ab52fSFrank Van Der Linden 			ROOTNEX_DPROBE1(rootnex__maxxfer__window, dev_info_t *,
323312f080e7Smrj 			    dma->dp_dip);
323412f080e7Smrj 
323512f080e7Smrj 		/* else this cookie fits in the current window */
323612f080e7Smrj 		} else {
323712f080e7Smrj 			window->wd_cookie_cnt++;
323812f080e7Smrj 			window->wd_size += dmac_size;
323912f080e7Smrj 		}
324012f080e7Smrj 
324112f080e7Smrj 		/* track our offset into the buffer, go to the next cookie */
324212f080e7Smrj 		ASSERT(dmac_size <= dma->dp_dma.dmao_size);
324312f080e7Smrj 		ASSERT(cookie->dmac_size <= dmac_size);
324412f080e7Smrj 		cur_offset += dmac_size;
324512f080e7Smrj 		cookie++;
324612f080e7Smrj 	}
324712f080e7Smrj 
324812f080e7Smrj 	/* if we ended up with a zero sized window in the end, clean it up */
324912f080e7Smrj 	if (window->wd_size == 0) {
325012f080e7Smrj 		hp->dmai_nwin--;
325112f080e7Smrj 		window--;
325212f080e7Smrj 	}
325312f080e7Smrj 
325412f080e7Smrj 	ASSERT(window->wd_trim.tr_trim_last == B_FALSE);
325512f080e7Smrj 
325612f080e7Smrj 	if (!partial) {
325712f080e7Smrj 		return (DDI_DMA_MAPPED);
325812f080e7Smrj 	}
325912f080e7Smrj 
326012f080e7Smrj 	ASSERT(dma->dp_partial_required);
326112f080e7Smrj 	return (DDI_DMA_PARTIAL_MAP);
326212f080e7Smrj }
326312f080e7Smrj 
326412f080e7Smrj 
326512f080e7Smrj /*
326612f080e7Smrj  * rootnex_setup_copybuf()
326712f080e7Smrj  *    Called in bind slowpath. Figures out if we're going to use the copy
326812f080e7Smrj  *    buffer, and if we do, sets up the basic state to handle it.
326912f080e7Smrj  */
327012f080e7Smrj static int
327112f080e7Smrj rootnex_setup_copybuf(ddi_dma_impl_t *hp, struct ddi_dma_req *dmareq,
327212f080e7Smrj     rootnex_dma_t *dma, ddi_dma_attr_t *attr)
327312f080e7Smrj {
327412f080e7Smrj 	rootnex_sglinfo_t *sinfo;
327512f080e7Smrj 	ddi_dma_attr_t lattr;
327612f080e7Smrj 	size_t max_copybuf;
327712f080e7Smrj 	int cansleep;
327812f080e7Smrj 	int e;
327912f080e7Smrj #if !defined(__amd64)
328012f080e7Smrj 	int vmflag;
328112f080e7Smrj #endif
328212f080e7Smrj 
328312f080e7Smrj 
328412f080e7Smrj 	sinfo = &dma->dp_sglinfo;
328512f080e7Smrj 
328636945f79Smrj 	/* read this first so it's consistent through the routine  */
328736945f79Smrj 	max_copybuf = i_ddi_copybuf_size() & MMU_PAGEMASK;
328812f080e7Smrj 
328912f080e7Smrj 	/* We need to call into the rootnex on ddi_dma_sync() */
329012f080e7Smrj 	hp->dmai_rflags &= ~DMP_NOSYNC;
329112f080e7Smrj 
329212f080e7Smrj 	/* make sure the copybuf size <= the max size */
329312f080e7Smrj 	dma->dp_copybuf_size = MIN(sinfo->si_copybuf_req, max_copybuf);
329412f080e7Smrj 	ASSERT((dma->dp_copybuf_size & MMU_PAGEOFFSET) == 0);
329512f080e7Smrj 
329612f080e7Smrj #if !defined(__amd64)
329712f080e7Smrj 	/*
329812f080e7Smrj 	 * if we don't have kva space to copy to/from, allocate the KVA space
329912f080e7Smrj 	 * now. We only do this for the 32-bit kernel. We use seg kpm space for
330012f080e7Smrj 	 * the 64-bit kernel.
330112f080e7Smrj 	 */
330212f080e7Smrj 	if ((dmareq->dmar_object.dmao_type == DMA_OTYP_PAGES) ||
330312f080e7Smrj 	    (dmareq->dmar_object.dmao_obj.virt_obj.v_as != NULL)) {
330412f080e7Smrj 
330512f080e7Smrj 		/* convert the sleep flags */
330612f080e7Smrj 		if (dmareq->dmar_fp == DDI_DMA_SLEEP) {
330712f080e7Smrj 			vmflag = VM_SLEEP;
330812f080e7Smrj 		} else {
330912f080e7Smrj 			vmflag = VM_NOSLEEP;
331012f080e7Smrj 		}
331112f080e7Smrj 
331212f080e7Smrj 		/* allocate Kernel VA space that we can bcopy to/from */
331312f080e7Smrj 		dma->dp_kva = vmem_alloc(heap_arena, dma->dp_copybuf_size,
331412f080e7Smrj 		    vmflag);
331512f080e7Smrj 		if (dma->dp_kva == NULL) {
331612f080e7Smrj 			return (DDI_DMA_NORESOURCES);
331712f080e7Smrj 		}
331812f080e7Smrj 	}
331912f080e7Smrj #endif
332012f080e7Smrj 
332112f080e7Smrj 	/* convert the sleep flags */
332212f080e7Smrj 	if (dmareq->dmar_fp == DDI_DMA_SLEEP) {
332312f080e7Smrj 		cansleep = 1;
332412f080e7Smrj 	} else {
332512f080e7Smrj 		cansleep = 0;
332612f080e7Smrj 	}
332712f080e7Smrj 
332812f080e7Smrj 	/*
3329d21b39ddSmrj 	 * Allocate the actual copy buffer. This needs to fit within the DMA
3330d21b39ddSmrj 	 * engine limits, so we can't use kmem_alloc... We don't need
3331d21b39ddSmrj 	 * contiguous memory (sgllen) since we will be forcing windows on
3332d21b39ddSmrj 	 * sgllen anyway.
333312f080e7Smrj 	 */
333412f080e7Smrj 	lattr = *attr;
333512f080e7Smrj 	lattr.dma_attr_align = MMU_PAGESIZE;
3336d21b39ddSmrj 	/*
3337d21b39ddSmrj 	 * this should be < 0 to indicate no limit, but due to a bug in
3338d21b39ddSmrj 	 * the rootnex, we'll set it to the maximum positive int.
3339d21b39ddSmrj 	 */
3340d21b39ddSmrj 	lattr.dma_attr_sgllen = 0x7fffffff;
334107c6692fSMark Johnson 	/*
334207c6692fSMark Johnson 	 * if we're using the copy buffer because of seg, use that for our
334307c6692fSMark Johnson 	 * upper address limit.
334407c6692fSMark Johnson 	 */
334507c6692fSMark Johnson 	if (sinfo->si_bounce_on_seg) {
334607c6692fSMark Johnson 		lattr.dma_attr_addr_hi = lattr.dma_attr_seg;
334707c6692fSMark Johnson 	}
334812f080e7Smrj 	e = i_ddi_mem_alloc(dma->dp_dip, &lattr, dma->dp_copybuf_size, cansleep,
334912f080e7Smrj 	    0, NULL, &dma->dp_cbaddr, &dma->dp_cbsize, NULL);
335012f080e7Smrj 	if (e != DDI_SUCCESS) {
335112f080e7Smrj #if !defined(__amd64)
335212f080e7Smrj 		if (dma->dp_kva != NULL) {
335312f080e7Smrj 			vmem_free(heap_arena, dma->dp_kva,
335412f080e7Smrj 			    dma->dp_copybuf_size);
335512f080e7Smrj 		}
335612f080e7Smrj #endif
335712f080e7Smrj 		return (DDI_DMA_NORESOURCES);
335812f080e7Smrj 	}
335912f080e7Smrj 
3360*ef4ab52fSFrank Van Der Linden 	ROOTNEX_DPROBE2(rootnex__alloc__copybuf, dev_info_t *, dma->dp_dip,
336112f080e7Smrj 	    size_t, dma->dp_copybuf_size);
336212f080e7Smrj 
336312f080e7Smrj 	return (DDI_SUCCESS);
336412f080e7Smrj }
336512f080e7Smrj 
336612f080e7Smrj 
336712f080e7Smrj /*
336812f080e7Smrj  * rootnex_setup_windows()
336912f080e7Smrj  *    Called in bind slowpath to setup the window state. We always have windows
337012f080e7Smrj  *    in the slowpath. Even if the window count = 1.
337112f080e7Smrj  */
337212f080e7Smrj static int
337312f080e7Smrj rootnex_setup_windows(ddi_dma_impl_t *hp, rootnex_dma_t *dma,
337412f080e7Smrj     ddi_dma_attr_t *attr, int kmflag)
337512f080e7Smrj {
337612f080e7Smrj 	rootnex_window_t *windowp;
337712f080e7Smrj 	rootnex_sglinfo_t *sinfo;
337812f080e7Smrj 	size_t copy_state_size;
337912f080e7Smrj 	size_t win_state_size;
338012f080e7Smrj 	size_t state_available;
338112f080e7Smrj 	size_t space_needed;
338212f080e7Smrj 	uint_t copybuf_win;
338312f080e7Smrj 	uint_t maxxfer_win;
338412f080e7Smrj 	size_t space_used;
338512f080e7Smrj 	uint_t sglwin;
338612f080e7Smrj 
338712f080e7Smrj 
338812f080e7Smrj 	sinfo = &dma->dp_sglinfo;
338912f080e7Smrj 
339012f080e7Smrj 	dma->dp_current_win = 0;
339112f080e7Smrj 	hp->dmai_nwin = 0;
339212f080e7Smrj 
339312f080e7Smrj 	/* If we don't need to do a partial, we only have one window */
339412f080e7Smrj 	if (!dma->dp_partial_required) {
339512f080e7Smrj 		dma->dp_max_win = 1;
339612f080e7Smrj 
339712f080e7Smrj 	/*
339812f080e7Smrj 	 * we need multiple windows, need to figure out the worse case number
339912f080e7Smrj 	 * of windows.
340012f080e7Smrj 	 */
34017c478bd9Sstevel@tonic-gate 	} else {
34027c478bd9Sstevel@tonic-gate 		/*
340312f080e7Smrj 		 * if we need windows because we need more copy buffer that
340412f080e7Smrj 		 * we allow, the worse case number of windows we could need
340512f080e7Smrj 		 * here would be (copybuf space required / copybuf space that
340612f080e7Smrj 		 * we have) plus one for remainder, and plus 2 to handle the
340712f080e7Smrj 		 * extra pages on the trim for the first and last pages of the
340812f080e7Smrj 		 * buffer (a page is the minimum window size so under the right
340912f080e7Smrj 		 * attr settings, you could have a window for each page).
341012f080e7Smrj 		 * The last page will only be hit here if the size is not a
341112f080e7Smrj 		 * multiple of the granularity (which theoretically shouldn't
341212f080e7Smrj 		 * be the case but never has been enforced, so we could have
341312f080e7Smrj 		 * broken things without it).
34147c478bd9Sstevel@tonic-gate 		 */
341512f080e7Smrj 		if (sinfo->si_copybuf_req > dma->dp_copybuf_size) {
341612f080e7Smrj 			ASSERT(dma->dp_copybuf_size > 0);
341712f080e7Smrj 			copybuf_win = (sinfo->si_copybuf_req /
341812f080e7Smrj 			    dma->dp_copybuf_size) + 1 + 2;
34197c478bd9Sstevel@tonic-gate 		} else {
342012f080e7Smrj 			copybuf_win = 0;
34217c478bd9Sstevel@tonic-gate 		}
342212f080e7Smrj 
342312f080e7Smrj 		/*
342412f080e7Smrj 		 * if we need windows because we have more cookies than the H/W
342512f080e7Smrj 		 * can handle, the number of windows we would need here would
3426b57cd2d3SMark Johnson 		 * be (cookie count / cookies count H/W supports minus 1[for
3427b57cd2d3SMark Johnson 		 * trim]) plus one for remainder.
342812f080e7Smrj 		 */
342912f080e7Smrj 		if (attr->dma_attr_sgllen < sinfo->si_sgl_size) {
3430b57cd2d3SMark Johnson 			sglwin = (sinfo->si_sgl_size /
3431b57cd2d3SMark Johnson 			    (attr->dma_attr_sgllen - 1)) + 1;
34327c478bd9Sstevel@tonic-gate 		} else {
343312f080e7Smrj 			sglwin = 0;
34347c478bd9Sstevel@tonic-gate 		}
343512f080e7Smrj 
343612f080e7Smrj 		/*
343712f080e7Smrj 		 * if we need windows because we're binding more memory than the
343812f080e7Smrj 		 * H/W can transfer at once, the number of windows we would need
343912f080e7Smrj 		 * here would be (xfer count / max xfer H/W supports) plus one
344012f080e7Smrj 		 * for remainder, and plus 2 to handle the extra pages on the
344112f080e7Smrj 		 * trim (see above comment about trim)
344212f080e7Smrj 		 */
344312f080e7Smrj 		if (dma->dp_dma.dmao_size > dma->dp_maxxfer) {
344412f080e7Smrj 			maxxfer_win = (dma->dp_dma.dmao_size /
344512f080e7Smrj 			    dma->dp_maxxfer) + 1 + 2;
344612f080e7Smrj 		} else {
344712f080e7Smrj 			maxxfer_win = 0;
34487c478bd9Sstevel@tonic-gate 		}
344912f080e7Smrj 		dma->dp_max_win =  copybuf_win + sglwin + maxxfer_win;
345012f080e7Smrj 		ASSERT(dma->dp_max_win > 0);
345112f080e7Smrj 	}
345212f080e7Smrj 	win_state_size = dma->dp_max_win * sizeof (rootnex_window_t);
345312f080e7Smrj 
345412f080e7Smrj 	/*
345512f080e7Smrj 	 * Get space for window and potential copy buffer state. Before we
345612f080e7Smrj 	 * go and allocate memory, see if we can get away with using what's
345712f080e7Smrj 	 * left in the pre-allocted state or the dynamically allocated sgl.
345812f080e7Smrj 	 */
345912f080e7Smrj 	space_used = (uintptr_t)(sinfo->si_sgl_size *
346012f080e7Smrj 	    sizeof (ddi_dma_cookie_t));
346112f080e7Smrj 
346212f080e7Smrj 	/* if we dynamically allocated space for the cookies */
346312f080e7Smrj 	if (dma->dp_need_to_free_cookie) {
346412f080e7Smrj 		/* if we have more space in the pre-allocted buffer, use it */
346512f080e7Smrj 		ASSERT(space_used <= dma->dp_cookie_size);
346612f080e7Smrj 		if ((dma->dp_cookie_size - space_used) <=
346712f080e7Smrj 		    rootnex_state->r_prealloc_size) {
346812f080e7Smrj 			state_available = rootnex_state->r_prealloc_size;
346912f080e7Smrj 			windowp = (rootnex_window_t *)dma->dp_prealloc_buffer;
347012f080e7Smrj 
347112f080e7Smrj 		/*
347212f080e7Smrj 		 * else, we have more free space in the dynamically allocated
347312f080e7Smrj 		 * buffer, i.e. the buffer wasn't worse case fragmented so we
347412f080e7Smrj 		 * didn't need a lot of cookies.
347512f080e7Smrj 		 */
347612f080e7Smrj 		} else {
347712f080e7Smrj 			state_available = dma->dp_cookie_size - space_used;
347812f080e7Smrj 			windowp = (rootnex_window_t *)
347912f080e7Smrj 			    &dma->dp_cookies[sinfo->si_sgl_size];
348012f080e7Smrj 		}
348112f080e7Smrj 
348212f080e7Smrj 	/* we used the pre-alloced buffer */
348312f080e7Smrj 	} else {
348412f080e7Smrj 		ASSERT(space_used <= rootnex_state->r_prealloc_size);
348512f080e7Smrj 		state_available = rootnex_state->r_prealloc_size - space_used;
348612f080e7Smrj 		windowp = (rootnex_window_t *)
348712f080e7Smrj 		    &dma->dp_cookies[sinfo->si_sgl_size];
348812f080e7Smrj 	}
348912f080e7Smrj 
349012f080e7Smrj 	/*
349112f080e7Smrj 	 * figure out how much state we need to track the copy buffer. Add an
349212f080e7Smrj 	 * addition 8 bytes for pointer alignemnt later.
349312f080e7Smrj 	 */
349412f080e7Smrj 	if (dma->dp_copybuf_size > 0) {
349512f080e7Smrj 		copy_state_size = sinfo->si_max_pages *
349612f080e7Smrj 		    sizeof (rootnex_pgmap_t);
349712f080e7Smrj 	} else {
349812f080e7Smrj 		copy_state_size = 0;
349912f080e7Smrj 	}
350012f080e7Smrj 	/* add an additional 8 bytes for pointer alignment */
350112f080e7Smrj 	space_needed = win_state_size + copy_state_size + 0x8;
350212f080e7Smrj 
350312f080e7Smrj 	/* if we have enough space already, use it */
350412f080e7Smrj 	if (state_available >= space_needed) {
350512f080e7Smrj 		dma->dp_window = windowp;
350612f080e7Smrj 		dma->dp_need_to_free_window = B_FALSE;
350712f080e7Smrj 
350812f080e7Smrj 	/* not enough space, need to allocate more. */
350912f080e7Smrj 	} else {
351012f080e7Smrj 		dma->dp_window = kmem_alloc(space_needed, kmflag);
351112f080e7Smrj 		if (dma->dp_window == NULL) {
351212f080e7Smrj 			return (DDI_DMA_NORESOURCES);
351312f080e7Smrj 		}
351412f080e7Smrj 		dma->dp_need_to_free_window = B_TRUE;
351512f080e7Smrj 		dma->dp_window_size = space_needed;
3516*ef4ab52fSFrank Van Der Linden 		ROOTNEX_DPROBE2(rootnex__bind__sp__alloc, dev_info_t *,
351712f080e7Smrj 		    dma->dp_dip, size_t, space_needed);
351812f080e7Smrj 	}
351912f080e7Smrj 
352012f080e7Smrj 	/*
352112f080e7Smrj 	 * we allocate copy buffer state and window state at the same time.
352212f080e7Smrj 	 * setup our copy buffer state pointers. Make sure it's aligned.
352312f080e7Smrj 	 */
352412f080e7Smrj 	if (dma->dp_copybuf_size > 0) {
352512f080e7Smrj 		dma->dp_pgmap = (rootnex_pgmap_t *)(((uintptr_t)
352612f080e7Smrj 		    &dma->dp_window[dma->dp_max_win] + 0x7) & ~0x7);
352712f080e7Smrj 
352812f080e7Smrj #if !defined(__amd64)
352912f080e7Smrj 		/*
353012f080e7Smrj 		 * make sure all pm_mapped, pm_vaddr, and pm_pp are set to
353112f080e7Smrj 		 * false/NULL. Should be quicker to bzero vs loop and set.
353212f080e7Smrj 		 */
353312f080e7Smrj 		bzero(dma->dp_pgmap, copy_state_size);
353412f080e7Smrj #endif
353512f080e7Smrj 	} else {
353612f080e7Smrj 		dma->dp_pgmap = NULL;
353712f080e7Smrj 	}
353812f080e7Smrj 
353912f080e7Smrj 	return (DDI_SUCCESS);
354012f080e7Smrj }
354112f080e7Smrj 
354212f080e7Smrj 
354312f080e7Smrj /*
354412f080e7Smrj  * rootnex_teardown_copybuf()
354512f080e7Smrj  *    cleans up after rootnex_setup_copybuf()
354612f080e7Smrj  */
354712f080e7Smrj static void
354812f080e7Smrj rootnex_teardown_copybuf(rootnex_dma_t *dma)
354912f080e7Smrj {
355012f080e7Smrj #if !defined(__amd64)
355112f080e7Smrj 	int i;
355212f080e7Smrj 
355312f080e7Smrj 	/*
355412f080e7Smrj 	 * if we allocated kernel heap VMEM space, go through all the pages and
355512f080e7Smrj 	 * map out any of the ones that we're mapped into the kernel heap VMEM
355612f080e7Smrj 	 * arena. Then free the VMEM space.
355712f080e7Smrj 	 */
355812f080e7Smrj 	if (dma->dp_kva != NULL) {
355912f080e7Smrj 		for (i = 0; i < dma->dp_sglinfo.si_max_pages; i++) {
356012f080e7Smrj 			if (dma->dp_pgmap[i].pm_mapped) {
356112f080e7Smrj 				hat_unload(kas.a_hat, dma->dp_pgmap[i].pm_kaddr,
356212f080e7Smrj 				    MMU_PAGESIZE, HAT_UNLOAD);
356312f080e7Smrj 				dma->dp_pgmap[i].pm_mapped = B_FALSE;
356412f080e7Smrj 			}
356512f080e7Smrj 		}
356612f080e7Smrj 
356712f080e7Smrj 		vmem_free(heap_arena, dma->dp_kva, dma->dp_copybuf_size);
356812f080e7Smrj 	}
356912f080e7Smrj 
357012f080e7Smrj #endif
357112f080e7Smrj 
357212f080e7Smrj 	/* if we allocated a copy buffer, free it */
357312f080e7Smrj 	if (dma->dp_cbaddr != NULL) {
35747b93957cSeota 		i_ddi_mem_free(dma->dp_cbaddr, NULL);
357512f080e7Smrj 	}
357612f080e7Smrj }
357712f080e7Smrj 
357812f080e7Smrj 
357912f080e7Smrj /*
358012f080e7Smrj  * rootnex_teardown_windows()
358112f080e7Smrj  *    cleans up after rootnex_setup_windows()
358212f080e7Smrj  */
358312f080e7Smrj static void
358412f080e7Smrj rootnex_teardown_windows(rootnex_dma_t *dma)
358512f080e7Smrj {
358612f080e7Smrj 	/*
358712f080e7Smrj 	 * if we had to allocate window state on the last bind (because we
358812f080e7Smrj 	 * didn't have enough pre-allocated space in the handle), free it.
358912f080e7Smrj 	 */
359012f080e7Smrj 	if (dma->dp_need_to_free_window) {
359112f080e7Smrj 		kmem_free(dma->dp_window, dma->dp_window_size);
359212f080e7Smrj 	}
359312f080e7Smrj }
359412f080e7Smrj 
359512f080e7Smrj 
359612f080e7Smrj /*
359712f080e7Smrj  * rootnex_init_win()
359812f080e7Smrj  *    Called in bind slow path during creation of a new window. Initializes
359912f080e7Smrj  *    window state to default values.
360012f080e7Smrj  */
360112f080e7Smrj /*ARGSUSED*/
360212f080e7Smrj static void
360312f080e7Smrj rootnex_init_win(ddi_dma_impl_t *hp, rootnex_dma_t *dma,
360412f080e7Smrj     rootnex_window_t *window, ddi_dma_cookie_t *cookie, off_t cur_offset)
360512f080e7Smrj {
360612f080e7Smrj 	hp->dmai_nwin++;
360712f080e7Smrj 	window->wd_dosync = B_FALSE;
360812f080e7Smrj 	window->wd_offset = cur_offset;
360912f080e7Smrj 	window->wd_size = 0;
361012f080e7Smrj 	window->wd_first_cookie = cookie;
361112f080e7Smrj 	window->wd_cookie_cnt = 0;
361212f080e7Smrj 	window->wd_trim.tr_trim_first = B_FALSE;
361312f080e7Smrj 	window->wd_trim.tr_trim_last = B_FALSE;
361412f080e7Smrj 	window->wd_trim.tr_first_copybuf_win = B_FALSE;
361512f080e7Smrj 	window->wd_trim.tr_last_copybuf_win = B_FALSE;
361612f080e7Smrj #if !defined(__amd64)
361712f080e7Smrj 	window->wd_remap_copybuf = dma->dp_cb_remaping;
361812f080e7Smrj #endif
361912f080e7Smrj }
362012f080e7Smrj 
362112f080e7Smrj 
362212f080e7Smrj /*
362312f080e7Smrj  * rootnex_setup_cookie()
362412f080e7Smrj  *    Called in the bind slow path when the sgl uses the copy buffer. If any of
362512f080e7Smrj  *    the sgl uses the copy buffer, we need to go through each cookie, figure
362612f080e7Smrj  *    out if it uses the copy buffer, and if it does, save away everything we'll
362712f080e7Smrj  *    need during sync.
362812f080e7Smrj  */
362912f080e7Smrj static void
363012f080e7Smrj rootnex_setup_cookie(ddi_dma_obj_t *dmar_object, rootnex_dma_t *dma,
363112f080e7Smrj     ddi_dma_cookie_t *cookie, off_t cur_offset, size_t *copybuf_used,
363212f080e7Smrj     page_t **cur_pp)
363312f080e7Smrj {
363412f080e7Smrj 	boolean_t copybuf_sz_power_2;
363512f080e7Smrj 	rootnex_sglinfo_t *sinfo;
3636843e1988Sjohnlev 	paddr_t paddr;
363712f080e7Smrj 	uint_t pidx;
363812f080e7Smrj 	uint_t pcnt;
363912f080e7Smrj 	off_t poff;
364012f080e7Smrj #if defined(__amd64)
364112f080e7Smrj 	pfn_t pfn;
364212f080e7Smrj #else
364312f080e7Smrj 	page_t **pplist;
364412f080e7Smrj #endif
364512f080e7Smrj 
364612f080e7Smrj 	sinfo = &dma->dp_sglinfo;
364712f080e7Smrj 
364812f080e7Smrj 	/*
364912f080e7Smrj 	 * Calculate the page index relative to the start of the buffer. The
365012f080e7Smrj 	 * index to the current page for our buffer is the offset into the
365112f080e7Smrj 	 * first page of the buffer plus our current offset into the buffer
365212f080e7Smrj 	 * itself, shifted of course...
365312f080e7Smrj 	 */
365412f080e7Smrj 	pidx = (sinfo->si_buf_offset + cur_offset) >> MMU_PAGESHIFT;
365512f080e7Smrj 	ASSERT(pidx < sinfo->si_max_pages);
365612f080e7Smrj 
365712f080e7Smrj 	/* if this cookie uses the copy buffer */
365812f080e7Smrj 	if (cookie->dmac_type & ROOTNEX_USES_COPYBUF) {
365912f080e7Smrj 		/*
366012f080e7Smrj 		 * NOTE: we know that since this cookie uses the copy buffer, it
366112f080e7Smrj 		 * is <= MMU_PAGESIZE.
366212f080e7Smrj 		 */
366312f080e7Smrj 
366412f080e7Smrj 		/*
366512f080e7Smrj 		 * get the offset into the page. For the 64-bit kernel, get the
366612f080e7Smrj 		 * pfn which we'll use with seg kpm.
366712f080e7Smrj 		 */
3668843e1988Sjohnlev 		poff = cookie->dmac_laddress & MMU_PAGEOFFSET;
366912f080e7Smrj #if defined(__amd64)
3670843e1988Sjohnlev 		/* mfn_to_pfn() is a NOP on i86pc */
3671843e1988Sjohnlev 		pfn = mfn_to_pfn(cookie->dmac_laddress >> MMU_PAGESHIFT);
3672843e1988Sjohnlev #endif /* __amd64 */
367312f080e7Smrj 
367412f080e7Smrj 		/* figure out if the copybuf size is a power of 2 */
367512f080e7Smrj 		if (dma->dp_copybuf_size & (dma->dp_copybuf_size - 1)) {
367612f080e7Smrj 			copybuf_sz_power_2 = B_FALSE;
367712f080e7Smrj 		} else {
367812f080e7Smrj 			copybuf_sz_power_2 = B_TRUE;
367912f080e7Smrj 		}
368012f080e7Smrj 
368112f080e7Smrj 		/* This page uses the copy buffer */
368212f080e7Smrj 		dma->dp_pgmap[pidx].pm_uses_copybuf = B_TRUE;
368312f080e7Smrj 
368412f080e7Smrj 		/*
368512f080e7Smrj 		 * save the copy buffer KVA that we'll use with this page.
368612f080e7Smrj 		 * if we still fit within the copybuf, it's a simple add.
368712f080e7Smrj 		 * otherwise, we need to wrap over using & or % accordingly.
368812f080e7Smrj 		 */
368912f080e7Smrj 		if ((*copybuf_used + MMU_PAGESIZE) <= dma->dp_copybuf_size) {
369012f080e7Smrj 			dma->dp_pgmap[pidx].pm_cbaddr = dma->dp_cbaddr +
369112f080e7Smrj 			    *copybuf_used;
369212f080e7Smrj 		} else {
369312f080e7Smrj 			if (copybuf_sz_power_2) {
369412f080e7Smrj 				dma->dp_pgmap[pidx].pm_cbaddr = (caddr_t)(
369512f080e7Smrj 				    (uintptr_t)dma->dp_cbaddr +
369612f080e7Smrj 				    (*copybuf_used &
369712f080e7Smrj 				    (dma->dp_copybuf_size - 1)));
369812f080e7Smrj 			} else {
369912f080e7Smrj 				dma->dp_pgmap[pidx].pm_cbaddr = (caddr_t)(
370012f080e7Smrj 				    (uintptr_t)dma->dp_cbaddr +
370112f080e7Smrj 				    (*copybuf_used % dma->dp_copybuf_size));
370212f080e7Smrj 			}
370312f080e7Smrj 		}
370412f080e7Smrj 
370512f080e7Smrj 		/*
370612f080e7Smrj 		 * over write the cookie physical address with the address of
370712f080e7Smrj 		 * the physical address of the copy buffer page that we will
370812f080e7Smrj 		 * use.
370912f080e7Smrj 		 */
3710843e1988Sjohnlev 		paddr = pfn_to_pa(hat_getpfnum(kas.a_hat,
371112f080e7Smrj 		    dma->dp_pgmap[pidx].pm_cbaddr)) + poff;
371212f080e7Smrj 
3713843e1988Sjohnlev #ifdef __xpv
3714843e1988Sjohnlev 		/*
3715843e1988Sjohnlev 		 * If we're dom0, we're using a real device so we need to load
3716843e1988Sjohnlev 		 * the cookies with MAs instead of PAs.
3717843e1988Sjohnlev 		 */
3718843e1988Sjohnlev 		cookie->dmac_laddress = ROOTNEX_PADDR_TO_RBASE(xen_info, paddr);
3719843e1988Sjohnlev #else
3720843e1988Sjohnlev 		cookie->dmac_laddress = paddr;
3721843e1988Sjohnlev #endif
3722843e1988Sjohnlev 
372312f080e7Smrj 		/* if we have a kernel VA, it's easy, just save that address */
372412f080e7Smrj 		if ((dmar_object->dmao_type != DMA_OTYP_PAGES) &&
372512f080e7Smrj 		    (sinfo->si_asp == &kas)) {
372612f080e7Smrj 			/*
372712f080e7Smrj 			 * save away the page aligned virtual address of the
372812f080e7Smrj 			 * driver buffer. Offsets are handled in the sync code.
372912f080e7Smrj 			 */
373012f080e7Smrj 			dma->dp_pgmap[pidx].pm_kaddr = (caddr_t)(((uintptr_t)
373112f080e7Smrj 			    dmar_object->dmao_obj.virt_obj.v_addr + cur_offset)
373212f080e7Smrj 			    & MMU_PAGEMASK);
373312f080e7Smrj #if !defined(__amd64)
373412f080e7Smrj 			/*
373512f080e7Smrj 			 * we didn't need to, and will never need to map this
373612f080e7Smrj 			 * page.
373712f080e7Smrj 			 */
373812f080e7Smrj 			dma->dp_pgmap[pidx].pm_mapped = B_FALSE;
373912f080e7Smrj #endif
374012f080e7Smrj 
374112f080e7Smrj 		/* we don't have a kernel VA. We need one for the bcopy. */
374212f080e7Smrj 		} else {
374312f080e7Smrj #if defined(__amd64)
374412f080e7Smrj 			/*
374512f080e7Smrj 			 * for the 64-bit kernel, it's easy. We use seg kpm to
374612f080e7Smrj 			 * get a Kernel VA for the corresponding pfn.
374712f080e7Smrj 			 */
374812f080e7Smrj 			dma->dp_pgmap[pidx].pm_kaddr = hat_kpm_pfn2va(pfn);
374912f080e7Smrj #else
375012f080e7Smrj 			/*
375112f080e7Smrj 			 * for the 32-bit kernel, this is a pain. First we'll
375212f080e7Smrj 			 * save away the page_t or user VA for this page. This
375312f080e7Smrj 			 * is needed in rootnex_dma_win() when we switch to a
375412f080e7Smrj 			 * new window which requires us to re-map the copy
375512f080e7Smrj 			 * buffer.
375612f080e7Smrj 			 */
375712f080e7Smrj 			pplist = dmar_object->dmao_obj.virt_obj.v_priv;
375812f080e7Smrj 			if (dmar_object->dmao_type == DMA_OTYP_PAGES) {
375912f080e7Smrj 				dma->dp_pgmap[pidx].pm_pp = *cur_pp;
376012f080e7Smrj 				dma->dp_pgmap[pidx].pm_vaddr = NULL;
376112f080e7Smrj 			} else if (pplist != NULL) {
376212f080e7Smrj 				dma->dp_pgmap[pidx].pm_pp = pplist[pidx];
376312f080e7Smrj 				dma->dp_pgmap[pidx].pm_vaddr = NULL;
376412f080e7Smrj 			} else {
376512f080e7Smrj 				dma->dp_pgmap[pidx].pm_pp = NULL;
376612f080e7Smrj 				dma->dp_pgmap[pidx].pm_vaddr = (caddr_t)
376712f080e7Smrj 				    (((uintptr_t)
376812f080e7Smrj 				    dmar_object->dmao_obj.virt_obj.v_addr +
376912f080e7Smrj 				    cur_offset) & MMU_PAGEMASK);
377012f080e7Smrj 			}
377112f080e7Smrj 
377212f080e7Smrj 			/*
377312f080e7Smrj 			 * save away the page aligned virtual address which was
377412f080e7Smrj 			 * allocated from the kernel heap arena (taking into
377512f080e7Smrj 			 * account if we need more copy buffer than we alloced
377612f080e7Smrj 			 * and use multiple windows to handle this, i.e. &,%).
377712f080e7Smrj 			 * NOTE: there isn't and physical memory backing up this
377812f080e7Smrj 			 * virtual address space currently.
377912f080e7Smrj 			 */
378012f080e7Smrj 			if ((*copybuf_used + MMU_PAGESIZE) <=
378112f080e7Smrj 			    dma->dp_copybuf_size) {
378212f080e7Smrj 				dma->dp_pgmap[pidx].pm_kaddr = (caddr_t)
378312f080e7Smrj 				    (((uintptr_t)dma->dp_kva + *copybuf_used) &
378412f080e7Smrj 				    MMU_PAGEMASK);
378512f080e7Smrj 			} else {
378612f080e7Smrj 				if (copybuf_sz_power_2) {
378712f080e7Smrj 					dma->dp_pgmap[pidx].pm_kaddr = (caddr_t)
378812f080e7Smrj 					    (((uintptr_t)dma->dp_kva +
378912f080e7Smrj 					    (*copybuf_used &
379012f080e7Smrj 					    (dma->dp_copybuf_size - 1))) &
379112f080e7Smrj 					    MMU_PAGEMASK);
379212f080e7Smrj 				} else {
379312f080e7Smrj 					dma->dp_pgmap[pidx].pm_kaddr = (caddr_t)
379412f080e7Smrj 					    (((uintptr_t)dma->dp_kva +
379512f080e7Smrj 					    (*copybuf_used %
379612f080e7Smrj 					    dma->dp_copybuf_size)) &
379712f080e7Smrj 					    MMU_PAGEMASK);
379812f080e7Smrj 				}
379912f080e7Smrj 			}
380012f080e7Smrj 
380112f080e7Smrj 			/*
380212f080e7Smrj 			 * if we haven't used up the available copy buffer yet,
380312f080e7Smrj 			 * map the kva to the physical page.
380412f080e7Smrj 			 */
380512f080e7Smrj 			if (!dma->dp_cb_remaping && ((*copybuf_used +
380612f080e7Smrj 			    MMU_PAGESIZE) <= dma->dp_copybuf_size)) {
380712f080e7Smrj 				dma->dp_pgmap[pidx].pm_mapped = B_TRUE;
380812f080e7Smrj 				if (dma->dp_pgmap[pidx].pm_pp != NULL) {
380912f080e7Smrj 					i86_pp_map(dma->dp_pgmap[pidx].pm_pp,
381012f080e7Smrj 					    dma->dp_pgmap[pidx].pm_kaddr);
381112f080e7Smrj 				} else {
381212f080e7Smrj 					i86_va_map(dma->dp_pgmap[pidx].pm_vaddr,
381312f080e7Smrj 					    sinfo->si_asp,
381412f080e7Smrj 					    dma->dp_pgmap[pidx].pm_kaddr);
381512f080e7Smrj 				}
381612f080e7Smrj 
381712f080e7Smrj 			/*
381812f080e7Smrj 			 * we've used up the available copy buffer, this page
381912f080e7Smrj 			 * will have to be mapped during rootnex_dma_win() when
382012f080e7Smrj 			 * we switch to a new window which requires a re-map
382112f080e7Smrj 			 * the copy buffer. (32-bit kernel only)
382212f080e7Smrj 			 */
382312f080e7Smrj 			} else {
382412f080e7Smrj 				dma->dp_pgmap[pidx].pm_mapped = B_FALSE;
382512f080e7Smrj 			}
382612f080e7Smrj #endif
382712f080e7Smrj 			/* go to the next page_t */
382812f080e7Smrj 			if (dmar_object->dmao_type == DMA_OTYP_PAGES) {
382912f080e7Smrj 				*cur_pp = (*cur_pp)->p_next;
383012f080e7Smrj 			}
383112f080e7Smrj 		}
383212f080e7Smrj 
383312f080e7Smrj 		/* add to the copy buffer count */
383412f080e7Smrj 		*copybuf_used += MMU_PAGESIZE;
383512f080e7Smrj 
383612f080e7Smrj 	/*
383712f080e7Smrj 	 * This cookie doesn't use the copy buffer. Walk through the pages this
383812f080e7Smrj 	 * cookie occupies to reflect this.
383912f080e7Smrj 	 */
384012f080e7Smrj 	} else {
384112f080e7Smrj 		/*
384212f080e7Smrj 		 * figure out how many pages the cookie occupies. We need to
384312f080e7Smrj 		 * use the original page offset of the buffer and the cookies
384412f080e7Smrj 		 * offset in the buffer to do this.
384512f080e7Smrj 		 */
384612f080e7Smrj 		poff = (sinfo->si_buf_offset + cur_offset) & MMU_PAGEOFFSET;
384712f080e7Smrj 		pcnt = mmu_btopr(cookie->dmac_size + poff);
384812f080e7Smrj 
384912f080e7Smrj 		while (pcnt > 0) {
385012f080e7Smrj #if !defined(__amd64)
385112f080e7Smrj 			/*
385212f080e7Smrj 			 * the 32-bit kernel doesn't have seg kpm, so we need
385312f080e7Smrj 			 * to map in the driver buffer (if it didn't come down
385412f080e7Smrj 			 * with a kernel VA) on the fly. Since this page doesn't
385512f080e7Smrj 			 * use the copy buffer, it's not, or will it ever, have
385612f080e7Smrj 			 * to be mapped in.
385712f080e7Smrj 			 */
385812f080e7Smrj 			dma->dp_pgmap[pidx].pm_mapped = B_FALSE;
385912f080e7Smrj #endif
386012f080e7Smrj 			dma->dp_pgmap[pidx].pm_uses_copybuf = B_FALSE;
386112f080e7Smrj 
386212f080e7Smrj 			/*
386312f080e7Smrj 			 * we need to update pidx and cur_pp or we'll loose
386412f080e7Smrj 			 * track of where we are.
386512f080e7Smrj 			 */
386612f080e7Smrj 			if (dmar_object->dmao_type == DMA_OTYP_PAGES) {
386712f080e7Smrj 				*cur_pp = (*cur_pp)->p_next;
386812f080e7Smrj 			}
386912f080e7Smrj 			pidx++;
387012f080e7Smrj 			pcnt--;
387112f080e7Smrj 		}
387212f080e7Smrj 	}
387312f080e7Smrj }
387412f080e7Smrj 
387512f080e7Smrj 
387612f080e7Smrj /*
387712f080e7Smrj  * rootnex_sgllen_window_boundary()
387812f080e7Smrj  *    Called in the bind slow path when the next cookie causes us to exceed (in
387912f080e7Smrj  *    this case == since we start at 0 and sgllen starts at 1) the maximum sgl
388012f080e7Smrj  *    length supported by the DMA H/W.
388112f080e7Smrj  */
388212f080e7Smrj static int
388312f080e7Smrj rootnex_sgllen_window_boundary(ddi_dma_impl_t *hp, rootnex_dma_t *dma,
388412f080e7Smrj     rootnex_window_t **windowp, ddi_dma_cookie_t *cookie, ddi_dma_attr_t *attr,
388512f080e7Smrj     off_t cur_offset)
388612f080e7Smrj {
388712f080e7Smrj 	off_t new_offset;
388812f080e7Smrj 	size_t trim_sz;
388912f080e7Smrj 	off_t coffset;
389012f080e7Smrj 
389112f080e7Smrj 
389212f080e7Smrj 	/*
389312f080e7Smrj 	 * if we know we'll never have to trim, it's pretty easy. Just move to
389412f080e7Smrj 	 * the next window and init it. We're done.
389512f080e7Smrj 	 */
389612f080e7Smrj 	if (!dma->dp_trim_required) {
389712f080e7Smrj 		(*windowp)++;
389812f080e7Smrj 		rootnex_init_win(hp, dma, *windowp, cookie, cur_offset);
389912f080e7Smrj 		(*windowp)->wd_cookie_cnt++;
390012f080e7Smrj 		(*windowp)->wd_size = cookie->dmac_size;
390112f080e7Smrj 		return (DDI_SUCCESS);
390212f080e7Smrj 	}
390312f080e7Smrj 
390412f080e7Smrj 	/* figure out how much we need to trim from the window */
390512f080e7Smrj 	ASSERT(attr->dma_attr_granular != 0);
390612f080e7Smrj 	if (dma->dp_granularity_power_2) {
390712f080e7Smrj 		trim_sz = (*windowp)->wd_size & (attr->dma_attr_granular - 1);
390812f080e7Smrj 	} else {
390912f080e7Smrj 		trim_sz = (*windowp)->wd_size % attr->dma_attr_granular;
391012f080e7Smrj 	}
391112f080e7Smrj 
391212f080e7Smrj 	/* The window's a whole multiple of granularity. We're done */
391312f080e7Smrj 	if (trim_sz == 0) {
391412f080e7Smrj 		(*windowp)++;
391512f080e7Smrj 		rootnex_init_win(hp, dma, *windowp, cookie, cur_offset);
391612f080e7Smrj 		(*windowp)->wd_cookie_cnt++;
391712f080e7Smrj 		(*windowp)->wd_size = cookie->dmac_size;
391812f080e7Smrj 		return (DDI_SUCCESS);
391912f080e7Smrj 	}
392012f080e7Smrj 
392112f080e7Smrj 	/*
392212f080e7Smrj 	 * The window's not a whole multiple of granularity, since we know this
392312f080e7Smrj 	 * is due to the sgllen, we need to go back to the last cookie and trim
392412f080e7Smrj 	 * that one, add the left over part of the old cookie into the new
392512f080e7Smrj 	 * window, and then add in the new cookie into the new window.
392612f080e7Smrj 	 */
392712f080e7Smrj 
392812f080e7Smrj 	/*
392912f080e7Smrj 	 * make sure the driver isn't making us do something bad... Trimming and
393012f080e7Smrj 	 * sgllen == 1 don't go together.
393112f080e7Smrj 	 */
393212f080e7Smrj 	if (attr->dma_attr_sgllen == 1) {
393312f080e7Smrj 		return (DDI_DMA_NOMAPPING);
393412f080e7Smrj 	}
393512f080e7Smrj 
393612f080e7Smrj 	/*
393712f080e7Smrj 	 * first, setup the current window to account for the trim. Need to go
393812f080e7Smrj 	 * back to the last cookie for this.
393912f080e7Smrj 	 */
394012f080e7Smrj 	cookie--;
394112f080e7Smrj 	(*windowp)->wd_trim.tr_trim_last = B_TRUE;
394212f080e7Smrj 	(*windowp)->wd_trim.tr_last_cookie = cookie;
3943843e1988Sjohnlev 	(*windowp)->wd_trim.tr_last_paddr = cookie->dmac_laddress;
394412f080e7Smrj 	ASSERT(cookie->dmac_size > trim_sz);
394512f080e7Smrj 	(*windowp)->wd_trim.tr_last_size = cookie->dmac_size - trim_sz;
394612f080e7Smrj 	(*windowp)->wd_size -= trim_sz;
394712f080e7Smrj 
394812f080e7Smrj 	/* save the buffer offsets for the next window */
394912f080e7Smrj 	coffset = cookie->dmac_size - trim_sz;
395012f080e7Smrj 	new_offset = (*windowp)->wd_offset + (*windowp)->wd_size;
395112f080e7Smrj 
395212f080e7Smrj 	/*
395312f080e7Smrj 	 * set this now in case this is the first window. all other cases are
395412f080e7Smrj 	 * set in dma_win()
395512f080e7Smrj 	 */
395612f080e7Smrj 	cookie->dmac_size = (*windowp)->wd_trim.tr_last_size;
395712f080e7Smrj 
395812f080e7Smrj 	/*
395912f080e7Smrj 	 * initialize the next window using what's left over in the previous
396012f080e7Smrj 	 * cookie.
396112f080e7Smrj 	 */
396212f080e7Smrj 	(*windowp)++;
396312f080e7Smrj 	rootnex_init_win(hp, dma, *windowp, cookie, new_offset);
396412f080e7Smrj 	(*windowp)->wd_cookie_cnt++;
396512f080e7Smrj 	(*windowp)->wd_trim.tr_trim_first = B_TRUE;
3966843e1988Sjohnlev 	(*windowp)->wd_trim.tr_first_paddr = cookie->dmac_laddress + coffset;
396712f080e7Smrj 	(*windowp)->wd_trim.tr_first_size = trim_sz;
396812f080e7Smrj 	if (cookie->dmac_type & ROOTNEX_USES_COPYBUF) {
396912f080e7Smrj 		(*windowp)->wd_dosync = B_TRUE;
397012f080e7Smrj 	}
397112f080e7Smrj 
397212f080e7Smrj 	/*
397312f080e7Smrj 	 * now go back to the current cookie and add it to the new window. set
397412f080e7Smrj 	 * the new window size to the what was left over from the previous
397512f080e7Smrj 	 * cookie and what's in the current cookie.
397612f080e7Smrj 	 */
397712f080e7Smrj 	cookie++;
397812f080e7Smrj 	(*windowp)->wd_cookie_cnt++;
397912f080e7Smrj 	(*windowp)->wd_size = trim_sz + cookie->dmac_size;
398012f080e7Smrj 
398112f080e7Smrj 	/*
398212f080e7Smrj 	 * trim plus the next cookie could put us over maxxfer (a cookie can be
398312f080e7Smrj 	 * a max size of maxxfer). Handle that case.
398412f080e7Smrj 	 */
398512f080e7Smrj 	if ((*windowp)->wd_size > dma->dp_maxxfer) {
398612f080e7Smrj 		/*
398712f080e7Smrj 		 * maxxfer is already a whole multiple of granularity, and this
398812f080e7Smrj 		 * trim will be <= the previous trim (since a cookie can't be
398912f080e7Smrj 		 * larger than maxxfer). Make things simple here.
399012f080e7Smrj 		 */
399112f080e7Smrj 		trim_sz = (*windowp)->wd_size - dma->dp_maxxfer;
399212f080e7Smrj 		(*windowp)->wd_trim.tr_trim_last = B_TRUE;
399312f080e7Smrj 		(*windowp)->wd_trim.tr_last_cookie = cookie;
3994843e1988Sjohnlev 		(*windowp)->wd_trim.tr_last_paddr = cookie->dmac_laddress;
399512f080e7Smrj 		(*windowp)->wd_trim.tr_last_size = cookie->dmac_size - trim_sz;
399612f080e7Smrj 		(*windowp)->wd_size -= trim_sz;
399712f080e7Smrj 		ASSERT((*windowp)->wd_size == dma->dp_maxxfer);
399812f080e7Smrj 
399912f080e7Smrj 		/* save the buffer offsets for the next window */
400012f080e7Smrj 		coffset = cookie->dmac_size - trim_sz;
400112f080e7Smrj 		new_offset = (*windowp)->wd_offset + (*windowp)->wd_size;
400212f080e7Smrj 
400312f080e7Smrj 		/* setup the next window */
400412f080e7Smrj 		(*windowp)++;
400512f080e7Smrj 		rootnex_init_win(hp, dma, *windowp, cookie, new_offset);
400612f080e7Smrj 		(*windowp)->wd_cookie_cnt++;
400712f080e7Smrj 		(*windowp)->wd_trim.tr_trim_first = B_TRUE;
4008843e1988Sjohnlev 		(*windowp)->wd_trim.tr_first_paddr = cookie->dmac_laddress +
400912f080e7Smrj 		    coffset;
401012f080e7Smrj 		(*windowp)->wd_trim.tr_first_size = trim_sz;
401112f080e7Smrj 	}
401212f080e7Smrj 
401312f080e7Smrj 	return (DDI_SUCCESS);
401412f080e7Smrj }
401512f080e7Smrj 
401612f080e7Smrj 
401712f080e7Smrj /*
401812f080e7Smrj  * rootnex_copybuf_window_boundary()
401912f080e7Smrj  *    Called in bind slowpath when we get to a window boundary because we used
402012f080e7Smrj  *    up all the copy buffer that we have.
402112f080e7Smrj  */
402212f080e7Smrj static int
402312f080e7Smrj rootnex_copybuf_window_boundary(ddi_dma_impl_t *hp, rootnex_dma_t *dma,
402412f080e7Smrj     rootnex_window_t **windowp, ddi_dma_cookie_t *cookie, off_t cur_offset,
402512f080e7Smrj     size_t *copybuf_used)
402612f080e7Smrj {
402712f080e7Smrj 	rootnex_sglinfo_t *sinfo;
402812f080e7Smrj 	off_t new_offset;
402912f080e7Smrj 	size_t trim_sz;
4030843e1988Sjohnlev 	paddr_t paddr;
403112f080e7Smrj 	off_t coffset;
403212f080e7Smrj 	uint_t pidx;
403312f080e7Smrj 	off_t poff;
403412f080e7Smrj 
403512f080e7Smrj 
403612f080e7Smrj 	sinfo = &dma->dp_sglinfo;
403712f080e7Smrj 
403812f080e7Smrj 	/*
403912f080e7Smrj 	 * the copy buffer should be a whole multiple of page size. We know that
404012f080e7Smrj 	 * this cookie is <= MMU_PAGESIZE.
404112f080e7Smrj 	 */
404212f080e7Smrj 	ASSERT(cookie->dmac_size <= MMU_PAGESIZE);
404312f080e7Smrj 
404412f080e7Smrj 	/*
404512f080e7Smrj 	 * from now on, all new windows in this bind need to be re-mapped during
404612f080e7Smrj 	 * ddi_dma_getwin() (32-bit kernel only). i.e. we ran out out copybuf
404712f080e7Smrj 	 * space...
404812f080e7Smrj 	 */
404912f080e7Smrj #if !defined(__amd64)
405012f080e7Smrj 	dma->dp_cb_remaping = B_TRUE;
405112f080e7Smrj #endif
405212f080e7Smrj 
405312f080e7Smrj 	/* reset copybuf used */
405412f080e7Smrj 	*copybuf_used = 0;
405512f080e7Smrj 
405612f080e7Smrj 	/*
405712f080e7Smrj 	 * if we don't have to trim (since granularity is set to 1), go to the
405812f080e7Smrj 	 * next window and add the current cookie to it. We know the current
405912f080e7Smrj 	 * cookie uses the copy buffer since we're in this code path.
406012f080e7Smrj 	 */
406112f080e7Smrj 	if (!dma->dp_trim_required) {
406212f080e7Smrj 		(*windowp)++;
406312f080e7Smrj 		rootnex_init_win(hp, dma, *windowp, cookie, cur_offset);
406412f080e7Smrj 
406512f080e7Smrj 		/* Add this cookie to the new window */
406612f080e7Smrj 		(*windowp)->wd_cookie_cnt++;
406712f080e7Smrj 		(*windowp)->wd_size += cookie->dmac_size;
406812f080e7Smrj 		*copybuf_used += MMU_PAGESIZE;
406912f080e7Smrj 		return (DDI_SUCCESS);
407012f080e7Smrj 	}
407112f080e7Smrj 
407212f080e7Smrj 	/*
407312f080e7Smrj 	 * *** may need to trim, figure it out.
407412f080e7Smrj 	 */
407512f080e7Smrj 
407612f080e7Smrj 	/* figure out how much we need to trim from the window */
407712f080e7Smrj 	if (dma->dp_granularity_power_2) {
407812f080e7Smrj 		trim_sz = (*windowp)->wd_size &
407912f080e7Smrj 		    (hp->dmai_attr.dma_attr_granular - 1);
408012f080e7Smrj 	} else {
408112f080e7Smrj 		trim_sz = (*windowp)->wd_size % hp->dmai_attr.dma_attr_granular;
408212f080e7Smrj 	}
408312f080e7Smrj 
408412f080e7Smrj 	/*
408512f080e7Smrj 	 * if the window's a whole multiple of granularity, go to the next
408612f080e7Smrj 	 * window, init it, then add in the current cookie. We know the current
408712f080e7Smrj 	 * cookie uses the copy buffer since we're in this code path.
408812f080e7Smrj 	 */
408912f080e7Smrj 	if (trim_sz == 0) {
409012f080e7Smrj 		(*windowp)++;
409112f080e7Smrj 		rootnex_init_win(hp, dma, *windowp, cookie, cur_offset);
409212f080e7Smrj 
409312f080e7Smrj 		/* Add this cookie to the new window */
409412f080e7Smrj 		(*windowp)->wd_cookie_cnt++;
409512f080e7Smrj 		(*windowp)->wd_size += cookie->dmac_size;
409612f080e7Smrj 		*copybuf_used += MMU_PAGESIZE;
409712f080e7Smrj 		return (DDI_SUCCESS);
409812f080e7Smrj 	}
409912f080e7Smrj 
410012f080e7Smrj 	/*
410112f080e7Smrj 	 * *** We figured it out, we definitly need to trim
410212f080e7Smrj 	 */
410312f080e7Smrj 
410412f080e7Smrj 	/*
410512f080e7Smrj 	 * make sure the driver isn't making us do something bad...
410612f080e7Smrj 	 * Trimming and sgllen == 1 don't go together.
410712f080e7Smrj 	 */
410812f080e7Smrj 	if (hp->dmai_attr.dma_attr_sgllen == 1) {
410912f080e7Smrj 		return (DDI_DMA_NOMAPPING);
411012f080e7Smrj 	}
411112f080e7Smrj 
411212f080e7Smrj 	/*
411312f080e7Smrj 	 * first, setup the current window to account for the trim. Need to go
411412f080e7Smrj 	 * back to the last cookie for this. Some of the last cookie will be in
411512f080e7Smrj 	 * the current window, and some of the last cookie will be in the new
411612f080e7Smrj 	 * window. All of the current cookie will be in the new window.
411712f080e7Smrj 	 */
411812f080e7Smrj 	cookie--;
411912f080e7Smrj 	(*windowp)->wd_trim.tr_trim_last = B_TRUE;
412012f080e7Smrj 	(*windowp)->wd_trim.tr_last_cookie = cookie;
4121843e1988Sjohnlev 	(*windowp)->wd_trim.tr_last_paddr = cookie->dmac_laddress;
412212f080e7Smrj 	ASSERT(cookie->dmac_size > trim_sz);
412312f080e7Smrj 	(*windowp)->wd_trim.tr_last_size = cookie->dmac_size - trim_sz;
412412f080e7Smrj 	(*windowp)->wd_size -= trim_sz;
412512f080e7Smrj 
412612f080e7Smrj 	/*
412712f080e7Smrj 	 * we're trimming the last cookie (not the current cookie). So that
412812f080e7Smrj 	 * last cookie may have or may not have been using the copy buffer (
412912f080e7Smrj 	 * we know the cookie passed in uses the copy buffer since we're in
413012f080e7Smrj 	 * this code path).
413112f080e7Smrj 	 *
413212f080e7Smrj 	 * If the last cookie doesn't use the copy buffer, nothing special to
413312f080e7Smrj 	 * do. However, if it does uses the copy buffer, it will be both the
413412f080e7Smrj 	 * last page in the current window and the first page in the next
413512f080e7Smrj 	 * window. Since we are reusing the copy buffer (and KVA space on the
413612f080e7Smrj 	 * 32-bit kernel), this page will use the end of the copy buffer in the
413712f080e7Smrj 	 * current window, and the start of the copy buffer in the next window.
413812f080e7Smrj 	 * Track that info... The cookie physical address was already set to
413912f080e7Smrj 	 * the copy buffer physical address in setup_cookie..
414012f080e7Smrj 	 */
414112f080e7Smrj 	if (cookie->dmac_type & ROOTNEX_USES_COPYBUF) {
414212f080e7Smrj 		pidx = (sinfo->si_buf_offset + (*windowp)->wd_offset +
414312f080e7Smrj 		    (*windowp)->wd_size) >> MMU_PAGESHIFT;
414412f080e7Smrj 		(*windowp)->wd_trim.tr_last_copybuf_win = B_TRUE;
414512f080e7Smrj 		(*windowp)->wd_trim.tr_last_pidx = pidx;
414612f080e7Smrj 		(*windowp)->wd_trim.tr_last_cbaddr =
414712f080e7Smrj 		    dma->dp_pgmap[pidx].pm_cbaddr;
414812f080e7Smrj #if !defined(__amd64)
414912f080e7Smrj 		(*windowp)->wd_trim.tr_last_kaddr =
415012f080e7Smrj 		    dma->dp_pgmap[pidx].pm_kaddr;
415112f080e7Smrj #endif
415212f080e7Smrj 	}
415312f080e7Smrj 
415412f080e7Smrj 	/* save the buffer offsets for the next window */
415512f080e7Smrj 	coffset = cookie->dmac_size - trim_sz;
415612f080e7Smrj 	new_offset = (*windowp)->wd_offset + (*windowp)->wd_size;
415712f080e7Smrj 
415812f080e7Smrj 	/*
415912f080e7Smrj 	 * set this now in case this is the first window. all other cases are
416012f080e7Smrj 	 * set in dma_win()
416112f080e7Smrj 	 */
416212f080e7Smrj 	cookie->dmac_size = (*windowp)->wd_trim.tr_last_size;
416312f080e7Smrj 
416412f080e7Smrj 	/*
416512f080e7Smrj 	 * initialize the next window using what's left over in the previous
416612f080e7Smrj 	 * cookie.
416712f080e7Smrj 	 */
416812f080e7Smrj 	(*windowp)++;
416912f080e7Smrj 	rootnex_init_win(hp, dma, *windowp, cookie, new_offset);
417012f080e7Smrj 	(*windowp)->wd_cookie_cnt++;
417112f080e7Smrj 	(*windowp)->wd_trim.tr_trim_first = B_TRUE;
4172843e1988Sjohnlev 	(*windowp)->wd_trim.tr_first_paddr = cookie->dmac_laddress + coffset;
417312f080e7Smrj 	(*windowp)->wd_trim.tr_first_size = trim_sz;
417412f080e7Smrj 
417512f080e7Smrj 	/*
417612f080e7Smrj 	 * again, we're tracking if the last cookie uses the copy buffer.
417712f080e7Smrj 	 * read the comment above for more info on why we need to track
417812f080e7Smrj 	 * additional state.
417912f080e7Smrj 	 *
418012f080e7Smrj 	 * For the first cookie in the new window, we need reset the physical
418112f080e7Smrj 	 * address to DMA into to the start of the copy buffer plus any
418212f080e7Smrj 	 * initial page offset which may be present.
418312f080e7Smrj 	 */
418412f080e7Smrj 	if (cookie->dmac_type & ROOTNEX_USES_COPYBUF) {
418512f080e7Smrj 		(*windowp)->wd_dosync = B_TRUE;
418612f080e7Smrj 		(*windowp)->wd_trim.tr_first_copybuf_win = B_TRUE;
418712f080e7Smrj 		(*windowp)->wd_trim.tr_first_pidx = pidx;
418812f080e7Smrj 		(*windowp)->wd_trim.tr_first_cbaddr = dma->dp_cbaddr;
418912f080e7Smrj 		poff = (*windowp)->wd_trim.tr_first_paddr & MMU_PAGEOFFSET;
4190843e1988Sjohnlev 
4191843e1988Sjohnlev 		paddr = pfn_to_pa(hat_getpfnum(kas.a_hat, dma->dp_cbaddr)) +
4192843e1988Sjohnlev 		    poff;
4193843e1988Sjohnlev #ifdef __xpv
4194843e1988Sjohnlev 		/*
4195843e1988Sjohnlev 		 * If we're dom0, we're using a real device so we need to load
4196843e1988Sjohnlev 		 * the cookies with MAs instead of PAs.
4197843e1988Sjohnlev 		 */
4198843e1988Sjohnlev 		(*windowp)->wd_trim.tr_first_paddr =
4199843e1988Sjohnlev 		    ROOTNEX_PADDR_TO_RBASE(xen_info, paddr);
4200843e1988Sjohnlev #else
4201843e1988Sjohnlev 		(*windowp)->wd_trim.tr_first_paddr = paddr;
4202843e1988Sjohnlev #endif
4203843e1988Sjohnlev 
420412f080e7Smrj #if !defined(__amd64)
420512f080e7Smrj 		(*windowp)->wd_trim.tr_first_kaddr = dma->dp_kva;
420612f080e7Smrj #endif
420712f080e7Smrj 		/* account for the cookie copybuf usage in the new window */
420812f080e7Smrj 		*copybuf_used += MMU_PAGESIZE;
420912f080e7Smrj 
421012f080e7Smrj 		/*
421112f080e7Smrj 		 * every piece of code has to have a hack, and here is this
421212f080e7Smrj 		 * ones :-)
421312f080e7Smrj 		 *
421412f080e7Smrj 		 * There is a complex interaction between setup_cookie and the
421512f080e7Smrj 		 * copybuf window boundary. The complexity had to be in either
421612f080e7Smrj 		 * the maxxfer window, or the copybuf window, and I chose the
421712f080e7Smrj 		 * copybuf code.
421812f080e7Smrj 		 *
421912f080e7Smrj 		 * So in this code path, we have taken the last cookie,
422012f080e7Smrj 		 * virtually broken it in half due to the trim, and it happens
422112f080e7Smrj 		 * to use the copybuf which further complicates life. At the
422212f080e7Smrj 		 * same time, we have already setup the current cookie, which
422312f080e7Smrj 		 * is now wrong. More background info: the current cookie uses
422412f080e7Smrj 		 * the copybuf, so it is only a page long max. So we need to
422512f080e7Smrj 		 * fix the current cookies copy buffer address, physical
422612f080e7Smrj 		 * address, and kva for the 32-bit kernel. We due this by
422712f080e7Smrj 		 * bumping them by page size (of course, we can't due this on
422812f080e7Smrj 		 * the physical address since the copy buffer may not be
422912f080e7Smrj 		 * physically contiguous).
423012f080e7Smrj 		 */
423112f080e7Smrj 		cookie++;
423212f080e7Smrj 		dma->dp_pgmap[pidx + 1].pm_cbaddr += MMU_PAGESIZE;
4233843e1988Sjohnlev 		poff = cookie->dmac_laddress & MMU_PAGEOFFSET;
4234843e1988Sjohnlev 
4235843e1988Sjohnlev 		paddr = pfn_to_pa(hat_getpfnum(kas.a_hat,
423612f080e7Smrj 		    dma->dp_pgmap[pidx + 1].pm_cbaddr)) + poff;
4237843e1988Sjohnlev #ifdef __xpv
4238843e1988Sjohnlev 		/*
4239843e1988Sjohnlev 		 * If we're dom0, we're using a real device so we need to load
4240843e1988Sjohnlev 		 * the cookies with MAs instead of PAs.
4241843e1988Sjohnlev 		 */
4242843e1988Sjohnlev 		cookie->dmac_laddress = ROOTNEX_PADDR_TO_RBASE(xen_info, paddr);
4243843e1988Sjohnlev #else
4244843e1988Sjohnlev 		cookie->dmac_laddress = paddr;
4245843e1988Sjohnlev #endif
4246843e1988Sjohnlev 
424712f080e7Smrj #if !defined(__amd64)
424812f080e7Smrj 		ASSERT(dma->dp_pgmap[pidx + 1].pm_mapped == B_FALSE);
424912f080e7Smrj 		dma->dp_pgmap[pidx + 1].pm_kaddr += MMU_PAGESIZE;
425012f080e7Smrj #endif
425112f080e7Smrj 	} else {
425212f080e7Smrj 		/* go back to the current cookie */
425312f080e7Smrj 		cookie++;
425412f080e7Smrj 	}
425512f080e7Smrj 
425612f080e7Smrj 	/*
425712f080e7Smrj 	 * add the current cookie to the new window. set the new window size to
425812f080e7Smrj 	 * the what was left over from the previous cookie and what's in the
425912f080e7Smrj 	 * current cookie.
426012f080e7Smrj 	 */
426112f080e7Smrj 	(*windowp)->wd_cookie_cnt++;
426212f080e7Smrj 	(*windowp)->wd_size = trim_sz + cookie->dmac_size;
426312f080e7Smrj 	ASSERT((*windowp)->wd_size < dma->dp_maxxfer);
426412f080e7Smrj 
426512f080e7Smrj 	/*
426612f080e7Smrj 	 * we know that the cookie passed in always uses the copy buffer. We
426712f080e7Smrj 	 * wouldn't be here if it didn't.
426812f080e7Smrj 	 */
426912f080e7Smrj 	*copybuf_used += MMU_PAGESIZE;
427012f080e7Smrj 
427112f080e7Smrj 	return (DDI_SUCCESS);
427212f080e7Smrj }
427312f080e7Smrj 
427412f080e7Smrj 
427512f080e7Smrj /*
427612f080e7Smrj  * rootnex_maxxfer_window_boundary()
427712f080e7Smrj  *    Called in bind slowpath when we get to a window boundary because we will
427812f080e7Smrj  *    go over maxxfer.
427912f080e7Smrj  */
428012f080e7Smrj static int
428112f080e7Smrj rootnex_maxxfer_window_boundary(ddi_dma_impl_t *hp, rootnex_dma_t *dma,
428212f080e7Smrj     rootnex_window_t **windowp, ddi_dma_cookie_t *cookie)
428312f080e7Smrj {
428412f080e7Smrj 	size_t dmac_size;
428512f080e7Smrj 	off_t new_offset;
428612f080e7Smrj 	size_t trim_sz;
428712f080e7Smrj 	off_t coffset;
428812f080e7Smrj 
428912f080e7Smrj 
429012f080e7Smrj 	/*
429112f080e7Smrj 	 * calculate how much we have to trim off of the current cookie to equal
429212f080e7Smrj 	 * maxxfer. We don't have to account for granularity here since our
429312f080e7Smrj 	 * maxxfer already takes that into account.
429412f080e7Smrj 	 */
429512f080e7Smrj 	trim_sz = ((*windowp)->wd_size + cookie->dmac_size) - dma->dp_maxxfer;
429612f080e7Smrj 	ASSERT(trim_sz <= cookie->dmac_size);
429712f080e7Smrj 	ASSERT(trim_sz <= dma->dp_maxxfer);
429812f080e7Smrj 
429912f080e7Smrj 	/* save cookie size since we need it later and we might change it */
430012f080e7Smrj 	dmac_size = cookie->dmac_size;
430112f080e7Smrj 
430212f080e7Smrj 	/*
430312f080e7Smrj 	 * if we're not trimming the entire cookie, setup the current window to
430412f080e7Smrj 	 * account for the trim.
430512f080e7Smrj 	 */
430612f080e7Smrj 	if (trim_sz < cookie->dmac_size) {
430712f080e7Smrj 		(*windowp)->wd_cookie_cnt++;
430812f080e7Smrj 		(*windowp)->wd_trim.tr_trim_last = B_TRUE;
430912f080e7Smrj 		(*windowp)->wd_trim.tr_last_cookie = cookie;
4310843e1988Sjohnlev 		(*windowp)->wd_trim.tr_last_paddr = cookie->dmac_laddress;
431112f080e7Smrj 		(*windowp)->wd_trim.tr_last_size = cookie->dmac_size - trim_sz;
431212f080e7Smrj 		(*windowp)->wd_size = dma->dp_maxxfer;
431312f080e7Smrj 
431412f080e7Smrj 		/*
431512f080e7Smrj 		 * set the adjusted cookie size now in case this is the first
431612f080e7Smrj 		 * window. All other windows are taken care of in get win
431712f080e7Smrj 		 */
431812f080e7Smrj 		cookie->dmac_size = (*windowp)->wd_trim.tr_last_size;
431912f080e7Smrj 	}
432012f080e7Smrj 
432112f080e7Smrj 	/*
432212f080e7Smrj 	 * coffset is the current offset within the cookie, new_offset is the
432312f080e7Smrj 	 * current offset with the entire buffer.
432412f080e7Smrj 	 */
432512f080e7Smrj 	coffset = dmac_size - trim_sz;
432612f080e7Smrj 	new_offset = (*windowp)->wd_offset + (*windowp)->wd_size;
432712f080e7Smrj 
432812f080e7Smrj 	/* initialize the next window */
432912f080e7Smrj 	(*windowp)++;
433012f080e7Smrj 	rootnex_init_win(hp, dma, *windowp, cookie, new_offset);
433112f080e7Smrj 	(*windowp)->wd_cookie_cnt++;
433212f080e7Smrj 	(*windowp)->wd_size = trim_sz;
433312f080e7Smrj 	if (trim_sz < dmac_size) {
433412f080e7Smrj 		(*windowp)->wd_trim.tr_trim_first = B_TRUE;
4335843e1988Sjohnlev 		(*windowp)->wd_trim.tr_first_paddr = cookie->dmac_laddress +
433612f080e7Smrj 		    coffset;
433712f080e7Smrj 		(*windowp)->wd_trim.tr_first_size = trim_sz;
433812f080e7Smrj 	}
433912f080e7Smrj 
434012f080e7Smrj 	return (DDI_SUCCESS);
434112f080e7Smrj }
434212f080e7Smrj 
434312f080e7Smrj 
434412f080e7Smrj /*ARGSUSED*/
434512f080e7Smrj static int
434620906b23SVikram Hegde rootnex_coredma_sync(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle,
434712f080e7Smrj     off_t off, size_t len, uint_t cache_flags)
434812f080e7Smrj {
434912f080e7Smrj 	rootnex_sglinfo_t *sinfo;
435012f080e7Smrj 	rootnex_pgmap_t *cbpage;
435112f080e7Smrj 	rootnex_window_t *win;
435212f080e7Smrj 	ddi_dma_impl_t *hp;
435312f080e7Smrj 	rootnex_dma_t *dma;
435412f080e7Smrj 	caddr_t fromaddr;
435512f080e7Smrj 	caddr_t toaddr;
435612f080e7Smrj 	uint_t psize;
435712f080e7Smrj 	off_t offset;
435812f080e7Smrj 	uint_t pidx;
435912f080e7Smrj 	size_t size;
436012f080e7Smrj 	off_t poff;
436112f080e7Smrj 	int e;
436212f080e7Smrj 
436312f080e7Smrj 
436412f080e7Smrj 	hp = (ddi_dma_impl_t *)handle;
436512f080e7Smrj 	dma = (rootnex_dma_t *)hp->dmai_private;
436612f080e7Smrj 	sinfo = &dma->dp_sglinfo;
436712f080e7Smrj 
436812f080e7Smrj 	/*
436912f080e7Smrj 	 * if we don't have any windows, we don't need to sync. A copybuf
437012f080e7Smrj 	 * will cause us to have at least one window.
437112f080e7Smrj 	 */
437212f080e7Smrj 	if (dma->dp_window == NULL) {
437312f080e7Smrj 		return (DDI_SUCCESS);
437412f080e7Smrj 	}
437512f080e7Smrj 
437612f080e7Smrj 	/* This window may not need to be sync'd */
437712f080e7Smrj 	win = &dma->dp_window[dma->dp_current_win];
437812f080e7Smrj 	if (!win->wd_dosync) {
437912f080e7Smrj 		return (DDI_SUCCESS);
438012f080e7Smrj 	}
438112f080e7Smrj 
438212f080e7Smrj 	/* handle off and len special cases */
438312f080e7Smrj 	if ((off == 0) || (rootnex_sync_ignore_params)) {
438412f080e7Smrj 		offset = win->wd_offset;
438512f080e7Smrj 	} else {
438612f080e7Smrj 		offset = off;
438712f080e7Smrj 	}
438812f080e7Smrj 	if ((len == 0) || (rootnex_sync_ignore_params)) {
438912f080e7Smrj 		size = win->wd_size;
439012f080e7Smrj 	} else {
439112f080e7Smrj 		size = len;
439212f080e7Smrj 	}
439312f080e7Smrj 
439412f080e7Smrj 	/* check the sync args to make sure they make a little sense */
439512f080e7Smrj 	if (rootnex_sync_check_parms) {
439612f080e7Smrj 		e = rootnex_valid_sync_parms(hp, win, offset, size,
439712f080e7Smrj 		    cache_flags);
439812f080e7Smrj 		if (e != DDI_SUCCESS) {
4399*ef4ab52fSFrank Van Der Linden 			ROOTNEX_DPROF_INC(&rootnex_cnt[ROOTNEX_CNT_SYNC_FAIL]);
440012f080e7Smrj 			return (DDI_FAILURE);
440112f080e7Smrj 		}
440212f080e7Smrj 	}
440312f080e7Smrj 
440412f080e7Smrj 	/*
440512f080e7Smrj 	 * special case the first page to handle the offset into the page. The
440612f080e7Smrj 	 * offset to the current page for our buffer is the offset into the
440712f080e7Smrj 	 * first page of the buffer plus our current offset into the buffer
440812f080e7Smrj 	 * itself, masked of course.
440912f080e7Smrj 	 */
441012f080e7Smrj 	poff = (sinfo->si_buf_offset + offset) & MMU_PAGEOFFSET;
441112f080e7Smrj 	psize = MIN((MMU_PAGESIZE - poff), size);
441212f080e7Smrj 
441312f080e7Smrj 	/* go through all the pages that we want to sync */
441412f080e7Smrj 	while (size > 0) {
441512f080e7Smrj 		/*
441612f080e7Smrj 		 * Calculate the page index relative to the start of the buffer.
441712f080e7Smrj 		 * The index to the current page for our buffer is the offset
441812f080e7Smrj 		 * into the first page of the buffer plus our current offset
441912f080e7Smrj 		 * into the buffer itself, shifted of course...
442012f080e7Smrj 		 */
442112f080e7Smrj 		pidx = (sinfo->si_buf_offset + offset) >> MMU_PAGESHIFT;
442212f080e7Smrj 		ASSERT(pidx < sinfo->si_max_pages);
442312f080e7Smrj 
442412f080e7Smrj 		/*
442512f080e7Smrj 		 * if this page uses the copy buffer, we need to sync it,
442612f080e7Smrj 		 * otherwise, go on to the next page.
442712f080e7Smrj 		 */
442812f080e7Smrj 		cbpage = &dma->dp_pgmap[pidx];
442912f080e7Smrj 		ASSERT((cbpage->pm_uses_copybuf == B_TRUE) ||
443012f080e7Smrj 		    (cbpage->pm_uses_copybuf == B_FALSE));
443112f080e7Smrj 		if (cbpage->pm_uses_copybuf) {
443212f080e7Smrj 			/* cbaddr and kaddr should be page aligned */
443312f080e7Smrj 			ASSERT(((uintptr_t)cbpage->pm_cbaddr &
443412f080e7Smrj 			    MMU_PAGEOFFSET) == 0);
443512f080e7Smrj 			ASSERT(((uintptr_t)cbpage->pm_kaddr &
443612f080e7Smrj 			    MMU_PAGEOFFSET) == 0);
443712f080e7Smrj 
443812f080e7Smrj 			/*
443912f080e7Smrj 			 * if we're copying for the device, we are going to
444012f080e7Smrj 			 * copy from the drivers buffer and to the rootnex
444112f080e7Smrj 			 * allocated copy buffer.
444212f080e7Smrj 			 */
444312f080e7Smrj 			if (cache_flags == DDI_DMA_SYNC_FORDEV) {
444412f080e7Smrj 				fromaddr = cbpage->pm_kaddr + poff;
444512f080e7Smrj 				toaddr = cbpage->pm_cbaddr + poff;
4446*ef4ab52fSFrank Van Der Linden 				ROOTNEX_DPROBE2(rootnex__sync__dev,
444712f080e7Smrj 				    dev_info_t *, dma->dp_dip, size_t, psize);
444812f080e7Smrj 
444912f080e7Smrj 			/*
445012f080e7Smrj 			 * if we're copying for the cpu/kernel, we are going to
445112f080e7Smrj 			 * copy from the rootnex allocated copy buffer to the
445212f080e7Smrj 			 * drivers buffer.
445312f080e7Smrj 			 */
445412f080e7Smrj 			} else {
445512f080e7Smrj 				fromaddr = cbpage->pm_cbaddr + poff;
445612f080e7Smrj 				toaddr = cbpage->pm_kaddr + poff;
4457*ef4ab52fSFrank Van Der Linden 				ROOTNEX_DPROBE2(rootnex__sync__cpu,
445812f080e7Smrj 				    dev_info_t *, dma->dp_dip, size_t, psize);
445912f080e7Smrj 			}
446012f080e7Smrj 
446112f080e7Smrj 			bcopy(fromaddr, toaddr, psize);
446212f080e7Smrj 		}
446312f080e7Smrj 
446412f080e7Smrj 		/*
446512f080e7Smrj 		 * decrement size until we're done, update our offset into the
446612f080e7Smrj 		 * buffer, and get the next page size.
446712f080e7Smrj 		 */
446812f080e7Smrj 		size -= psize;
446912f080e7Smrj 		offset += psize;
447012f080e7Smrj 		psize = MIN(MMU_PAGESIZE, size);
447112f080e7Smrj 
447212f080e7Smrj 		/* page offset is zero for the rest of this loop */
447312f080e7Smrj 		poff = 0;
447412f080e7Smrj 	}
447512f080e7Smrj 
447612f080e7Smrj 	return (DDI_SUCCESS);
447712f080e7Smrj }
447812f080e7Smrj 
447920906b23SVikram Hegde /*
448020906b23SVikram Hegde  * rootnex_dma_sync()
448120906b23SVikram Hegde  *    called from ddi_dma_sync() if DMP_NOSYNC is not set in hp->dmai_rflags.
448220906b23SVikram Hegde  *    We set DMP_NOSYNC if we're not using the copy buffer. If DMP_NOSYNC
448320906b23SVikram Hegde  *    is set, ddi_dma_sync() returns immediately passing back success.
448420906b23SVikram Hegde  */
448520906b23SVikram Hegde /*ARGSUSED*/
448620906b23SVikram Hegde static int
448720906b23SVikram Hegde rootnex_dma_sync(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle,
448820906b23SVikram Hegde     off_t off, size_t len, uint_t cache_flags)
448920906b23SVikram Hegde {
44903a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
4491b51bbbf5SVikram Hegde 	if (IOMMU_USED(rdip)) {
449220906b23SVikram Hegde 		return (iommulib_nexdma_sync(dip, rdip, handle, off, len,
449320906b23SVikram Hegde 		    cache_flags));
449420906b23SVikram Hegde 	}
449520906b23SVikram Hegde #endif
449620906b23SVikram Hegde 	return (rootnex_coredma_sync(dip, rdip, handle, off, len,
449720906b23SVikram Hegde 	    cache_flags));
449820906b23SVikram Hegde }
449912f080e7Smrj 
450012f080e7Smrj /*
450112f080e7Smrj  * rootnex_valid_sync_parms()
450212f080e7Smrj  *    checks the parameters passed to sync to verify they are correct.
450312f080e7Smrj  */
450412f080e7Smrj static int
450512f080e7Smrj rootnex_valid_sync_parms(ddi_dma_impl_t *hp, rootnex_window_t *win,
450612f080e7Smrj     off_t offset, size_t size, uint_t cache_flags)
450712f080e7Smrj {
450812f080e7Smrj 	off_t woffset;
450912f080e7Smrj 
451012f080e7Smrj 
451112f080e7Smrj 	/*
451212f080e7Smrj 	 * the first part of the test to make sure the offset passed in is
451312f080e7Smrj 	 * within the window.
451412f080e7Smrj 	 */
451512f080e7Smrj 	if (offset < win->wd_offset) {
451612f080e7Smrj 		return (DDI_FAILURE);
451712f080e7Smrj 	}
451812f080e7Smrj 
451912f080e7Smrj 	/*
452012f080e7Smrj 	 * second and last part of the test to make sure the offset and length
452112f080e7Smrj 	 * passed in is within the window.
452212f080e7Smrj 	 */
452312f080e7Smrj 	woffset = offset - win->wd_offset;
452412f080e7Smrj 	if ((woffset + size) > win->wd_size) {
452512f080e7Smrj 		return (DDI_FAILURE);
452612f080e7Smrj 	}
452712f080e7Smrj 
452812f080e7Smrj 	/*
452912f080e7Smrj 	 * if we are sync'ing for the device, the DDI_DMA_WRITE flag should
453012f080e7Smrj 	 * be set too.
453112f080e7Smrj 	 */
453212f080e7Smrj 	if ((cache_flags == DDI_DMA_SYNC_FORDEV) &&
453312f080e7Smrj 	    (hp->dmai_rflags & DDI_DMA_WRITE)) {
453412f080e7Smrj 		return (DDI_SUCCESS);
453512f080e7Smrj 	}
453612f080e7Smrj 
453712f080e7Smrj 	/*
453812f080e7Smrj 	 * at this point, either DDI_DMA_SYNC_FORCPU or DDI_DMA_SYNC_FORKERNEL
453912f080e7Smrj 	 * should be set. Also DDI_DMA_READ should be set in the flags.
454012f080e7Smrj 	 */
454112f080e7Smrj 	if (((cache_flags == DDI_DMA_SYNC_FORCPU) ||
454212f080e7Smrj 	    (cache_flags == DDI_DMA_SYNC_FORKERNEL)) &&
454312f080e7Smrj 	    (hp->dmai_rflags & DDI_DMA_READ)) {
454412f080e7Smrj 		return (DDI_SUCCESS);
454512f080e7Smrj 	}
454612f080e7Smrj 
454712f080e7Smrj 	return (DDI_FAILURE);
454812f080e7Smrj }
454912f080e7Smrj 
455012f080e7Smrj 
455112f080e7Smrj /*ARGSUSED*/
455212f080e7Smrj static int
455320906b23SVikram Hegde rootnex_coredma_win(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle,
455412f080e7Smrj     uint_t win, off_t *offp, size_t *lenp, ddi_dma_cookie_t *cookiep,
455512f080e7Smrj     uint_t *ccountp)
455612f080e7Smrj {
455712f080e7Smrj 	rootnex_window_t *window;
455812f080e7Smrj 	rootnex_trim_t *trim;
455912f080e7Smrj 	ddi_dma_impl_t *hp;
456012f080e7Smrj 	rootnex_dma_t *dma;
456112f080e7Smrj #if !defined(__amd64)
456212f080e7Smrj 	rootnex_sglinfo_t *sinfo;
456312f080e7Smrj 	rootnex_pgmap_t *pmap;
456412f080e7Smrj 	uint_t pidx;
456512f080e7Smrj 	uint_t pcnt;
456612f080e7Smrj 	off_t poff;
456712f080e7Smrj 	int i;
456812f080e7Smrj #endif
456912f080e7Smrj 
457012f080e7Smrj 
457112f080e7Smrj 	hp = (ddi_dma_impl_t *)handle;
457212f080e7Smrj 	dma = (rootnex_dma_t *)hp->dmai_private;
457312f080e7Smrj #if !defined(__amd64)
457412f080e7Smrj 	sinfo = &dma->dp_sglinfo;
457512f080e7Smrj #endif
457612f080e7Smrj 
457712f080e7Smrj 	/* If we try and get a window which doesn't exist, return failure */
457812f080e7Smrj 	if (win >= hp->dmai_nwin) {
4579*ef4ab52fSFrank Van Der Linden 		ROOTNEX_DPROF_INC(&rootnex_cnt[ROOTNEX_CNT_GETWIN_FAIL]);
458012f080e7Smrj 		return (DDI_FAILURE);
458112f080e7Smrj 	}
458212f080e7Smrj 
458312f080e7Smrj 	/*
458412f080e7Smrj 	 * if we don't have any windows, and they're asking for the first
458512f080e7Smrj 	 * window, setup the cookie pointer to the first cookie in the bind.
458612f080e7Smrj 	 * setup our return values, then increment the cookie since we return
458712f080e7Smrj 	 * the first cookie on the stack.
458812f080e7Smrj 	 */
458912f080e7Smrj 	if (dma->dp_window == NULL) {
459012f080e7Smrj 		if (win != 0) {
4591*ef4ab52fSFrank Van Der Linden 			ROOTNEX_DPROF_INC(
4592*ef4ab52fSFrank Van Der Linden 			    &rootnex_cnt[ROOTNEX_CNT_GETWIN_FAIL]);
459312f080e7Smrj 			return (DDI_FAILURE);
459412f080e7Smrj 		}
459512f080e7Smrj 		hp->dmai_cookie = dma->dp_cookies;
459612f080e7Smrj 		*offp = 0;
459712f080e7Smrj 		*lenp = dma->dp_dma.dmao_size;
459812f080e7Smrj 		*ccountp = dma->dp_sglinfo.si_sgl_size;
459912f080e7Smrj 		*cookiep = hp->dmai_cookie[0];
460012f080e7Smrj 		hp->dmai_cookie++;
460112f080e7Smrj 		return (DDI_SUCCESS);
460212f080e7Smrj 	}
460312f080e7Smrj 
460412f080e7Smrj 	/* sync the old window before moving on to the new one */
460512f080e7Smrj 	window = &dma->dp_window[dma->dp_current_win];
460612f080e7Smrj 	if ((window->wd_dosync) && (hp->dmai_rflags & DDI_DMA_READ)) {
460794f1124eSVikram Hegde 		(void) rootnex_coredma_sync(dip, rdip, handle, 0, 0,
460812f080e7Smrj 		    DDI_DMA_SYNC_FORCPU);
460912f080e7Smrj 	}
461012f080e7Smrj 
461112f080e7Smrj #if !defined(__amd64)
461212f080e7Smrj 	/*
461312f080e7Smrj 	 * before we move to the next window, if we need to re-map, unmap all
461412f080e7Smrj 	 * the pages in this window.
461512f080e7Smrj 	 */
461612f080e7Smrj 	if (dma->dp_cb_remaping) {
461712f080e7Smrj 		/*
461812f080e7Smrj 		 * If we switch to this window again, we'll need to map in
461912f080e7Smrj 		 * on the fly next time.
462012f080e7Smrj 		 */
462112f080e7Smrj 		window->wd_remap_copybuf = B_TRUE;
462212f080e7Smrj 
462312f080e7Smrj 		/*
462412f080e7Smrj 		 * calculate the page index into the buffer where this window
462512f080e7Smrj 		 * starts, and the number of pages this window takes up.
462612f080e7Smrj 		 */
462712f080e7Smrj 		pidx = (sinfo->si_buf_offset + window->wd_offset) >>
462812f080e7Smrj 		    MMU_PAGESHIFT;
462912f080e7Smrj 		poff = (sinfo->si_buf_offset + window->wd_offset) &
463012f080e7Smrj 		    MMU_PAGEOFFSET;
463112f080e7Smrj 		pcnt = mmu_btopr(window->wd_size + poff);
463212f080e7Smrj 		ASSERT((pidx + pcnt) <= sinfo->si_max_pages);
463312f080e7Smrj 
463412f080e7Smrj 		/* unmap pages which are currently mapped in this window */
463512f080e7Smrj 		for (i = 0; i < pcnt; i++) {
463612f080e7Smrj 			if (dma->dp_pgmap[pidx].pm_mapped) {
463712f080e7Smrj 				hat_unload(kas.a_hat,
463812f080e7Smrj 				    dma->dp_pgmap[pidx].pm_kaddr, MMU_PAGESIZE,
463912f080e7Smrj 				    HAT_UNLOAD);
464012f080e7Smrj 				dma->dp_pgmap[pidx].pm_mapped = B_FALSE;
464112f080e7Smrj 			}
464212f080e7Smrj 			pidx++;
464312f080e7Smrj 		}
464412f080e7Smrj 	}
464512f080e7Smrj #endif
464612f080e7Smrj 
464712f080e7Smrj 	/*
464812f080e7Smrj 	 * Move to the new window.
464912f080e7Smrj 	 * NOTE: current_win must be set for sync to work right
465012f080e7Smrj 	 */
465112f080e7Smrj 	dma->dp_current_win = win;
465212f080e7Smrj 	window = &dma->dp_window[win];
465312f080e7Smrj 
465412f080e7Smrj 	/* if needed, adjust the first and/or last cookies for trim */
465512f080e7Smrj 	trim = &window->wd_trim;
465612f080e7Smrj 	if (trim->tr_trim_first) {
4657843e1988Sjohnlev 		window->wd_first_cookie->dmac_laddress = trim->tr_first_paddr;
465812f080e7Smrj 		window->wd_first_cookie->dmac_size = trim->tr_first_size;
465912f080e7Smrj #if !defined(__amd64)
466012f080e7Smrj 		window->wd_first_cookie->dmac_type =
466112f080e7Smrj 		    (window->wd_first_cookie->dmac_type &
466212f080e7Smrj 		    ROOTNEX_USES_COPYBUF) + window->wd_offset;
466312f080e7Smrj #endif
466412f080e7Smrj 		if (trim->tr_first_copybuf_win) {
466512f080e7Smrj 			dma->dp_pgmap[trim->tr_first_pidx].pm_cbaddr =
466612f080e7Smrj 			    trim->tr_first_cbaddr;
466712f080e7Smrj #if !defined(__amd64)
466812f080e7Smrj 			dma->dp_pgmap[trim->tr_first_pidx].pm_kaddr =
466912f080e7Smrj 			    trim->tr_first_kaddr;
467012f080e7Smrj #endif
467112f080e7Smrj 		}
467212f080e7Smrj 	}
467312f080e7Smrj 	if (trim->tr_trim_last) {
4674843e1988Sjohnlev 		trim->tr_last_cookie->dmac_laddress = trim->tr_last_paddr;
467512f080e7Smrj 		trim->tr_last_cookie->dmac_size = trim->tr_last_size;
467612f080e7Smrj 		if (trim->tr_last_copybuf_win) {
467712f080e7Smrj 			dma->dp_pgmap[trim->tr_last_pidx].pm_cbaddr =
467812f080e7Smrj 			    trim->tr_last_cbaddr;
467912f080e7Smrj #if !defined(__amd64)
468012f080e7Smrj 			dma->dp_pgmap[trim->tr_last_pidx].pm_kaddr =
468112f080e7Smrj 			    trim->tr_last_kaddr;
468212f080e7Smrj #endif
468312f080e7Smrj 		}
468412f080e7Smrj 	}
468512f080e7Smrj 
468612f080e7Smrj 	/*
468712f080e7Smrj 	 * setup the cookie pointer to the first cookie in the window. setup
468812f080e7Smrj 	 * our return values, then increment the cookie since we return the
468912f080e7Smrj 	 * first cookie on the stack.
469012f080e7Smrj 	 */
469112f080e7Smrj 	hp->dmai_cookie = window->wd_first_cookie;
469212f080e7Smrj 	*offp = window->wd_offset;
469312f080e7Smrj 	*lenp = window->wd_size;
469412f080e7Smrj 	*ccountp = window->wd_cookie_cnt;
469512f080e7Smrj 	*cookiep = hp->dmai_cookie[0];
469612f080e7Smrj 	hp->dmai_cookie++;
469712f080e7Smrj 
469812f080e7Smrj #if !defined(__amd64)
469912f080e7Smrj 	/* re-map copybuf if required for this window */
470012f080e7Smrj 	if (dma->dp_cb_remaping) {
470112f080e7Smrj 		/*
470212f080e7Smrj 		 * calculate the page index into the buffer where this
470312f080e7Smrj 		 * window starts.
470412f080e7Smrj 		 */
470512f080e7Smrj 		pidx = (sinfo->si_buf_offset + window->wd_offset) >>
470612f080e7Smrj 		    MMU_PAGESHIFT;
470712f080e7Smrj 		ASSERT(pidx < sinfo->si_max_pages);
470812f080e7Smrj 
470912f080e7Smrj 		/*
471012f080e7Smrj 		 * the first page can get unmapped if it's shared with the
471112f080e7Smrj 		 * previous window. Even if the rest of this window is already
471212f080e7Smrj 		 * mapped in, we need to still check this one.
471312f080e7Smrj 		 */
471412f080e7Smrj 		pmap = &dma->dp_pgmap[pidx];
471512f080e7Smrj 		if ((pmap->pm_uses_copybuf) && (pmap->pm_mapped == B_FALSE)) {
471612f080e7Smrj 			if (pmap->pm_pp != NULL) {
471712f080e7Smrj 				pmap->pm_mapped = B_TRUE;
471812f080e7Smrj 				i86_pp_map(pmap->pm_pp, pmap->pm_kaddr);
471912f080e7Smrj 			} else if (pmap->pm_vaddr != NULL) {
472012f080e7Smrj 				pmap->pm_mapped = B_TRUE;
472112f080e7Smrj 				i86_va_map(pmap->pm_vaddr, sinfo->si_asp,
472212f080e7Smrj 				    pmap->pm_kaddr);
472312f080e7Smrj 			}
472412f080e7Smrj 		}
472512f080e7Smrj 		pidx++;
472612f080e7Smrj 
472712f080e7Smrj 		/* map in the rest of the pages if required */
472812f080e7Smrj 		if (window->wd_remap_copybuf) {
472912f080e7Smrj 			window->wd_remap_copybuf = B_FALSE;
473012f080e7Smrj 
473112f080e7Smrj 			/* figure out many pages this window takes up */
473212f080e7Smrj 			poff = (sinfo->si_buf_offset + window->wd_offset) &
473312f080e7Smrj 			    MMU_PAGEOFFSET;
473412f080e7Smrj 			pcnt = mmu_btopr(window->wd_size + poff);
473512f080e7Smrj 			ASSERT(((pidx - 1) + pcnt) <= sinfo->si_max_pages);
473612f080e7Smrj 
473712f080e7Smrj 			/* map pages which require it */
473812f080e7Smrj 			for (i = 1; i < pcnt; i++) {
473912f080e7Smrj 				pmap = &dma->dp_pgmap[pidx];
474012f080e7Smrj 				if (pmap->pm_uses_copybuf) {
474112f080e7Smrj 					ASSERT(pmap->pm_mapped == B_FALSE);
474212f080e7Smrj 					if (pmap->pm_pp != NULL) {
474312f080e7Smrj 						pmap->pm_mapped = B_TRUE;
474412f080e7Smrj 						i86_pp_map(pmap->pm_pp,
474512f080e7Smrj 						    pmap->pm_kaddr);
474612f080e7Smrj 					} else if (pmap->pm_vaddr != NULL) {
474712f080e7Smrj 						pmap->pm_mapped = B_TRUE;
474812f080e7Smrj 						i86_va_map(pmap->pm_vaddr,
474912f080e7Smrj 						    sinfo->si_asp,
475012f080e7Smrj 						    pmap->pm_kaddr);
475112f080e7Smrj 					}
475212f080e7Smrj 				}
475312f080e7Smrj 				pidx++;
475412f080e7Smrj 			}
475512f080e7Smrj 		}
475612f080e7Smrj 	}
475712f080e7Smrj #endif
475812f080e7Smrj 
475912f080e7Smrj 	/* if the new window uses the copy buffer, sync it for the device */
476012f080e7Smrj 	if ((window->wd_dosync) && (hp->dmai_rflags & DDI_DMA_WRITE)) {
476194f1124eSVikram Hegde 		(void) rootnex_coredma_sync(dip, rdip, handle, 0, 0,
476212f080e7Smrj 		    DDI_DMA_SYNC_FORDEV);
476312f080e7Smrj 	}
476412f080e7Smrj 
476512f080e7Smrj 	return (DDI_SUCCESS);
476612f080e7Smrj }
476712f080e7Smrj 
476820906b23SVikram Hegde /*
476920906b23SVikram Hegde  * rootnex_dma_win()
477020906b23SVikram Hegde  *    called from ddi_dma_getwin()
477120906b23SVikram Hegde  */
477220906b23SVikram Hegde /*ARGSUSED*/
477320906b23SVikram Hegde static int
477420906b23SVikram Hegde rootnex_dma_win(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle,
477520906b23SVikram Hegde     uint_t win, off_t *offp, size_t *lenp, ddi_dma_cookie_t *cookiep,
477620906b23SVikram Hegde     uint_t *ccountp)
477720906b23SVikram Hegde {
47783a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
4779b51bbbf5SVikram Hegde 	if (IOMMU_USED(rdip)) {
478020906b23SVikram Hegde 		return (iommulib_nexdma_win(dip, rdip, handle, win, offp, lenp,
478120906b23SVikram Hegde 		    cookiep, ccountp));
478220906b23SVikram Hegde 	}
478320906b23SVikram Hegde #endif
478412f080e7Smrj 
478520906b23SVikram Hegde 	return (rootnex_coredma_win(dip, rdip, handle, win, offp, lenp,
478620906b23SVikram Hegde 	    cookiep, ccountp));
478720906b23SVikram Hegde }
478812f080e7Smrj 
478912f080e7Smrj /*
479012f080e7Smrj  * ************************
479112f080e7Smrj  *  obsoleted dma routines
479212f080e7Smrj  * ************************
479312f080e7Smrj  */
479412f080e7Smrj 
4795b51bbbf5SVikram Hegde /*
4796b51bbbf5SVikram Hegde  * rootnex_dma_map()
4797b51bbbf5SVikram Hegde  *    called from ddi_dma_setup()
4798b51bbbf5SVikram Hegde  * NO IOMMU in 32 bit mode. The below routines doesn't work in 64 bit mode.
4799b51bbbf5SVikram Hegde  */
480012f080e7Smrj /* ARGSUSED */
480112f080e7Smrj static int
4802b51bbbf5SVikram Hegde rootnex_dma_map(dev_info_t *dip, dev_info_t *rdip,
480320906b23SVikram Hegde     struct ddi_dma_req *dmareq, ddi_dma_handle_t *handlep)
480412f080e7Smrj {
480512f080e7Smrj #if defined(__amd64)
480612f080e7Smrj 	/*
480712f080e7Smrj 	 * this interface is not supported in 64-bit x86 kernel. See comment in
480812f080e7Smrj 	 * rootnex_dma_mctl()
480912f080e7Smrj 	 */
481012f080e7Smrj 	return (DDI_DMA_NORESOURCES);
481112f080e7Smrj 
481212f080e7Smrj #else /* 32-bit x86 kernel */
481312f080e7Smrj 	ddi_dma_handle_t *lhandlep;
481412f080e7Smrj 	ddi_dma_handle_t lhandle;
481512f080e7Smrj 	ddi_dma_cookie_t cookie;
481612f080e7Smrj 	ddi_dma_attr_t dma_attr;
481712f080e7Smrj 	ddi_dma_lim_t *dma_lim;
481812f080e7Smrj 	uint_t ccnt;
481912f080e7Smrj 	int e;
482012f080e7Smrj 
482112f080e7Smrj 
482212f080e7Smrj 	/*
482312f080e7Smrj 	 * if the driver is just testing to see if it's possible to do the bind,
482412f080e7Smrj 	 * we'll use local state. Otherwise, use the handle pointer passed in.
482512f080e7Smrj 	 */
482612f080e7Smrj 	if (handlep == NULL) {
482712f080e7Smrj 		lhandlep = &lhandle;
482812f080e7Smrj 	} else {
482912f080e7Smrj 		lhandlep = handlep;
483012f080e7Smrj 	}
483112f080e7Smrj 
483212f080e7Smrj 	/* convert the limit structure to a dma_attr one */
483312f080e7Smrj 	dma_lim = dmareq->dmar_limits;
483412f080e7Smrj 	dma_attr.dma_attr_version = DMA_ATTR_V0;
483512f080e7Smrj 	dma_attr.dma_attr_addr_lo = dma_lim->dlim_addr_lo;
483612f080e7Smrj 	dma_attr.dma_attr_addr_hi = dma_lim->dlim_addr_hi;
483712f080e7Smrj 	dma_attr.dma_attr_minxfer = dma_lim->dlim_minxfer;
483812f080e7Smrj 	dma_attr.dma_attr_seg = dma_lim->dlim_adreg_max;
483912f080e7Smrj 	dma_attr.dma_attr_count_max = dma_lim->dlim_ctreg_max;
484012f080e7Smrj 	dma_attr.dma_attr_granular = dma_lim->dlim_granular;
484112f080e7Smrj 	dma_attr.dma_attr_sgllen = dma_lim->dlim_sgllen;
484212f080e7Smrj 	dma_attr.dma_attr_maxxfer = dma_lim->dlim_reqsize;
484312f080e7Smrj 	dma_attr.dma_attr_burstsizes = dma_lim->dlim_burstsizes;
484412f080e7Smrj 	dma_attr.dma_attr_align = MMU_PAGESIZE;
484512f080e7Smrj 	dma_attr.dma_attr_flags = 0;
484612f080e7Smrj 
484712f080e7Smrj 	e = rootnex_dma_allochdl(dip, rdip, &dma_attr, dmareq->dmar_fp,
484812f080e7Smrj 	    dmareq->dmar_arg, lhandlep);
484912f080e7Smrj 	if (e != DDI_SUCCESS) {
485012f080e7Smrj 		return (e);
485112f080e7Smrj 	}
485212f080e7Smrj 
485312f080e7Smrj 	e = rootnex_dma_bindhdl(dip, rdip, *lhandlep, dmareq, &cookie, &ccnt);
485412f080e7Smrj 	if ((e != DDI_DMA_MAPPED) && (e != DDI_DMA_PARTIAL_MAP)) {
485512f080e7Smrj 		(void) rootnex_dma_freehdl(dip, rdip, *lhandlep);
485612f080e7Smrj 		return (e);
485712f080e7Smrj 	}
485812f080e7Smrj 
485912f080e7Smrj 	/*
486012f080e7Smrj 	 * if the driver is just testing to see if it's possible to do the bind,
486112f080e7Smrj 	 * free up the local state and return the result.
486212f080e7Smrj 	 */
486312f080e7Smrj 	if (handlep == NULL) {
486412f080e7Smrj 		(void) rootnex_dma_unbindhdl(dip, rdip, *lhandlep);
486512f080e7Smrj 		(void) rootnex_dma_freehdl(dip, rdip, *lhandlep);
486612f080e7Smrj 		if (e == DDI_DMA_MAPPED) {
486712f080e7Smrj 			return (DDI_DMA_MAPOK);
486812f080e7Smrj 		} else {
486912f080e7Smrj 			return (DDI_DMA_NOMAPPING);
487012f080e7Smrj 		}
487112f080e7Smrj 	}
487212f080e7Smrj 
487312f080e7Smrj 	return (e);
487412f080e7Smrj #endif /* defined(__amd64) */
487512f080e7Smrj }
487612f080e7Smrj 
487720906b23SVikram Hegde /*
487812f080e7Smrj  * rootnex_dma_mctl()
487912f080e7Smrj  *
4880b51bbbf5SVikram Hegde  * No IOMMU in 32 bit mode. The below routine doesn't work in 64 bit mode.
488112f080e7Smrj  */
488212f080e7Smrj /* ARGSUSED */
488312f080e7Smrj static int
4884b51bbbf5SVikram Hegde rootnex_dma_mctl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle,
488512f080e7Smrj     enum ddi_dma_ctlops request, off_t *offp, size_t *lenp, caddr_t *objpp,
488612f080e7Smrj     uint_t cache_flags)
488712f080e7Smrj {
488812f080e7Smrj #if defined(__amd64)
488912f080e7Smrj 	/*
489012f080e7Smrj 	 * DDI_DMA_SMEM_ALLOC & DDI_DMA_IOPB_ALLOC we're changed to have a
489112f080e7Smrj 	 * common implementation in genunix, so they no longer have x86
489212f080e7Smrj 	 * specific functionality which called into dma_ctl.
489312f080e7Smrj 	 *
489412f080e7Smrj 	 * The rest of the obsoleted interfaces were never supported in the
489512f080e7Smrj 	 * 64-bit x86 kernel. For s10, the obsoleted DDI_DMA_SEGTOC interface
489612f080e7Smrj 	 * was not ported to the x86 64-bit kernel do to serious x86 rootnex
489712f080e7Smrj 	 * implementation issues.
489812f080e7Smrj 	 *
489912f080e7Smrj 	 * If you can't use DDI_DMA_SEGTOC; DDI_DMA_NEXTSEG, DDI_DMA_FREE, and
490012f080e7Smrj 	 * DDI_DMA_NEXTWIN are useless since you can get to the cookie, so we
490112f080e7Smrj 	 * reflect that now too...
490212f080e7Smrj 	 *
490312f080e7Smrj 	 * Even though we fixed the pointer problem in DDI_DMA_SEGTOC, we are
490412f080e7Smrj 	 * not going to put this functionality into the 64-bit x86 kernel now.
490512f080e7Smrj 	 * It wasn't ported to the 64-bit kernel for s10, no reason to change
490612f080e7Smrj 	 * that in a future release.
490712f080e7Smrj 	 */
490812f080e7Smrj 	return (DDI_FAILURE);
490912f080e7Smrj 
491012f080e7Smrj #else /* 32-bit x86 kernel */
491112f080e7Smrj 	ddi_dma_cookie_t lcookie;
491212f080e7Smrj 	ddi_dma_cookie_t *cookie;
491312f080e7Smrj 	rootnex_window_t *window;
491412f080e7Smrj 	ddi_dma_impl_t *hp;
491512f080e7Smrj 	rootnex_dma_t *dma;
491612f080e7Smrj 	uint_t nwin;
491712f080e7Smrj 	uint_t ccnt;
491812f080e7Smrj 	size_t len;
491912f080e7Smrj 	off_t off;
492012f080e7Smrj 	int e;
492112f080e7Smrj 
492212f080e7Smrj 
492312f080e7Smrj 	/*
492412f080e7Smrj 	 * DDI_DMA_SEGTOC, DDI_DMA_NEXTSEG, and DDI_DMA_NEXTWIN are a little
492512f080e7Smrj 	 * hacky since were optimizing for the current interfaces and so we can
492612f080e7Smrj 	 * cleanup the mess in genunix. Hopefully we will remove the this
492712f080e7Smrj 	 * obsoleted routines someday soon.
492812f080e7Smrj 	 */
492912f080e7Smrj 
493012f080e7Smrj 	switch (request) {
493112f080e7Smrj 
493212f080e7Smrj 	case DDI_DMA_SEGTOC: /* ddi_dma_segtocookie() */
493312f080e7Smrj 		hp = (ddi_dma_impl_t *)handle;
493412f080e7Smrj 		cookie = (ddi_dma_cookie_t *)objpp;
493512f080e7Smrj 
493612f080e7Smrj 		/*
493712f080e7Smrj 		 * convert segment to cookie. We don't distinguish between the
493812f080e7Smrj 		 * two :-)
493912f080e7Smrj 		 */
494012f080e7Smrj 		*cookie = *hp->dmai_cookie;
494112f080e7Smrj 		*lenp = cookie->dmac_size;
494212f080e7Smrj 		*offp = cookie->dmac_type & ~ROOTNEX_USES_COPYBUF;
494312f080e7Smrj 		return (DDI_SUCCESS);
494412f080e7Smrj 
494512f080e7Smrj 	case DDI_DMA_NEXTSEG: /* ddi_dma_nextseg() */
494612f080e7Smrj 		hp = (ddi_dma_impl_t *)handle;
494712f080e7Smrj 		dma = (rootnex_dma_t *)hp->dmai_private;
494812f080e7Smrj 
494912f080e7Smrj 		if ((*lenp != NULL) && ((uintptr_t)*lenp != (uintptr_t)hp)) {
495012f080e7Smrj 			return (DDI_DMA_STALE);
495112f080e7Smrj 		}
495212f080e7Smrj 
495312f080e7Smrj 		/* handle the case where we don't have any windows */
495412f080e7Smrj 		if (dma->dp_window == NULL) {
495512f080e7Smrj 			/*
495612f080e7Smrj 			 * if seg == NULL, and we don't have any windows,
495712f080e7Smrj 			 * return the first cookie in the sgl.
495812f080e7Smrj 			 */
495912f080e7Smrj 			if (*lenp == NULL) {
496012f080e7Smrj 				dma->dp_current_cookie = 0;
496112f080e7Smrj 				hp->dmai_cookie = dma->dp_cookies;
496212f080e7Smrj 				*objpp = (caddr_t)handle;
496312f080e7Smrj 				return (DDI_SUCCESS);
496412f080e7Smrj 
496512f080e7Smrj 			/* if we have more cookies, go to the next cookie */
496612f080e7Smrj 			} else {
496712f080e7Smrj 				if ((dma->dp_current_cookie + 1) >=
496812f080e7Smrj 				    dma->dp_sglinfo.si_sgl_size) {
496912f080e7Smrj 					return (DDI_DMA_DONE);
497012f080e7Smrj 				}
497112f080e7Smrj 				dma->dp_current_cookie++;
497212f080e7Smrj 				hp->dmai_cookie++;
497312f080e7Smrj 				return (DDI_SUCCESS);
497412f080e7Smrj 			}
497512f080e7Smrj 		}
497612f080e7Smrj 
497712f080e7Smrj 		/* We have one or more windows */
497812f080e7Smrj 		window = &dma->dp_window[dma->dp_current_win];
497912f080e7Smrj 
498012f080e7Smrj 		/*
498112f080e7Smrj 		 * if seg == NULL, return the first cookie in the current
498212f080e7Smrj 		 * window
498312f080e7Smrj 		 */
498412f080e7Smrj 		if (*lenp == NULL) {
498512f080e7Smrj 			dma->dp_current_cookie = 0;
4986cf4e9a1dSmrj 			hp->dmai_cookie = window->wd_first_cookie;
498712f080e7Smrj 
498812f080e7Smrj 		/*
498912f080e7Smrj 		 * go to the next cookie in the window then see if we done with
499012f080e7Smrj 		 * this window.
499112f080e7Smrj 		 */
499212f080e7Smrj 		} else {
499312f080e7Smrj 			if ((dma->dp_current_cookie + 1) >=
499412f080e7Smrj 			    window->wd_cookie_cnt) {
499512f080e7Smrj 				return (DDI_DMA_DONE);
499612f080e7Smrj 			}
499712f080e7Smrj 			dma->dp_current_cookie++;
499812f080e7Smrj 			hp->dmai_cookie++;
499912f080e7Smrj 		}
500012f080e7Smrj 		*objpp = (caddr_t)handle;
500112f080e7Smrj 		return (DDI_SUCCESS);
500212f080e7Smrj 
500312f080e7Smrj 	case DDI_DMA_NEXTWIN: /* ddi_dma_nextwin() */
500412f080e7Smrj 		hp = (ddi_dma_impl_t *)handle;
500512f080e7Smrj 		dma = (rootnex_dma_t *)hp->dmai_private;
500612f080e7Smrj 
500712f080e7Smrj 		if ((*offp != NULL) && ((uintptr_t)*offp != (uintptr_t)hp)) {
500812f080e7Smrj 			return (DDI_DMA_STALE);
500912f080e7Smrj 		}
501012f080e7Smrj 
501112f080e7Smrj 		/* if win == NULL, return the first window in the bind */
501212f080e7Smrj 		if (*offp == NULL) {
501312f080e7Smrj 			nwin = 0;
501412f080e7Smrj 
501512f080e7Smrj 		/*
501612f080e7Smrj 		 * else, go to the next window then see if we're done with all
501712f080e7Smrj 		 * the windows.
501812f080e7Smrj 		 */
501912f080e7Smrj 		} else {
502012f080e7Smrj 			nwin = dma->dp_current_win + 1;
502112f080e7Smrj 			if (nwin >= hp->dmai_nwin) {
502212f080e7Smrj 				return (DDI_DMA_DONE);
502312f080e7Smrj 			}
502412f080e7Smrj 		}
502512f080e7Smrj 
502612f080e7Smrj 		/* switch to the next window */
502712f080e7Smrj 		e = rootnex_dma_win(dip, rdip, handle, nwin, &off, &len,
502812f080e7Smrj 		    &lcookie, &ccnt);
502912f080e7Smrj 		ASSERT(e == DDI_SUCCESS);
503012f080e7Smrj 		if (e != DDI_SUCCESS) {
503112f080e7Smrj 			return (DDI_DMA_STALE);
503212f080e7Smrj 		}
503312f080e7Smrj 
503412f080e7Smrj 		/* reset the cookie back to the first cookie in the window */
503512f080e7Smrj 		if (dma->dp_window != NULL) {
503612f080e7Smrj 			window = &dma->dp_window[dma->dp_current_win];
503712f080e7Smrj 			hp->dmai_cookie = window->wd_first_cookie;
503812f080e7Smrj 		} else {
503912f080e7Smrj 			hp->dmai_cookie = dma->dp_cookies;
504012f080e7Smrj 		}
504112f080e7Smrj 
504212f080e7Smrj 		*objpp = (caddr_t)handle;
504312f080e7Smrj 		return (DDI_SUCCESS);
504412f080e7Smrj 
504512f080e7Smrj 	case DDI_DMA_FREE: /* ddi_dma_free() */
504612f080e7Smrj 		(void) rootnex_dma_unbindhdl(dip, rdip, handle);
504712f080e7Smrj 		(void) rootnex_dma_freehdl(dip, rdip, handle);
504812f080e7Smrj 		if (rootnex_state->r_dvma_call_list_id) {
504912f080e7Smrj 			ddi_run_callback(&rootnex_state->r_dvma_call_list_id);
505012f080e7Smrj 		}
505112f080e7Smrj 		return (DDI_SUCCESS);
505212f080e7Smrj 
505312f080e7Smrj 	case DDI_DMA_IOPB_ALLOC:	/* get contiguous DMA-able memory */
505412f080e7Smrj 	case DDI_DMA_SMEM_ALLOC:	/* get contiguous DMA-able memory */
505512f080e7Smrj 		/* should never get here, handled in genunix */
505612f080e7Smrj 		ASSERT(0);
505712f080e7Smrj 		return (DDI_FAILURE);
505812f080e7Smrj 
505912f080e7Smrj 	case DDI_DMA_KVADDR:
506012f080e7Smrj 	case DDI_DMA_GETERR:
506112f080e7Smrj 	case DDI_DMA_COFF:
506212f080e7Smrj 		return (DDI_FAILURE);
506312f080e7Smrj 	}
506412f080e7Smrj 
506512f080e7Smrj 	return (DDI_FAILURE);
506612f080e7Smrj #endif /* defined(__amd64) */
50677c478bd9Sstevel@tonic-gate }
50687aec1d6eScindi 
506920906b23SVikram Hegde /*
507000d0963fSdilpreet  * *********
507100d0963fSdilpreet  *  FMA Code
507200d0963fSdilpreet  * *********
507300d0963fSdilpreet  */
507400d0963fSdilpreet 
507500d0963fSdilpreet /*
507600d0963fSdilpreet  * rootnex_fm_init()
507700d0963fSdilpreet  *    FMA init busop
507800d0963fSdilpreet  */
50797aec1d6eScindi /* ARGSUSED */
50807aec1d6eScindi static int
508100d0963fSdilpreet rootnex_fm_init(dev_info_t *dip, dev_info_t *tdip, int tcap,
508200d0963fSdilpreet     ddi_iblock_cookie_t *ibc)
50837aec1d6eScindi {
508400d0963fSdilpreet 	*ibc = rootnex_state->r_err_ibc;
508500d0963fSdilpreet 
508600d0963fSdilpreet 	return (ddi_system_fmcap);
508700d0963fSdilpreet }
508800d0963fSdilpreet 
508900d0963fSdilpreet /*
509000d0963fSdilpreet  * rootnex_dma_check()
509100d0963fSdilpreet  *    Function called after a dma fault occurred to find out whether the
509200d0963fSdilpreet  *    fault address is associated with a driver that is able to handle faults
509300d0963fSdilpreet  *    and recover from faults.
509400d0963fSdilpreet  */
509500d0963fSdilpreet /* ARGSUSED */
509600d0963fSdilpreet static int
509700d0963fSdilpreet rootnex_dma_check(dev_info_t *dip, const void *handle, const void *addr,
509800d0963fSdilpreet     const void *not_used)
509900d0963fSdilpreet {
510000d0963fSdilpreet 	rootnex_window_t *window;
510100d0963fSdilpreet 	uint64_t start_addr;
510200d0963fSdilpreet 	uint64_t fault_addr;
510300d0963fSdilpreet 	ddi_dma_impl_t *hp;
510400d0963fSdilpreet 	rootnex_dma_t *dma;
510500d0963fSdilpreet 	uint64_t end_addr;
510600d0963fSdilpreet 	size_t csize;
510700d0963fSdilpreet 	int i;
510800d0963fSdilpreet 	int j;
510900d0963fSdilpreet 
511000d0963fSdilpreet 
511100d0963fSdilpreet 	/* The driver has to set DDI_DMA_FLAGERR to recover from dma faults */
511200d0963fSdilpreet 	hp = (ddi_dma_impl_t *)handle;
511300d0963fSdilpreet 	ASSERT(hp);
511400d0963fSdilpreet 
511500d0963fSdilpreet 	dma = (rootnex_dma_t *)hp->dmai_private;
511600d0963fSdilpreet 
511700d0963fSdilpreet 	/* Get the address that we need to search for */
511800d0963fSdilpreet 	fault_addr = *(uint64_t *)addr;
511900d0963fSdilpreet 
512000d0963fSdilpreet 	/*
512100d0963fSdilpreet 	 * if we don't have any windows, we can just walk through all the
512200d0963fSdilpreet 	 * cookies.
512300d0963fSdilpreet 	 */
512400d0963fSdilpreet 	if (dma->dp_window == NULL) {
512500d0963fSdilpreet 		/* for each cookie */
512600d0963fSdilpreet 		for (i = 0; i < dma->dp_sglinfo.si_sgl_size; i++) {
512700d0963fSdilpreet 			/*
512800d0963fSdilpreet 			 * if the faulted address is within the physical address
512900d0963fSdilpreet 			 * range of the cookie, return DDI_FM_NONFATAL.
513000d0963fSdilpreet 			 */
513100d0963fSdilpreet 			if ((fault_addr >= dma->dp_cookies[i].dmac_laddress) &&
513200d0963fSdilpreet 			    (fault_addr <= (dma->dp_cookies[i].dmac_laddress +
513300d0963fSdilpreet 			    dma->dp_cookies[i].dmac_size))) {
513400d0963fSdilpreet 				return (DDI_FM_NONFATAL);
513500d0963fSdilpreet 			}
513600d0963fSdilpreet 		}
513700d0963fSdilpreet 
513800d0963fSdilpreet 		/* fault_addr not within this DMA handle */
513900d0963fSdilpreet 		return (DDI_FM_UNKNOWN);
514000d0963fSdilpreet 	}
514100d0963fSdilpreet 
514200d0963fSdilpreet 	/* we have mutiple windows, walk through each window */
514300d0963fSdilpreet 	for (i = 0; i < hp->dmai_nwin; i++) {
514400d0963fSdilpreet 		window = &dma->dp_window[i];
514500d0963fSdilpreet 
514600d0963fSdilpreet 		/* Go through all the cookies in the window */
514700d0963fSdilpreet 		for (j = 0; j < window->wd_cookie_cnt; j++) {
514800d0963fSdilpreet 
514900d0963fSdilpreet 			start_addr = window->wd_first_cookie[j].dmac_laddress;
515000d0963fSdilpreet 			csize = window->wd_first_cookie[j].dmac_size;
515100d0963fSdilpreet 
515200d0963fSdilpreet 			/*
515300d0963fSdilpreet 			 * if we are trimming the first cookie in the window,
515400d0963fSdilpreet 			 * and this is the first cookie, adjust the start
515500d0963fSdilpreet 			 * address and size of the cookie to account for the
515600d0963fSdilpreet 			 * trim.
515700d0963fSdilpreet 			 */
515800d0963fSdilpreet 			if (window->wd_trim.tr_trim_first && (j == 0)) {
515900d0963fSdilpreet 				start_addr = window->wd_trim.tr_first_paddr;
516000d0963fSdilpreet 				csize = window->wd_trim.tr_first_size;
516100d0963fSdilpreet 			}
516200d0963fSdilpreet 
516300d0963fSdilpreet 			/*
516400d0963fSdilpreet 			 * if we are trimming the last cookie in the window,
516500d0963fSdilpreet 			 * and this is the last cookie, adjust the start
516600d0963fSdilpreet 			 * address and size of the cookie to account for the
516700d0963fSdilpreet 			 * trim.
516800d0963fSdilpreet 			 */
516900d0963fSdilpreet 			if (window->wd_trim.tr_trim_last &&
517000d0963fSdilpreet 			    (j == (window->wd_cookie_cnt - 1))) {
517100d0963fSdilpreet 				start_addr = window->wd_trim.tr_last_paddr;
517200d0963fSdilpreet 				csize = window->wd_trim.tr_last_size;
517300d0963fSdilpreet 			}
517400d0963fSdilpreet 
517500d0963fSdilpreet 			end_addr = start_addr + csize;
517600d0963fSdilpreet 
517700d0963fSdilpreet 			/*
51783a634bfcSVikram Hegde 			 * if the faulted address is within the physical
51793a634bfcSVikram Hegde 			 * address of the cookie, return DDI_FM_NONFATAL.
518000d0963fSdilpreet 			 */
518100d0963fSdilpreet 			if ((fault_addr >= start_addr) &&
518200d0963fSdilpreet 			    (fault_addr <= end_addr)) {
518300d0963fSdilpreet 				return (DDI_FM_NONFATAL);
518400d0963fSdilpreet 			}
518500d0963fSdilpreet 		}
518600d0963fSdilpreet 	}
518700d0963fSdilpreet 
518800d0963fSdilpreet 	/* fault_addr not within this DMA handle */
518900d0963fSdilpreet 	return (DDI_FM_UNKNOWN);
51907aec1d6eScindi }
51913a634bfcSVikram Hegde 
51923a634bfcSVikram Hegde /*ARGSUSED*/
51933a634bfcSVikram Hegde static int
51943a634bfcSVikram Hegde rootnex_quiesce(dev_info_t *dip)
51953a634bfcSVikram Hegde {
51963a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
51973a634bfcSVikram Hegde 	return (immu_quiesce());
51983a634bfcSVikram Hegde #else
51993a634bfcSVikram Hegde 	return (DDI_SUCCESS);
52003a634bfcSVikram Hegde #endif
52013a634bfcSVikram Hegde }
52023a634bfcSVikram Hegde 
52033a634bfcSVikram Hegde #if defined(__xpv)
52043a634bfcSVikram Hegde void
52053a634bfcSVikram Hegde immu_init(void)
52063a634bfcSVikram Hegde {
52073a634bfcSVikram Hegde 	;
52083a634bfcSVikram Hegde }
52093a634bfcSVikram Hegde 
52103a634bfcSVikram Hegde void
52113a634bfcSVikram Hegde immu_startup(void)
52123a634bfcSVikram Hegde {
52133a634bfcSVikram Hegde 	;
52143a634bfcSVikram Hegde }
52153a634bfcSVikram Hegde /*ARGSUSED*/
52163a634bfcSVikram Hegde void
52173a634bfcSVikram Hegde immu_physmem_update(uint64_t addr, uint64_t size)
52183a634bfcSVikram Hegde {
52193a634bfcSVikram Hegde 	;
52203a634bfcSVikram Hegde }
52213a634bfcSVikram Hegde #endif
5222