xref: /titanic_44/usr/src/uts/i86pc/io/rootnex.c (revision fd435bccec40cb841f712413108d8577422fd9ab)
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  */
24bd155b87SGarrett D'Amore /*
25bd155b87SGarrett D'Amore  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
2689b43686SBayard Bell  * Copyright (c) 2011 Bayard G. Bell.  All rights reserved.
27cd21e7c5SGarrett D'Amore  * Copyright 2012 Garrett D'Amore <garrett@damore.org>.  All rights reserved.
28bd155b87SGarrett D'Amore  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
3112f080e7Smrj  * x86 root nexus driver
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
357c478bd9Sstevel@tonic-gate #include <sys/conf.h>
367c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
377c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
387c478bd9Sstevel@tonic-gate #include <sys/debug.h>
397c478bd9Sstevel@tonic-gate #include <sys/psw.h>
407c478bd9Sstevel@tonic-gate #include <sys/ddidmareq.h>
417c478bd9Sstevel@tonic-gate #include <sys/promif.h>
427c478bd9Sstevel@tonic-gate #include <sys/devops.h>
437c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
447c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
457c478bd9Sstevel@tonic-gate #include <vm/seg.h>
467c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
477c478bd9Sstevel@tonic-gate #include <vm/seg_dev.h>
487c478bd9Sstevel@tonic-gate #include <sys/vmem.h>
497c478bd9Sstevel@tonic-gate #include <sys/mman.h>
507c478bd9Sstevel@tonic-gate #include <vm/hat.h>
517c478bd9Sstevel@tonic-gate #include <vm/as.h>
527c478bd9Sstevel@tonic-gate #include <vm/page.h>
537c478bd9Sstevel@tonic-gate #include <sys/avintr.h>
547c478bd9Sstevel@tonic-gate #include <sys/errno.h>
557c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
567c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
577c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
587c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
597a364d25Sschwartz #include <sys/mach_intr.h>
607c478bd9Sstevel@tonic-gate #include <sys/psm.h>
617c478bd9Sstevel@tonic-gate #include <sys/ontrap.h>
6212f080e7Smrj #include <sys/atomic.h>
6312f080e7Smrj #include <sys/sdt.h>
6412f080e7Smrj #include <sys/rootnex.h>
6512f080e7Smrj #include <vm/hat_i86.h>
6600d0963fSdilpreet #include <sys/ddifm.h>
6736945f79Smrj #include <sys/ddi_isa.h>
687ff178cdSJimmy Vetayases #include <sys/apic.h>
697c478bd9Sstevel@tonic-gate 
70843e1988Sjohnlev #ifdef __xpv
71843e1988Sjohnlev #include <sys/bootinfo.h>
72843e1988Sjohnlev #include <sys/hypervisor.h>
73843e1988Sjohnlev #include <sys/bootconf.h>
74843e1988Sjohnlev #include <vm/kboot_mmu.h>
753a634bfcSVikram Hegde #endif
763a634bfcSVikram Hegde 
773a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
783a634bfcSVikram Hegde #include <sys/immu.h>
79843e1988Sjohnlev #endif
80843e1988Sjohnlev 
8186c1f4dcSVikram Hegde 
8212f080e7Smrj /*
8312f080e7Smrj  * enable/disable extra checking of function parameters. Useful for debugging
8412f080e7Smrj  * drivers.
8512f080e7Smrj  */
8612f080e7Smrj #ifdef	DEBUG
8712f080e7Smrj int rootnex_alloc_check_parms = 1;
8812f080e7Smrj int rootnex_bind_check_parms = 1;
8912f080e7Smrj int rootnex_bind_check_inuse = 1;
9012f080e7Smrj int rootnex_unbind_verify_buffer = 0;
9112f080e7Smrj int rootnex_sync_check_parms = 1;
9212f080e7Smrj #else
9312f080e7Smrj int rootnex_alloc_check_parms = 0;
9412f080e7Smrj int rootnex_bind_check_parms = 0;
9512f080e7Smrj int rootnex_bind_check_inuse = 0;
9612f080e7Smrj int rootnex_unbind_verify_buffer = 0;
9712f080e7Smrj int rootnex_sync_check_parms = 0;
9812f080e7Smrj #endif
997c478bd9Sstevel@tonic-gate 
1003a634bfcSVikram Hegde boolean_t rootnex_dmar_not_setup;
1013a634bfcSVikram Hegde 
1027aec1d6eScindi /* Master Abort and Target Abort panic flag */
1037aec1d6eScindi int rootnex_fm_ma_ta_panic_flag = 0;
1047aec1d6eScindi 
10512f080e7Smrj /* Semi-temporary patchables to phase in bug fixes, test drivers, etc. */
1067c478bd9Sstevel@tonic-gate int rootnex_bind_fail = 1;
1077c478bd9Sstevel@tonic-gate int rootnex_bind_warn = 1;
1087c478bd9Sstevel@tonic-gate uint8_t *rootnex_warn_list;
1097c478bd9Sstevel@tonic-gate /* bitmasks for rootnex_warn_list. Up to 8 different warnings with uint8_t */
1107c478bd9Sstevel@tonic-gate #define	ROOTNEX_BIND_WARNING	(0x1 << 0)
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate /*
11312f080e7Smrj  * revert back to old broken behavior of always sync'ing entire copy buffer.
11412f080e7Smrj  * This is useful if be have a buggy driver which doesn't correctly pass in
11512f080e7Smrj  * the offset and size into ddi_dma_sync().
1167c478bd9Sstevel@tonic-gate  */
11712f080e7Smrj int rootnex_sync_ignore_params = 0;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate /*
12012f080e7Smrj  * For the 64-bit kernel, pre-alloc enough cookies for a 256K buffer plus 1
12112f080e7Smrj  * page for alignment. For the 32-bit kernel, pre-alloc enough cookies for a
12212f080e7Smrj  * 64K buffer plus 1 page for alignment (we have less kernel space in a 32-bit
12312f080e7Smrj  * kernel). Allocate enough windows to handle a 256K buffer w/ at least 65
12412f080e7Smrj  * sgllen DMA engine, and enough copybuf buffer state pages to handle 2 pages
12512f080e7Smrj  * (< 8K). We will still need to allocate the copy buffer during bind though
12612f080e7Smrj  * (if we need one). These can only be modified in /etc/system before rootnex
12712f080e7Smrj  * attach.
1287c478bd9Sstevel@tonic-gate  */
12912f080e7Smrj #if defined(__amd64)
13012f080e7Smrj int rootnex_prealloc_cookies = 65;
13112f080e7Smrj int rootnex_prealloc_windows = 4;
13212f080e7Smrj int rootnex_prealloc_copybuf = 2;
13312f080e7Smrj #else
13412f080e7Smrj int rootnex_prealloc_cookies = 33;
13512f080e7Smrj int rootnex_prealloc_windows = 4;
13612f080e7Smrj int rootnex_prealloc_copybuf = 2;
13712f080e7Smrj #endif
1387c478bd9Sstevel@tonic-gate 
13912f080e7Smrj /* driver global state */
14012f080e7Smrj static rootnex_state_t *rootnex_state;
14112f080e7Smrj 
142ef4ab52fSFrank Van Der Linden #ifdef DEBUG
14312f080e7Smrj /* shortcut to rootnex counters */
14412f080e7Smrj static uint64_t *rootnex_cnt;
145ef4ab52fSFrank Van Der Linden #endif
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate /*
14812f080e7Smrj  * XXX - does x86 even need these or are they left over from the SPARC days?
1497c478bd9Sstevel@tonic-gate  */
15012f080e7Smrj /* statically defined integer/boolean properties for the root node */
15112f080e7Smrj static rootnex_intprop_t rootnex_intprp[] = {
15212f080e7Smrj 	{ "PAGESIZE",			PAGESIZE },
15312f080e7Smrj 	{ "MMU_PAGESIZE",		MMU_PAGESIZE },
15412f080e7Smrj 	{ "MMU_PAGEOFFSET",		MMU_PAGEOFFSET },
15512f080e7Smrj 	{ DDI_RELATIVE_ADDRESSING,	1 },
15612f080e7Smrj };
15712f080e7Smrj #define	NROOT_INTPROPS	(sizeof (rootnex_intprp) / sizeof (rootnex_intprop_t))
1587c478bd9Sstevel@tonic-gate 
15950200e77SFrank Van Der Linden /*
16050200e77SFrank Van Der Linden  * If we're dom0, we're using a real device so we need to load
16150200e77SFrank Van Der Linden  * the cookies with MFNs instead of PFNs.
16250200e77SFrank Van Der Linden  */
163843e1988Sjohnlev #ifdef __xpv
164843e1988Sjohnlev typedef maddr_t rootnex_addr_t;
16550200e77SFrank Van Der Linden #define	ROOTNEX_PADDR_TO_RBASE(pa)	\
16650200e77SFrank Van Der Linden 	(DOMAIN_IS_INITDOMAIN(xen_info) ? pa_to_ma(pa) : (pa))
167843e1988Sjohnlev #else
168843e1988Sjohnlev typedef paddr_t rootnex_addr_t;
16950200e77SFrank Van Der Linden #define	ROOTNEX_PADDR_TO_RBASE(pa)	(pa)
170843e1988Sjohnlev #endif
171843e1988Sjohnlev 
17212f080e7Smrj static struct cb_ops rootnex_cb_ops = {
17312f080e7Smrj 	nodev,		/* open */
17412f080e7Smrj 	nodev,		/* close */
17512f080e7Smrj 	nodev,		/* strategy */
17612f080e7Smrj 	nodev,		/* print */
17712f080e7Smrj 	nodev,		/* dump */
17812f080e7Smrj 	nodev,		/* read */
17912f080e7Smrj 	nodev,		/* write */
18012f080e7Smrj 	nodev,		/* ioctl */
18112f080e7Smrj 	nodev,		/* devmap */
18212f080e7Smrj 	nodev,		/* mmap */
18312f080e7Smrj 	nodev,		/* segmap */
18412f080e7Smrj 	nochpoll,	/* chpoll */
18512f080e7Smrj 	ddi_prop_op,	/* cb_prop_op */
18612f080e7Smrj 	NULL,		/* struct streamtab */
18712f080e7Smrj 	D_NEW | D_MP | D_HOTPLUG, /* compatibility flags */
18812f080e7Smrj 	CB_REV,		/* Rev */
18912f080e7Smrj 	nodev,		/* cb_aread */
19012f080e7Smrj 	nodev		/* cb_awrite */
19112f080e7Smrj };
1927c478bd9Sstevel@tonic-gate 
19312f080e7Smrj static int rootnex_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
1947c478bd9Sstevel@tonic-gate     off_t offset, off_t len, caddr_t *vaddrp);
19512f080e7Smrj static int rootnex_map_fault(dev_info_t *dip, dev_info_t *rdip,
1967c478bd9Sstevel@tonic-gate     struct hat *hat, struct seg *seg, caddr_t addr,
1977c478bd9Sstevel@tonic-gate     struct devpage *dp, pfn_t pfn, uint_t prot, uint_t lock);
19812f080e7Smrj static int rootnex_dma_allochdl(dev_info_t *dip, dev_info_t *rdip,
19912f080e7Smrj     ddi_dma_attr_t *attr, int (*waitfp)(caddr_t), caddr_t arg,
20012f080e7Smrj     ddi_dma_handle_t *handlep);
20112f080e7Smrj static int rootnex_dma_freehdl(dev_info_t *dip, dev_info_t *rdip,
20212f080e7Smrj     ddi_dma_handle_t handle);
20312f080e7Smrj static int rootnex_dma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
20412f080e7Smrj     ddi_dma_handle_t handle, struct ddi_dma_req *dmareq,
20512f080e7Smrj     ddi_dma_cookie_t *cookiep, uint_t *ccountp);
20612f080e7Smrj static int rootnex_dma_unbindhdl(dev_info_t *dip, dev_info_t *rdip,
20712f080e7Smrj     ddi_dma_handle_t handle);
20812f080e7Smrj static int rootnex_dma_sync(dev_info_t *dip, dev_info_t *rdip,
20912f080e7Smrj     ddi_dma_handle_t handle, off_t off, size_t len, uint_t cache_flags);
21012f080e7Smrj static int rootnex_dma_win(dev_info_t *dip, dev_info_t *rdip,
21112f080e7Smrj     ddi_dma_handle_t handle, uint_t win, off_t *offp, size_t *lenp,
21212f080e7Smrj     ddi_dma_cookie_t *cookiep, uint_t *ccountp);
21312f080e7Smrj static int rootnex_dma_mctl(dev_info_t *dip, dev_info_t *rdip,
2147c478bd9Sstevel@tonic-gate     ddi_dma_handle_t handle, enum ddi_dma_ctlops request,
2157c478bd9Sstevel@tonic-gate     off_t *offp, size_t *lenp, caddr_t *objp, uint_t cache_flags);
21612f080e7Smrj static int rootnex_ctlops(dev_info_t *dip, dev_info_t *rdip,
21712f080e7Smrj     ddi_ctl_enum_t ctlop, void *arg, void *result);
21800d0963fSdilpreet static int rootnex_fm_init(dev_info_t *dip, dev_info_t *tdip, int tcap,
21900d0963fSdilpreet     ddi_iblock_cookie_t *ibc);
22012f080e7Smrj static int rootnex_intr_ops(dev_info_t *pdip, dev_info_t *rdip,
22112f080e7Smrj     ddi_intr_op_t intr_op, ddi_intr_handle_impl_t *hdlp, void *result);
2227ff178cdSJimmy Vetayases static int rootnex_alloc_intr_fixed(dev_info_t *, ddi_intr_handle_impl_t *,
2237ff178cdSJimmy Vetayases     void *);
2247ff178cdSJimmy Vetayases static int rootnex_free_intr_fixed(dev_info_t *, ddi_intr_handle_impl_t *);
2257c478bd9Sstevel@tonic-gate 
22620906b23SVikram Hegde static int rootnex_coredma_allochdl(dev_info_t *dip, dev_info_t *rdip,
22720906b23SVikram Hegde     ddi_dma_attr_t *attr, int (*waitfp)(caddr_t), caddr_t arg,
22820906b23SVikram Hegde     ddi_dma_handle_t *handlep);
22920906b23SVikram Hegde static int rootnex_coredma_freehdl(dev_info_t *dip, dev_info_t *rdip,
23020906b23SVikram Hegde     ddi_dma_handle_t handle);
23120906b23SVikram Hegde static int rootnex_coredma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
23220906b23SVikram Hegde     ddi_dma_handle_t handle, struct ddi_dma_req *dmareq,
23320906b23SVikram Hegde     ddi_dma_cookie_t *cookiep, uint_t *ccountp);
23420906b23SVikram Hegde static int rootnex_coredma_unbindhdl(dev_info_t *dip, dev_info_t *rdip,
23520906b23SVikram Hegde     ddi_dma_handle_t handle);
2363a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
23720906b23SVikram Hegde static void rootnex_coredma_reset_cookies(dev_info_t *dip,
23820906b23SVikram Hegde     ddi_dma_handle_t handle);
23920906b23SVikram Hegde static int rootnex_coredma_get_cookies(dev_info_t *dip, ddi_dma_handle_t handle,
24094f1124eSVikram Hegde     ddi_dma_cookie_t **cookiepp, uint_t *ccountp);
24194f1124eSVikram Hegde static int rootnex_coredma_set_cookies(dev_info_t *dip, ddi_dma_handle_t handle,
24294f1124eSVikram Hegde     ddi_dma_cookie_t *cookiep, uint_t ccount);
24394f1124eSVikram Hegde static int rootnex_coredma_clear_cookies(dev_info_t *dip,
24494f1124eSVikram Hegde     ddi_dma_handle_t handle);
24594f1124eSVikram Hegde static int rootnex_coredma_get_sleep_flags(ddi_dma_handle_t handle);
2465dfdb46bSVikram Hegde #endif
24720906b23SVikram Hegde static int rootnex_coredma_sync(dev_info_t *dip, dev_info_t *rdip,
24820906b23SVikram Hegde     ddi_dma_handle_t handle, off_t off, size_t len, uint_t cache_flags);
24920906b23SVikram Hegde static int rootnex_coredma_win(dev_info_t *dip, dev_info_t *rdip,
25020906b23SVikram Hegde     ddi_dma_handle_t handle, uint_t win, off_t *offp, size_t *lenp,
25120906b23SVikram Hegde     ddi_dma_cookie_t *cookiep, uint_t *ccountp);
2527c478bd9Sstevel@tonic-gate 
253ee7536e8SFrank Van Der Linden #if defined(__amd64) && !defined(__xpv)
25450200e77SFrank Van Der Linden static int rootnex_coredma_hdl_setprivate(dev_info_t *dip, dev_info_t *rdip,
25550200e77SFrank Van Der Linden     ddi_dma_handle_t handle, void *v);
25650200e77SFrank Van Der Linden static void *rootnex_coredma_hdl_getprivate(dev_info_t *dip, dev_info_t *rdip,
25750200e77SFrank Van Der Linden     ddi_dma_handle_t handle);
258ee7536e8SFrank Van Der Linden #endif
25950200e77SFrank Van Der Linden 
26050200e77SFrank Van Der Linden 
2617c478bd9Sstevel@tonic-gate static struct bus_ops rootnex_bus_ops = {
2627c478bd9Sstevel@tonic-gate 	BUSO_REV,
2637c478bd9Sstevel@tonic-gate 	rootnex_map,
2647c478bd9Sstevel@tonic-gate 	NULL,
2657c478bd9Sstevel@tonic-gate 	NULL,
2667c478bd9Sstevel@tonic-gate 	NULL,
2677c478bd9Sstevel@tonic-gate 	rootnex_map_fault,
268cd21e7c5SGarrett D'Amore 	0,
2697c478bd9Sstevel@tonic-gate 	rootnex_dma_allochdl,
2707c478bd9Sstevel@tonic-gate 	rootnex_dma_freehdl,
2717c478bd9Sstevel@tonic-gate 	rootnex_dma_bindhdl,
2727c478bd9Sstevel@tonic-gate 	rootnex_dma_unbindhdl,
27312f080e7Smrj 	rootnex_dma_sync,
2747c478bd9Sstevel@tonic-gate 	rootnex_dma_win,
2757c478bd9Sstevel@tonic-gate 	rootnex_dma_mctl,
2767c478bd9Sstevel@tonic-gate 	rootnex_ctlops,
2777c478bd9Sstevel@tonic-gate 	ddi_bus_prop_op,
2787c478bd9Sstevel@tonic-gate 	i_ddi_rootnex_get_eventcookie,
2797c478bd9Sstevel@tonic-gate 	i_ddi_rootnex_add_eventcall,
2807c478bd9Sstevel@tonic-gate 	i_ddi_rootnex_remove_eventcall,
2817c478bd9Sstevel@tonic-gate 	i_ddi_rootnex_post_event,
2827c478bd9Sstevel@tonic-gate 	0,			/* bus_intr_ctl */
2837c478bd9Sstevel@tonic-gate 	0,			/* bus_config */
2847c478bd9Sstevel@tonic-gate 	0,			/* bus_unconfig */
28500d0963fSdilpreet 	rootnex_fm_init,	/* bus_fm_init */
2867c478bd9Sstevel@tonic-gate 	NULL,			/* bus_fm_fini */
2877c478bd9Sstevel@tonic-gate 	NULL,			/* bus_fm_access_enter */
2887c478bd9Sstevel@tonic-gate 	NULL,			/* bus_fm_access_exit */
2897c478bd9Sstevel@tonic-gate 	NULL,			/* bus_powr */
2907c478bd9Sstevel@tonic-gate 	rootnex_intr_ops	/* bus_intr_op */
2917c478bd9Sstevel@tonic-gate };
2927c478bd9Sstevel@tonic-gate 
29312f080e7Smrj static int rootnex_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
29412f080e7Smrj static int rootnex_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
2953a634bfcSVikram Hegde static int rootnex_quiesce(dev_info_t *dip);
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate static struct dev_ops rootnex_ops = {
2987c478bd9Sstevel@tonic-gate 	DEVO_REV,
29912f080e7Smrj 	0,
30012f080e7Smrj 	ddi_no_info,
3017c478bd9Sstevel@tonic-gate 	nulldev,
30212f080e7Smrj 	nulldev,
3037c478bd9Sstevel@tonic-gate 	rootnex_attach,
30412f080e7Smrj 	rootnex_detach,
30512f080e7Smrj 	nulldev,
30612f080e7Smrj 	&rootnex_cb_ops,
30719397407SSherry Moore 	&rootnex_bus_ops,
30819397407SSherry Moore 	NULL,
3093a634bfcSVikram Hegde 	rootnex_quiesce,		/* quiesce */
3107c478bd9Sstevel@tonic-gate };
3117c478bd9Sstevel@tonic-gate 
31212f080e7Smrj static struct modldrv rootnex_modldrv = {
31312f080e7Smrj 	&mod_driverops,
314613b2871SRichard Bean 	"i86pc root nexus",
31512f080e7Smrj 	&rootnex_ops
3167c478bd9Sstevel@tonic-gate };
3177c478bd9Sstevel@tonic-gate 
31812f080e7Smrj static struct modlinkage rootnex_modlinkage = {
31912f080e7Smrj 	MODREV_1,
32012f080e7Smrj 	(void *)&rootnex_modldrv,
32112f080e7Smrj 	NULL
3227c478bd9Sstevel@tonic-gate };
3237c478bd9Sstevel@tonic-gate 
3243a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
32520906b23SVikram Hegde static iommulib_nexops_t iommulib_nexops = {
32620906b23SVikram Hegde 	IOMMU_NEXOPS_VERSION,
32720906b23SVikram Hegde 	"Rootnex IOMMU ops Vers 1.1",
32820906b23SVikram Hegde 	NULL,
32920906b23SVikram Hegde 	rootnex_coredma_allochdl,
33020906b23SVikram Hegde 	rootnex_coredma_freehdl,
33120906b23SVikram Hegde 	rootnex_coredma_bindhdl,
33220906b23SVikram Hegde 	rootnex_coredma_unbindhdl,
33320906b23SVikram Hegde 	rootnex_coredma_reset_cookies,
33420906b23SVikram Hegde 	rootnex_coredma_get_cookies,
33594f1124eSVikram Hegde 	rootnex_coredma_set_cookies,
33694f1124eSVikram Hegde 	rootnex_coredma_clear_cookies,
33794f1124eSVikram Hegde 	rootnex_coredma_get_sleep_flags,
33820906b23SVikram Hegde 	rootnex_coredma_sync,
33920906b23SVikram Hegde 	rootnex_coredma_win,
34050200e77SFrank Van Der Linden 	rootnex_coredma_hdl_setprivate,
34150200e77SFrank Van Der Linden 	rootnex_coredma_hdl_getprivate
34220906b23SVikram Hegde };
3435dfdb46bSVikram Hegde #endif
3447c478bd9Sstevel@tonic-gate 
34512f080e7Smrj /*
34612f080e7Smrj  *  extern hacks
34712f080e7Smrj  */
34812f080e7Smrj extern struct seg_ops segdev_ops;
34912f080e7Smrj extern int ignore_hardware_nodes;	/* force flag from ddi_impl.c */
35012f080e7Smrj #ifdef	DDI_MAP_DEBUG
35112f080e7Smrj extern int ddi_map_debug_flag;
35212f080e7Smrj #define	ddi_map_debug	if (ddi_map_debug_flag) prom_printf
35312f080e7Smrj #endif
35412f080e7Smrj extern void i86_pp_map(page_t *pp, caddr_t kaddr);
35512f080e7Smrj extern void i86_va_map(caddr_t vaddr, struct as *asp, caddr_t kaddr);
35612f080e7Smrj extern int (*psm_intr_ops)(dev_info_t *, ddi_intr_handle_impl_t *,
35712f080e7Smrj     psm_intr_op_t, int *);
35812f080e7Smrj extern int impl_ddi_sunbus_initchild(dev_info_t *dip);
35912f080e7Smrj extern void impl_ddi_sunbus_removechild(dev_info_t *dip);
36036945f79Smrj 
36112f080e7Smrj /*
36212f080e7Smrj  * Use device arena to use for device control register mappings.
36312f080e7Smrj  * Various kernel memory walkers (debugger, dtrace) need to know
36412f080e7Smrj  * to avoid this address range to prevent undesired device activity.
36512f080e7Smrj  */
36612f080e7Smrj extern void *device_arena_alloc(size_t size, int vm_flag);
36712f080e7Smrj extern void device_arena_free(void * vaddr, size_t size);
36812f080e7Smrj 
36912f080e7Smrj 
37012f080e7Smrj /*
37112f080e7Smrj  *  Internal functions
37212f080e7Smrj  */
37312f080e7Smrj static int rootnex_dma_init();
37412f080e7Smrj static void rootnex_add_props(dev_info_t *);
37512f080e7Smrj static int rootnex_ctl_reportdev(dev_info_t *dip);
37612f080e7Smrj static struct intrspec *rootnex_get_ispec(dev_info_t *rdip, int inum);
37712f080e7Smrj static int rootnex_map_regspec(ddi_map_req_t *mp, caddr_t *vaddrp);
37812f080e7Smrj static int rootnex_unmap_regspec(ddi_map_req_t *mp, caddr_t *vaddrp);
37912f080e7Smrj static int rootnex_map_handle(ddi_map_req_t *mp);
38012f080e7Smrj static void rootnex_clean_dmahdl(ddi_dma_impl_t *hp);
38112f080e7Smrj static int rootnex_valid_alloc_parms(ddi_dma_attr_t *attr, uint_t maxsegsize);
38212f080e7Smrj static int rootnex_valid_bind_parms(ddi_dma_req_t *dmareq,
38312f080e7Smrj     ddi_dma_attr_t *attr);
38412f080e7Smrj static void rootnex_get_sgl(ddi_dma_obj_t *dmar_object, ddi_dma_cookie_t *sgl,
38512f080e7Smrj     rootnex_sglinfo_t *sglinfo);
38650200e77SFrank Van Der Linden static void rootnex_dvma_get_sgl(ddi_dma_obj_t *dmar_object,
38750200e77SFrank Van Der Linden     ddi_dma_cookie_t *sgl, rootnex_sglinfo_t *sglinfo);
38812f080e7Smrj static int rootnex_bind_slowpath(ddi_dma_impl_t *hp, struct ddi_dma_req *dmareq,
38950200e77SFrank Van Der Linden     rootnex_dma_t *dma, ddi_dma_attr_t *attr, ddi_dma_obj_t *dmao, int kmflag);
39012f080e7Smrj static int rootnex_setup_copybuf(ddi_dma_impl_t *hp, struct ddi_dma_req *dmareq,
39112f080e7Smrj     rootnex_dma_t *dma, ddi_dma_attr_t *attr);
39212f080e7Smrj static void rootnex_teardown_copybuf(rootnex_dma_t *dma);
39312f080e7Smrj static int rootnex_setup_windows(ddi_dma_impl_t *hp, rootnex_dma_t *dma,
39450200e77SFrank Van Der Linden     ddi_dma_attr_t *attr, ddi_dma_obj_t *dmao, int kmflag);
39512f080e7Smrj static void rootnex_teardown_windows(rootnex_dma_t *dma);
39612f080e7Smrj static void rootnex_init_win(ddi_dma_impl_t *hp, rootnex_dma_t *dma,
39712f080e7Smrj     rootnex_window_t *window, ddi_dma_cookie_t *cookie, off_t cur_offset);
39812f080e7Smrj static void rootnex_setup_cookie(ddi_dma_obj_t *dmar_object,
39912f080e7Smrj     rootnex_dma_t *dma, ddi_dma_cookie_t *cookie, off_t cur_offset,
40012f080e7Smrj     size_t *copybuf_used, page_t **cur_pp);
40112f080e7Smrj static int rootnex_sgllen_window_boundary(ddi_dma_impl_t *hp,
40212f080e7Smrj     rootnex_dma_t *dma, rootnex_window_t **windowp, ddi_dma_cookie_t *cookie,
40312f080e7Smrj     ddi_dma_attr_t *attr, off_t cur_offset);
40412f080e7Smrj static int rootnex_copybuf_window_boundary(ddi_dma_impl_t *hp,
40512f080e7Smrj     rootnex_dma_t *dma, rootnex_window_t **windowp,
40612f080e7Smrj     ddi_dma_cookie_t *cookie, off_t cur_offset, size_t *copybuf_used);
40712f080e7Smrj static int rootnex_maxxfer_window_boundary(ddi_dma_impl_t *hp,
40812f080e7Smrj     rootnex_dma_t *dma, rootnex_window_t **windowp, ddi_dma_cookie_t *cookie);
40912f080e7Smrj static int rootnex_valid_sync_parms(ddi_dma_impl_t *hp, rootnex_window_t *win,
41012f080e7Smrj     off_t offset, size_t size, uint_t cache_flags);
41112f080e7Smrj static int rootnex_verify_buffer(rootnex_dma_t *dma);
41200d0963fSdilpreet static int rootnex_dma_check(dev_info_t *dip, const void *handle,
41300d0963fSdilpreet     const void *comp_addr, const void *not_used);
41407c6692fSMark Johnson static boolean_t rootnex_need_bounce_seg(ddi_dma_obj_t *dmar_object,
41507c6692fSMark Johnson     rootnex_sglinfo_t *sglinfo);
41650200e77SFrank Van Der Linden static struct as *rootnex_get_as(ddi_dma_obj_t *dmar_object);
41712f080e7Smrj 
41812f080e7Smrj /*
41912f080e7Smrj  * _init()
42012f080e7Smrj  *
42112f080e7Smrj  */
4227c478bd9Sstevel@tonic-gate int
_init(void)4237c478bd9Sstevel@tonic-gate _init(void)
4247c478bd9Sstevel@tonic-gate {
42512f080e7Smrj 
42612f080e7Smrj 	rootnex_state = NULL;
42712f080e7Smrj 	return (mod_install(&rootnex_modlinkage));
4287c478bd9Sstevel@tonic-gate }
4297c478bd9Sstevel@tonic-gate 
43012f080e7Smrj 
43112f080e7Smrj /*
43212f080e7Smrj  * _info()
43312f080e7Smrj  *
43412f080e7Smrj  */
43512f080e7Smrj int
_info(struct modinfo * modinfop)43612f080e7Smrj _info(struct modinfo *modinfop)
43712f080e7Smrj {
43812f080e7Smrj 	return (mod_info(&rootnex_modlinkage, modinfop));
43912f080e7Smrj }
44012f080e7Smrj 
44112f080e7Smrj 
44212f080e7Smrj /*
44312f080e7Smrj  * _fini()
44412f080e7Smrj  *
44512f080e7Smrj  */
4467c478bd9Sstevel@tonic-gate int
_fini(void)4477c478bd9Sstevel@tonic-gate _fini(void)
4487c478bd9Sstevel@tonic-gate {
4497c478bd9Sstevel@tonic-gate 	return (EBUSY);
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate 
45212f080e7Smrj 
45312f080e7Smrj /*
45412f080e7Smrj  * rootnex_attach()
45512f080e7Smrj  *
45612f080e7Smrj  */
45712f080e7Smrj static int
rootnex_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)45812f080e7Smrj rootnex_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
4597c478bd9Sstevel@tonic-gate {
4607aec1d6eScindi 	int fmcap;
46112f080e7Smrj 	int e;
46212f080e7Smrj 
46312f080e7Smrj 	switch (cmd) {
46412f080e7Smrj 	case DDI_ATTACH:
46512f080e7Smrj 		break;
46612f080e7Smrj 	case DDI_RESUME:
4673a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
4683a634bfcSVikram Hegde 		return (immu_unquiesce());
4693a634bfcSVikram Hegde #else
47012f080e7Smrj 		return (DDI_SUCCESS);
4713a634bfcSVikram Hegde #endif
47212f080e7Smrj 	default:
47312f080e7Smrj 		return (DDI_FAILURE);
4747c478bd9Sstevel@tonic-gate 	}
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	/*
47712f080e7Smrj 	 * We should only have one instance of rootnex. Save it away since we
47812f080e7Smrj 	 * don't have an easy way to get it back later.
4797c478bd9Sstevel@tonic-gate 	 */
48012f080e7Smrj 	ASSERT(rootnex_state == NULL);
48112f080e7Smrj 	rootnex_state = kmem_zalloc(sizeof (rootnex_state_t), KM_SLEEP);
4827c478bd9Sstevel@tonic-gate 
48312f080e7Smrj 	rootnex_state->r_dip = dip;
4847aec1d6eScindi 	rootnex_state->r_err_ibc = (ddi_iblock_cookie_t)ipltospl(15);
48512f080e7Smrj 	rootnex_state->r_reserved_msg_printed = B_FALSE;
486ef4ab52fSFrank Van Der Linden #ifdef DEBUG
48712f080e7Smrj 	rootnex_cnt = &rootnex_state->r_counters[0];
488ef4ab52fSFrank Van Der Linden #endif
4897c478bd9Sstevel@tonic-gate 
4907aec1d6eScindi 	/*
4917aec1d6eScindi 	 * Set minimum fm capability level for i86pc platforms and then
4927aec1d6eScindi 	 * initialize error handling. Since we're the rootnex, we don't
4937aec1d6eScindi 	 * care what's returned in the fmcap field.
4947aec1d6eScindi 	 */
49500d0963fSdilpreet 	ddi_system_fmcap = DDI_FM_EREPORT_CAPABLE | DDI_FM_ERRCB_CAPABLE |
49600d0963fSdilpreet 	    DDI_FM_ACCCHK_CAPABLE | DDI_FM_DMACHK_CAPABLE;
4977aec1d6eScindi 	fmcap = ddi_system_fmcap;
4987aec1d6eScindi 	ddi_fm_init(dip, &fmcap, &rootnex_state->r_err_ibc);
4997aec1d6eScindi 
50012f080e7Smrj 	/* initialize DMA related state */
50112f080e7Smrj 	e = rootnex_dma_init();
50212f080e7Smrj 	if (e != DDI_SUCCESS) {
50312f080e7Smrj 		kmem_free(rootnex_state, sizeof (rootnex_state_t));
50412f080e7Smrj 		return (DDI_FAILURE);
50512f080e7Smrj 	}
50612f080e7Smrj 
50712f080e7Smrj 	/* Add static root node properties */
50812f080e7Smrj 	rootnex_add_props(dip);
50912f080e7Smrj 
51012f080e7Smrj 	/* since we can't call ddi_report_dev() */
51112f080e7Smrj 	cmn_err(CE_CONT, "?root nexus = %s\n", ddi_get_name(dip));
51212f080e7Smrj 
51312f080e7Smrj 	/* Initialize rootnex event handle */
51412f080e7Smrj 	i_ddi_rootnex_init_events(dip);
51512f080e7Smrj 
5163a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
51720906b23SVikram Hegde 	e = iommulib_nexus_register(dip, &iommulib_nexops,
51820906b23SVikram Hegde 	    &rootnex_state->r_iommulib_handle);
51920906b23SVikram Hegde 
52020906b23SVikram Hegde 	ASSERT(e == DDI_SUCCESS);
52120906b23SVikram Hegde #endif
52220906b23SVikram Hegde 
52312f080e7Smrj 	return (DDI_SUCCESS);
52412f080e7Smrj }
52512f080e7Smrj 
52612f080e7Smrj 
52712f080e7Smrj /*
52812f080e7Smrj  * rootnex_detach()
52912f080e7Smrj  *
53012f080e7Smrj  */
5317c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5327c478bd9Sstevel@tonic-gate static int
rootnex_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)53312f080e7Smrj rootnex_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
5347c478bd9Sstevel@tonic-gate {
53512f080e7Smrj 	switch (cmd) {
53612f080e7Smrj 	case DDI_SUSPEND:
5373a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
5383a634bfcSVikram Hegde 		return (immu_quiesce());
5393a634bfcSVikram Hegde #else
5403a634bfcSVikram Hegde 		return (DDI_SUCCESS);
5413a634bfcSVikram Hegde #endif
54212f080e7Smrj 	default:
54312f080e7Smrj 		return (DDI_FAILURE);
54412f080e7Smrj 	}
5453a634bfcSVikram Hegde 	/*NOTREACHED*/
5467c478bd9Sstevel@tonic-gate 
54712f080e7Smrj }
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 
55012f080e7Smrj /*
55112f080e7Smrj  * rootnex_dma_init()
55212f080e7Smrj  *
55312f080e7Smrj  */
55412f080e7Smrj /*ARGSUSED*/
55512f080e7Smrj static int
rootnex_dma_init()55612f080e7Smrj rootnex_dma_init()
55712f080e7Smrj {
55812f080e7Smrj 	size_t bufsize;
55912f080e7Smrj 
56012f080e7Smrj 
56112f080e7Smrj 	/*
56212f080e7Smrj 	 * size of our cookie/window/copybuf state needed in dma bind that we
56312f080e7Smrj 	 * pre-alloc in dma_alloc_handle
56412f080e7Smrj 	 */
56512f080e7Smrj 	rootnex_state->r_prealloc_cookies = rootnex_prealloc_cookies;
56612f080e7Smrj 	rootnex_state->r_prealloc_size =
56712f080e7Smrj 	    (rootnex_state->r_prealloc_cookies * sizeof (ddi_dma_cookie_t)) +
56812f080e7Smrj 	    (rootnex_prealloc_windows * sizeof (rootnex_window_t)) +
56912f080e7Smrj 	    (rootnex_prealloc_copybuf * sizeof (rootnex_pgmap_t));
57012f080e7Smrj 
57112f080e7Smrj 	/*
57212f080e7Smrj 	 * setup DDI DMA handle kmem cache, align each handle on 64 bytes,
57312f080e7Smrj 	 * allocate 16 extra bytes for struct pointer alignment
57412f080e7Smrj 	 * (p->dmai_private & dma->dp_prealloc_buffer)
57512f080e7Smrj 	 */
57612f080e7Smrj 	bufsize = sizeof (ddi_dma_impl_t) + sizeof (rootnex_dma_t) +
57712f080e7Smrj 	    rootnex_state->r_prealloc_size + 0x10;
57812f080e7Smrj 	rootnex_state->r_dmahdl_cache = kmem_cache_create("rootnex_dmahdl",
57912f080e7Smrj 	    bufsize, 64, NULL, NULL, NULL, NULL, NULL, 0);
58012f080e7Smrj 	if (rootnex_state->r_dmahdl_cache == NULL) {
58112f080e7Smrj 		return (DDI_FAILURE);
58212f080e7Smrj 	}
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	/*
5857c478bd9Sstevel@tonic-gate 	 * allocate array to track which major numbers we have printed warnings
5867c478bd9Sstevel@tonic-gate 	 * for.
5877c478bd9Sstevel@tonic-gate 	 */
5887c478bd9Sstevel@tonic-gate 	rootnex_warn_list = kmem_zalloc(devcnt * sizeof (*rootnex_warn_list),
5897c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
5927c478bd9Sstevel@tonic-gate }
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate /*
59612f080e7Smrj  * rootnex_add_props()
59712f080e7Smrj  *
5987c478bd9Sstevel@tonic-gate  */
5997c478bd9Sstevel@tonic-gate static void
rootnex_add_props(dev_info_t * dip)60012f080e7Smrj rootnex_add_props(dev_info_t *dip)
6017c478bd9Sstevel@tonic-gate {
60212f080e7Smrj 	rootnex_intprop_t *rpp;
6037c478bd9Sstevel@tonic-gate 	int i;
6047c478bd9Sstevel@tonic-gate 
60512f080e7Smrj 	/* Add static integer/boolean properties to the root node */
60612f080e7Smrj 	rpp = rootnex_intprp;
60712f080e7Smrj 	for (i = 0; i < NROOT_INTPROPS; i++) {
60812f080e7Smrj 		(void) e_ddi_prop_update_int(DDI_DEV_T_NONE, dip,
60912f080e7Smrj 		    rpp[i].prop_name, rpp[i].prop_value);
61012f080e7Smrj 	}
6117c478bd9Sstevel@tonic-gate }
6127c478bd9Sstevel@tonic-gate 
61312f080e7Smrj 
61412f080e7Smrj 
6157c478bd9Sstevel@tonic-gate /*
61612f080e7Smrj  * *************************
61712f080e7Smrj  *  ctlops related routines
61812f080e7Smrj  * *************************
61912f080e7Smrj  */
62012f080e7Smrj 
62112f080e7Smrj /*
62212f080e7Smrj  * rootnex_ctlops()
6237c478bd9Sstevel@tonic-gate  *
6247c478bd9Sstevel@tonic-gate  */
625a195726fSgovinda /*ARGSUSED*/
6267c478bd9Sstevel@tonic-gate static int
rootnex_ctlops(dev_info_t * dip,dev_info_t * rdip,ddi_ctl_enum_t ctlop,void * arg,void * result)62712f080e7Smrj rootnex_ctlops(dev_info_t *dip, dev_info_t *rdip, ddi_ctl_enum_t ctlop,
62812f080e7Smrj     void *arg, void *result)
6297c478bd9Sstevel@tonic-gate {
63012f080e7Smrj 	int n, *ptr;
63112f080e7Smrj 	struct ddi_parent_private_data *pdp;
6327c478bd9Sstevel@tonic-gate 
63312f080e7Smrj 	switch (ctlop) {
63412f080e7Smrj 	case DDI_CTLOPS_DMAPMAPC:
6357c478bd9Sstevel@tonic-gate 		/*
63612f080e7Smrj 		 * Return 'partial' to indicate that dma mapping
63712f080e7Smrj 		 * has to be done in the main MMU.
6387c478bd9Sstevel@tonic-gate 		 */
63912f080e7Smrj 		return (DDI_DMA_PARTIAL);
6407c478bd9Sstevel@tonic-gate 
64112f080e7Smrj 	case DDI_CTLOPS_BTOP:
6427c478bd9Sstevel@tonic-gate 		/*
64312f080e7Smrj 		 * Convert byte count input to physical page units.
64412f080e7Smrj 		 * (byte counts that are not a page-size multiple
64512f080e7Smrj 		 * are rounded down)
6467c478bd9Sstevel@tonic-gate 		 */
64712f080e7Smrj 		*(ulong_t *)result = btop(*(ulong_t *)arg);
6487c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
6497c478bd9Sstevel@tonic-gate 
65012f080e7Smrj 	case DDI_CTLOPS_PTOB:
6517c478bd9Sstevel@tonic-gate 		/*
65212f080e7Smrj 		 * Convert size in physical pages to bytes
6537c478bd9Sstevel@tonic-gate 		 */
65412f080e7Smrj 		*(ulong_t *)result = ptob(*(ulong_t *)arg);
6557c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
6567c478bd9Sstevel@tonic-gate 
65712f080e7Smrj 	case DDI_CTLOPS_BTOPR:
6587c478bd9Sstevel@tonic-gate 		/*
65912f080e7Smrj 		 * Convert byte count input to physical page units
66012f080e7Smrj 		 * (byte counts that are not a page-size multiple
66112f080e7Smrj 		 * are rounded up)
6627c478bd9Sstevel@tonic-gate 		 */
66312f080e7Smrj 		*(ulong_t *)result = btopr(*(ulong_t *)arg);
66412f080e7Smrj 		return (DDI_SUCCESS);
66512f080e7Smrj 
66612f080e7Smrj 	case DDI_CTLOPS_INITCHILD:
66712f080e7Smrj 		return (impl_ddi_sunbus_initchild(arg));
66812f080e7Smrj 
66912f080e7Smrj 	case DDI_CTLOPS_UNINITCHILD:
67012f080e7Smrj 		impl_ddi_sunbus_removechild(arg);
67112f080e7Smrj 		return (DDI_SUCCESS);
67212f080e7Smrj 
67312f080e7Smrj 	case DDI_CTLOPS_REPORTDEV:
67412f080e7Smrj 		return (rootnex_ctl_reportdev(rdip));
67512f080e7Smrj 
67612f080e7Smrj 	case DDI_CTLOPS_IOMIN:
6777c478bd9Sstevel@tonic-gate 		/*
67812f080e7Smrj 		 * Nothing to do here but reflect back..
6797c478bd9Sstevel@tonic-gate 		 */
6807c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
6817c478bd9Sstevel@tonic-gate 
68212f080e7Smrj 	case DDI_CTLOPS_REGSIZE:
68312f080e7Smrj 	case DDI_CTLOPS_NREGS:
68412f080e7Smrj 		break;
6857c478bd9Sstevel@tonic-gate 
68612f080e7Smrj 	case DDI_CTLOPS_SIDDEV:
68712f080e7Smrj 		if (ndi_dev_is_prom_node(rdip))
6887c478bd9Sstevel@tonic-gate 			return (DDI_SUCCESS);
68912f080e7Smrj 		if (ndi_dev_is_persistent_node(rdip))
69012f080e7Smrj 			return (DDI_SUCCESS);
6917c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6927c478bd9Sstevel@tonic-gate 
69312f080e7Smrj 	case DDI_CTLOPS_POWER:
69412f080e7Smrj 		return ((*pm_platform_power)((power_req_t *)arg));
69512f080e7Smrj 
696a195726fSgovinda 	case DDI_CTLOPS_RESERVED0: /* Was DDI_CTLOPS_NINTRS, obsolete */
69712f080e7Smrj 	case DDI_CTLOPS_RESERVED1: /* Was DDI_CTLOPS_POKE_INIT, obsolete */
69812f080e7Smrj 	case DDI_CTLOPS_RESERVED2: /* Was DDI_CTLOPS_POKE_FLUSH, obsolete */
69912f080e7Smrj 	case DDI_CTLOPS_RESERVED3: /* Was DDI_CTLOPS_POKE_FINI, obsolete */
700a195726fSgovinda 	case DDI_CTLOPS_RESERVED4: /* Was DDI_CTLOPS_INTR_HILEVEL, obsolete */
701a195726fSgovinda 	case DDI_CTLOPS_RESERVED5: /* Was DDI_CTLOPS_XLATE_INTRS, obsolete */
70212f080e7Smrj 		if (!rootnex_state->r_reserved_msg_printed) {
70312f080e7Smrj 			rootnex_state->r_reserved_msg_printed = B_TRUE;
70412f080e7Smrj 			cmn_err(CE_WARN, "Failing ddi_ctlops call(s) for "
70512f080e7Smrj 			    "1 or more reserved/obsolete operations.");
7067c478bd9Sstevel@tonic-gate 		}
70712f080e7Smrj 		return (DDI_FAILURE);
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	default:
7107c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
7117c478bd9Sstevel@tonic-gate 	}
71212f080e7Smrj 	/*
71312f080e7Smrj 	 * The rest are for "hardware" properties
71412f080e7Smrj 	 */
71512f080e7Smrj 	if ((pdp = ddi_get_parent_data(rdip)) == NULL)
71612f080e7Smrj 		return (DDI_FAILURE);
7177c478bd9Sstevel@tonic-gate 
71812f080e7Smrj 	if (ctlop == DDI_CTLOPS_NREGS) {
71912f080e7Smrj 		ptr = (int *)result;
72012f080e7Smrj 		*ptr = pdp->par_nreg;
72112f080e7Smrj 	} else {
72212f080e7Smrj 		off_t *size = (off_t *)result;
7237c478bd9Sstevel@tonic-gate 
72412f080e7Smrj 		ptr = (int *)arg;
72512f080e7Smrj 		n = *ptr;
72612f080e7Smrj 		if (n >= pdp->par_nreg) {
72712f080e7Smrj 			return (DDI_FAILURE);
72812f080e7Smrj 		}
72912f080e7Smrj 		*size = (off_t)pdp->par_reg[n].regspec_size;
73012f080e7Smrj 	}
7317c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
7327c478bd9Sstevel@tonic-gate }
7337c478bd9Sstevel@tonic-gate 
73412f080e7Smrj 
73512f080e7Smrj /*
73612f080e7Smrj  * rootnex_ctl_reportdev()
73712f080e7Smrj  *
73812f080e7Smrj  */
7397c478bd9Sstevel@tonic-gate static int
rootnex_ctl_reportdev(dev_info_t * dev)74012f080e7Smrj rootnex_ctl_reportdev(dev_info_t *dev)
74112f080e7Smrj {
74212f080e7Smrj 	int i, n, len, f_len = 0;
74312f080e7Smrj 	char *buf;
74412f080e7Smrj 
74512f080e7Smrj 	buf = kmem_alloc(REPORTDEV_BUFSIZE, KM_SLEEP);
74612f080e7Smrj 	f_len += snprintf(buf, REPORTDEV_BUFSIZE,
74712f080e7Smrj 	    "%s%d at root", ddi_driver_name(dev), ddi_get_instance(dev));
74812f080e7Smrj 	len = strlen(buf);
74912f080e7Smrj 
75012f080e7Smrj 	for (i = 0; i < sparc_pd_getnreg(dev); i++) {
75112f080e7Smrj 
75212f080e7Smrj 		struct regspec *rp = sparc_pd_getreg(dev, i);
75312f080e7Smrj 
75412f080e7Smrj 		if (i == 0)
75512f080e7Smrj 			f_len += snprintf(buf + len, REPORTDEV_BUFSIZE - len,
75612f080e7Smrj 			    ": ");
75712f080e7Smrj 		else
75812f080e7Smrj 			f_len += snprintf(buf + len, REPORTDEV_BUFSIZE - len,
75912f080e7Smrj 			    " and ");
76012f080e7Smrj 		len = strlen(buf);
76112f080e7Smrj 
76212f080e7Smrj 		switch (rp->regspec_bustype) {
76312f080e7Smrj 
76412f080e7Smrj 		case BTEISA:
76512f080e7Smrj 			f_len += snprintf(buf + len, REPORTDEV_BUFSIZE - len,
76612f080e7Smrj 			    "%s 0x%x", DEVI_EISA_NEXNAME, rp->regspec_addr);
76712f080e7Smrj 			break;
76812f080e7Smrj 
76912f080e7Smrj 		case BTISA:
77012f080e7Smrj 			f_len += snprintf(buf + len, REPORTDEV_BUFSIZE - len,
77112f080e7Smrj 			    "%s 0x%x", DEVI_ISA_NEXNAME, rp->regspec_addr);
77212f080e7Smrj 			break;
77312f080e7Smrj 
77412f080e7Smrj 		default:
77512f080e7Smrj 			f_len += snprintf(buf + len, REPORTDEV_BUFSIZE - len,
77612f080e7Smrj 			    "space %x offset %x",
77712f080e7Smrj 			    rp->regspec_bustype, rp->regspec_addr);
77812f080e7Smrj 			break;
77912f080e7Smrj 		}
78012f080e7Smrj 		len = strlen(buf);
78112f080e7Smrj 	}
78212f080e7Smrj 	for (i = 0, n = sparc_pd_getnintr(dev); i < n; i++) {
78312f080e7Smrj 		int pri;
78412f080e7Smrj 
78512f080e7Smrj 		if (i != 0) {
78612f080e7Smrj 			f_len += snprintf(buf + len, REPORTDEV_BUFSIZE - len,
78712f080e7Smrj 			    ",");
78812f080e7Smrj 			len = strlen(buf);
78912f080e7Smrj 		}
79012f080e7Smrj 		pri = INT_IPL(sparc_pd_getintr(dev, i)->intrspec_pri);
79112f080e7Smrj 		f_len += snprintf(buf + len, REPORTDEV_BUFSIZE - len,
79212f080e7Smrj 		    " sparc ipl %d", pri);
79312f080e7Smrj 		len = strlen(buf);
79412f080e7Smrj 	}
79512f080e7Smrj #ifdef DEBUG
79612f080e7Smrj 	if (f_len + 1 >= REPORTDEV_BUFSIZE) {
79712f080e7Smrj 		cmn_err(CE_NOTE, "next message is truncated: "
79812f080e7Smrj 		    "printed length 1024, real length %d", f_len);
79912f080e7Smrj 	}
80012f080e7Smrj #endif /* DEBUG */
80112f080e7Smrj 	cmn_err(CE_CONT, "?%s\n", buf);
80212f080e7Smrj 	kmem_free(buf, REPORTDEV_BUFSIZE);
80312f080e7Smrj 	return (DDI_SUCCESS);
80412f080e7Smrj }
80512f080e7Smrj 
80612f080e7Smrj 
80712f080e7Smrj /*
80812f080e7Smrj  * ******************
80912f080e7Smrj  *  map related code
81012f080e7Smrj  * ******************
81112f080e7Smrj  */
81212f080e7Smrj 
81312f080e7Smrj /*
81412f080e7Smrj  * rootnex_map()
81512f080e7Smrj  *
81612f080e7Smrj  */
81712f080e7Smrj static int
rootnex_map(dev_info_t * dip,dev_info_t * rdip,ddi_map_req_t * mp,off_t offset,off_t len,caddr_t * vaddrp)81812f080e7Smrj rootnex_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp, off_t offset,
81912f080e7Smrj     off_t len, caddr_t *vaddrp)
8207c478bd9Sstevel@tonic-gate {
8213304b547SRobert Mustacchi 	struct regspec *orp = NULL;
8223304b547SRobert Mustacchi 	struct regspec64 rp = { 0 };
8237c478bd9Sstevel@tonic-gate 	ddi_map_req_t mr = *mp;		/* Get private copy of request */
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	mp = &mr;
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	switch (mp->map_op)  {
8287c478bd9Sstevel@tonic-gate 	case DDI_MO_MAP_LOCKED:
8297c478bd9Sstevel@tonic-gate 	case DDI_MO_UNMAP:
8307c478bd9Sstevel@tonic-gate 	case DDI_MO_MAP_HANDLE:
8317c478bd9Sstevel@tonic-gate 		break;
8327c478bd9Sstevel@tonic-gate 	default:
8337c478bd9Sstevel@tonic-gate #ifdef	DDI_MAP_DEBUG
8347c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "rootnex_map: unimplemented map op %d.",
8357c478bd9Sstevel@tonic-gate 		    mp->map_op);
8367c478bd9Sstevel@tonic-gate #endif	/* DDI_MAP_DEBUG */
8377c478bd9Sstevel@tonic-gate 		return (DDI_ME_UNIMPLEMENTED);
8387c478bd9Sstevel@tonic-gate 	}
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 	if (mp->map_flags & DDI_MF_USER_MAPPING)  {
8417c478bd9Sstevel@tonic-gate #ifdef	DDI_MAP_DEBUG
8427c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "rootnex_map: unimplemented map type: user.");
8437c478bd9Sstevel@tonic-gate #endif	/* DDI_MAP_DEBUG */
8447c478bd9Sstevel@tonic-gate 		return (DDI_ME_UNIMPLEMENTED);
8457c478bd9Sstevel@tonic-gate 	}
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	/*
8483304b547SRobert Mustacchi 	 * First, we need to get the original regspec out before we convert it
8493304b547SRobert Mustacchi 	 * to the extended format. If we have a register number, then we need to
8503304b547SRobert Mustacchi 	 * convert that to a regspec.
8517c478bd9Sstevel@tonic-gate 	 */
8527c478bd9Sstevel@tonic-gate 	if (mp->map_type == DDI_MT_RNUMBER)  {
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 		int rnumber = mp->map_obj.rnumber;
8557c478bd9Sstevel@tonic-gate #ifdef	DDI_MAP_DEBUG
8567c478bd9Sstevel@tonic-gate 		static char *out_of_range =
8577c478bd9Sstevel@tonic-gate 		    "rootnex_map: Out of range rnumber <%d>, device <%s>";
8587c478bd9Sstevel@tonic-gate #endif	/* DDI_MAP_DEBUG */
8597c478bd9Sstevel@tonic-gate 
8603304b547SRobert Mustacchi 		orp = i_ddi_rnumber_to_regspec(rdip, rnumber);
8613304b547SRobert Mustacchi 		if (orp == NULL) {
8627c478bd9Sstevel@tonic-gate #ifdef	DDI_MAP_DEBUG
8637c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, out_of_range, rnumber,
8647c478bd9Sstevel@tonic-gate 			    ddi_get_name(rdip));
8657c478bd9Sstevel@tonic-gate #endif	/* DDI_MAP_DEBUG */
8667c478bd9Sstevel@tonic-gate 			return (DDI_ME_RNUMBER_RANGE);
8677c478bd9Sstevel@tonic-gate 		}
8683304b547SRobert Mustacchi 	} else if (!(mp->map_flags & DDI_MF_EXT_REGSPEC)) {
8693304b547SRobert Mustacchi 		orp = mp->map_obj.rp;
8703304b547SRobert Mustacchi 	}
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	/*
8733304b547SRobert Mustacchi 	 * Ensure that we are always using a 64-bit extended regspec regardless
8743304b547SRobert Mustacchi 	 * of what was passed into us. If the child driver is using a 64-bit
8753304b547SRobert Mustacchi 	 * regspec, then we need to make sure that we copy this to the local
8763304b547SRobert Mustacchi 	 * regspec64, rp.
8777c478bd9Sstevel@tonic-gate 	 */
8783304b547SRobert Mustacchi 	if (orp != NULL) {
8793304b547SRobert Mustacchi 		rp.regspec_bustype = orp->regspec_bustype;
8803304b547SRobert Mustacchi 		rp.regspec_addr = orp->regspec_addr;
8813304b547SRobert Mustacchi 		rp.regspec_size = orp->regspec_size;
8823304b547SRobert Mustacchi 	} else {
8833304b547SRobert Mustacchi 		struct regspec64 *rp64;
8843304b547SRobert Mustacchi 		rp64 = (struct regspec64 *)mp->map_obj.rp;
8853304b547SRobert Mustacchi 		rp = *rp64;
8863304b547SRobert Mustacchi 	}
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	mp->map_type = DDI_MT_REGSPEC;
8893304b547SRobert Mustacchi 	mp->map_flags |= DDI_MF_EXT_REGSPEC;
8903304b547SRobert Mustacchi 	mp->map_obj.rp = (struct regspec *)&rp;
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 	/*
8937c478bd9Sstevel@tonic-gate 	 * Adjust offset and length correspnding to called values...
8947c478bd9Sstevel@tonic-gate 	 * XXX: A non-zero length means override the one in the regspec
8957c478bd9Sstevel@tonic-gate 	 * XXX: (regardless of what's in the parent's range?)
8967c478bd9Sstevel@tonic-gate 	 */
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate #ifdef	DDI_MAP_DEBUG
899843e1988Sjohnlev 	cmn_err(CE_CONT, "rootnex: <%s,%s> <0x%x, 0x%x, 0x%d> offset %d len %d "
900843e1988Sjohnlev 	    "handle 0x%x\n", ddi_get_name(dip), ddi_get_name(rdip),
9013304b547SRobert Mustacchi 	    rp.regspec_bustype, rp.regspec_addr, rp.regspec_size, offset,
902843e1988Sjohnlev 	    len, mp->map_handlep);
9037c478bd9Sstevel@tonic-gate #endif	/* DDI_MAP_DEBUG */
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 	/*
9067c478bd9Sstevel@tonic-gate 	 * I/O or memory mapping:
9077c478bd9Sstevel@tonic-gate 	 *
9087c478bd9Sstevel@tonic-gate 	 *	<bustype=0, addr=x, len=x>: memory
9097c478bd9Sstevel@tonic-gate 	 *	<bustype=1, addr=x, len=x>: i/o
9107c478bd9Sstevel@tonic-gate 	 *	<bustype>1, addr=0, len=x>: x86-compatibility i/o
9117c478bd9Sstevel@tonic-gate 	 */
9127c478bd9Sstevel@tonic-gate 
9133304b547SRobert Mustacchi 	if (rp.regspec_bustype > 1 && rp.regspec_addr != 0) {
9147c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "<%s,%s> invalid register spec"
9153304b547SRobert Mustacchi 		    " <0x%" PRIx64 ", 0x%" PRIx64 ", 0x%" PRIx64 ">",
9163304b547SRobert Mustacchi 		    ddi_get_name(dip), ddi_get_name(rdip), rp.regspec_bustype,
9173304b547SRobert Mustacchi 		    rp.regspec_addr, rp.regspec_size);
9187c478bd9Sstevel@tonic-gate 		return (DDI_ME_INVAL);
9197c478bd9Sstevel@tonic-gate 	}
9207c478bd9Sstevel@tonic-gate 
9213304b547SRobert Mustacchi 	if (rp.regspec_bustype > 1 && rp.regspec_addr == 0) {
9227c478bd9Sstevel@tonic-gate 		/*
9237c478bd9Sstevel@tonic-gate 		 * compatibility i/o mapping
9247c478bd9Sstevel@tonic-gate 		 */
9253304b547SRobert Mustacchi 		rp.regspec_bustype += offset;
9267c478bd9Sstevel@tonic-gate 	} else {
9277c478bd9Sstevel@tonic-gate 		/*
9287c478bd9Sstevel@tonic-gate 		 * Normal memory or i/o mapping
9297c478bd9Sstevel@tonic-gate 		 */
9303304b547SRobert Mustacchi 		rp.regspec_addr += offset;
9317c478bd9Sstevel@tonic-gate 	}
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 	if (len != 0)
9343304b547SRobert Mustacchi 		rp.regspec_size = len;
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate #ifdef	DDI_MAP_DEBUG
9373304b547SRobert Mustacchi 	cmn_err(CE_CONT, "             <%s,%s> <0x%" PRIx64 ", 0x%" PRIx64
9383304b547SRobert Mustacchi 	    ", 0x%" PRId64 "> offset %d len %d handle 0x%x\n",
9393304b547SRobert Mustacchi 	    ddi_get_name(dip), ddi_get_name(rdip), rp.regspec_bustype,
9403304b547SRobert Mustacchi 	    rp.regspec_addr, rp.regspec_size, offset, len, mp->map_handlep);
9417c478bd9Sstevel@tonic-gate #endif	/* DDI_MAP_DEBUG */
9427c478bd9Sstevel@tonic-gate 
9433304b547SRobert Mustacchi 
9447c478bd9Sstevel@tonic-gate 	/*
9453304b547SRobert Mustacchi 	 * The x86 root nexus does not have any notion of valid ranges of
9463304b547SRobert Mustacchi 	 * addresses. Its children have valid ranges, but because there are none
9473304b547SRobert Mustacchi 	 * for the nexus, we don't need to call i_ddi_apply_range().  Verify
9483304b547SRobert Mustacchi 	 * that is the case.
9497c478bd9Sstevel@tonic-gate 	 */
9503304b547SRobert Mustacchi 	ASSERT0(sparc_pd_getnrng(dip));
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 	switch (mp->map_op)  {
9537c478bd9Sstevel@tonic-gate 	case DDI_MO_MAP_LOCKED:
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 		/*
9567c478bd9Sstevel@tonic-gate 		 * Set up the locked down kernel mapping to the regspec...
9577c478bd9Sstevel@tonic-gate 		 */
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 		return (rootnex_map_regspec(mp, vaddrp));
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 	case DDI_MO_UNMAP:
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 		/*
9647c478bd9Sstevel@tonic-gate 		 * Release mapping...
9657c478bd9Sstevel@tonic-gate 		 */
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 		return (rootnex_unmap_regspec(mp, vaddrp));
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	case DDI_MO_MAP_HANDLE:
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 		return (rootnex_map_handle(mp));
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 	default:
9747c478bd9Sstevel@tonic-gate 		return (DDI_ME_UNIMPLEMENTED);
9757c478bd9Sstevel@tonic-gate 	}
9767c478bd9Sstevel@tonic-gate }
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate /*
98012f080e7Smrj  * rootnex_map_fault()
9817c478bd9Sstevel@tonic-gate  *
9827c478bd9Sstevel@tonic-gate  *	fault in mappings for requestors
9837c478bd9Sstevel@tonic-gate  */
9847c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9857c478bd9Sstevel@tonic-gate static int
rootnex_map_fault(dev_info_t * dip,dev_info_t * rdip,struct hat * hat,struct seg * seg,caddr_t addr,struct devpage * dp,pfn_t pfn,uint_t prot,uint_t lock)98612f080e7Smrj rootnex_map_fault(dev_info_t *dip, dev_info_t *rdip, struct hat *hat,
98712f080e7Smrj     struct seg *seg, caddr_t addr, struct devpage *dp, pfn_t pfn, uint_t prot,
98812f080e7Smrj     uint_t lock)
9897c478bd9Sstevel@tonic-gate {
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate #ifdef	DDI_MAP_DEBUG
9927c478bd9Sstevel@tonic-gate 	ddi_map_debug("rootnex_map_fault: address <%x> pfn <%x>", addr, pfn);
9937c478bd9Sstevel@tonic-gate 	ddi_map_debug(" Seg <%s>\n",
9947c478bd9Sstevel@tonic-gate 	    seg->s_ops == &segdev_ops ? "segdev" :
9957c478bd9Sstevel@tonic-gate 	    seg == &kvseg ? "segkmem" : "NONE!");
9967c478bd9Sstevel@tonic-gate #endif	/* DDI_MAP_DEBUG */
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 	/*
9997c478bd9Sstevel@tonic-gate 	 * This is all terribly broken, but it is a start
10007c478bd9Sstevel@tonic-gate 	 *
10017c478bd9Sstevel@tonic-gate 	 * XXX	Note that this test means that segdev_ops
10027c478bd9Sstevel@tonic-gate 	 *	must be exported from seg_dev.c.
10037c478bd9Sstevel@tonic-gate 	 * XXX	What about devices with their own segment drivers?
10047c478bd9Sstevel@tonic-gate 	 */
10057c478bd9Sstevel@tonic-gate 	if (seg->s_ops == &segdev_ops) {
1006843e1988Sjohnlev 		struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 		if (hat == NULL) {
10097c478bd9Sstevel@tonic-gate 			/*
10107c478bd9Sstevel@tonic-gate 			 * This is one plausible interpretation of
10117c478bd9Sstevel@tonic-gate 			 * a null hat i.e. use the first hat on the
10127c478bd9Sstevel@tonic-gate 			 * address space hat list which by convention is
10137c478bd9Sstevel@tonic-gate 			 * the hat of the system MMU.  At alternative
10147c478bd9Sstevel@tonic-gate 			 * would be to panic .. this might well be better ..
10157c478bd9Sstevel@tonic-gate 			 */
1016*fd435bccSJosef 'Jeff' Sipek 			ASSERT(AS_READ_HELD(seg->s_as));
10177c478bd9Sstevel@tonic-gate 			hat = seg->s_as->a_hat;
10187c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "rootnex_map_fault: nil hat");
10197c478bd9Sstevel@tonic-gate 		}
10207c478bd9Sstevel@tonic-gate 		hat_devload(hat, addr, MMU_PAGESIZE, pfn, prot | sdp->hat_attr,
10217c478bd9Sstevel@tonic-gate 		    (lock ? HAT_LOAD_LOCK : HAT_LOAD));
10227c478bd9Sstevel@tonic-gate 	} else if (seg == &kvseg && dp == NULL) {
10237c478bd9Sstevel@tonic-gate 		hat_devload(kas.a_hat, addr, MMU_PAGESIZE, pfn, prot,
10247c478bd9Sstevel@tonic-gate 		    HAT_LOAD_LOCK);
10257c478bd9Sstevel@tonic-gate 	} else
10267c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
10277c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
10287c478bd9Sstevel@tonic-gate }
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate static int
rootnex_map_regspec(ddi_map_req_t * mp,caddr_t * vaddrp)103212f080e7Smrj rootnex_map_regspec(ddi_map_req_t *mp, caddr_t *vaddrp)
10337c478bd9Sstevel@tonic-gate {
1034843e1988Sjohnlev 	rootnex_addr_t rbase;
103512f080e7Smrj 	void *cvaddr;
10363304b547SRobert Mustacchi 	uint64_t npages, pgoffset;
10373304b547SRobert Mustacchi 	struct regspec64 *rp;
103812f080e7Smrj 	ddi_acc_hdl_t *hp;
103912f080e7Smrj 	ddi_acc_impl_t *ap;
104012f080e7Smrj 	uint_t	hat_acc_flags;
1041843e1988Sjohnlev 	paddr_t pbase;
10427c478bd9Sstevel@tonic-gate 
10433304b547SRobert Mustacchi 	ASSERT(mp->map_flags & DDI_MF_EXT_REGSPEC);
10443304b547SRobert Mustacchi 	rp = (struct regspec64 *)mp->map_obj.rp;
104512f080e7Smrj 	hp = mp->map_handlep;
104612f080e7Smrj 
104712f080e7Smrj #ifdef	DDI_MAP_DEBUG
104812f080e7Smrj 	ddi_map_debug(
104912f080e7Smrj 	    "rootnex_map_regspec: <0x%x 0x%x 0x%x> handle 0x%x\n",
105012f080e7Smrj 	    rp->regspec_bustype, rp->regspec_addr,
105112f080e7Smrj 	    rp->regspec_size, mp->map_handlep);
105212f080e7Smrj #endif	/* DDI_MAP_DEBUG */
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	/*
105512f080e7Smrj 	 * I/O or memory mapping
105612f080e7Smrj 	 *
105712f080e7Smrj 	 *	<bustype=0, addr=x, len=x>: memory
105812f080e7Smrj 	 *	<bustype=1, addr=x, len=x>: i/o
105912f080e7Smrj 	 *	<bustype>1, addr=0, len=x>: x86-compatibility i/o
10607c478bd9Sstevel@tonic-gate 	 */
106112f080e7Smrj 
106212f080e7Smrj 	if (rp->regspec_bustype > 1 && rp->regspec_addr != 0) {
106312f080e7Smrj 		cmn_err(CE_WARN, "rootnex: invalid register spec"
10643304b547SRobert Mustacchi 		    " <0x%" PRIx64 ", 0x%" PRIx64", 0x%" PRIx64">",
10653304b547SRobert Mustacchi 		    rp->regspec_bustype, rp->regspec_addr, rp->regspec_size);
106612f080e7Smrj 		return (DDI_FAILURE);
10677c478bd9Sstevel@tonic-gate 	}
106812f080e7Smrj 
106912f080e7Smrj 	if (rp->regspec_bustype != 0) {
10707c478bd9Sstevel@tonic-gate 		/*
107112f080e7Smrj 		 * I/O space - needs a handle.
10727c478bd9Sstevel@tonic-gate 		 */
10737c478bd9Sstevel@tonic-gate 		if (hp == NULL) {
107412f080e7Smrj 			return (DDI_FAILURE);
10757c478bd9Sstevel@tonic-gate 		}
107612f080e7Smrj 		ap = (ddi_acc_impl_t *)hp->ah_platform_private;
107712f080e7Smrj 		ap->ahi_acc_attr |= DDI_ACCATTR_IO_SPACE;
107812f080e7Smrj 		impl_acc_hdl_init(hp);
10797c478bd9Sstevel@tonic-gate 
108012f080e7Smrj 		if (mp->map_flags & DDI_MF_DEVICE_MAPPING) {
108112f080e7Smrj #ifdef  DDI_MAP_DEBUG
1082843e1988Sjohnlev 			ddi_map_debug("rootnex_map_regspec: mmap() "
1083843e1988Sjohnlev 			    "to I/O space is not supported.\n");
108412f080e7Smrj #endif  /* DDI_MAP_DEBUG */
108512f080e7Smrj 			return (DDI_ME_INVAL);
10867c478bd9Sstevel@tonic-gate 		} else {
10877c478bd9Sstevel@tonic-gate 			/*
108812f080e7Smrj 			 * 1275-compliant vs. compatibility i/o mapping
10897c478bd9Sstevel@tonic-gate 			 */
109012f080e7Smrj 			*vaddrp =
109112f080e7Smrj 			    (rp->regspec_bustype > 1 && rp->regspec_addr == 0) ?
109212f080e7Smrj 			    ((caddr_t)(uintptr_t)rp->regspec_bustype) :
109312f080e7Smrj 			    ((caddr_t)(uintptr_t)rp->regspec_addr);
1094843e1988Sjohnlev #ifdef __xpv
1095843e1988Sjohnlev 			if (DOMAIN_IS_INITDOMAIN(xen_info)) {
1096843e1988Sjohnlev 				hp->ah_pfn = xen_assign_pfn(
1097843e1988Sjohnlev 				    mmu_btop((ulong_t)rp->regspec_addr &
1098843e1988Sjohnlev 				    MMU_PAGEMASK));
1099843e1988Sjohnlev 			} else {
1100843e1988Sjohnlev 				hp->ah_pfn = mmu_btop(
1101843e1988Sjohnlev 				    (ulong_t)rp->regspec_addr & MMU_PAGEMASK);
1102843e1988Sjohnlev 			}
1103843e1988Sjohnlev #else
110400d0963fSdilpreet 			hp->ah_pfn = mmu_btop((ulong_t)rp->regspec_addr &
1105843e1988Sjohnlev 			    MMU_PAGEMASK);
1106843e1988Sjohnlev #endif
110700d0963fSdilpreet 			hp->ah_pnum = mmu_btopr(rp->regspec_size +
110800d0963fSdilpreet 			    (ulong_t)rp->regspec_addr & MMU_PAGEOFFSET);
11097c478bd9Sstevel@tonic-gate 		}
11107c478bd9Sstevel@tonic-gate 
111112f080e7Smrj #ifdef	DDI_MAP_DEBUG
111212f080e7Smrj 		ddi_map_debug(
111312f080e7Smrj 	    "rootnex_map_regspec: \"Mapping\" %d bytes I/O space at 0x%x\n",
111412f080e7Smrj 		    rp->regspec_size, *vaddrp);
111512f080e7Smrj #endif	/* DDI_MAP_DEBUG */
111612f080e7Smrj 		return (DDI_SUCCESS);
11177c478bd9Sstevel@tonic-gate 	}
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 	/*
112012f080e7Smrj 	 * Memory space
112112f080e7Smrj 	 */
112212f080e7Smrj 
112312f080e7Smrj 	if (hp != NULL) {
112412f080e7Smrj 		/*
112512f080e7Smrj 		 * hat layer ignores
112612f080e7Smrj 		 * hp->ah_acc.devacc_attr_endian_flags.
112712f080e7Smrj 		 */
112812f080e7Smrj 		switch (hp->ah_acc.devacc_attr_dataorder) {
112912f080e7Smrj 		case DDI_STRICTORDER_ACC:
113012f080e7Smrj 			hat_acc_flags = HAT_STRICTORDER;
113112f080e7Smrj 			break;
113212f080e7Smrj 		case DDI_UNORDERED_OK_ACC:
113312f080e7Smrj 			hat_acc_flags = HAT_UNORDERED_OK;
113412f080e7Smrj 			break;
113512f080e7Smrj 		case DDI_MERGING_OK_ACC:
113612f080e7Smrj 			hat_acc_flags = HAT_MERGING_OK;
113712f080e7Smrj 			break;
113812f080e7Smrj 		case DDI_LOADCACHING_OK_ACC:
113912f080e7Smrj 			hat_acc_flags = HAT_LOADCACHING_OK;
114012f080e7Smrj 			break;
114112f080e7Smrj 		case DDI_STORECACHING_OK_ACC:
114212f080e7Smrj 			hat_acc_flags = HAT_STORECACHING_OK;
114312f080e7Smrj 			break;
114412f080e7Smrj 		}
114512f080e7Smrj 		ap = (ddi_acc_impl_t *)hp->ah_platform_private;
114612f080e7Smrj 		ap->ahi_acc_attr |= DDI_ACCATTR_CPU_VADDR;
114712f080e7Smrj 		impl_acc_hdl_init(hp);
114812f080e7Smrj 		hp->ah_hat_flags = hat_acc_flags;
114912f080e7Smrj 	} else {
115012f080e7Smrj 		hat_acc_flags = HAT_STRICTORDER;
115112f080e7Smrj 	}
115212f080e7Smrj 
1153843e1988Sjohnlev 	rbase = (rootnex_addr_t)(rp->regspec_addr & MMU_PAGEMASK);
1154843e1988Sjohnlev #ifdef __xpv
1155843e1988Sjohnlev 	/*
1156843e1988Sjohnlev 	 * If we're dom0, we're using a real device so we need to translate
1157843e1988Sjohnlev 	 * the MA to a PA.
1158843e1988Sjohnlev 	 */
1159843e1988Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
1160843e1988Sjohnlev 		pbase = pfn_to_pa(xen_assign_pfn(mmu_btop(rbase)));
1161843e1988Sjohnlev 	} else {
1162843e1988Sjohnlev 		pbase = rbase;
1163843e1988Sjohnlev 	}
1164843e1988Sjohnlev #else
1165843e1988Sjohnlev 	pbase = rbase;
1166843e1988Sjohnlev #endif
1167843e1988Sjohnlev 	pgoffset = (ulong_t)rp->regspec_addr & MMU_PAGEOFFSET;
116812f080e7Smrj 
116912f080e7Smrj 	if (rp->regspec_size == 0) {
117012f080e7Smrj #ifdef  DDI_MAP_DEBUG
117112f080e7Smrj 		ddi_map_debug("rootnex_map_regspec: zero regspec_size\n");
117212f080e7Smrj #endif  /* DDI_MAP_DEBUG */
117312f080e7Smrj 		return (DDI_ME_INVAL);
117412f080e7Smrj 	}
117512f080e7Smrj 
117612f080e7Smrj 	if (mp->map_flags & DDI_MF_DEVICE_MAPPING) {
1177843e1988Sjohnlev 		/* extra cast to make gcc happy */
1178843e1988Sjohnlev 		*vaddrp = (caddr_t)((uintptr_t)mmu_btop(pbase));
117912f080e7Smrj 	} else {
118012f080e7Smrj 		npages = mmu_btopr(rp->regspec_size + pgoffset);
118112f080e7Smrj 
118212f080e7Smrj #ifdef	DDI_MAP_DEBUG
1183843e1988Sjohnlev 		ddi_map_debug("rootnex_map_regspec: Mapping %d pages "
1184843e1988Sjohnlev 		    "physical %llx", npages, pbase);
118512f080e7Smrj #endif	/* DDI_MAP_DEBUG */
118612f080e7Smrj 
118712f080e7Smrj 		cvaddr = device_arena_alloc(ptob(npages), VM_NOSLEEP);
118812f080e7Smrj 		if (cvaddr == NULL)
118912f080e7Smrj 			return (DDI_ME_NORESOURCES);
119012f080e7Smrj 
119112f080e7Smrj 		/*
119212f080e7Smrj 		 * Now map in the pages we've allocated...
119312f080e7Smrj 		 */
1194843e1988Sjohnlev 		hat_devload(kas.a_hat, cvaddr, mmu_ptob(npages),
1195843e1988Sjohnlev 		    mmu_btop(pbase), mp->map_prot | hat_acc_flags,
1196843e1988Sjohnlev 		    HAT_LOAD_LOCK);
119712f080e7Smrj 		*vaddrp = (caddr_t)cvaddr + pgoffset;
119800d0963fSdilpreet 
119900d0963fSdilpreet 		/* save away pfn and npages for FMA */
120000d0963fSdilpreet 		hp = mp->map_handlep;
120100d0963fSdilpreet 		if (hp) {
1202843e1988Sjohnlev 			hp->ah_pfn = mmu_btop(pbase);
120300d0963fSdilpreet 			hp->ah_pnum = npages;
120400d0963fSdilpreet 		}
120512f080e7Smrj 	}
120612f080e7Smrj 
120712f080e7Smrj #ifdef	DDI_MAP_DEBUG
120812f080e7Smrj 	ddi_map_debug("at virtual 0x%x\n", *vaddrp);
120912f080e7Smrj #endif	/* DDI_MAP_DEBUG */
121012f080e7Smrj 	return (DDI_SUCCESS);
121112f080e7Smrj }
121212f080e7Smrj 
121312f080e7Smrj 
12147c478bd9Sstevel@tonic-gate static int
rootnex_unmap_regspec(ddi_map_req_t * mp,caddr_t * vaddrp)121512f080e7Smrj rootnex_unmap_regspec(ddi_map_req_t *mp, caddr_t *vaddrp)
12167c478bd9Sstevel@tonic-gate {
121712f080e7Smrj 	caddr_t addr = (caddr_t)*vaddrp;
12183304b547SRobert Mustacchi 	uint64_t npages, pgoffset;
12193304b547SRobert Mustacchi 	struct regspec64 *rp;
12207c478bd9Sstevel@tonic-gate 
122112f080e7Smrj 	if (mp->map_flags & DDI_MF_DEVICE_MAPPING)
122212f080e7Smrj 		return (0);
12237c478bd9Sstevel@tonic-gate 
12243304b547SRobert Mustacchi 	ASSERT(mp->map_flags & DDI_MF_EXT_REGSPEC);
12253304b547SRobert Mustacchi 	rp = (struct regspec64 *)mp->map_obj.rp;
12267c478bd9Sstevel@tonic-gate 
122712f080e7Smrj 	if (rp->regspec_size == 0) {
122812f080e7Smrj #ifdef  DDI_MAP_DEBUG
122912f080e7Smrj 		ddi_map_debug("rootnex_unmap_regspec: zero regspec_size\n");
123012f080e7Smrj #endif  /* DDI_MAP_DEBUG */
123112f080e7Smrj 		return (DDI_ME_INVAL);
12327c478bd9Sstevel@tonic-gate 	}
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate 	/*
123512f080e7Smrj 	 * I/O or memory mapping:
12367c478bd9Sstevel@tonic-gate 	 *
123712f080e7Smrj 	 *	<bustype=0, addr=x, len=x>: memory
123812f080e7Smrj 	 *	<bustype=1, addr=x, len=x>: i/o
123912f080e7Smrj 	 *	<bustype>1, addr=0, len=x>: x86-compatibility i/o
12407c478bd9Sstevel@tonic-gate 	 */
124112f080e7Smrj 	if (rp->regspec_bustype != 0) {
12427c478bd9Sstevel@tonic-gate 		/*
124312f080e7Smrj 		 * This is I/O space, which requires no particular
124412f080e7Smrj 		 * processing on unmap since it isn't mapped in the
124512f080e7Smrj 		 * first place.
12467c478bd9Sstevel@tonic-gate 		 */
12477c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
12487c478bd9Sstevel@tonic-gate 	}
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 	/*
125112f080e7Smrj 	 * Memory space
12527c478bd9Sstevel@tonic-gate 	 */
125312f080e7Smrj 	pgoffset = (uintptr_t)addr & MMU_PAGEOFFSET;
125412f080e7Smrj 	npages = mmu_btopr(rp->regspec_size + pgoffset);
125512f080e7Smrj 	hat_unload(kas.a_hat, addr - pgoffset, ptob(npages), HAT_UNLOAD_UNLOCK);
125612f080e7Smrj 	device_arena_free(addr - pgoffset, ptob(npages));
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate 	/*
125912f080e7Smrj 	 * Destroy the pointer - the mapping has logically gone
12607c478bd9Sstevel@tonic-gate 	 */
126112f080e7Smrj 	*vaddrp = NULL;
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
12647c478bd9Sstevel@tonic-gate }
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate static int
rootnex_map_handle(ddi_map_req_t * mp)126712f080e7Smrj rootnex_map_handle(ddi_map_req_t *mp)
12687c478bd9Sstevel@tonic-gate {
1269843e1988Sjohnlev 	rootnex_addr_t rbase;
127012f080e7Smrj 	ddi_acc_hdl_t *hp;
12713304b547SRobert Mustacchi 	uint64_t pgoffset;
12723304b547SRobert Mustacchi 	struct regspec64 *rp;
1273843e1988Sjohnlev 	paddr_t pbase;
12747c478bd9Sstevel@tonic-gate 
12753304b547SRobert Mustacchi 	rp = (struct regspec64 *)mp->map_obj.rp;
12767c478bd9Sstevel@tonic-gate 
127712f080e7Smrj #ifdef	DDI_MAP_DEBUG
127812f080e7Smrj 	ddi_map_debug(
127912f080e7Smrj 	    "rootnex_map_handle: <0x%x 0x%x 0x%x> handle 0x%x\n",
128012f080e7Smrj 	    rp->regspec_bustype, rp->regspec_addr,
128112f080e7Smrj 	    rp->regspec_size, mp->map_handlep);
128212f080e7Smrj #endif	/* DDI_MAP_DEBUG */
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate 	/*
128512f080e7Smrj 	 * I/O or memory mapping:
128612f080e7Smrj 	 *
128712f080e7Smrj 	 *	<bustype=0, addr=x, len=x>: memory
128812f080e7Smrj 	 *	<bustype=1, addr=x, len=x>: i/o
128912f080e7Smrj 	 *	<bustype>1, addr=0, len=x>: x86-compatibility i/o
12907c478bd9Sstevel@tonic-gate 	 */
129112f080e7Smrj 	if (rp->regspec_bustype != 0) {
129212f080e7Smrj 		/*
129312f080e7Smrj 		 * This refers to I/O space, and we don't support "mapping"
129412f080e7Smrj 		 * I/O space to a user.
129512f080e7Smrj 		 */
12967c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
12977c478bd9Sstevel@tonic-gate 	}
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate 	/*
130012f080e7Smrj 	 * Set up the hat_flags for the mapping.
13017c478bd9Sstevel@tonic-gate 	 */
130212f080e7Smrj 	hp = mp->map_handlep;
13037c478bd9Sstevel@tonic-gate 
130412f080e7Smrj 	switch (hp->ah_acc.devacc_attr_endian_flags) {
130512f080e7Smrj 	case DDI_NEVERSWAP_ACC:
130612f080e7Smrj 		hp->ah_hat_flags = HAT_NEVERSWAP | HAT_STRICTORDER;
13077c478bd9Sstevel@tonic-gate 		break;
130812f080e7Smrj 	case DDI_STRUCTURE_LE_ACC:
130912f080e7Smrj 		hp->ah_hat_flags = HAT_STRUCTURE_LE;
13107c478bd9Sstevel@tonic-gate 		break;
131112f080e7Smrj 	case DDI_STRUCTURE_BE_ACC:
13127c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
13137c478bd9Sstevel@tonic-gate 	default:
131412f080e7Smrj 		return (DDI_REGS_ACC_CONFLICT);
13157c478bd9Sstevel@tonic-gate 	}
13167c478bd9Sstevel@tonic-gate 
131712f080e7Smrj 	switch (hp->ah_acc.devacc_attr_dataorder) {
131812f080e7Smrj 	case DDI_STRICTORDER_ACC:
13197c478bd9Sstevel@tonic-gate 		break;
132012f080e7Smrj 	case DDI_UNORDERED_OK_ACC:
132112f080e7Smrj 		hp->ah_hat_flags |= HAT_UNORDERED_OK;
13227c478bd9Sstevel@tonic-gate 		break;
132312f080e7Smrj 	case DDI_MERGING_OK_ACC:
132412f080e7Smrj 		hp->ah_hat_flags |= HAT_MERGING_OK;
13257c478bd9Sstevel@tonic-gate 		break;
132612f080e7Smrj 	case DDI_LOADCACHING_OK_ACC:
132712f080e7Smrj 		hp->ah_hat_flags |= HAT_LOADCACHING_OK;
132812f080e7Smrj 		break;
132912f080e7Smrj 	case DDI_STORECACHING_OK_ACC:
133012f080e7Smrj 		hp->ah_hat_flags |= HAT_STORECACHING_OK;
133112f080e7Smrj 		break;
13327c478bd9Sstevel@tonic-gate 	default:
13337c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
13347c478bd9Sstevel@tonic-gate 	}
13357c478bd9Sstevel@tonic-gate 
1336843e1988Sjohnlev 	rbase = (rootnex_addr_t)rp->regspec_addr &
1337843e1988Sjohnlev 	    (~(rootnex_addr_t)MMU_PAGEOFFSET);
1338843e1988Sjohnlev 	pgoffset = (ulong_t)rp->regspec_addr & MMU_PAGEOFFSET;
13397c478bd9Sstevel@tonic-gate 
134012f080e7Smrj 	if (rp->regspec_size == 0)
134112f080e7Smrj 		return (DDI_ME_INVAL);
13427c478bd9Sstevel@tonic-gate 
1343843e1988Sjohnlev #ifdef __xpv
1344843e1988Sjohnlev 	/*
1345843e1988Sjohnlev 	 * If we're dom0, we're using a real device so we need to translate
1346843e1988Sjohnlev 	 * the MA to a PA.
1347843e1988Sjohnlev 	 */
1348843e1988Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
1349843e1988Sjohnlev 		pbase = pfn_to_pa(xen_assign_pfn(mmu_btop(rbase))) |
1350843e1988Sjohnlev 		    (rbase & MMU_PAGEOFFSET);
1351843e1988Sjohnlev 	} else {
1352843e1988Sjohnlev 		pbase = rbase;
1353843e1988Sjohnlev 	}
1354843e1988Sjohnlev #else
1355843e1988Sjohnlev 	pbase = rbase;
1356843e1988Sjohnlev #endif
1357843e1988Sjohnlev 
1358843e1988Sjohnlev 	hp->ah_pfn = mmu_btop(pbase);
135912f080e7Smrj 	hp->ah_pnum = mmu_btopr(rp->regspec_size + pgoffset);
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
13627c478bd9Sstevel@tonic-gate }
13637c478bd9Sstevel@tonic-gate 
136412f080e7Smrj 
136512f080e7Smrj 
13667c478bd9Sstevel@tonic-gate /*
136712f080e7Smrj  * ************************
136812f080e7Smrj  *  interrupt related code
136912f080e7Smrj  * ************************
13707c478bd9Sstevel@tonic-gate  */
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate /*
137312f080e7Smrj  * rootnex_intr_ops()
13747c478bd9Sstevel@tonic-gate  *	bus_intr_op() function for interrupt support
13757c478bd9Sstevel@tonic-gate  */
13767c478bd9Sstevel@tonic-gate /* ARGSUSED */
13777c478bd9Sstevel@tonic-gate static int
rootnex_intr_ops(dev_info_t * pdip,dev_info_t * rdip,ddi_intr_op_t intr_op,ddi_intr_handle_impl_t * hdlp,void * result)13787c478bd9Sstevel@tonic-gate rootnex_intr_ops(dev_info_t *pdip, dev_info_t *rdip, ddi_intr_op_t intr_op,
13797c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, void *result)
13807c478bd9Sstevel@tonic-gate {
13817c478bd9Sstevel@tonic-gate 	struct intrspec			*ispec;
13827c478bd9Sstevel@tonic-gate 
13837c478bd9Sstevel@tonic-gate 	DDI_INTR_NEXDBG((CE_CONT,
13847c478bd9Sstevel@tonic-gate 	    "rootnex_intr_ops: pdip = %p, rdip = %p, intr_op = %x, hdlp = %p\n",
13857c478bd9Sstevel@tonic-gate 	    (void *)pdip, (void *)rdip, intr_op, (void *)hdlp));
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate 	/* Process the interrupt operation */
13887c478bd9Sstevel@tonic-gate 	switch (intr_op) {
13897c478bd9Sstevel@tonic-gate 	case DDI_INTROP_GETCAP:
13907c478bd9Sstevel@tonic-gate 		/* First check with pcplusmp */
13917c478bd9Sstevel@tonic-gate 		if (psm_intr_ops == NULL)
13927c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate 		if ((*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_GET_CAP, result)) {
13957c478bd9Sstevel@tonic-gate 			*(int *)result = 0;
13967c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
13977c478bd9Sstevel@tonic-gate 		}
13987c478bd9Sstevel@tonic-gate 		break;
13997c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SETCAP:
14007c478bd9Sstevel@tonic-gate 		if (psm_intr_ops == NULL)
14017c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate 		if ((*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_SET_CAP, result))
14047c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14057c478bd9Sstevel@tonic-gate 		break;
14067c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ALLOC:
14077ff178cdSJimmy Vetayases 		ASSERT(hdlp->ih_type == DDI_INTR_TYPE_FIXED);
14087ff178cdSJimmy Vetayases 		return (rootnex_alloc_intr_fixed(rdip, hdlp, result));
14097c478bd9Sstevel@tonic-gate 	case DDI_INTROP_FREE:
14107ff178cdSJimmy Vetayases 		ASSERT(hdlp->ih_type == DDI_INTR_TYPE_FIXED);
14117ff178cdSJimmy Vetayases 		return (rootnex_free_intr_fixed(rdip, hdlp));
14127c478bd9Sstevel@tonic-gate 	case DDI_INTROP_GETPRI:
14137c478bd9Sstevel@tonic-gate 		if ((ispec = rootnex_get_ispec(rdip, hdlp->ih_inum)) == NULL)
14147c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14157c478bd9Sstevel@tonic-gate 		*(int *)result = ispec->intrspec_pri;
14167c478bd9Sstevel@tonic-gate 		break;
14177c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SETPRI:
14187c478bd9Sstevel@tonic-gate 		/* Validate the interrupt priority passed to us */
14197c478bd9Sstevel@tonic-gate 		if (*(int *)result > LOCK_LEVEL)
14207c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate 		/* Ensure that PSM is all initialized and ispec is ok */
14237c478bd9Sstevel@tonic-gate 		if ((psm_intr_ops == NULL) ||
14247c478bd9Sstevel@tonic-gate 		    ((ispec = rootnex_get_ispec(rdip, hdlp->ih_inum)) == NULL))
14257c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate 		/* Change the priority */
14287c478bd9Sstevel@tonic-gate 		if ((*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_SET_PRI, result) ==
14297c478bd9Sstevel@tonic-gate 		    PSM_FAILURE)
14307c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate 		/* update the ispec with the new priority */
14337c478bd9Sstevel@tonic-gate 		ispec->intrspec_pri =  *(int *)result;
14347c478bd9Sstevel@tonic-gate 		break;
14357c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ADDISR:
14367c478bd9Sstevel@tonic-gate 		if ((ispec = rootnex_get_ispec(rdip, hdlp->ih_inum)) == NULL)
14377c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14387c478bd9Sstevel@tonic-gate 		ispec->intrspec_func = hdlp->ih_cb_func;
14397c478bd9Sstevel@tonic-gate 		break;
14407c478bd9Sstevel@tonic-gate 	case DDI_INTROP_REMISR:
14417c478bd9Sstevel@tonic-gate 		if ((ispec = rootnex_get_ispec(rdip, hdlp->ih_inum)) == NULL)
14427c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14437c478bd9Sstevel@tonic-gate 		ispec->intrspec_func = (uint_t (*)()) 0;
14447c478bd9Sstevel@tonic-gate 		break;
14457c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ENABLE:
14467c478bd9Sstevel@tonic-gate 		if ((ispec = rootnex_get_ispec(rdip, hdlp->ih_inum)) == NULL)
14477c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 		/* Call psmi to translate irq with the dip */
14507c478bd9Sstevel@tonic-gate 		if (psm_intr_ops == NULL)
14517c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14527c478bd9Sstevel@tonic-gate 
14537a364d25Sschwartz 		((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp = ispec;
145486a9c507SGuoli Shu 		if ((*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_XLATE_VECTOR,
145586a9c507SGuoli Shu 		    (int *)&hdlp->ih_vector) == PSM_FAILURE)
145686a9c507SGuoli Shu 			return (DDI_FAILURE);
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate 		/* Add the interrupt handler */
14597c478bd9Sstevel@tonic-gate 		if (!add_avintr((void *)hdlp, ispec->intrspec_pri,
14607c478bd9Sstevel@tonic-gate 		    hdlp->ih_cb_func, DEVI(rdip)->devi_name, hdlp->ih_vector,
14617a364d25Sschwartz 		    hdlp->ih_cb_arg1, hdlp->ih_cb_arg2, NULL, rdip))
14627c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14637c478bd9Sstevel@tonic-gate 		break;
14647c478bd9Sstevel@tonic-gate 	case DDI_INTROP_DISABLE:
14657c478bd9Sstevel@tonic-gate 		if ((ispec = rootnex_get_ispec(rdip, hdlp->ih_inum)) == NULL)
14667c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate 		/* Call psm_ops() to translate irq with the dip */
14697c478bd9Sstevel@tonic-gate 		if (psm_intr_ops == NULL)
14707c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14717c478bd9Sstevel@tonic-gate 
14727a364d25Sschwartz 		((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp = ispec;
14737c478bd9Sstevel@tonic-gate 		(void) (*psm_intr_ops)(rdip, hdlp,
14747c478bd9Sstevel@tonic-gate 		    PSM_INTR_OP_XLATE_VECTOR, (int *)&hdlp->ih_vector);
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate 		/* Remove the interrupt handler */
14777c478bd9Sstevel@tonic-gate 		rem_avintr((void *)hdlp, ispec->intrspec_pri,
14787c478bd9Sstevel@tonic-gate 		    hdlp->ih_cb_func, hdlp->ih_vector);
14797c478bd9Sstevel@tonic-gate 		break;
14807c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SETMASK:
14817c478bd9Sstevel@tonic-gate 		if (psm_intr_ops == NULL)
14827c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate 		if ((*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_SET_MASK, NULL))
14857c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14867c478bd9Sstevel@tonic-gate 		break;
14877c478bd9Sstevel@tonic-gate 	case DDI_INTROP_CLRMASK:
14887c478bd9Sstevel@tonic-gate 		if (psm_intr_ops == NULL)
14897c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate 		if ((*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_CLEAR_MASK, NULL))
14927c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14937c478bd9Sstevel@tonic-gate 		break;
14947c478bd9Sstevel@tonic-gate 	case DDI_INTROP_GETPENDING:
14957c478bd9Sstevel@tonic-gate 		if (psm_intr_ops == NULL)
14967c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 		if ((*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_GET_PENDING,
14997c478bd9Sstevel@tonic-gate 		    result)) {
15007c478bd9Sstevel@tonic-gate 			*(int *)result = 0;
15017c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
15027c478bd9Sstevel@tonic-gate 		}
15037c478bd9Sstevel@tonic-gate 		break;
1504a54f81fbSanish 	case DDI_INTROP_NAVAIL:
15057c478bd9Sstevel@tonic-gate 	case DDI_INTROP_NINTRS:
1506a54f81fbSanish 		*(int *)result = i_ddi_get_intx_nintrs(rdip);
1507a54f81fbSanish 		if (*(int *)result == 0) {
15087c478bd9Sstevel@tonic-gate 			/*
15097c478bd9Sstevel@tonic-gate 			 * Special case for 'pcic' driver' only. This driver
15107c478bd9Sstevel@tonic-gate 			 * driver is a child of 'isa' and 'rootnex' drivers.
15117c478bd9Sstevel@tonic-gate 			 *
15127c478bd9Sstevel@tonic-gate 			 * See detailed comments on this in the function
15137c478bd9Sstevel@tonic-gate 			 * rootnex_get_ispec().
15147c478bd9Sstevel@tonic-gate 			 *
15157c478bd9Sstevel@tonic-gate 			 * Children of 'pcic' send 'NINITR' request all the
15167c478bd9Sstevel@tonic-gate 			 * way to rootnex driver. But, the 'pdp->par_nintr'
15177c478bd9Sstevel@tonic-gate 			 * field may not initialized. So, we fake it here
15187c478bd9Sstevel@tonic-gate 			 * to return 1 (a la what PCMCIA nexus does).
15197c478bd9Sstevel@tonic-gate 			 */
15207c478bd9Sstevel@tonic-gate 			if (strcmp(ddi_get_name(rdip), "pcic") == 0)
15217c478bd9Sstevel@tonic-gate 				*(int *)result = 1;
1522a54f81fbSanish 			else
1523a54f81fbSanish 				return (DDI_FAILURE);
15247c478bd9Sstevel@tonic-gate 		}
15257c478bd9Sstevel@tonic-gate 		break;
15267c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SUPPORTED_TYPES:
1527a54f81fbSanish 		*(int *)result = DDI_INTR_TYPE_FIXED;	/* Always ... */
15287c478bd9Sstevel@tonic-gate 		break;
15297c478bd9Sstevel@tonic-gate 	default:
15307c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
15317c478bd9Sstevel@tonic-gate 	}
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
15347c478bd9Sstevel@tonic-gate }
15357c478bd9Sstevel@tonic-gate 
15367c478bd9Sstevel@tonic-gate 
15377c478bd9Sstevel@tonic-gate /*
153812f080e7Smrj  * rootnex_get_ispec()
153912f080e7Smrj  *	convert an interrupt number to an interrupt specification.
154012f080e7Smrj  *	The interrupt number determines which interrupt spec will be
154112f080e7Smrj  *	returned if more than one exists.
154212f080e7Smrj  *
154312f080e7Smrj  *	Look into the parent private data area of the 'rdip' to find out
154412f080e7Smrj  *	the interrupt specification.  First check to make sure there is
154512f080e7Smrj  *	one that matchs "inumber" and then return a pointer to it.
154612f080e7Smrj  *
154712f080e7Smrj  *	Return NULL if one could not be found.
154812f080e7Smrj  *
154912f080e7Smrj  *	NOTE: This is needed for rootnex_intr_ops()
15507c478bd9Sstevel@tonic-gate  */
155112f080e7Smrj static struct intrspec *
rootnex_get_ispec(dev_info_t * rdip,int inum)155212f080e7Smrj rootnex_get_ispec(dev_info_t *rdip, int inum)
15537c478bd9Sstevel@tonic-gate {
155412f080e7Smrj 	struct ddi_parent_private_data *pdp = ddi_get_parent_data(rdip);
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate 	/*
155712f080e7Smrj 	 * Special case handling for drivers that provide their own
155812f080e7Smrj 	 * intrspec structures instead of relying on the DDI framework.
155912f080e7Smrj 	 *
156012f080e7Smrj 	 * A broken hardware driver in ON could potentially provide its
156112f080e7Smrj 	 * own intrspec structure, instead of relying on the hardware.
156212f080e7Smrj 	 * If these drivers are children of 'rootnex' then we need to
156312f080e7Smrj 	 * continue to provide backward compatibility to them here.
156412f080e7Smrj 	 *
156512f080e7Smrj 	 * Following check is a special case for 'pcic' driver which
156612f080e7Smrj 	 * was found to have broken hardwre andby provides its own intrspec.
156712f080e7Smrj 	 *
156812f080e7Smrj 	 * Verbatim comments from this driver are shown here:
156912f080e7Smrj 	 * "Don't use the ddi_add_intr since we don't have a
157012f080e7Smrj 	 * default intrspec in all cases."
157112f080e7Smrj 	 *
157212f080e7Smrj 	 * Since an 'ispec' may not be always created for it,
157312f080e7Smrj 	 * check for that and create one if so.
157412f080e7Smrj 	 *
157512f080e7Smrj 	 * NOTE: Currently 'pcic' is the only driver found to do this.
15767c478bd9Sstevel@tonic-gate 	 */
157712f080e7Smrj 	if (!pdp->par_intr && strcmp(ddi_get_name(rdip), "pcic") == 0) {
157812f080e7Smrj 		pdp->par_nintr = 1;
157912f080e7Smrj 		pdp->par_intr = kmem_zalloc(sizeof (struct intrspec) *
158012f080e7Smrj 		    pdp->par_nintr, KM_SLEEP);
158112f080e7Smrj 	}
158212f080e7Smrj 
158312f080e7Smrj 	/* Validate the interrupt number */
158412f080e7Smrj 	if (inum >= pdp->par_nintr)
158512f080e7Smrj 		return (NULL);
158612f080e7Smrj 
158712f080e7Smrj 	/* Get the interrupt structure pointer and return that */
158812f080e7Smrj 	return ((struct intrspec *)&pdp->par_intr[inum]);
158912f080e7Smrj }
159012f080e7Smrj 
15917ff178cdSJimmy Vetayases /*
15927ff178cdSJimmy Vetayases  * Allocate interrupt vector for FIXED (legacy) type.
15937ff178cdSJimmy Vetayases  */
15947ff178cdSJimmy Vetayases static int
rootnex_alloc_intr_fixed(dev_info_t * rdip,ddi_intr_handle_impl_t * hdlp,void * result)15957ff178cdSJimmy Vetayases rootnex_alloc_intr_fixed(dev_info_t *rdip, ddi_intr_handle_impl_t *hdlp,
15967ff178cdSJimmy Vetayases     void *result)
15977ff178cdSJimmy Vetayases {
15987ff178cdSJimmy Vetayases 	struct intrspec		*ispec;
15997ff178cdSJimmy Vetayases 	ddi_intr_handle_impl_t	info_hdl;
16007ff178cdSJimmy Vetayases 	int			ret;
16017ff178cdSJimmy Vetayases 	int			free_phdl = 0;
16027ff178cdSJimmy Vetayases 	apic_get_type_t		type_info;
16037ff178cdSJimmy Vetayases 
16047ff178cdSJimmy Vetayases 	if (psm_intr_ops == NULL)
16057ff178cdSJimmy Vetayases 		return (DDI_FAILURE);
16067ff178cdSJimmy Vetayases 
16077ff178cdSJimmy Vetayases 	if ((ispec = rootnex_get_ispec(rdip, hdlp->ih_inum)) == NULL)
16087ff178cdSJimmy Vetayases 		return (DDI_FAILURE);
16097ff178cdSJimmy Vetayases 
16107ff178cdSJimmy Vetayases 	/*
16117ff178cdSJimmy Vetayases 	 * If the PSM module is "APIX" then pass the request for it
16127ff178cdSJimmy Vetayases 	 * to allocate the vector now.
16137ff178cdSJimmy Vetayases 	 */
16147ff178cdSJimmy Vetayases 	bzero(&info_hdl, sizeof (ddi_intr_handle_impl_t));
16157ff178cdSJimmy Vetayases 	info_hdl.ih_private = &type_info;
16167ff178cdSJimmy Vetayases 	if ((*psm_intr_ops)(NULL, &info_hdl, PSM_INTR_OP_APIC_TYPE, NULL) ==
16177ff178cdSJimmy Vetayases 	    PSM_SUCCESS && strcmp(type_info.avgi_type, APIC_APIX_NAME) == 0) {
16187ff178cdSJimmy Vetayases 		if (hdlp->ih_private == NULL) { /* allocate phdl structure */
16197ff178cdSJimmy Vetayases 			free_phdl = 1;
16207ff178cdSJimmy Vetayases 			i_ddi_alloc_intr_phdl(hdlp);
16217ff178cdSJimmy Vetayases 		}
16227ff178cdSJimmy Vetayases 		((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp = ispec;
16237ff178cdSJimmy Vetayases 		ret = (*psm_intr_ops)(rdip, hdlp,
16247ff178cdSJimmy Vetayases 		    PSM_INTR_OP_ALLOC_VECTORS, result);
16257ff178cdSJimmy Vetayases 		if (free_phdl) { /* free up the phdl structure */
16267ff178cdSJimmy Vetayases 			free_phdl = 0;
16277ff178cdSJimmy Vetayases 			i_ddi_free_intr_phdl(hdlp);
16287ff178cdSJimmy Vetayases 			hdlp->ih_private = NULL;
16297ff178cdSJimmy Vetayases 		}
16307ff178cdSJimmy Vetayases 	} else {
16317ff178cdSJimmy Vetayases 		/*
16327ff178cdSJimmy Vetayases 		 * No APIX module; fall back to the old scheme where the
16337ff178cdSJimmy Vetayases 		 * interrupt vector is allocated during ddi_enable_intr() call.
16347ff178cdSJimmy Vetayases 		 */
16357ff178cdSJimmy Vetayases 		hdlp->ih_pri = ispec->intrspec_pri;
16367ff178cdSJimmy Vetayases 		*(int *)result = hdlp->ih_scratch1;
16377ff178cdSJimmy Vetayases 		ret = DDI_SUCCESS;
16387ff178cdSJimmy Vetayases 	}
16397ff178cdSJimmy Vetayases 
16407ff178cdSJimmy Vetayases 	return (ret);
16417ff178cdSJimmy Vetayases }
16427ff178cdSJimmy Vetayases 
16437ff178cdSJimmy Vetayases /*
16447ff178cdSJimmy Vetayases  * Free up interrupt vector for FIXED (legacy) type.
16457ff178cdSJimmy Vetayases  */
16467ff178cdSJimmy Vetayases static int
rootnex_free_intr_fixed(dev_info_t * rdip,ddi_intr_handle_impl_t * hdlp)16477ff178cdSJimmy Vetayases rootnex_free_intr_fixed(dev_info_t *rdip, ddi_intr_handle_impl_t *hdlp)
16487ff178cdSJimmy Vetayases {
16497ff178cdSJimmy Vetayases 	struct intrspec			*ispec;
16507ff178cdSJimmy Vetayases 	struct ddi_parent_private_data	*pdp;
16517ff178cdSJimmy Vetayases 	ddi_intr_handle_impl_t		info_hdl;
16527ff178cdSJimmy Vetayases 	int				ret;
16537ff178cdSJimmy Vetayases 	apic_get_type_t			type_info;
16547ff178cdSJimmy Vetayases 
16557ff178cdSJimmy Vetayases 	if (psm_intr_ops == NULL)
16567ff178cdSJimmy Vetayases 		return (DDI_FAILURE);
16577ff178cdSJimmy Vetayases 
16587ff178cdSJimmy Vetayases 	/*
16597ff178cdSJimmy Vetayases 	 * If the PSM module is "APIX" then pass the request for it
16607ff178cdSJimmy Vetayases 	 * to free up the vector now.
16617ff178cdSJimmy Vetayases 	 */
16627ff178cdSJimmy Vetayases 	bzero(&info_hdl, sizeof (ddi_intr_handle_impl_t));
16637ff178cdSJimmy Vetayases 	info_hdl.ih_private = &type_info;
16647ff178cdSJimmy Vetayases 	if ((*psm_intr_ops)(NULL, &info_hdl, PSM_INTR_OP_APIC_TYPE, NULL) ==
16657ff178cdSJimmy Vetayases 	    PSM_SUCCESS && strcmp(type_info.avgi_type, APIC_APIX_NAME) == 0) {
16667ff178cdSJimmy Vetayases 		if ((ispec = rootnex_get_ispec(rdip, hdlp->ih_inum)) == NULL)
16677ff178cdSJimmy Vetayases 			return (DDI_FAILURE);
16687ff178cdSJimmy Vetayases 		((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp = ispec;
16697ff178cdSJimmy Vetayases 		ret = (*psm_intr_ops)(rdip, hdlp,
16707ff178cdSJimmy Vetayases 		    PSM_INTR_OP_FREE_VECTORS, NULL);
16717ff178cdSJimmy Vetayases 	} else {
16727ff178cdSJimmy Vetayases 		/*
16737ff178cdSJimmy Vetayases 		 * No APIX module; fall back to the old scheme where
16747ff178cdSJimmy Vetayases 		 * the interrupt vector was already freed during
16757ff178cdSJimmy Vetayases 		 * ddi_disable_intr() call.
16767ff178cdSJimmy Vetayases 		 */
16777ff178cdSJimmy Vetayases 		ret = DDI_SUCCESS;
16787ff178cdSJimmy Vetayases 	}
16797ff178cdSJimmy Vetayases 
16807ff178cdSJimmy Vetayases 	pdp = ddi_get_parent_data(rdip);
16817ff178cdSJimmy Vetayases 
16827ff178cdSJimmy Vetayases 	/*
16837ff178cdSJimmy Vetayases 	 * Special case for 'pcic' driver' only.
16847ff178cdSJimmy Vetayases 	 * If an intrspec was created for it, clean it up here
16857ff178cdSJimmy Vetayases 	 * See detailed comments on this in the function
16867ff178cdSJimmy Vetayases 	 * rootnex_get_ispec().
16877ff178cdSJimmy Vetayases 	 */
16887ff178cdSJimmy Vetayases 	if (pdp->par_intr && strcmp(ddi_get_name(rdip), "pcic") == 0) {
16897ff178cdSJimmy Vetayases 		kmem_free(pdp->par_intr, sizeof (struct intrspec) *
16907ff178cdSJimmy Vetayases 		    pdp->par_nintr);
16917ff178cdSJimmy Vetayases 		/*
16927ff178cdSJimmy Vetayases 		 * Set it to zero; so that
16937ff178cdSJimmy Vetayases 		 * DDI framework doesn't free it again
16947ff178cdSJimmy Vetayases 		 */
16957ff178cdSJimmy Vetayases 		pdp->par_intr = NULL;
16967ff178cdSJimmy Vetayases 		pdp->par_nintr = 0;
16977ff178cdSJimmy Vetayases 	}
16987ff178cdSJimmy Vetayases 
16997ff178cdSJimmy Vetayases 	return (ret);
17007ff178cdSJimmy Vetayases }
17017ff178cdSJimmy Vetayases 
170212f080e7Smrj 
170312f080e7Smrj /*
170412f080e7Smrj  * ******************
170512f080e7Smrj  *  dma related code
170612f080e7Smrj  * ******************
170712f080e7Smrj  */
170812f080e7Smrj 
170912f080e7Smrj /*ARGSUSED*/
171012f080e7Smrj static int
rootnex_coredma_allochdl(dev_info_t * dip,dev_info_t * rdip,ddi_dma_attr_t * attr,int (* waitfp)(caddr_t),caddr_t arg,ddi_dma_handle_t * handlep)171120906b23SVikram Hegde rootnex_coredma_allochdl(dev_info_t *dip, dev_info_t *rdip,
171220906b23SVikram Hegde     ddi_dma_attr_t *attr, int (*waitfp)(caddr_t), caddr_t arg,
171320906b23SVikram Hegde     ddi_dma_handle_t *handlep)
171412f080e7Smrj {
171512f080e7Smrj 	uint64_t maxsegmentsize_ll;
171612f080e7Smrj 	uint_t maxsegmentsize;
171712f080e7Smrj 	ddi_dma_impl_t *hp;
171812f080e7Smrj 	rootnex_dma_t *dma;
171912f080e7Smrj 	uint64_t count_max;
172012f080e7Smrj 	uint64_t seg;
172112f080e7Smrj 	int kmflag;
172212f080e7Smrj 	int e;
172312f080e7Smrj 
172412f080e7Smrj 
172512f080e7Smrj 	/* convert our sleep flags */
172612f080e7Smrj 	if (waitfp == DDI_DMA_SLEEP) {
172712f080e7Smrj 		kmflag = KM_SLEEP;
172812f080e7Smrj 	} else {
172912f080e7Smrj 		kmflag = KM_NOSLEEP;
173012f080e7Smrj 	}
173112f080e7Smrj 
173212f080e7Smrj 	/*
173312f080e7Smrj 	 * We try to do only one memory allocation here. We'll do a little
173412f080e7Smrj 	 * pointer manipulation later. If the bind ends up taking more than
173512f080e7Smrj 	 * our prealloc's space, we'll have to allocate more memory in the
173612f080e7Smrj 	 * bind operation. Not great, but much better than before and the
173712f080e7Smrj 	 * best we can do with the current bind interfaces.
173812f080e7Smrj 	 */
173912f080e7Smrj 	hp = kmem_cache_alloc(rootnex_state->r_dmahdl_cache, kmflag);
174050200e77SFrank Van Der Linden 	if (hp == NULL)
174112f080e7Smrj 		return (DDI_DMA_NORESOURCES);
174212f080e7Smrj 
174312f080e7Smrj 	/* Do our pointer manipulation now, align the structures */
174412f080e7Smrj 	hp->dmai_private = (void *)(((uintptr_t)hp +
174512f080e7Smrj 	    (uintptr_t)sizeof (ddi_dma_impl_t) + 0x7) & ~0x7);
174612f080e7Smrj 	dma = (rootnex_dma_t *)hp->dmai_private;
174712f080e7Smrj 	dma->dp_prealloc_buffer = (uchar_t *)(((uintptr_t)dma +
174812f080e7Smrj 	    sizeof (rootnex_dma_t) + 0x7) & ~0x7);
174912f080e7Smrj 
175012f080e7Smrj 	/* setup the handle */
175112f080e7Smrj 	rootnex_clean_dmahdl(hp);
1752567c0b92SStephen Hanson 	hp->dmai_error.err_fep = NULL;
1753567c0b92SStephen Hanson 	hp->dmai_error.err_cf = NULL;
175412f080e7Smrj 	dma->dp_dip = rdip;
175550200e77SFrank Van Der Linden 	dma->dp_sglinfo.si_flags = attr->dma_attr_flags;
175612f080e7Smrj 	dma->dp_sglinfo.si_min_addr = attr->dma_attr_addr_lo;
175750200e77SFrank Van Der Linden 
175850200e77SFrank Van Der Linden 	/*
175950200e77SFrank Van Der Linden 	 * The BOUNCE_ON_SEG workaround is not needed when an IOMMU
176050200e77SFrank Van Der Linden 	 * is being used. Set the upper limit to the seg value.
176150200e77SFrank Van Der Linden 	 * There will be enough DVMA space to always get addresses
176250200e77SFrank Van Der Linden 	 * that will match the constraints.
176350200e77SFrank Van Der Linden 	 */
176450200e77SFrank Van Der Linden 	if (IOMMU_USED(rdip) &&
176550200e77SFrank Van Der Linden 	    (attr->dma_attr_flags & _DDI_DMA_BOUNCE_ON_SEG)) {
176650200e77SFrank Van Der Linden 		dma->dp_sglinfo.si_max_addr = attr->dma_attr_seg;
176750200e77SFrank Van Der Linden 		dma->dp_sglinfo.si_flags &= ~_DDI_DMA_BOUNCE_ON_SEG;
176850200e77SFrank Van Der Linden 	} else
176912f080e7Smrj 		dma->dp_sglinfo.si_max_addr = attr->dma_attr_addr_hi;
177050200e77SFrank Van Der Linden 
177112f080e7Smrj 	hp->dmai_minxfer = attr->dma_attr_minxfer;
177212f080e7Smrj 	hp->dmai_burstsizes = attr->dma_attr_burstsizes;
177312f080e7Smrj 	hp->dmai_rdip = rdip;
177412f080e7Smrj 	hp->dmai_attr = *attr;
177512f080e7Smrj 
177650200e77SFrank Van Der Linden 	if (attr->dma_attr_seg >= dma->dp_sglinfo.si_max_addr)
177750200e77SFrank Van Der Linden 		dma->dp_sglinfo.si_cancross = B_FALSE;
177850200e77SFrank Van Der Linden 	else
177950200e77SFrank Van Der Linden 		dma->dp_sglinfo.si_cancross = B_TRUE;
178050200e77SFrank Van Der Linden 
178112f080e7Smrj 	/* we don't need to worry about the SPL since we do a tryenter */
178212f080e7Smrj 	mutex_init(&dma->dp_mutex, NULL, MUTEX_DRIVER, NULL);
178312f080e7Smrj 
178412f080e7Smrj 	/*
178512f080e7Smrj 	 * Figure out our maximum segment size. If the segment size is greater
178612f080e7Smrj 	 * than 4G, we will limit it to (4G - 1) since the max size of a dma
178712f080e7Smrj 	 * object (ddi_dma_obj_t.dmao_size) is 32 bits. dma_attr_seg and
178812f080e7Smrj 	 * dma_attr_count_max are size-1 type values.
178912f080e7Smrj 	 *
179012f080e7Smrj 	 * Maximum segment size is the largest physically contiguous chunk of
179112f080e7Smrj 	 * memory that we can return from a bind (i.e. the maximum size of a
179212f080e7Smrj 	 * single cookie).
179312f080e7Smrj 	 */
179412f080e7Smrj 
179512f080e7Smrj 	/* handle the rollover cases */
179612f080e7Smrj 	seg = attr->dma_attr_seg + 1;
179712f080e7Smrj 	if (seg < attr->dma_attr_seg) {
179812f080e7Smrj 		seg = attr->dma_attr_seg;
179912f080e7Smrj 	}
180012f080e7Smrj 	count_max = attr->dma_attr_count_max + 1;
180112f080e7Smrj 	if (count_max < attr->dma_attr_count_max) {
180212f080e7Smrj 		count_max = attr->dma_attr_count_max;
180312f080e7Smrj 	}
180412f080e7Smrj 
180512f080e7Smrj 	/*
180612f080e7Smrj 	 * granularity may or may not be a power of two. If it isn't, we can't
180712f080e7Smrj 	 * use a simple mask.
180812f080e7Smrj 	 */
1809de710d24SJosef 'Jeff' Sipek 	if (!ISP2(attr->dma_attr_granular)) {
181012f080e7Smrj 		dma->dp_granularity_power_2 = B_FALSE;
181112f080e7Smrj 	} else {
181212f080e7Smrj 		dma->dp_granularity_power_2 = B_TRUE;
181312f080e7Smrj 	}
181412f080e7Smrj 
181512f080e7Smrj 	/*
181612f080e7Smrj 	 * maxxfer should be a whole multiple of granularity. If we're going to
181712f080e7Smrj 	 * break up a window because we're greater than maxxfer, we might as
181812f080e7Smrj 	 * well make sure it's maxxfer is a whole multiple so we don't have to
181912f080e7Smrj 	 * worry about triming the window later on for this case.
182012f080e7Smrj 	 */
182112f080e7Smrj 	if (attr->dma_attr_granular > 1) {
182212f080e7Smrj 		if (dma->dp_granularity_power_2) {
182312f080e7Smrj 			dma->dp_maxxfer = attr->dma_attr_maxxfer -
182412f080e7Smrj 			    (attr->dma_attr_maxxfer &
182512f080e7Smrj 			    (attr->dma_attr_granular - 1));
182612f080e7Smrj 		} else {
182712f080e7Smrj 			dma->dp_maxxfer = attr->dma_attr_maxxfer -
182812f080e7Smrj 			    (attr->dma_attr_maxxfer % attr->dma_attr_granular);
182912f080e7Smrj 		}
183012f080e7Smrj 	} else {
183112f080e7Smrj 		dma->dp_maxxfer = attr->dma_attr_maxxfer;
183212f080e7Smrj 	}
183312f080e7Smrj 
183412f080e7Smrj 	maxsegmentsize_ll = MIN(seg, dma->dp_maxxfer);
183512f080e7Smrj 	maxsegmentsize_ll = MIN(maxsegmentsize_ll, count_max);
183612f080e7Smrj 	if (maxsegmentsize_ll == 0 || (maxsegmentsize_ll > 0xFFFFFFFF)) {
183712f080e7Smrj 		maxsegmentsize = 0xFFFFFFFF;
183812f080e7Smrj 	} else {
183912f080e7Smrj 		maxsegmentsize = maxsegmentsize_ll;
184012f080e7Smrj 	}
184112f080e7Smrj 	dma->dp_sglinfo.si_max_cookie_size = maxsegmentsize;
184212f080e7Smrj 	dma->dp_sglinfo.si_segmask = attr->dma_attr_seg;
184312f080e7Smrj 
184412f080e7Smrj 	/* check the ddi_dma_attr arg to make sure it makes a little sense */
184512f080e7Smrj 	if (rootnex_alloc_check_parms) {
184612f080e7Smrj 		e = rootnex_valid_alloc_parms(attr, maxsegmentsize);
184712f080e7Smrj 		if (e != DDI_SUCCESS) {
1848ef4ab52fSFrank Van Der Linden 			ROOTNEX_DPROF_INC(&rootnex_cnt[ROOTNEX_CNT_ALLOC_FAIL]);
184912f080e7Smrj 			(void) rootnex_dma_freehdl(dip, rdip,
185012f080e7Smrj 			    (ddi_dma_handle_t)hp);
185112f080e7Smrj 			return (e);
185212f080e7Smrj 		}
185312f080e7Smrj 	}
185412f080e7Smrj 
185512f080e7Smrj 	*handlep = (ddi_dma_handle_t)hp;
185612f080e7Smrj 
18570b7ba611SMark Johnson 	ROOTNEX_DPROF_INC(&rootnex_cnt[ROOTNEX_CNT_ACTIVE_HDLS]);
18580b7ba611SMark Johnson 	ROOTNEX_DPROBE1(rootnex__alloc__handle, uint64_t,
185912f080e7Smrj 	    rootnex_cnt[ROOTNEX_CNT_ACTIVE_HDLS]);
186012f080e7Smrj 
186112f080e7Smrj 	return (DDI_SUCCESS);
186212f080e7Smrj }
186312f080e7Smrj 
186412f080e7Smrj 
186512f080e7Smrj /*
186620906b23SVikram Hegde  * rootnex_dma_allochdl()
186720906b23SVikram Hegde  *    called from ddi_dma_alloc_handle().
186812f080e7Smrj  */
186920906b23SVikram Hegde static int
rootnex_dma_allochdl(dev_info_t * dip,dev_info_t * rdip,ddi_dma_attr_t * attr,int (* waitfp)(caddr_t),caddr_t arg,ddi_dma_handle_t * handlep)187020906b23SVikram Hegde rootnex_dma_allochdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_attr_t *attr,
187120906b23SVikram Hegde     int (*waitfp)(caddr_t), caddr_t arg, ddi_dma_handle_t *handlep)
187220906b23SVikram Hegde {
187350200e77SFrank Van Der Linden 	int retval = DDI_SUCCESS;
18743a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
187520906b23SVikram Hegde 
187650200e77SFrank Van Der Linden 	if (IOMMU_UNITIALIZED(rdip)) {
187750200e77SFrank Van Der Linden 		retval = iommulib_nex_open(dip, rdip);
187820906b23SVikram Hegde 
187950200e77SFrank Van Der Linden 		if (retval != DDI_SUCCESS && retval != DDI_ENOTSUP)
188050200e77SFrank Van Der Linden 			return (retval);
188120906b23SVikram Hegde 	}
188220906b23SVikram Hegde 
188350200e77SFrank Van Der Linden 	if (IOMMU_UNUSED(rdip)) {
188450200e77SFrank Van Der Linden 		retval = rootnex_coredma_allochdl(dip, rdip, attr, waitfp, arg,
188550200e77SFrank Van Der Linden 		    handlep);
188650200e77SFrank Van Der Linden 	} else {
1887567c0b92SStephen Hanson 		retval = iommulib_nexdma_allochdl(dip, rdip, attr,
1888567c0b92SStephen Hanson 		    waitfp, arg, handlep);
188950200e77SFrank Van Der Linden 	}
189020906b23SVikram Hegde #else
1891567c0b92SStephen Hanson 	retval = rootnex_coredma_allochdl(dip, rdip, attr, waitfp, arg,
1892567c0b92SStephen Hanson 	    handlep);
189320906b23SVikram Hegde #endif
189450200e77SFrank Van Der Linden 	switch (retval) {
189550200e77SFrank Van Der Linden 	case DDI_DMA_NORESOURCES:
189650200e77SFrank Van Der Linden 		if (waitfp != DDI_DMA_DONTWAIT) {
189750200e77SFrank Van Der Linden 			ddi_set_callback(waitfp, arg,
189850200e77SFrank Van Der Linden 			    &rootnex_state->r_dvma_call_list_id);
189950200e77SFrank Van Der Linden 		}
190050200e77SFrank Van Der Linden 		break;
190150200e77SFrank Van Der Linden 	case DDI_SUCCESS:
1902567c0b92SStephen Hanson 		ndi_fmc_insert(rdip, DMA_HANDLE, *handlep, NULL);
190350200e77SFrank Van Der Linden 		break;
190450200e77SFrank Van Der Linden 	default:
190550200e77SFrank Van Der Linden 		break;
190650200e77SFrank Van Der Linden 	}
1907567c0b92SStephen Hanson 	return (retval);
190820906b23SVikram Hegde }
190920906b23SVikram Hegde 
191012f080e7Smrj /*ARGSUSED*/
191112f080e7Smrj static int
rootnex_coredma_freehdl(dev_info_t * dip,dev_info_t * rdip,ddi_dma_handle_t handle)191220906b23SVikram Hegde rootnex_coredma_freehdl(dev_info_t *dip, dev_info_t *rdip,
191320906b23SVikram Hegde     ddi_dma_handle_t handle)
191412f080e7Smrj {
191512f080e7Smrj 	ddi_dma_impl_t *hp;
191612f080e7Smrj 	rootnex_dma_t *dma;
191712f080e7Smrj 
191812f080e7Smrj 
191912f080e7Smrj 	hp = (ddi_dma_impl_t *)handle;
192012f080e7Smrj 	dma = (rootnex_dma_t *)hp->dmai_private;
192112f080e7Smrj 
192212f080e7Smrj 	/* unbind should have been called first */
192312f080e7Smrj 	ASSERT(!dma->dp_inuse);
192412f080e7Smrj 
192512f080e7Smrj 	mutex_destroy(&dma->dp_mutex);
192612f080e7Smrj 	kmem_cache_free(rootnex_state->r_dmahdl_cache, hp);
192712f080e7Smrj 
19280b7ba611SMark Johnson 	ROOTNEX_DPROF_DEC(&rootnex_cnt[ROOTNEX_CNT_ACTIVE_HDLS]);
19290b7ba611SMark Johnson 	ROOTNEX_DPROBE1(rootnex__free__handle, uint64_t,
193012f080e7Smrj 	    rootnex_cnt[ROOTNEX_CNT_ACTIVE_HDLS]);
193112f080e7Smrj 
193212f080e7Smrj 	return (DDI_SUCCESS);
193312f080e7Smrj }
193412f080e7Smrj 
193512f080e7Smrj /*
193620906b23SVikram Hegde  * rootnex_dma_freehdl()
193720906b23SVikram Hegde  *    called from ddi_dma_free_handle().
193812f080e7Smrj  */
193920906b23SVikram Hegde static int
rootnex_dma_freehdl(dev_info_t * dip,dev_info_t * rdip,ddi_dma_handle_t handle)194020906b23SVikram Hegde rootnex_dma_freehdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle)
194120906b23SVikram Hegde {
194250200e77SFrank Van Der Linden 	int ret;
194350200e77SFrank Van Der Linden 
1944567c0b92SStephen Hanson 	ndi_fmc_remove(rdip, DMA_HANDLE, handle);
19453a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
194650200e77SFrank Van Der Linden 	if (IOMMU_USED(rdip))
194750200e77SFrank Van Der Linden 		ret = iommulib_nexdma_freehdl(dip, rdip, handle);
194850200e77SFrank Van Der Linden 	else
194920906b23SVikram Hegde #endif
195050200e77SFrank Van Der Linden 	ret = rootnex_coredma_freehdl(dip, rdip, handle);
195150200e77SFrank Van Der Linden 
195250200e77SFrank Van Der Linden 	if (rootnex_state->r_dvma_call_list_id)
195350200e77SFrank Van Der Linden 		ddi_run_callback(&rootnex_state->r_dvma_call_list_id);
195450200e77SFrank Van Der Linden 
195550200e77SFrank Van Der Linden 	return (ret);
195620906b23SVikram Hegde }
195720906b23SVikram Hegde 
195812f080e7Smrj /*ARGSUSED*/
195912f080e7Smrj static int
rootnex_coredma_bindhdl(dev_info_t * dip,dev_info_t * rdip,ddi_dma_handle_t handle,struct ddi_dma_req * dmareq,ddi_dma_cookie_t * cookiep,uint_t * ccountp)196020906b23SVikram Hegde rootnex_coredma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
196120906b23SVikram Hegde     ddi_dma_handle_t handle, struct ddi_dma_req *dmareq,
196220906b23SVikram Hegde     ddi_dma_cookie_t *cookiep, uint_t *ccountp)
196312f080e7Smrj {
196412f080e7Smrj 	rootnex_sglinfo_t *sinfo;
196550200e77SFrank Van Der Linden 	ddi_dma_obj_t *dmao;
1966ee7536e8SFrank Van Der Linden #if defined(__amd64) && !defined(__xpv)
196750200e77SFrank Van Der Linden 	struct dvmaseg *dvs;
196850200e77SFrank Van Der Linden 	ddi_dma_cookie_t *cookie;
1969ee7536e8SFrank Van Der Linden #endif
197012f080e7Smrj 	ddi_dma_attr_t *attr;
197112f080e7Smrj 	ddi_dma_impl_t *hp;
197212f080e7Smrj 	rootnex_dma_t *dma;
197312f080e7Smrj 	int kmflag;
197412f080e7Smrj 	int e;
197550200e77SFrank Van Der Linden 	uint_t ncookies;
197612f080e7Smrj 
197712f080e7Smrj 	hp = (ddi_dma_impl_t *)handle;
197812f080e7Smrj 	dma = (rootnex_dma_t *)hp->dmai_private;
1979ee7536e8SFrank Van Der Linden 	dmao = &dma->dp_dma;
198012f080e7Smrj 	sinfo = &dma->dp_sglinfo;
198112f080e7Smrj 	attr = &hp->dmai_attr;
198212f080e7Smrj 
198396992ee7SEthindra Ramamurthy 	/* convert the sleep flags */
198494f1124eSVikram Hegde 	if (dmareq->dmar_fp == DDI_DMA_SLEEP) {
198596992ee7SEthindra Ramamurthy 		dma->dp_sleep_flags = kmflag = KM_SLEEP;
198694f1124eSVikram Hegde 	} else {
198796992ee7SEthindra Ramamurthy 		dma->dp_sleep_flags = kmflag = KM_NOSLEEP;
198894f1124eSVikram Hegde 	}
198994f1124eSVikram Hegde 
199012f080e7Smrj 	hp->dmai_rflags = dmareq->dmar_flags & DMP_DDIFLAGS;
199112f080e7Smrj 
199212f080e7Smrj 	/*
199312f080e7Smrj 	 * This is useful for debugging a driver. Not as useful in a production
199412f080e7Smrj 	 * system. The only time this will fail is if you have a driver bug.
199512f080e7Smrj 	 */
199612f080e7Smrj 	if (rootnex_bind_check_inuse) {
199712f080e7Smrj 		/*
199812f080e7Smrj 		 * No one else should ever have this lock unless someone else
199912f080e7Smrj 		 * is trying to use this handle. So contention on the lock
200012f080e7Smrj 		 * is the same as inuse being set.
200112f080e7Smrj 		 */
200212f080e7Smrj 		e = mutex_tryenter(&dma->dp_mutex);
200312f080e7Smrj 		if (e == 0) {
2004ef4ab52fSFrank Van Der Linden 			ROOTNEX_DPROF_INC(&rootnex_cnt[ROOTNEX_CNT_BIND_FAIL]);
200512f080e7Smrj 			return (DDI_DMA_INUSE);
200612f080e7Smrj 		}
200712f080e7Smrj 		if (dma->dp_inuse) {
200812f080e7Smrj 			mutex_exit(&dma->dp_mutex);
2009ef4ab52fSFrank Van Der Linden 			ROOTNEX_DPROF_INC(&rootnex_cnt[ROOTNEX_CNT_BIND_FAIL]);
201012f080e7Smrj 			return (DDI_DMA_INUSE);
201112f080e7Smrj 		}
201212f080e7Smrj 		dma->dp_inuse = B_TRUE;
201312f080e7Smrj 		mutex_exit(&dma->dp_mutex);
201412f080e7Smrj 	}
201512f080e7Smrj 
201612f080e7Smrj 	/* check the ddi_dma_attr arg to make sure it makes a little sense */
201712f080e7Smrj 	if (rootnex_bind_check_parms) {
201812f080e7Smrj 		e = rootnex_valid_bind_parms(dmareq, attr);
201912f080e7Smrj 		if (e != DDI_SUCCESS) {
2020ef4ab52fSFrank Van Der Linden 			ROOTNEX_DPROF_INC(&rootnex_cnt[ROOTNEX_CNT_BIND_FAIL]);
202112f080e7Smrj 			rootnex_clean_dmahdl(hp);
202212f080e7Smrj 			return (e);
202312f080e7Smrj 		}
202412f080e7Smrj 	}
202512f080e7Smrj 
202612f080e7Smrj 	/* save away the original bind info */
202712f080e7Smrj 	dma->dp_dma = dmareq->dmar_object;
202812f080e7Smrj 
20293a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
203050200e77SFrank Van Der Linden 	if (IOMMU_USED(rdip)) {
203150200e77SFrank Van Der Linden 		dmao = &dma->dp_dvma;
203250200e77SFrank Van Der Linden 		e = iommulib_nexdma_mapobject(dip, rdip, handle, dmareq, dmao);
203386c1f4dcSVikram Hegde 		switch (e) {
203450200e77SFrank Van Der Linden 		case DDI_SUCCESS:
203550200e77SFrank Van Der Linden 			if (sinfo->si_cancross ||
203650200e77SFrank Van Der Linden 			    dmao->dmao_obj.dvma_obj.dv_nseg != 1 ||
203750200e77SFrank Van Der Linden 			    dmao->dmao_size > sinfo->si_max_cookie_size) {
203850200e77SFrank Van Der Linden 				dma->dp_dvma_used = B_TRUE;
20393a634bfcSVikram Hegde 				break;
204050200e77SFrank Van Der Linden 			}
204150200e77SFrank Van Der Linden 			sinfo->si_sgl_size = 1;
204250200e77SFrank Van Der Linden 			hp->dmai_rflags |= DMP_NOSYNC;
204350200e77SFrank Van Der Linden 
204450200e77SFrank Van Der Linden 			dma->dp_dvma_used = B_TRUE;
204550200e77SFrank Van Der Linden 			dma->dp_need_to_free_cookie = B_FALSE;
204650200e77SFrank Van Der Linden 
204750200e77SFrank Van Der Linden 			dvs = &dmao->dmao_obj.dvma_obj.dv_seg[0];
204850200e77SFrank Van Der Linden 			cookie = hp->dmai_cookie = dma->dp_cookies =
204950200e77SFrank Van Der Linden 			    (ddi_dma_cookie_t *)dma->dp_prealloc_buffer;
205050200e77SFrank Van Der Linden 			cookie->dmac_laddress = dvs->dvs_start +
205150200e77SFrank Van Der Linden 			    dmao->dmao_obj.dvma_obj.dv_off;
205250200e77SFrank Van Der Linden 			cookie->dmac_size = dvs->dvs_len;
205350200e77SFrank Van Der Linden 			cookie->dmac_type = 0;
205450200e77SFrank Van Der Linden 
205550200e77SFrank Van Der Linden 			ROOTNEX_DPROBE1(rootnex__bind__dvmafast, dev_info_t *,
205650200e77SFrank Van Der Linden 			    rdip);
205750200e77SFrank Van Der Linden 			goto fast;
205850200e77SFrank Van Der Linden 		case DDI_ENOTSUP:
205950200e77SFrank Van Der Linden 			break;
206086c1f4dcSVikram Hegde 		default:
206186c1f4dcSVikram Hegde 			rootnex_clean_dmahdl(hp);
20623a634bfcSVikram Hegde 			return (e);
206386c1f4dcSVikram Hegde 		}
206450200e77SFrank Van Der Linden 	}
206520906b23SVikram Hegde #endif
206686c1f4dcSVikram Hegde 
206712f080e7Smrj 	/*
206850200e77SFrank Van Der Linden 	 * Figure out a rough estimate of what maximum number of pages
206950200e77SFrank Van Der Linden 	 * this buffer could use (a high estimate of course).
207012f080e7Smrj 	 */
207112f080e7Smrj 	sinfo->si_max_pages = mmu_btopr(dma->dp_dma.dmao_size) + 1;
207212f080e7Smrj 
207350200e77SFrank Van Der Linden 	if (dma->dp_dvma_used) {
207450200e77SFrank Van Der Linden 		/*
207550200e77SFrank Van Der Linden 		 * The number of physical pages is the worst case.
207650200e77SFrank Van Der Linden 		 *
207750200e77SFrank Van Der Linden 		 * For DVMA, the worst case is the length divided
207850200e77SFrank Van Der Linden 		 * by the maximum cookie length, plus 1. Add to that
207950200e77SFrank Van Der Linden 		 * the number of segment boundaries potentially crossed, and
208050200e77SFrank Van Der Linden 		 * the additional number of DVMA segments that was returned.
208150200e77SFrank Van Der Linden 		 *
208250200e77SFrank Van Der Linden 		 * In the normal case, for modern devices, si_cancross will
208350200e77SFrank Van Der Linden 		 * be false, and dv_nseg will be 1, and the fast path will
208450200e77SFrank Van Der Linden 		 * have been taken above.
208550200e77SFrank Van Der Linden 		 */
208650200e77SFrank Van Der Linden 		ncookies = (dma->dp_dma.dmao_size / sinfo->si_max_cookie_size)
208750200e77SFrank Van Der Linden 		    + 1;
208850200e77SFrank Van Der Linden 		if (sinfo->si_cancross)
208950200e77SFrank Van Der Linden 			ncookies +=
209050200e77SFrank Van Der Linden 			    (dma->dp_dma.dmao_size / attr->dma_attr_seg) + 1;
209150200e77SFrank Van Der Linden 		ncookies += (dmao->dmao_obj.dvma_obj.dv_nseg - 1);
209250200e77SFrank Van Der Linden 
209350200e77SFrank Van Der Linden 		sinfo->si_max_pages = MIN(sinfo->si_max_pages, ncookies);
209450200e77SFrank Van Der Linden 	}
209550200e77SFrank Van Der Linden 
209612f080e7Smrj 	/*
209712f080e7Smrj 	 * We'll use the pre-allocated cookies for any bind that will *always*
209812f080e7Smrj 	 * fit (more important to be consistent, we don't want to create
209912f080e7Smrj 	 * additional degenerate cases).
210012f080e7Smrj 	 */
210112f080e7Smrj 	if (sinfo->si_max_pages <= rootnex_state->r_prealloc_cookies) {
210212f080e7Smrj 		dma->dp_cookies = (ddi_dma_cookie_t *)dma->dp_prealloc_buffer;
210312f080e7Smrj 		dma->dp_need_to_free_cookie = B_FALSE;
2104ef4ab52fSFrank Van Der Linden 		ROOTNEX_DPROBE2(rootnex__bind__prealloc, dev_info_t *, rdip,
210512f080e7Smrj 		    uint_t, sinfo->si_max_pages);
210612f080e7Smrj 
210712f080e7Smrj 	/*
210812f080e7Smrj 	 * For anything larger than that, we'll go ahead and allocate the
210912f080e7Smrj 	 * maximum number of pages we expect to see. Hopefuly, we won't be
211012f080e7Smrj 	 * seeing this path in the fast path for high performance devices very
211112f080e7Smrj 	 * frequently.
211212f080e7Smrj 	 *
211312f080e7Smrj 	 * a ddi bind interface that allowed the driver to provide storage to
211412f080e7Smrj 	 * the bind interface would speed this case up.
211512f080e7Smrj 	 */
211612f080e7Smrj 	} else {
211712f080e7Smrj 		/*
211812f080e7Smrj 		 * Save away how much memory we allocated. If we're doing a
211912f080e7Smrj 		 * nosleep, the alloc could fail...
212012f080e7Smrj 		 */
212112f080e7Smrj 		dma->dp_cookie_size = sinfo->si_max_pages *
212212f080e7Smrj 		    sizeof (ddi_dma_cookie_t);
212312f080e7Smrj 		dma->dp_cookies = kmem_alloc(dma->dp_cookie_size, kmflag);
212412f080e7Smrj 		if (dma->dp_cookies == NULL) {
2125ef4ab52fSFrank Van Der Linden 			ROOTNEX_DPROF_INC(&rootnex_cnt[ROOTNEX_CNT_BIND_FAIL]);
212612f080e7Smrj 			rootnex_clean_dmahdl(hp);
212712f080e7Smrj 			return (DDI_DMA_NORESOURCES);
212812f080e7Smrj 		}
212912f080e7Smrj 		dma->dp_need_to_free_cookie = B_TRUE;
2130ef4ab52fSFrank Van Der Linden 		ROOTNEX_DPROBE2(rootnex__bind__alloc, dev_info_t *, rdip,
2131ef4ab52fSFrank Van Der Linden 		    uint_t, sinfo->si_max_pages);
213212f080e7Smrj 	}
213312f080e7Smrj 	hp->dmai_cookie = dma->dp_cookies;
213412f080e7Smrj 
213512f080e7Smrj 	/*
213612f080e7Smrj 	 * Get the real sgl. rootnex_get_sgl will fill in cookie array while
21373a634bfcSVikram Hegde 	 * looking at the constraints in the dma structure. It will then put
21383a634bfcSVikram Hegde 	 * some additional state about the sgl in the dma struct (i.e. is
21393a634bfcSVikram Hegde 	 * the sgl clean, or do we need to do some munging; how many pages
21403a634bfcSVikram Hegde 	 * need to be copied, etc.)
214112f080e7Smrj 	 */
214250200e77SFrank Van Der Linden 	if (dma->dp_dvma_used)
214350200e77SFrank Van Der Linden 		rootnex_dvma_get_sgl(dmao, dma->dp_cookies, &dma->dp_sglinfo);
214450200e77SFrank Van Der Linden 	else
214550200e77SFrank Van Der Linden 		rootnex_get_sgl(dmao, dma->dp_cookies, &dma->dp_sglinfo);
214612f080e7Smrj 
21473a634bfcSVikram Hegde out:
214886c1f4dcSVikram Hegde 	ASSERT(sinfo->si_sgl_size <= sinfo->si_max_pages);
214912f080e7Smrj 	/* if we don't need a copy buffer, we don't need to sync */
215012f080e7Smrj 	if (sinfo->si_copybuf_req == 0) {
215112f080e7Smrj 		hp->dmai_rflags |= DMP_NOSYNC;
215212f080e7Smrj 	}
215312f080e7Smrj 
215412f080e7Smrj 	/*
215512f080e7Smrj 	 * if we don't need the copybuf and we don't need to do a partial,  we
215612f080e7Smrj 	 * hit the fast path. All the high performance devices should be trying
215712f080e7Smrj 	 * to hit this path. To hit this path, a device should be able to reach
215812f080e7Smrj 	 * all of memory, shouldn't try to bind more than it can transfer, and
215912f080e7Smrj 	 * the buffer shouldn't require more cookies than the driver/device can
216012f080e7Smrj 	 * handle [sgllen]).
2161877f730dSGarrett D'Amore 	 *
2162877f730dSGarrett D'Amore 	 * Note that negative values of dma_attr_sgllen are supposed
2163877f730dSGarrett D'Amore 	 * to mean unlimited, but we just cast them to mean a
2164877f730dSGarrett D'Amore 	 * "ridiculous large limit".  This saves some extra checks on
2165877f730dSGarrett D'Amore 	 * hot paths.
216612f080e7Smrj 	 */
216712f080e7Smrj 	if ((sinfo->si_copybuf_req == 0) &&
2168877f730dSGarrett D'Amore 	    (sinfo->si_sgl_size <= (unsigned)attr->dma_attr_sgllen) &&
216950200e77SFrank Van Der Linden 	    (dmao->dmao_size < dma->dp_maxxfer)) {
217050200e77SFrank Van Der Linden fast:
217112f080e7Smrj 		/*
217285c8e0e8Sstephh 		 * If the driver supports FMA, insert the handle in the FMA DMA
217385c8e0e8Sstephh 		 * handle cache.
217485c8e0e8Sstephh 		 */
2175567c0b92SStephen Hanson 		if (attr->dma_attr_flags & DDI_DMA_FLAGERR)
217685c8e0e8Sstephh 			hp->dmai_error.err_cf = rootnex_dma_check;
217785c8e0e8Sstephh 
217885c8e0e8Sstephh 		/*
217912f080e7Smrj 		 * copy out the first cookie and ccountp, set the cookie
218012f080e7Smrj 		 * pointer to the second cookie. The first cookie is passed
218112f080e7Smrj 		 * back on the stack. Additional cookies are accessed via
218212f080e7Smrj 		 * ddi_dma_nextcookie()
218312f080e7Smrj 		 */
218412f080e7Smrj 		*cookiep = dma->dp_cookies[0];
218512f080e7Smrj 		*ccountp = sinfo->si_sgl_size;
218612f080e7Smrj 		hp->dmai_cookie++;
218712f080e7Smrj 		hp->dmai_rflags &= ~DDI_DMA_PARTIAL;
2188ef4ab52fSFrank Van Der Linden 		ROOTNEX_DPROF_INC(&rootnex_cnt[ROOTNEX_CNT_ACTIVE_BINDS]);
218950200e77SFrank Van Der Linden 		ROOTNEX_DPROBE4(rootnex__bind__fast, dev_info_t *, rdip,
21903a634bfcSVikram Hegde 		    uint64_t, rootnex_cnt[ROOTNEX_CNT_ACTIVE_BINDS],
219150200e77SFrank Van Der Linden 		    uint_t, dmao->dmao_size, uint_t, *ccountp);
21923a634bfcSVikram Hegde 
21933a634bfcSVikram Hegde 
219412f080e7Smrj 		return (DDI_DMA_MAPPED);
219512f080e7Smrj 	}
219612f080e7Smrj 
219712f080e7Smrj 	/*
219812f080e7Smrj 	 * go to the slow path, we may need to alloc more memory, create
219912f080e7Smrj 	 * multiple windows, and munge up a sgl to make the device happy.
220012f080e7Smrj 	 */
220150200e77SFrank Van Der Linden 
220250200e77SFrank Van Der Linden 	/*
220350200e77SFrank Van Der Linden 	 * With the IOMMU mapobject method used, we should never hit
220450200e77SFrank Van Der Linden 	 * the slow path. If we do, something is seriously wrong.
220550200e77SFrank Van Der Linden 	 * Clean up and return an error.
220650200e77SFrank Van Der Linden 	 */
220750200e77SFrank Van Der Linden 
22085800d066SAlbert Lee #if defined(__amd64) && !defined(__xpv)
22095800d066SAlbert Lee 
221050200e77SFrank Van Der Linden 	if (dma->dp_dvma_used) {
221150200e77SFrank Van Der Linden 		(void) iommulib_nexdma_unmapobject(dip, rdip, handle,
221250200e77SFrank Van Der Linden 		    &dma->dp_dvma);
221350200e77SFrank Van Der Linden 		e = DDI_DMA_NOMAPPING;
221450200e77SFrank Van Der Linden 	} else {
22155800d066SAlbert Lee #endif
221650200e77SFrank Van Der Linden 		e = rootnex_bind_slowpath(hp, dmareq, dma, attr, &dma->dp_dma,
221750200e77SFrank Van Der Linden 		    kmflag);
22185800d066SAlbert Lee #if defined(__amd64) && !defined(__xpv)
221950200e77SFrank Van Der Linden 	}
22205800d066SAlbert Lee #endif
222112f080e7Smrj 	if ((e != DDI_DMA_MAPPED) && (e != DDI_DMA_PARTIAL_MAP)) {
222212f080e7Smrj 		if (dma->dp_need_to_free_cookie) {
222312f080e7Smrj 			kmem_free(dma->dp_cookies, dma->dp_cookie_size);
222412f080e7Smrj 		}
2225ef4ab52fSFrank Van Der Linden 		ROOTNEX_DPROF_INC(&rootnex_cnt[ROOTNEX_CNT_BIND_FAIL]);
222612f080e7Smrj 		rootnex_clean_dmahdl(hp); /* must be after free cookie */
222712f080e7Smrj 		return (e);
222812f080e7Smrj 	}
222912f080e7Smrj 
223085c8e0e8Sstephh 	/*
223185c8e0e8Sstephh 	 * If the driver supports FMA, insert the handle in the FMA DMA handle
223285c8e0e8Sstephh 	 * cache.
223385c8e0e8Sstephh 	 */
2234567c0b92SStephen Hanson 	if (attr->dma_attr_flags & DDI_DMA_FLAGERR)
223585c8e0e8Sstephh 		hp->dmai_error.err_cf = rootnex_dma_check;
223685c8e0e8Sstephh 
223712f080e7Smrj 	/* if the first window uses the copy buffer, sync it for the device */
223812f080e7Smrj 	if ((dma->dp_window[dma->dp_current_win].wd_dosync) &&
223912f080e7Smrj 	    (hp->dmai_rflags & DDI_DMA_WRITE)) {
224094f1124eSVikram Hegde 		(void) rootnex_coredma_sync(dip, rdip, handle, 0, 0,
224112f080e7Smrj 		    DDI_DMA_SYNC_FORDEV);
224212f080e7Smrj 	}
224312f080e7Smrj 
224412f080e7Smrj 	/*
224512f080e7Smrj 	 * copy out the first cookie and ccountp, set the cookie pointer to the
224612f080e7Smrj 	 * second cookie. Make sure the partial flag is set/cleared correctly.
224712f080e7Smrj 	 * If we have a partial map (i.e. multiple windows), the number of
224812f080e7Smrj 	 * cookies we return is the number of cookies in the first window.
224912f080e7Smrj 	 */
225012f080e7Smrj 	if (e == DDI_DMA_MAPPED) {
225112f080e7Smrj 		hp->dmai_rflags &= ~DDI_DMA_PARTIAL;
225212f080e7Smrj 		*ccountp = sinfo->si_sgl_size;
22533a634bfcSVikram Hegde 		hp->dmai_nwin = 1;
225412f080e7Smrj 	} else {
225512f080e7Smrj 		hp->dmai_rflags |= DDI_DMA_PARTIAL;
225612f080e7Smrj 		*ccountp = dma->dp_window[dma->dp_current_win].wd_cookie_cnt;
225712f080e7Smrj 		ASSERT(hp->dmai_nwin <= dma->dp_max_win);
225812f080e7Smrj 	}
225912f080e7Smrj 	*cookiep = dma->dp_cookies[0];
226012f080e7Smrj 	hp->dmai_cookie++;
226112f080e7Smrj 
22620b7ba611SMark Johnson 	ROOTNEX_DPROF_INC(&rootnex_cnt[ROOTNEX_CNT_ACTIVE_BINDS]);
226350200e77SFrank Van Der Linden 	ROOTNEX_DPROBE4(rootnex__bind__slow, dev_info_t *, rdip, uint64_t,
226412f080e7Smrj 	    rootnex_cnt[ROOTNEX_CNT_ACTIVE_BINDS], uint_t,
226550200e77SFrank Van Der Linden 	    dmao->dmao_size, uint_t, *ccountp);
226612f080e7Smrj 	return (e);
226712f080e7Smrj }
226812f080e7Smrj 
226912f080e7Smrj /*
227020906b23SVikram Hegde  * rootnex_dma_bindhdl()
227120906b23SVikram Hegde  *    called from ddi_dma_addr_bind_handle() and ddi_dma_buf_bind_handle().
227212f080e7Smrj  */
227320906b23SVikram Hegde static int
rootnex_dma_bindhdl(dev_info_t * dip,dev_info_t * rdip,ddi_dma_handle_t handle,struct ddi_dma_req * dmareq,ddi_dma_cookie_t * cookiep,uint_t * ccountp)227420906b23SVikram Hegde rootnex_dma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
227520906b23SVikram Hegde     ddi_dma_handle_t handle, struct ddi_dma_req *dmareq,
227620906b23SVikram Hegde     ddi_dma_cookie_t *cookiep, uint_t *ccountp)
227720906b23SVikram Hegde {
227850200e77SFrank Van Der Linden 	int ret;
22793a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
228050200e77SFrank Van Der Linden 	if (IOMMU_USED(rdip))
228150200e77SFrank Van Der Linden 		ret = iommulib_nexdma_bindhdl(dip, rdip, handle, dmareq,
228250200e77SFrank Van Der Linden 		    cookiep, ccountp);
228350200e77SFrank Van Der Linden 	else
228420906b23SVikram Hegde #endif
228550200e77SFrank Van Der Linden 	ret = rootnex_coredma_bindhdl(dip, rdip, handle, dmareq,
228650200e77SFrank Van Der Linden 	    cookiep, ccountp);
228750200e77SFrank Van Der Linden 
228850200e77SFrank Van Der Linden 	if (ret == DDI_DMA_NORESOURCES && dmareq->dmar_fp != DDI_DMA_DONTWAIT) {
228950200e77SFrank Van Der Linden 		ddi_set_callback(dmareq->dmar_fp, dmareq->dmar_arg,
229050200e77SFrank Van Der Linden 		    &rootnex_state->r_dvma_call_list_id);
229150200e77SFrank Van Der Linden 	}
229250200e77SFrank Van Der Linden 
229350200e77SFrank Van Der Linden 	return (ret);
229420906b23SVikram Hegde }
229520906b23SVikram Hegde 
22963a634bfcSVikram Hegde 
22973a634bfcSVikram Hegde 
229812f080e7Smrj /*ARGSUSED*/
229912f080e7Smrj static int
rootnex_coredma_unbindhdl(dev_info_t * dip,dev_info_t * rdip,ddi_dma_handle_t handle)230020906b23SVikram Hegde rootnex_coredma_unbindhdl(dev_info_t *dip, dev_info_t *rdip,
230112f080e7Smrj     ddi_dma_handle_t handle)
230212f080e7Smrj {
230312f080e7Smrj 	ddi_dma_impl_t *hp;
230412f080e7Smrj 	rootnex_dma_t *dma;
230512f080e7Smrj 	int e;
230612f080e7Smrj 
230712f080e7Smrj 	hp = (ddi_dma_impl_t *)handle;
230812f080e7Smrj 	dma = (rootnex_dma_t *)hp->dmai_private;
230912f080e7Smrj 
231012f080e7Smrj 	/* make sure the buffer wasn't free'd before calling unbind */
231112f080e7Smrj 	if (rootnex_unbind_verify_buffer) {
231212f080e7Smrj 		e = rootnex_verify_buffer(dma);
231312f080e7Smrj 		if (e != DDI_SUCCESS) {
231412f080e7Smrj 			ASSERT(0);
231512f080e7Smrj 			return (DDI_FAILURE);
231612f080e7Smrj 		}
231712f080e7Smrj 	}
231812f080e7Smrj 
231912f080e7Smrj 	/* sync the current window before unbinding the buffer */
232012f080e7Smrj 	if (dma->dp_window && dma->dp_window[dma->dp_current_win].wd_dosync &&
232112f080e7Smrj 	    (hp->dmai_rflags & DDI_DMA_READ)) {
232294f1124eSVikram Hegde 		(void) rootnex_coredma_sync(dip, rdip, handle, 0, 0,
232312f080e7Smrj 		    DDI_DMA_SYNC_FORCPU);
232412f080e7Smrj 	}
232512f080e7Smrj 
232612f080e7Smrj 	/*
232712f080e7Smrj 	 * cleanup and copy buffer or window state. if we didn't use the copy
232812f080e7Smrj 	 * buffer or windows, there won't be much to do :-)
232912f080e7Smrj 	 */
233012f080e7Smrj 	rootnex_teardown_copybuf(dma);
233112f080e7Smrj 	rootnex_teardown_windows(dma);
233212f080e7Smrj 
23335800d066SAlbert Lee #if defined(__amd64) && !defined(__xpv)
233450200e77SFrank Van Der Linden 	if (IOMMU_USED(rdip))
233550200e77SFrank Van Der Linden 		(void) iommulib_nexdma_unmapobject(dip, rdip, handle,
233650200e77SFrank Van Der Linden 		    &dma->dp_dvma);
23375800d066SAlbert Lee #endif
233886c1f4dcSVikram Hegde 
233986c1f4dcSVikram Hegde 	/*
234012f080e7Smrj 	 * If we had to allocate space to for the worse case sgl (it didn't
234112f080e7Smrj 	 * fit into our pre-allocate buffer), free that up now
234212f080e7Smrj 	 */
234312f080e7Smrj 	if (dma->dp_need_to_free_cookie) {
234412f080e7Smrj 		kmem_free(dma->dp_cookies, dma->dp_cookie_size);
234512f080e7Smrj 	}
234612f080e7Smrj 
234712f080e7Smrj 	/*
234812f080e7Smrj 	 * clean up the handle so it's ready for the next bind (i.e. if the
234912f080e7Smrj 	 * handle is reused).
235012f080e7Smrj 	 */
235112f080e7Smrj 	rootnex_clean_dmahdl(hp);
2352567c0b92SStephen Hanson 	hp->dmai_error.err_cf = NULL;
235312f080e7Smrj 
23540b7ba611SMark Johnson 	ROOTNEX_DPROF_DEC(&rootnex_cnt[ROOTNEX_CNT_ACTIVE_BINDS]);
23550b7ba611SMark Johnson 	ROOTNEX_DPROBE1(rootnex__unbind, uint64_t,
235612f080e7Smrj 	    rootnex_cnt[ROOTNEX_CNT_ACTIVE_BINDS]);
235712f080e7Smrj 
235812f080e7Smrj 	return (DDI_SUCCESS);
235912f080e7Smrj }
236012f080e7Smrj 
236120906b23SVikram Hegde /*
236220906b23SVikram Hegde  * rootnex_dma_unbindhdl()
236320906b23SVikram Hegde  *    called from ddi_dma_unbind_handle()
236420906b23SVikram Hegde  */
236520906b23SVikram Hegde /*ARGSUSED*/
236620906b23SVikram Hegde static int
rootnex_dma_unbindhdl(dev_info_t * dip,dev_info_t * rdip,ddi_dma_handle_t handle)236720906b23SVikram Hegde rootnex_dma_unbindhdl(dev_info_t *dip, dev_info_t *rdip,
236820906b23SVikram Hegde     ddi_dma_handle_t handle)
236920906b23SVikram Hegde {
237050200e77SFrank Van Der Linden 	int ret;
237150200e77SFrank Van Der Linden 
23723a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
237350200e77SFrank Van Der Linden 	if (IOMMU_USED(rdip))
237450200e77SFrank Van Der Linden 		ret = iommulib_nexdma_unbindhdl(dip, rdip, handle);
237550200e77SFrank Van Der Linden 	else
237620906b23SVikram Hegde #endif
237750200e77SFrank Van Der Linden 	ret = rootnex_coredma_unbindhdl(dip, rdip, handle);
237850200e77SFrank Van Der Linden 
237950200e77SFrank Van Der Linden 	if (rootnex_state->r_dvma_call_list_id)
238050200e77SFrank Van Der Linden 		ddi_run_callback(&rootnex_state->r_dvma_call_list_id);
238150200e77SFrank Van Der Linden 
238250200e77SFrank Van Der Linden 	return (ret);
238320906b23SVikram Hegde }
238420906b23SVikram Hegde 
23853a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
238694f1124eSVikram Hegde 
238794f1124eSVikram Hegde static int
rootnex_coredma_get_sleep_flags(ddi_dma_handle_t handle)238894f1124eSVikram Hegde rootnex_coredma_get_sleep_flags(ddi_dma_handle_t handle)
238994f1124eSVikram Hegde {
239094f1124eSVikram Hegde 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
239194f1124eSVikram Hegde 	rootnex_dma_t *dma = (rootnex_dma_t *)hp->dmai_private;
239294f1124eSVikram Hegde 
239394f1124eSVikram Hegde 	if (dma->dp_sleep_flags != KM_SLEEP &&
239494f1124eSVikram Hegde 	    dma->dp_sleep_flags != KM_NOSLEEP)
239594f1124eSVikram Hegde 		cmn_err(CE_PANIC, "kmem sleep flags not set in DMA handle");
239694f1124eSVikram Hegde 	return (dma->dp_sleep_flags);
239794f1124eSVikram Hegde }
239820906b23SVikram Hegde /*ARGSUSED*/
239920906b23SVikram Hegde static void
rootnex_coredma_reset_cookies(dev_info_t * dip,ddi_dma_handle_t handle)240020906b23SVikram Hegde rootnex_coredma_reset_cookies(dev_info_t *dip, ddi_dma_handle_t handle)
240120906b23SVikram Hegde {
240220906b23SVikram Hegde 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
240320906b23SVikram Hegde 	rootnex_dma_t *dma = (rootnex_dma_t *)hp->dmai_private;
240494f1124eSVikram Hegde 	rootnex_window_t *window;
240520906b23SVikram Hegde 
240694f1124eSVikram Hegde 	if (dma->dp_window) {
240794f1124eSVikram Hegde 		window = &dma->dp_window[dma->dp_current_win];
240894f1124eSVikram Hegde 		hp->dmai_cookie = window->wd_first_cookie;
240994f1124eSVikram Hegde 	} else {
241094f1124eSVikram Hegde 		hp->dmai_cookie = dma->dp_cookies;
241194f1124eSVikram Hegde 	}
241220906b23SVikram Hegde 	hp->dmai_cookie++;
241320906b23SVikram Hegde }
241420906b23SVikram Hegde 
241520906b23SVikram Hegde /*ARGSUSED*/
241620906b23SVikram Hegde static int
rootnex_coredma_get_cookies(dev_info_t * dip,ddi_dma_handle_t handle,ddi_dma_cookie_t ** cookiepp,uint_t * ccountp)241720906b23SVikram Hegde rootnex_coredma_get_cookies(dev_info_t *dip, ddi_dma_handle_t handle,
241894f1124eSVikram Hegde     ddi_dma_cookie_t **cookiepp, uint_t *ccountp)
241920906b23SVikram Hegde {
242094f1124eSVikram Hegde 	int i;
242194f1124eSVikram Hegde 	int km_flags;
242220906b23SVikram Hegde 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
242320906b23SVikram Hegde 	rootnex_dma_t *dma = (rootnex_dma_t *)hp->dmai_private;
242494f1124eSVikram Hegde 	rootnex_window_t *window;
242594f1124eSVikram Hegde 	ddi_dma_cookie_t *cp;
242694f1124eSVikram Hegde 	ddi_dma_cookie_t *cookie;
242720906b23SVikram Hegde 
242894f1124eSVikram Hegde 	ASSERT(*cookiepp == NULL);
242994f1124eSVikram Hegde 	ASSERT(*ccountp == 0);
243020906b23SVikram Hegde 
243194f1124eSVikram Hegde 	if (dma->dp_window) {
243294f1124eSVikram Hegde 		window = &dma->dp_window[dma->dp_current_win];
243394f1124eSVikram Hegde 		cp = window->wd_first_cookie;
243494f1124eSVikram Hegde 		*ccountp = window->wd_cookie_cnt;
243520906b23SVikram Hegde 	} else {
243694f1124eSVikram Hegde 		cp = dma->dp_cookies;
243720906b23SVikram Hegde 		*ccountp = dma->dp_sglinfo.si_sgl_size;
243820906b23SVikram Hegde 	}
243920906b23SVikram Hegde 
244094f1124eSVikram Hegde 	km_flags = rootnex_coredma_get_sleep_flags(handle);
244194f1124eSVikram Hegde 	cookie = kmem_zalloc(sizeof (ddi_dma_cookie_t) * (*ccountp), km_flags);
244294f1124eSVikram Hegde 	if (cookie == NULL) {
244394f1124eSVikram Hegde 		return (DDI_DMA_NORESOURCES);
244494f1124eSVikram Hegde 	}
244594f1124eSVikram Hegde 
244694f1124eSVikram Hegde 	for (i = 0; i < *ccountp; i++) {
244794f1124eSVikram Hegde 		cookie[i].dmac_notused = cp[i].dmac_notused;
244894f1124eSVikram Hegde 		cookie[i].dmac_type = cp[i].dmac_type;
244994f1124eSVikram Hegde 		cookie[i].dmac_address = cp[i].dmac_address;
245094f1124eSVikram Hegde 		cookie[i].dmac_size = cp[i].dmac_size;
245194f1124eSVikram Hegde 	}
245294f1124eSVikram Hegde 
245394f1124eSVikram Hegde 	*cookiepp = cookie;
245420906b23SVikram Hegde 
245520906b23SVikram Hegde 	return (DDI_SUCCESS);
245620906b23SVikram Hegde }
245794f1124eSVikram Hegde 
245894f1124eSVikram Hegde /*ARGSUSED*/
245994f1124eSVikram Hegde static int
rootnex_coredma_set_cookies(dev_info_t * dip,ddi_dma_handle_t handle,ddi_dma_cookie_t * cookiep,uint_t ccount)246094f1124eSVikram Hegde rootnex_coredma_set_cookies(dev_info_t *dip, ddi_dma_handle_t handle,
246194f1124eSVikram Hegde     ddi_dma_cookie_t *cookiep, uint_t ccount)
246294f1124eSVikram Hegde {
246394f1124eSVikram Hegde 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
246494f1124eSVikram Hegde 	rootnex_dma_t *dma = (rootnex_dma_t *)hp->dmai_private;
246594f1124eSVikram Hegde 	rootnex_window_t *window;
246694f1124eSVikram Hegde 	ddi_dma_cookie_t *cur_cookiep;
246794f1124eSVikram Hegde 
246894f1124eSVikram Hegde 	ASSERT(cookiep);
246994f1124eSVikram Hegde 	ASSERT(ccount != 0);
247094f1124eSVikram Hegde 	ASSERT(dma->dp_need_to_switch_cookies == B_FALSE);
247194f1124eSVikram Hegde 
247294f1124eSVikram Hegde 	if (dma->dp_window) {
247394f1124eSVikram Hegde 		window = &dma->dp_window[dma->dp_current_win];
247494f1124eSVikram Hegde 		dma->dp_saved_cookies = window->wd_first_cookie;
247594f1124eSVikram Hegde 		window->wd_first_cookie = cookiep;
247694f1124eSVikram Hegde 		ASSERT(ccount == window->wd_cookie_cnt);
247794f1124eSVikram Hegde 		cur_cookiep = (hp->dmai_cookie - dma->dp_saved_cookies)
247894f1124eSVikram Hegde 		    + window->wd_first_cookie;
247994f1124eSVikram Hegde 	} else {
248094f1124eSVikram Hegde 		dma->dp_saved_cookies = dma->dp_cookies;
248194f1124eSVikram Hegde 		dma->dp_cookies = cookiep;
248294f1124eSVikram Hegde 		ASSERT(ccount == dma->dp_sglinfo.si_sgl_size);
248394f1124eSVikram Hegde 		cur_cookiep = (hp->dmai_cookie - dma->dp_saved_cookies)
248494f1124eSVikram Hegde 		    + dma->dp_cookies;
248594f1124eSVikram Hegde 	}
248694f1124eSVikram Hegde 
248794f1124eSVikram Hegde 	dma->dp_need_to_switch_cookies = B_TRUE;
248894f1124eSVikram Hegde 	hp->dmai_cookie = cur_cookiep;
248994f1124eSVikram Hegde 
249094f1124eSVikram Hegde 	return (DDI_SUCCESS);
249194f1124eSVikram Hegde }
249294f1124eSVikram Hegde 
249394f1124eSVikram Hegde /*ARGSUSED*/
249494f1124eSVikram Hegde static int
rootnex_coredma_clear_cookies(dev_info_t * dip,ddi_dma_handle_t handle)249594f1124eSVikram Hegde rootnex_coredma_clear_cookies(dev_info_t *dip, ddi_dma_handle_t handle)
249694f1124eSVikram Hegde {
249794f1124eSVikram Hegde 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
249894f1124eSVikram Hegde 	rootnex_dma_t *dma = (rootnex_dma_t *)hp->dmai_private;
249994f1124eSVikram Hegde 	rootnex_window_t *window;
250094f1124eSVikram Hegde 	ddi_dma_cookie_t *cur_cookiep;
250194f1124eSVikram Hegde 	ddi_dma_cookie_t *cookie_array;
250294f1124eSVikram Hegde 	uint_t ccount;
250394f1124eSVikram Hegde 
250494f1124eSVikram Hegde 	/* check if cookies have not been switched */
250594f1124eSVikram Hegde 	if (dma->dp_need_to_switch_cookies == B_FALSE)
250694f1124eSVikram Hegde 		return (DDI_SUCCESS);
250794f1124eSVikram Hegde 
250894f1124eSVikram Hegde 	ASSERT(dma->dp_saved_cookies);
250994f1124eSVikram Hegde 
251094f1124eSVikram Hegde 	if (dma->dp_window) {
251194f1124eSVikram Hegde 		window = &dma->dp_window[dma->dp_current_win];
251294f1124eSVikram Hegde 		cookie_array = window->wd_first_cookie;
251394f1124eSVikram Hegde 		window->wd_first_cookie = dma->dp_saved_cookies;
251494f1124eSVikram Hegde 		dma->dp_saved_cookies = NULL;
251594f1124eSVikram Hegde 		ccount = window->wd_cookie_cnt;
251694f1124eSVikram Hegde 		cur_cookiep = (hp->dmai_cookie - cookie_array)
251794f1124eSVikram Hegde 		    + window->wd_first_cookie;
251894f1124eSVikram Hegde 	} else {
251994f1124eSVikram Hegde 		cookie_array = dma->dp_cookies;
252094f1124eSVikram Hegde 		dma->dp_cookies = dma->dp_saved_cookies;
252194f1124eSVikram Hegde 		dma->dp_saved_cookies = NULL;
252294f1124eSVikram Hegde 		ccount = dma->dp_sglinfo.si_sgl_size;
252394f1124eSVikram Hegde 		cur_cookiep = (hp->dmai_cookie - cookie_array)
252494f1124eSVikram Hegde 		    + dma->dp_cookies;
252594f1124eSVikram Hegde 	}
252694f1124eSVikram Hegde 
252794f1124eSVikram Hegde 	kmem_free(cookie_array, sizeof (ddi_dma_cookie_t) * ccount);
252894f1124eSVikram Hegde 
252994f1124eSVikram Hegde 	hp->dmai_cookie = cur_cookiep;
253094f1124eSVikram Hegde 
253194f1124eSVikram Hegde 	dma->dp_need_to_switch_cookies = B_FALSE;
253294f1124eSVikram Hegde 
253394f1124eSVikram Hegde 	return (DDI_SUCCESS);
253494f1124eSVikram Hegde }
253594f1124eSVikram Hegde 
25365dfdb46bSVikram Hegde #endif
253712f080e7Smrj 
253850200e77SFrank Van Der Linden static struct as *
rootnex_get_as(ddi_dma_obj_t * dmao)253950200e77SFrank Van Der Linden rootnex_get_as(ddi_dma_obj_t *dmao)
254050200e77SFrank Van Der Linden {
254150200e77SFrank Van Der Linden 	struct as *asp;
254250200e77SFrank Van Der Linden 
254350200e77SFrank Van Der Linden 	switch (dmao->dmao_type) {
254450200e77SFrank Van Der Linden 	case DMA_OTYP_VADDR:
254550200e77SFrank Van Der Linden 	case DMA_OTYP_BUFVADDR:
254650200e77SFrank Van Der Linden 		asp = dmao->dmao_obj.virt_obj.v_as;
254750200e77SFrank Van Der Linden 		if (asp == NULL)
254850200e77SFrank Van Der Linden 			asp = &kas;
254950200e77SFrank Van Der Linden 		break;
255050200e77SFrank Van Der Linden 	default:
255150200e77SFrank Van Der Linden 		asp = NULL;
255250200e77SFrank Van Der Linden 		break;
255350200e77SFrank Van Der Linden 	}
255450200e77SFrank Van Der Linden 	return (asp);
255550200e77SFrank Van Der Linden }
255650200e77SFrank Van Der Linden 
255712f080e7Smrj /*
255812f080e7Smrj  * rootnex_verify_buffer()
255912f080e7Smrj  *   verify buffer wasn't free'd
256012f080e7Smrj  */
256112f080e7Smrj static int
rootnex_verify_buffer(rootnex_dma_t * dma)256212f080e7Smrj rootnex_verify_buffer(rootnex_dma_t *dma)
256312f080e7Smrj {
256412f080e7Smrj 	page_t **pplist;
256512f080e7Smrj 	caddr_t vaddr;
256612f080e7Smrj 	uint_t pcnt;
256712f080e7Smrj 	uint_t poff;
256812f080e7Smrj 	page_t *pp;
256900d0963fSdilpreet 	char b;
257012f080e7Smrj 	int i;
257112f080e7Smrj 
257212f080e7Smrj 	/* Figure out how many pages this buffer occupies */
257312f080e7Smrj 	if (dma->dp_dma.dmao_type == DMA_OTYP_PAGES) {
257412f080e7Smrj 		poff = dma->dp_dma.dmao_obj.pp_obj.pp_offset & MMU_PAGEOFFSET;
257512f080e7Smrj 	} else {
257612f080e7Smrj 		vaddr = dma->dp_dma.dmao_obj.virt_obj.v_addr;
257712f080e7Smrj 		poff = (uintptr_t)vaddr & MMU_PAGEOFFSET;
257812f080e7Smrj 	}
257912f080e7Smrj 	pcnt = mmu_btopr(dma->dp_dma.dmao_size + poff);
258012f080e7Smrj 
258112f080e7Smrj 	switch (dma->dp_dma.dmao_type) {
258212f080e7Smrj 	case DMA_OTYP_PAGES:
258312f080e7Smrj 		/*
258412f080e7Smrj 		 * for a linked list of pp's walk through them to make sure
258512f080e7Smrj 		 * they're locked and not free.
258612f080e7Smrj 		 */
258712f080e7Smrj 		pp = dma->dp_dma.dmao_obj.pp_obj.pp_pp;
258812f080e7Smrj 		for (i = 0; i < pcnt; i++) {
258912f080e7Smrj 			if (PP_ISFREE(pp) || !PAGE_LOCKED(pp)) {
259012f080e7Smrj 				return (DDI_FAILURE);
259112f080e7Smrj 			}
25927c478bd9Sstevel@tonic-gate 			pp = pp->p_next;
25937c478bd9Sstevel@tonic-gate 		}
25947c478bd9Sstevel@tonic-gate 		break;
259512f080e7Smrj 
25967c478bd9Sstevel@tonic-gate 	case DMA_OTYP_VADDR:
25977c478bd9Sstevel@tonic-gate 	case DMA_OTYP_BUFVADDR:
259812f080e7Smrj 		pplist = dma->dp_dma.dmao_obj.virt_obj.v_priv;
259912f080e7Smrj 		/*
260012f080e7Smrj 		 * for an array of pp's walk through them to make sure they're
260112f080e7Smrj 		 * not free. It's possible that they may not be locked.
260212f080e7Smrj 		 */
260312f080e7Smrj 		if (pplist) {
260412f080e7Smrj 			for (i = 0; i < pcnt; i++) {
260512f080e7Smrj 				if (PP_ISFREE(pplist[i])) {
260612f080e7Smrj 					return (DDI_FAILURE);
260712f080e7Smrj 				}
260812f080e7Smrj 			}
260912f080e7Smrj 
261012f080e7Smrj 		/* For a virtual address, try to peek at each page */
261112f080e7Smrj 		} else {
261250200e77SFrank Van Der Linden 			if (rootnex_get_as(&dma->dp_dma) == &kas) {
261312f080e7Smrj 				for (i = 0; i < pcnt; i++) {
261400d0963fSdilpreet 					if (ddi_peek8(NULL, vaddr, &b) ==
261500d0963fSdilpreet 					    DDI_FAILURE)
261612f080e7Smrj 						return (DDI_FAILURE);
261700d0963fSdilpreet 					vaddr += MMU_PAGESIZE;
261812f080e7Smrj 				}
261912f080e7Smrj 			}
262012f080e7Smrj 		}
262112f080e7Smrj 		break;
262212f080e7Smrj 
262312f080e7Smrj 	default:
262450200e77SFrank Van Der Linden 		cmn_err(CE_PANIC, "rootnex_verify_buffer: bad DMA object");
262512f080e7Smrj 		break;
262612f080e7Smrj 	}
262712f080e7Smrj 
262812f080e7Smrj 	return (DDI_SUCCESS);
262912f080e7Smrj }
263012f080e7Smrj 
263112f080e7Smrj 
263212f080e7Smrj /*
263312f080e7Smrj  * rootnex_clean_dmahdl()
263412f080e7Smrj  *    Clean the dma handle. This should be called on a handle alloc and an
263512f080e7Smrj  *    unbind handle. Set the handle state to the default settings.
263612f080e7Smrj  */
263712f080e7Smrj static void
rootnex_clean_dmahdl(ddi_dma_impl_t * hp)263812f080e7Smrj rootnex_clean_dmahdl(ddi_dma_impl_t *hp)
263912f080e7Smrj {
264012f080e7Smrj 	rootnex_dma_t *dma;
264112f080e7Smrj 
264212f080e7Smrj 
264312f080e7Smrj 	dma = (rootnex_dma_t *)hp->dmai_private;
264412f080e7Smrj 
264512f080e7Smrj 	hp->dmai_nwin = 0;
264612f080e7Smrj 	dma->dp_current_cookie = 0;
264712f080e7Smrj 	dma->dp_copybuf_size = 0;
264812f080e7Smrj 	dma->dp_window = NULL;
264912f080e7Smrj 	dma->dp_cbaddr = NULL;
265012f080e7Smrj 	dma->dp_inuse = B_FALSE;
265150200e77SFrank Van Der Linden 	dma->dp_dvma_used = B_FALSE;
265212f080e7Smrj 	dma->dp_need_to_free_cookie = B_FALSE;
265394f1124eSVikram Hegde 	dma->dp_need_to_switch_cookies = B_FALSE;
265494f1124eSVikram Hegde 	dma->dp_saved_cookies = NULL;
265594f1124eSVikram Hegde 	dma->dp_sleep_flags = KM_PANIC;
265612f080e7Smrj 	dma->dp_need_to_free_window = B_FALSE;
265712f080e7Smrj 	dma->dp_partial_required = B_FALSE;
265812f080e7Smrj 	dma->dp_trim_required = B_FALSE;
265912f080e7Smrj 	dma->dp_sglinfo.si_copybuf_req = 0;
266012f080e7Smrj #if !defined(__amd64)
266112f080e7Smrj 	dma->dp_cb_remaping = B_FALSE;
266212f080e7Smrj 	dma->dp_kva = NULL;
266312f080e7Smrj #endif
266412f080e7Smrj 
266512f080e7Smrj 	/* FMA related initialization */
266612f080e7Smrj 	hp->dmai_fault = 0;
266712f080e7Smrj 	hp->dmai_fault_check = NULL;
266812f080e7Smrj 	hp->dmai_fault_notify = NULL;
266912f080e7Smrj 	hp->dmai_error.err_ena = 0;
267012f080e7Smrj 	hp->dmai_error.err_status = DDI_FM_OK;
267112f080e7Smrj 	hp->dmai_error.err_expected = DDI_FM_ERR_UNEXPECTED;
267212f080e7Smrj 	hp->dmai_error.err_ontrap = NULL;
267312f080e7Smrj }
267412f080e7Smrj 
267512f080e7Smrj 
267612f080e7Smrj /*
267712f080e7Smrj  * rootnex_valid_alloc_parms()
267812f080e7Smrj  *    Called in ddi_dma_alloc_handle path to validate its parameters.
267912f080e7Smrj  */
268012f080e7Smrj static int
rootnex_valid_alloc_parms(ddi_dma_attr_t * attr,uint_t maxsegmentsize)268112f080e7Smrj rootnex_valid_alloc_parms(ddi_dma_attr_t *attr, uint_t maxsegmentsize)
268212f080e7Smrj {
268312f080e7Smrj 	if ((attr->dma_attr_seg < MMU_PAGEOFFSET) ||
268412f080e7Smrj 	    (attr->dma_attr_count_max < MMU_PAGEOFFSET) ||
268512f080e7Smrj 	    (attr->dma_attr_granular > MMU_PAGESIZE) ||
268612f080e7Smrj 	    (attr->dma_attr_maxxfer < MMU_PAGESIZE)) {
268712f080e7Smrj 		return (DDI_DMA_BADATTR);
268812f080e7Smrj 	}
268912f080e7Smrj 
269012f080e7Smrj 	if (attr->dma_attr_addr_hi <= attr->dma_attr_addr_lo) {
269112f080e7Smrj 		return (DDI_DMA_BADATTR);
269212f080e7Smrj 	}
269312f080e7Smrj 
269412f080e7Smrj 	if ((attr->dma_attr_seg & MMU_PAGEOFFSET) != MMU_PAGEOFFSET ||
269512f080e7Smrj 	    MMU_PAGESIZE & (attr->dma_attr_granular - 1) ||
2696877f730dSGarrett D'Amore 	    attr->dma_attr_sgllen == 0) {
269712f080e7Smrj 		return (DDI_DMA_BADATTR);
269812f080e7Smrj 	}
269912f080e7Smrj 
270012f080e7Smrj 	/* We should be able to DMA into every byte offset in a page */
270112f080e7Smrj 	if (maxsegmentsize < MMU_PAGESIZE) {
270212f080e7Smrj 		return (DDI_DMA_BADATTR);
270312f080e7Smrj 	}
270412f080e7Smrj 
270507c6692fSMark Johnson 	/* if we're bouncing on seg, seg must be <= addr_hi */
270607c6692fSMark Johnson 	if ((attr->dma_attr_flags & _DDI_DMA_BOUNCE_ON_SEG) &&
270707c6692fSMark Johnson 	    (attr->dma_attr_seg > attr->dma_attr_addr_hi)) {
270807c6692fSMark Johnson 		return (DDI_DMA_BADATTR);
270907c6692fSMark Johnson 	}
271012f080e7Smrj 	return (DDI_SUCCESS);
271112f080e7Smrj }
271212f080e7Smrj 
271312f080e7Smrj /*
271412f080e7Smrj  * rootnex_valid_bind_parms()
271512f080e7Smrj  *    Called in ddi_dma_*_bind_handle path to validate its parameters.
271612f080e7Smrj  */
271712f080e7Smrj /* ARGSUSED */
271812f080e7Smrj static int
rootnex_valid_bind_parms(ddi_dma_req_t * dmareq,ddi_dma_attr_t * attr)271912f080e7Smrj rootnex_valid_bind_parms(ddi_dma_req_t *dmareq, ddi_dma_attr_t *attr)
272012f080e7Smrj {
272112f080e7Smrj #if !defined(__amd64)
272212f080e7Smrj 	/*
272312f080e7Smrj 	 * we only support up to a 2G-1 transfer size on 32-bit kernels so
272412f080e7Smrj 	 * we can track the offset for the obsoleted interfaces.
272512f080e7Smrj 	 */
272612f080e7Smrj 	if (dmareq->dmar_object.dmao_size > 0x7FFFFFFF) {
272712f080e7Smrj 		return (DDI_DMA_TOOBIG);
272812f080e7Smrj 	}
272912f080e7Smrj #endif
273012f080e7Smrj 
273112f080e7Smrj 	return (DDI_SUCCESS);
273212f080e7Smrj }
273312f080e7Smrj 
273412f080e7Smrj 
273512f080e7Smrj /*
273607c6692fSMark Johnson  * rootnex_need_bounce_seg()
273707c6692fSMark Johnson  *    check to see if the buffer lives on both side of the seg.
273807c6692fSMark Johnson  */
273907c6692fSMark Johnson static boolean_t
rootnex_need_bounce_seg(ddi_dma_obj_t * dmar_object,rootnex_sglinfo_t * sglinfo)274007c6692fSMark Johnson rootnex_need_bounce_seg(ddi_dma_obj_t *dmar_object, rootnex_sglinfo_t *sglinfo)
274107c6692fSMark Johnson {
274207c6692fSMark Johnson 	ddi_dma_atyp_t buftype;
274307c6692fSMark Johnson 	rootnex_addr_t raddr;
274407c6692fSMark Johnson 	boolean_t lower_addr;
274507c6692fSMark Johnson 	boolean_t upper_addr;
274607c6692fSMark Johnson 	uint64_t offset;
274707c6692fSMark Johnson 	page_t **pplist;
274807c6692fSMark Johnson 	uint64_t paddr;
274907c6692fSMark Johnson 	uint32_t psize;
275007c6692fSMark Johnson 	uint32_t size;
275107c6692fSMark Johnson 	caddr_t vaddr;
275207c6692fSMark Johnson 	uint_t pcnt;
275307c6692fSMark Johnson 	page_t *pp;
275407c6692fSMark Johnson 
275507c6692fSMark Johnson 
275607c6692fSMark Johnson 	/* shortcuts */
275707c6692fSMark Johnson 	pplist = dmar_object->dmao_obj.virt_obj.v_priv;
275807c6692fSMark Johnson 	vaddr = dmar_object->dmao_obj.virt_obj.v_addr;
275907c6692fSMark Johnson 	buftype = dmar_object->dmao_type;
276007c6692fSMark Johnson 	size = dmar_object->dmao_size;
276107c6692fSMark Johnson 
276207c6692fSMark Johnson 	lower_addr = B_FALSE;
276307c6692fSMark Johnson 	upper_addr = B_FALSE;
276407c6692fSMark Johnson 	pcnt = 0;
276507c6692fSMark Johnson 
276607c6692fSMark Johnson 	/*
276707c6692fSMark Johnson 	 * Process the first page to handle the initial offset of the buffer.
276807c6692fSMark Johnson 	 * We'll use the base address we get later when we loop through all
276907c6692fSMark Johnson 	 * the pages.
277007c6692fSMark Johnson 	 */
277107c6692fSMark Johnson 	if (buftype == DMA_OTYP_PAGES) {
277207c6692fSMark Johnson 		pp = dmar_object->dmao_obj.pp_obj.pp_pp;
277307c6692fSMark Johnson 		offset =  dmar_object->dmao_obj.pp_obj.pp_offset &
277407c6692fSMark Johnson 		    MMU_PAGEOFFSET;
277507c6692fSMark Johnson 		paddr = pfn_to_pa(pp->p_pagenum) + offset;
277607c6692fSMark Johnson 		psize = MIN(size, (MMU_PAGESIZE - offset));
277707c6692fSMark Johnson 		pp = pp->p_next;
277807c6692fSMark Johnson 		sglinfo->si_asp = NULL;
277907c6692fSMark Johnson 	} else if (pplist != NULL) {
278007c6692fSMark Johnson 		offset = (uintptr_t)vaddr & MMU_PAGEOFFSET;
278107c6692fSMark Johnson 		sglinfo->si_asp = dmar_object->dmao_obj.virt_obj.v_as;
278207c6692fSMark Johnson 		if (sglinfo->si_asp == NULL) {
278307c6692fSMark Johnson 			sglinfo->si_asp = &kas;
278407c6692fSMark Johnson 		}
278507c6692fSMark Johnson 		paddr = pfn_to_pa(pplist[pcnt]->p_pagenum);
278607c6692fSMark Johnson 		paddr += offset;
278707c6692fSMark Johnson 		psize = MIN(size, (MMU_PAGESIZE - offset));
278807c6692fSMark Johnson 		pcnt++;
278907c6692fSMark Johnson 	} else {
279007c6692fSMark Johnson 		offset = (uintptr_t)vaddr & MMU_PAGEOFFSET;
279107c6692fSMark Johnson 		sglinfo->si_asp = dmar_object->dmao_obj.virt_obj.v_as;
279207c6692fSMark Johnson 		if (sglinfo->si_asp == NULL) {
279307c6692fSMark Johnson 			sglinfo->si_asp = &kas;
279407c6692fSMark Johnson 		}
279507c6692fSMark Johnson 		paddr = pfn_to_pa(hat_getpfnum(sglinfo->si_asp->a_hat, vaddr));
279607c6692fSMark Johnson 		paddr += offset;
279707c6692fSMark Johnson 		psize = MIN(size, (MMU_PAGESIZE - offset));
279807c6692fSMark Johnson 		vaddr += psize;
279907c6692fSMark Johnson 	}
280007c6692fSMark Johnson 
280150200e77SFrank Van Der Linden 	raddr = ROOTNEX_PADDR_TO_RBASE(paddr);
280207c6692fSMark Johnson 
280307c6692fSMark Johnson 	if ((raddr + psize) > sglinfo->si_segmask) {
280407c6692fSMark Johnson 		upper_addr = B_TRUE;
280507c6692fSMark Johnson 	} else {
280607c6692fSMark Johnson 		lower_addr = B_TRUE;
280707c6692fSMark Johnson 	}
280807c6692fSMark Johnson 	size -= psize;
280907c6692fSMark Johnson 
281007c6692fSMark Johnson 	/*
281107c6692fSMark Johnson 	 * Walk through the rest of the pages in the buffer. Track to see
281207c6692fSMark Johnson 	 * if we have pages on both sides of the segment boundary.
281307c6692fSMark Johnson 	 */
281407c6692fSMark Johnson 	while (size > 0) {
281507c6692fSMark Johnson 		/* partial or full page */
281607c6692fSMark Johnson 		psize = MIN(size, MMU_PAGESIZE);
281707c6692fSMark Johnson 
281807c6692fSMark Johnson 		if (buftype == DMA_OTYP_PAGES) {
281907c6692fSMark Johnson 			/* get the paddr from the page_t */
282007c6692fSMark Johnson 			ASSERT(!PP_ISFREE(pp) && PAGE_LOCKED(pp));
282107c6692fSMark Johnson 			paddr = pfn_to_pa(pp->p_pagenum);
282207c6692fSMark Johnson 			pp = pp->p_next;
282307c6692fSMark Johnson 		} else if (pplist != NULL) {
282407c6692fSMark Johnson 			/* index into the array of page_t's to get the paddr */
282507c6692fSMark Johnson 			ASSERT(!PP_ISFREE(pplist[pcnt]));
282607c6692fSMark Johnson 			paddr = pfn_to_pa(pplist[pcnt]->p_pagenum);
282707c6692fSMark Johnson 			pcnt++;
282807c6692fSMark Johnson 		} else {
282907c6692fSMark Johnson 			/* call into the VM to get the paddr */
283007c6692fSMark Johnson 			paddr =  pfn_to_pa(hat_getpfnum(sglinfo->si_asp->a_hat,
283107c6692fSMark Johnson 			    vaddr));
283207c6692fSMark Johnson 			vaddr += psize;
283307c6692fSMark Johnson 		}
283407c6692fSMark Johnson 
283550200e77SFrank Van Der Linden 		raddr = ROOTNEX_PADDR_TO_RBASE(paddr);
283607c6692fSMark Johnson 
283707c6692fSMark Johnson 		if ((raddr + psize) > sglinfo->si_segmask) {
283807c6692fSMark Johnson 			upper_addr = B_TRUE;
283907c6692fSMark Johnson 		} else {
284007c6692fSMark Johnson 			lower_addr = B_TRUE;
284107c6692fSMark Johnson 		}
284207c6692fSMark Johnson 		/*
284307c6692fSMark Johnson 		 * if the buffer lives both above and below the segment
284407c6692fSMark Johnson 		 * boundary, or the current page is the page immediately
284507c6692fSMark Johnson 		 * after the segment, we will use a copy/bounce buffer for
284607c6692fSMark Johnson 		 * all pages > seg.
284707c6692fSMark Johnson 		 */
284807c6692fSMark Johnson 		if ((lower_addr && upper_addr) ||
284907c6692fSMark Johnson 		    (raddr == (sglinfo->si_segmask + 1))) {
285007c6692fSMark Johnson 			return (B_TRUE);
285107c6692fSMark Johnson 		}
285207c6692fSMark Johnson 
285307c6692fSMark Johnson 		size -= psize;
285407c6692fSMark Johnson 	}
285507c6692fSMark Johnson 
285607c6692fSMark Johnson 	return (B_FALSE);
285707c6692fSMark Johnson }
285807c6692fSMark Johnson 
285907c6692fSMark Johnson /*
286012f080e7Smrj  * rootnex_get_sgl()
286112f080e7Smrj  *    Called in bind fastpath to get the sgl. Most of this will be replaced
286212f080e7Smrj  *    with a call to the vm layer when vm2.0 comes around...
286312f080e7Smrj  */
286412f080e7Smrj static void
rootnex_get_sgl(ddi_dma_obj_t * dmar_object,ddi_dma_cookie_t * sgl,rootnex_sglinfo_t * sglinfo)286512f080e7Smrj rootnex_get_sgl(ddi_dma_obj_t *dmar_object, ddi_dma_cookie_t *sgl,
286612f080e7Smrj     rootnex_sglinfo_t *sglinfo)
286712f080e7Smrj {
286812f080e7Smrj 	ddi_dma_atyp_t buftype;
2869843e1988Sjohnlev 	rootnex_addr_t raddr;
287012f080e7Smrj 	uint64_t last_page;
287112f080e7Smrj 	uint64_t offset;
287212f080e7Smrj 	uint64_t addrhi;
287312f080e7Smrj 	uint64_t addrlo;
287412f080e7Smrj 	uint64_t maxseg;
287512f080e7Smrj 	page_t **pplist;
287612f080e7Smrj 	uint64_t paddr;
287712f080e7Smrj 	uint32_t psize;
287812f080e7Smrj 	uint32_t size;
287912f080e7Smrj 	caddr_t vaddr;
288012f080e7Smrj 	uint_t pcnt;
288112f080e7Smrj 	page_t *pp;
288212f080e7Smrj 	uint_t cnt;
288312f080e7Smrj 
288412f080e7Smrj 
288512f080e7Smrj 	/* shortcuts */
288612f080e7Smrj 	pplist = dmar_object->dmao_obj.virt_obj.v_priv;
288712f080e7Smrj 	vaddr = dmar_object->dmao_obj.virt_obj.v_addr;
288812f080e7Smrj 	maxseg = sglinfo->si_max_cookie_size;
288912f080e7Smrj 	buftype = dmar_object->dmao_type;
289012f080e7Smrj 	addrhi = sglinfo->si_max_addr;
289112f080e7Smrj 	addrlo = sglinfo->si_min_addr;
289212f080e7Smrj 	size = dmar_object->dmao_size;
289312f080e7Smrj 
289412f080e7Smrj 	pcnt = 0;
289512f080e7Smrj 	cnt = 0;
289612f080e7Smrj 
289707c6692fSMark Johnson 
289807c6692fSMark Johnson 	/*
289907c6692fSMark Johnson 	 * check to see if we need to use the copy buffer for pages over
290007c6692fSMark Johnson 	 * the segment attr.
290107c6692fSMark Johnson 	 */
290207c6692fSMark Johnson 	sglinfo->si_bounce_on_seg = B_FALSE;
290307c6692fSMark Johnson 	if (sglinfo->si_flags & _DDI_DMA_BOUNCE_ON_SEG) {
290407c6692fSMark Johnson 		sglinfo->si_bounce_on_seg = rootnex_need_bounce_seg(
290507c6692fSMark Johnson 		    dmar_object, sglinfo);
290607c6692fSMark Johnson 	}
290707c6692fSMark Johnson 
290812f080e7Smrj 	/*
290912f080e7Smrj 	 * if we were passed down a linked list of pages, i.e. pointer to
291012f080e7Smrj 	 * page_t, use this to get our physical address and buf offset.
291112f080e7Smrj 	 */
291212f080e7Smrj 	if (buftype == DMA_OTYP_PAGES) {
291312f080e7Smrj 		pp = dmar_object->dmao_obj.pp_obj.pp_pp;
291412f080e7Smrj 		ASSERT(!PP_ISFREE(pp) && PAGE_LOCKED(pp));
291512f080e7Smrj 		offset =  dmar_object->dmao_obj.pp_obj.pp_offset &
291612f080e7Smrj 		    MMU_PAGEOFFSET;
2917843e1988Sjohnlev 		paddr = pfn_to_pa(pp->p_pagenum) + offset;
291812f080e7Smrj 		psize = MIN(size, (MMU_PAGESIZE - offset));
291912f080e7Smrj 		pp = pp->p_next;
292012f080e7Smrj 		sglinfo->si_asp = NULL;
292112f080e7Smrj 
292212f080e7Smrj 	/*
292312f080e7Smrj 	 * We weren't passed down a linked list of pages, but if we were passed
292412f080e7Smrj 	 * down an array of pages, use this to get our physical address and buf
292512f080e7Smrj 	 * offset.
292612f080e7Smrj 	 */
292712f080e7Smrj 	} else if (pplist != NULL) {
292812f080e7Smrj 		ASSERT((buftype == DMA_OTYP_VADDR) ||
292912f080e7Smrj 		    (buftype == DMA_OTYP_BUFVADDR));
293012f080e7Smrj 
293112f080e7Smrj 		offset = (uintptr_t)vaddr & MMU_PAGEOFFSET;
293212f080e7Smrj 		sglinfo->si_asp = dmar_object->dmao_obj.virt_obj.v_as;
293312f080e7Smrj 		if (sglinfo->si_asp == NULL) {
293412f080e7Smrj 			sglinfo->si_asp = &kas;
293512f080e7Smrj 		}
293612f080e7Smrj 
293712f080e7Smrj 		ASSERT(!PP_ISFREE(pplist[pcnt]));
2938843e1988Sjohnlev 		paddr = pfn_to_pa(pplist[pcnt]->p_pagenum);
293912f080e7Smrj 		paddr += offset;
294012f080e7Smrj 		psize = MIN(size, (MMU_PAGESIZE - offset));
294112f080e7Smrj 		pcnt++;
294212f080e7Smrj 
294312f080e7Smrj 	/*
294412f080e7Smrj 	 * All we have is a virtual address, we'll need to call into the VM
294512f080e7Smrj 	 * to get the physical address.
294612f080e7Smrj 	 */
294712f080e7Smrj 	} else {
294812f080e7Smrj 		ASSERT((buftype == DMA_OTYP_VADDR) ||
294912f080e7Smrj 		    (buftype == DMA_OTYP_BUFVADDR));
295012f080e7Smrj 
295112f080e7Smrj 		offset = (uintptr_t)vaddr & MMU_PAGEOFFSET;
295212f080e7Smrj 		sglinfo->si_asp = dmar_object->dmao_obj.virt_obj.v_as;
295312f080e7Smrj 		if (sglinfo->si_asp == NULL) {
295412f080e7Smrj 			sglinfo->si_asp = &kas;
295512f080e7Smrj 		}
295612f080e7Smrj 
2957843e1988Sjohnlev 		paddr = pfn_to_pa(hat_getpfnum(sglinfo->si_asp->a_hat, vaddr));
295812f080e7Smrj 		paddr += offset;
295912f080e7Smrj 		psize = MIN(size, (MMU_PAGESIZE - offset));
296012f080e7Smrj 		vaddr += psize;
296112f080e7Smrj 	}
296212f080e7Smrj 
296350200e77SFrank Van Der Linden 	raddr = ROOTNEX_PADDR_TO_RBASE(paddr);
2964843e1988Sjohnlev 
296512f080e7Smrj 	/*
296612f080e7Smrj 	 * Setup the first cookie with the physical address of the page and the
296712f080e7Smrj 	 * size of the page (which takes into account the initial offset into
296812f080e7Smrj 	 * the page.
296912f080e7Smrj 	 */
2970843e1988Sjohnlev 	sgl[cnt].dmac_laddress = raddr;
297112f080e7Smrj 	sgl[cnt].dmac_size = psize;
297212f080e7Smrj 	sgl[cnt].dmac_type = 0;
297312f080e7Smrj 
297412f080e7Smrj 	/*
297512f080e7Smrj 	 * Save away the buffer offset into the page. We'll need this later in
297612f080e7Smrj 	 * the copy buffer code to help figure out the page index within the
297712f080e7Smrj 	 * buffer and the offset into the current page.
297812f080e7Smrj 	 */
297912f080e7Smrj 	sglinfo->si_buf_offset = offset;
298012f080e7Smrj 
298112f080e7Smrj 	/*
298207c6692fSMark Johnson 	 * If we are using the copy buffer for anything over the segment
298307c6692fSMark Johnson 	 * boundary, and this page is over the segment boundary.
298407c6692fSMark Johnson 	 *   OR
298507c6692fSMark Johnson 	 * if the DMA engine can't reach the physical address.
298612f080e7Smrj 	 */
298707c6692fSMark Johnson 	if (((sglinfo->si_bounce_on_seg) &&
298807c6692fSMark Johnson 	    ((raddr + psize) > sglinfo->si_segmask)) ||
298907c6692fSMark Johnson 	    ((raddr < addrlo) || ((raddr + psize) > addrhi))) {
299007c6692fSMark Johnson 		/*
299107c6692fSMark Johnson 		 * Increase how much copy buffer we use. We always increase by
299207c6692fSMark Johnson 		 * pagesize so we don't have to worry about converting offsets.
299307c6692fSMark Johnson 		 * Set a flag in the cookies dmac_type to indicate that it uses
299407c6692fSMark Johnson 		 * the copy buffer. If this isn't the last cookie, go to the
299507c6692fSMark Johnson 		 * next cookie (since we separate each page which uses the copy
299607c6692fSMark Johnson 		 * buffer in case the copy buffer is not physically contiguous.
299707c6692fSMark Johnson 		 */
299812f080e7Smrj 		sglinfo->si_copybuf_req += MMU_PAGESIZE;
299912f080e7Smrj 		sgl[cnt].dmac_type = ROOTNEX_USES_COPYBUF;
300012f080e7Smrj 		if ((cnt + 1) < sglinfo->si_max_pages) {
300112f080e7Smrj 			cnt++;
300212f080e7Smrj 			sgl[cnt].dmac_laddress = 0;
300312f080e7Smrj 			sgl[cnt].dmac_size = 0;
300412f080e7Smrj 			sgl[cnt].dmac_type = 0;
300512f080e7Smrj 		}
300612f080e7Smrj 	}
300712f080e7Smrj 
300812f080e7Smrj 	/*
300912f080e7Smrj 	 * save this page's physical address so we can figure out if the next
301012f080e7Smrj 	 * page is physically contiguous. Keep decrementing size until we are
301112f080e7Smrj 	 * done with the buffer.
301212f080e7Smrj 	 */
3013843e1988Sjohnlev 	last_page = raddr & MMU_PAGEMASK;
301412f080e7Smrj 	size -= psize;
301512f080e7Smrj 
301612f080e7Smrj 	while (size > 0) {
301712f080e7Smrj 		/* Get the size for this page (i.e. partial or full page) */
301812f080e7Smrj 		psize = MIN(size, MMU_PAGESIZE);
301912f080e7Smrj 
302012f080e7Smrj 		if (buftype == DMA_OTYP_PAGES) {
302112f080e7Smrj 			/* get the paddr from the page_t */
302212f080e7Smrj 			ASSERT(!PP_ISFREE(pp) && PAGE_LOCKED(pp));
3023843e1988Sjohnlev 			paddr = pfn_to_pa(pp->p_pagenum);
302412f080e7Smrj 			pp = pp->p_next;
302512f080e7Smrj 		} else if (pplist != NULL) {
302612f080e7Smrj 			/* index into the array of page_t's to get the paddr */
302712f080e7Smrj 			ASSERT(!PP_ISFREE(pplist[pcnt]));
3028843e1988Sjohnlev 			paddr = pfn_to_pa(pplist[pcnt]->p_pagenum);
302912f080e7Smrj 			pcnt++;
303012f080e7Smrj 		} else {
303112f080e7Smrj 			/* call into the VM to get the paddr */
3032843e1988Sjohnlev 			paddr =  pfn_to_pa(hat_getpfnum(sglinfo->si_asp->a_hat,
303312f080e7Smrj 			    vaddr));
303412f080e7Smrj 			vaddr += psize;
303512f080e7Smrj 		}
303612f080e7Smrj 
303750200e77SFrank Van Der Linden 		raddr = ROOTNEX_PADDR_TO_RBASE(paddr);
303807c6692fSMark Johnson 
303907c6692fSMark Johnson 		/*
304007c6692fSMark Johnson 		 * If we are using the copy buffer for anything over the
304107c6692fSMark Johnson 		 * segment boundary, and this page is over the segment
304207c6692fSMark Johnson 		 * boundary.
304307c6692fSMark Johnson 		 *   OR
304407c6692fSMark Johnson 		 * if the DMA engine can't reach the physical address.
304507c6692fSMark Johnson 		 */
304607c6692fSMark Johnson 		if (((sglinfo->si_bounce_on_seg) &&
304707c6692fSMark Johnson 		    ((raddr + psize) > sglinfo->si_segmask)) ||
304807c6692fSMark Johnson 		    ((raddr < addrlo) || ((raddr + psize) > addrhi))) {
304907c6692fSMark Johnson 
305012f080e7Smrj 			sglinfo->si_copybuf_req += MMU_PAGESIZE;
305112f080e7Smrj 
305212f080e7Smrj 			/*
305312f080e7Smrj 			 * if there is something in the current cookie, go to
305412f080e7Smrj 			 * the next one. We only want one page in a cookie which
305512f080e7Smrj 			 * uses the copybuf since the copybuf doesn't have to
305612f080e7Smrj 			 * be physically contiguous.
305712f080e7Smrj 			 */
305812f080e7Smrj 			if (sgl[cnt].dmac_size != 0) {
305912f080e7Smrj 				cnt++;
306012f080e7Smrj 			}
3061843e1988Sjohnlev 			sgl[cnt].dmac_laddress = raddr;
306212f080e7Smrj 			sgl[cnt].dmac_size = psize;
306312f080e7Smrj #if defined(__amd64)
306412f080e7Smrj 			sgl[cnt].dmac_type = ROOTNEX_USES_COPYBUF;
306512f080e7Smrj #else
306612f080e7Smrj 			/*
306712f080e7Smrj 			 * save the buf offset for 32-bit kernel. used in the
306812f080e7Smrj 			 * obsoleted interfaces.
306912f080e7Smrj 			 */
307012f080e7Smrj 			sgl[cnt].dmac_type = ROOTNEX_USES_COPYBUF |
307112f080e7Smrj 			    (dmar_object->dmao_size - size);
307212f080e7Smrj #endif
307312f080e7Smrj 			/* if this isn't the last cookie, go to the next one */
307412f080e7Smrj 			if ((cnt + 1) < sglinfo->si_max_pages) {
307512f080e7Smrj 				cnt++;
307612f080e7Smrj 				sgl[cnt].dmac_laddress = 0;
307712f080e7Smrj 				sgl[cnt].dmac_size = 0;
307812f080e7Smrj 				sgl[cnt].dmac_type = 0;
307912f080e7Smrj 			}
308012f080e7Smrj 
308112f080e7Smrj 		/*
308212f080e7Smrj 		 * this page didn't need the copy buffer, if it's not physically
308312f080e7Smrj 		 * contiguous, or it would put us over a segment boundary, or it
308412f080e7Smrj 		 * puts us over the max cookie size, or the current sgl doesn't
308512f080e7Smrj 		 * have anything in it.
308612f080e7Smrj 		 */
3087843e1988Sjohnlev 		} else if (((last_page + MMU_PAGESIZE) != raddr) ||
3088843e1988Sjohnlev 		    !(raddr & sglinfo->si_segmask) ||
308912f080e7Smrj 		    ((sgl[cnt].dmac_size + psize) > maxseg) ||
309012f080e7Smrj 		    (sgl[cnt].dmac_size == 0)) {
309112f080e7Smrj 			/*
309212f080e7Smrj 			 * if we're not already in a new cookie, go to the next
309312f080e7Smrj 			 * cookie.
309412f080e7Smrj 			 */
309512f080e7Smrj 			if (sgl[cnt].dmac_size != 0) {
309612f080e7Smrj 				cnt++;
309712f080e7Smrj 			}
309812f080e7Smrj 
309912f080e7Smrj 			/* save the cookie information */
3100843e1988Sjohnlev 			sgl[cnt].dmac_laddress = raddr;
310112f080e7Smrj 			sgl[cnt].dmac_size = psize;
310212f080e7Smrj #if defined(__amd64)
310312f080e7Smrj 			sgl[cnt].dmac_type = 0;
310412f080e7Smrj #else
310512f080e7Smrj 			/*
310612f080e7Smrj 			 * save the buf offset for 32-bit kernel. used in the
310712f080e7Smrj 			 * obsoleted interfaces.
310812f080e7Smrj 			 */
310912f080e7Smrj 			sgl[cnt].dmac_type = dmar_object->dmao_size - size;
311012f080e7Smrj #endif
311112f080e7Smrj 
311212f080e7Smrj 		/*
311312f080e7Smrj 		 * this page didn't need the copy buffer, it is physically
311412f080e7Smrj 		 * contiguous with the last page, and it's <= the max cookie
311512f080e7Smrj 		 * size.
311612f080e7Smrj 		 */
311712f080e7Smrj 		} else {
311812f080e7Smrj 			sgl[cnt].dmac_size += psize;
311912f080e7Smrj 
312012f080e7Smrj 			/*
312112f080e7Smrj 			 * if this exactly ==  the maximum cookie size, and
312212f080e7Smrj 			 * it isn't the last cookie, go to the next cookie.
312312f080e7Smrj 			 */
312412f080e7Smrj 			if (((sgl[cnt].dmac_size + psize) == maxseg) &&
312512f080e7Smrj 			    ((cnt + 1) < sglinfo->si_max_pages)) {
312612f080e7Smrj 				cnt++;
312712f080e7Smrj 				sgl[cnt].dmac_laddress = 0;
312812f080e7Smrj 				sgl[cnt].dmac_size = 0;
312912f080e7Smrj 				sgl[cnt].dmac_type = 0;
313012f080e7Smrj 			}
313112f080e7Smrj 		}
313212f080e7Smrj 
313312f080e7Smrj 		/*
313412f080e7Smrj 		 * save this page's physical address so we can figure out if the
313512f080e7Smrj 		 * next page is physically contiguous. Keep decrementing size
313612f080e7Smrj 		 * until we are done with the buffer.
313712f080e7Smrj 		 */
3138843e1988Sjohnlev 		last_page = raddr;
313912f080e7Smrj 		size -= psize;
314012f080e7Smrj 	}
314112f080e7Smrj 
314212f080e7Smrj 	/* we're done, save away how many cookies the sgl has */
314312f080e7Smrj 	if (sgl[cnt].dmac_size == 0) {
314412f080e7Smrj 		ASSERT(cnt < sglinfo->si_max_pages);
314512f080e7Smrj 		sglinfo->si_sgl_size = cnt;
314612f080e7Smrj 	} else {
314712f080e7Smrj 		sglinfo->si_sgl_size = cnt + 1;
314812f080e7Smrj 	}
314912f080e7Smrj }
315012f080e7Smrj 
315150200e77SFrank Van Der Linden static void
rootnex_dvma_get_sgl(ddi_dma_obj_t * dmar_object,ddi_dma_cookie_t * sgl,rootnex_sglinfo_t * sglinfo)315250200e77SFrank Van Der Linden rootnex_dvma_get_sgl(ddi_dma_obj_t *dmar_object, ddi_dma_cookie_t *sgl,
315350200e77SFrank Van Der Linden     rootnex_sglinfo_t *sglinfo)
315450200e77SFrank Van Der Linden {
315550200e77SFrank Van Der Linden 	uint64_t offset;
315650200e77SFrank Van Der Linden 	uint64_t maxseg;
315750200e77SFrank Van Der Linden 	uint64_t dvaddr;
315850200e77SFrank Van Der Linden 	struct dvmaseg *dvs;
315950200e77SFrank Van Der Linden 	uint64_t paddr;
316050200e77SFrank Van Der Linden 	uint32_t psize, ssize;
316150200e77SFrank Van Der Linden 	uint32_t size;
316250200e77SFrank Van Der Linden 	uint_t cnt;
316350200e77SFrank Van Der Linden 	int physcontig;
316450200e77SFrank Van Der Linden 
316550200e77SFrank Van Der Linden 	ASSERT(dmar_object->dmao_type == DMA_OTYP_DVADDR);
316650200e77SFrank Van Der Linden 
316750200e77SFrank Van Der Linden 	/* shortcuts */
316850200e77SFrank Van Der Linden 	maxseg = sglinfo->si_max_cookie_size;
316950200e77SFrank Van Der Linden 	size = dmar_object->dmao_size;
317050200e77SFrank Van Der Linden 
317150200e77SFrank Van Der Linden 	cnt = 0;
317250200e77SFrank Van Der Linden 	sglinfo->si_bounce_on_seg = B_FALSE;
317350200e77SFrank Van Der Linden 
317450200e77SFrank Van Der Linden 	dvs = dmar_object->dmao_obj.dvma_obj.dv_seg;
317550200e77SFrank Van Der Linden 	offset = dmar_object->dmao_obj.dvma_obj.dv_off;
317650200e77SFrank Van Der Linden 	ssize = dvs->dvs_len;
317750200e77SFrank Van Der Linden 	paddr = dvs->dvs_start;
317850200e77SFrank Van Der Linden 	paddr += offset;
317950200e77SFrank Van Der Linden 	psize = MIN(ssize, (maxseg - offset));
318050200e77SFrank Van Der Linden 	dvaddr = paddr + psize;
318150200e77SFrank Van Der Linden 	ssize -= psize;
318250200e77SFrank Van Der Linden 
318350200e77SFrank Van Der Linden 	sgl[cnt].dmac_laddress = paddr;
318450200e77SFrank Van Der Linden 	sgl[cnt].dmac_size = psize;
318550200e77SFrank Van Der Linden 	sgl[cnt].dmac_type = 0;
318650200e77SFrank Van Der Linden 
318750200e77SFrank Van Der Linden 	size -= psize;
318850200e77SFrank Van Der Linden 	while (size > 0) {
318950200e77SFrank Van Der Linden 		if (ssize == 0) {
319050200e77SFrank Van Der Linden 			dvs++;
319150200e77SFrank Van Der Linden 			ssize = dvs->dvs_len;
319250200e77SFrank Van Der Linden 			dvaddr = dvs->dvs_start;
319350200e77SFrank Van Der Linden 			physcontig = 0;
319450200e77SFrank Van Der Linden 		} else
319550200e77SFrank Van Der Linden 			physcontig = 1;
319650200e77SFrank Van Der Linden 
319750200e77SFrank Van Der Linden 		paddr = dvaddr;
319850200e77SFrank Van Der Linden 		psize = MIN(ssize, maxseg);
319950200e77SFrank Van Der Linden 		dvaddr += psize;
320050200e77SFrank Van Der Linden 		ssize -= psize;
320150200e77SFrank Van Der Linden 
320250200e77SFrank Van Der Linden 		if (!physcontig || !(paddr & sglinfo->si_segmask) ||
320350200e77SFrank Van Der Linden 		    ((sgl[cnt].dmac_size + psize) > maxseg) ||
320450200e77SFrank Van Der Linden 		    (sgl[cnt].dmac_size == 0)) {
320550200e77SFrank Van Der Linden 			/*
320650200e77SFrank Van Der Linden 			 * if we're not already in a new cookie, go to the next
320750200e77SFrank Van Der Linden 			 * cookie.
320850200e77SFrank Van Der Linden 			 */
320950200e77SFrank Van Der Linden 			if (sgl[cnt].dmac_size != 0) {
321050200e77SFrank Van Der Linden 				cnt++;
321150200e77SFrank Van Der Linden 			}
321250200e77SFrank Van Der Linden 
321350200e77SFrank Van Der Linden 			/* save the cookie information */
321450200e77SFrank Van Der Linden 			sgl[cnt].dmac_laddress = paddr;
321550200e77SFrank Van Der Linden 			sgl[cnt].dmac_size = psize;
321650200e77SFrank Van Der Linden 			sgl[cnt].dmac_type = 0;
321750200e77SFrank Van Der Linden 		} else {
321850200e77SFrank Van Der Linden 			sgl[cnt].dmac_size += psize;
321950200e77SFrank Van Der Linden 
322050200e77SFrank Van Der Linden 			/*
322150200e77SFrank Van Der Linden 			 * if this exactly ==  the maximum cookie size, and
322250200e77SFrank Van Der Linden 			 * it isn't the last cookie, go to the next cookie.
322350200e77SFrank Van Der Linden 			 */
322450200e77SFrank Van Der Linden 			if (((sgl[cnt].dmac_size + psize) == maxseg) &&
322550200e77SFrank Van Der Linden 			    ((cnt + 1) < sglinfo->si_max_pages)) {
322650200e77SFrank Van Der Linden 				cnt++;
322750200e77SFrank Van Der Linden 				sgl[cnt].dmac_laddress = 0;
322850200e77SFrank Van Der Linden 				sgl[cnt].dmac_size = 0;
322950200e77SFrank Van Der Linden 				sgl[cnt].dmac_type = 0;
323050200e77SFrank Van Der Linden 			}
323150200e77SFrank Van Der Linden 		}
323250200e77SFrank Van Der Linden 		size -= psize;
323350200e77SFrank Van Der Linden 	}
323450200e77SFrank Van Der Linden 
323550200e77SFrank Van Der Linden 	/* we're done, save away how many cookies the sgl has */
323650200e77SFrank Van Der Linden 	if (sgl[cnt].dmac_size == 0) {
323750200e77SFrank Van Der Linden 		sglinfo->si_sgl_size = cnt;
323850200e77SFrank Van Der Linden 	} else {
323950200e77SFrank Van Der Linden 		sglinfo->si_sgl_size = cnt + 1;
324050200e77SFrank Van Der Linden 	}
324150200e77SFrank Van Der Linden }
324250200e77SFrank Van Der Linden 
324312f080e7Smrj /*
324412f080e7Smrj  * rootnex_bind_slowpath()
324512f080e7Smrj  *    Call in the bind path if the calling driver can't use the sgl without
324612f080e7Smrj  *    modifying it. We either need to use the copy buffer and/or we will end up
324712f080e7Smrj  *    with a partial bind.
324812f080e7Smrj  */
324912f080e7Smrj static int
rootnex_bind_slowpath(ddi_dma_impl_t * hp,struct ddi_dma_req * dmareq,rootnex_dma_t * dma,ddi_dma_attr_t * attr,ddi_dma_obj_t * dmao,int kmflag)325012f080e7Smrj rootnex_bind_slowpath(ddi_dma_impl_t *hp, struct ddi_dma_req *dmareq,
325150200e77SFrank Van Der Linden     rootnex_dma_t *dma, ddi_dma_attr_t *attr, ddi_dma_obj_t *dmao, int kmflag)
325212f080e7Smrj {
325312f080e7Smrj 	rootnex_sglinfo_t *sinfo;
325412f080e7Smrj 	rootnex_window_t *window;
325512f080e7Smrj 	ddi_dma_cookie_t *cookie;
325612f080e7Smrj 	size_t copybuf_used;
325712f080e7Smrj 	size_t dmac_size;
325812f080e7Smrj 	boolean_t partial;
325912f080e7Smrj 	off_t cur_offset;
326012f080e7Smrj 	page_t *cur_pp;
326112f080e7Smrj 	major_t mnum;
326212f080e7Smrj 	int e;
326312f080e7Smrj 	int i;
326412f080e7Smrj 
326512f080e7Smrj 
326612f080e7Smrj 	sinfo = &dma->dp_sglinfo;
326712f080e7Smrj 	copybuf_used = 0;
326812f080e7Smrj 	partial = B_FALSE;
326912f080e7Smrj 
327012f080e7Smrj 	/*
327112f080e7Smrj 	 * If we're using the copybuf, set the copybuf state in dma struct.
327212f080e7Smrj 	 * Needs to be first since it sets the copy buffer size.
327312f080e7Smrj 	 */
327412f080e7Smrj 	if (sinfo->si_copybuf_req != 0) {
327512f080e7Smrj 		e = rootnex_setup_copybuf(hp, dmareq, dma, attr);
327612f080e7Smrj 		if (e != DDI_SUCCESS) {
327712f080e7Smrj 			return (e);
327812f080e7Smrj 		}
327912f080e7Smrj 	} else {
328012f080e7Smrj 		dma->dp_copybuf_size = 0;
328112f080e7Smrj 	}
328212f080e7Smrj 
328312f080e7Smrj 	/*
328412f080e7Smrj 	 * Figure out if we need to do a partial mapping. If so, figure out
328512f080e7Smrj 	 * if we need to trim the buffers when we munge the sgl.
328612f080e7Smrj 	 */
328712f080e7Smrj 	if ((dma->dp_copybuf_size < sinfo->si_copybuf_req) ||
328850200e77SFrank Van Der Linden 	    (dmao->dmao_size > dma->dp_maxxfer) ||
3289877f730dSGarrett D'Amore 	    ((unsigned)attr->dma_attr_sgllen < sinfo->si_sgl_size)) {
329012f080e7Smrj 		dma->dp_partial_required = B_TRUE;
329112f080e7Smrj 		if (attr->dma_attr_granular != 1) {
329212f080e7Smrj 			dma->dp_trim_required = B_TRUE;
329312f080e7Smrj 		}
329412f080e7Smrj 	} else {
329512f080e7Smrj 		dma->dp_partial_required = B_FALSE;
329612f080e7Smrj 		dma->dp_trim_required = B_FALSE;
329712f080e7Smrj 	}
329812f080e7Smrj 
329912f080e7Smrj 	/* If we need to do a partial bind, make sure the driver supports it */
330012f080e7Smrj 	if (dma->dp_partial_required &&
330112f080e7Smrj 	    !(dmareq->dmar_flags & DDI_DMA_PARTIAL)) {
330212f080e7Smrj 
330312f080e7Smrj 		mnum = ddi_driver_major(dma->dp_dip);
330412f080e7Smrj 		/*
330512f080e7Smrj 		 * patchable which allows us to print one warning per major
330612f080e7Smrj 		 * number.
330712f080e7Smrj 		 */
330812f080e7Smrj 		if ((rootnex_bind_warn) &&
330912f080e7Smrj 		    ((rootnex_warn_list[mnum] & ROOTNEX_BIND_WARNING) == 0)) {
331012f080e7Smrj 			rootnex_warn_list[mnum] |= ROOTNEX_BIND_WARNING;
331112f080e7Smrj 			cmn_err(CE_WARN, "!%s: coding error detected, the "
331212f080e7Smrj 			    "driver is using ddi_dma_attr(9S) incorrectly. "
331312f080e7Smrj 			    "There is a small risk of data corruption in "
331412f080e7Smrj 			    "particular with large I/Os. The driver should be "
331512f080e7Smrj 			    "replaced with a corrected version for proper "
331612f080e7Smrj 			    "system operation. To disable this warning, add "
331712f080e7Smrj 			    "'set rootnex:rootnex_bind_warn=0' to "
331812f080e7Smrj 			    "/etc/system(4).", ddi_driver_name(dma->dp_dip));
331912f080e7Smrj 		}
332012f080e7Smrj 		return (DDI_DMA_TOOBIG);
332112f080e7Smrj 	}
332212f080e7Smrj 
332312f080e7Smrj 	/*
332412f080e7Smrj 	 * we might need multiple windows, setup state to handle them. In this
332512f080e7Smrj 	 * code path, we will have at least one window.
332612f080e7Smrj 	 */
332750200e77SFrank Van Der Linden 	e = rootnex_setup_windows(hp, dma, attr, dmao, kmflag);
332812f080e7Smrj 	if (e != DDI_SUCCESS) {
332912f080e7Smrj 		rootnex_teardown_copybuf(dma);
333012f080e7Smrj 		return (e);
333112f080e7Smrj 	}
333212f080e7Smrj 
333312f080e7Smrj 	window = &dma->dp_window[0];
333412f080e7Smrj 	cookie = &dma->dp_cookies[0];
333512f080e7Smrj 	cur_offset = 0;
333612f080e7Smrj 	rootnex_init_win(hp, dma, window, cookie, cur_offset);
333750200e77SFrank Van Der Linden 	if (dmao->dmao_type == DMA_OTYP_PAGES) {
333812f080e7Smrj 		cur_pp = dmareq->dmar_object.dmao_obj.pp_obj.pp_pp;
333912f080e7Smrj 	}
334012f080e7Smrj 
334112f080e7Smrj 	/* loop though all the cookies we got back from get_sgl() */
334212f080e7Smrj 	for (i = 0; i < sinfo->si_sgl_size; i++) {
334312f080e7Smrj 		/*
334412f080e7Smrj 		 * If we're using the copy buffer, check this cookie and setup
334512f080e7Smrj 		 * its associated copy buffer state. If this cookie uses the
334612f080e7Smrj 		 * copy buffer, make sure we sync this window during dma_sync.
334712f080e7Smrj 		 */
334812f080e7Smrj 		if (dma->dp_copybuf_size > 0) {
334950200e77SFrank Van Der Linden 			rootnex_setup_cookie(dmao, dma, cookie,
335012f080e7Smrj 			    cur_offset, &copybuf_used, &cur_pp);
335112f080e7Smrj 			if (cookie->dmac_type & ROOTNEX_USES_COPYBUF) {
335212f080e7Smrj 				window->wd_dosync = B_TRUE;
335312f080e7Smrj 			}
335412f080e7Smrj 		}
335512f080e7Smrj 
335612f080e7Smrj 		/*
335712f080e7Smrj 		 * save away the cookie size, since it could be modified in
335812f080e7Smrj 		 * the windowing code.
335912f080e7Smrj 		 */
336012f080e7Smrj 		dmac_size = cookie->dmac_size;
336112f080e7Smrj 
336212f080e7Smrj 		/* if we went over max copybuf size */
336312f080e7Smrj 		if (dma->dp_copybuf_size &&
336412f080e7Smrj 		    (copybuf_used > dma->dp_copybuf_size)) {
336512f080e7Smrj 			partial = B_TRUE;
336612f080e7Smrj 			e = rootnex_copybuf_window_boundary(hp, dma, &window,
336712f080e7Smrj 			    cookie, cur_offset, &copybuf_used);
336812f080e7Smrj 			if (e != DDI_SUCCESS) {
336912f080e7Smrj 				rootnex_teardown_copybuf(dma);
337012f080e7Smrj 				rootnex_teardown_windows(dma);
337112f080e7Smrj 				return (e);
337212f080e7Smrj 			}
337312f080e7Smrj 
337412f080e7Smrj 			/*
337512f080e7Smrj 			 * if the coookie uses the copy buffer, make sure the
337612f080e7Smrj 			 * new window we just moved to is set to sync.
337712f080e7Smrj 			 */
337812f080e7Smrj 			if (cookie->dmac_type & ROOTNEX_USES_COPYBUF) {
337912f080e7Smrj 				window->wd_dosync = B_TRUE;
338012f080e7Smrj 			}
3381ef4ab52fSFrank Van Der Linden 			ROOTNEX_DPROBE1(rootnex__copybuf__window, dev_info_t *,
338212f080e7Smrj 			    dma->dp_dip);
338312f080e7Smrj 
338412f080e7Smrj 		/* if the cookie cnt == max sgllen, move to the next window */
3385877f730dSGarrett D'Amore 		} else if (window->wd_cookie_cnt >=
3386877f730dSGarrett D'Amore 		    (unsigned)attr->dma_attr_sgllen) {
338712f080e7Smrj 			partial = B_TRUE;
338812f080e7Smrj 			ASSERT(window->wd_cookie_cnt == attr->dma_attr_sgllen);
338912f080e7Smrj 			e = rootnex_sgllen_window_boundary(hp, dma, &window,
339012f080e7Smrj 			    cookie, attr, cur_offset);
339112f080e7Smrj 			if (e != DDI_SUCCESS) {
339212f080e7Smrj 				rootnex_teardown_copybuf(dma);
339312f080e7Smrj 				rootnex_teardown_windows(dma);
339412f080e7Smrj 				return (e);
339512f080e7Smrj 			}
339612f080e7Smrj 
339712f080e7Smrj 			/*
339812f080e7Smrj 			 * if the coookie uses the copy buffer, make sure the
339912f080e7Smrj 			 * new window we just moved to is set to sync.
340012f080e7Smrj 			 */
340112f080e7Smrj 			if (cookie->dmac_type & ROOTNEX_USES_COPYBUF) {
340212f080e7Smrj 				window->wd_dosync = B_TRUE;
340312f080e7Smrj 			}
3404ef4ab52fSFrank Van Der Linden 			ROOTNEX_DPROBE1(rootnex__sgllen__window, dev_info_t *,
340512f080e7Smrj 			    dma->dp_dip);
340612f080e7Smrj 
340712f080e7Smrj 		/* else if we will be over maxxfer */
340812f080e7Smrj 		} else if ((window->wd_size + dmac_size) >
340912f080e7Smrj 		    dma->dp_maxxfer) {
341012f080e7Smrj 			partial = B_TRUE;
341112f080e7Smrj 			e = rootnex_maxxfer_window_boundary(hp, dma, &window,
341212f080e7Smrj 			    cookie);
341312f080e7Smrj 			if (e != DDI_SUCCESS) {
341412f080e7Smrj 				rootnex_teardown_copybuf(dma);
341512f080e7Smrj 				rootnex_teardown_windows(dma);
341612f080e7Smrj 				return (e);
341712f080e7Smrj 			}
341812f080e7Smrj 
341912f080e7Smrj 			/*
342012f080e7Smrj 			 * if the coookie uses the copy buffer, make sure the
342112f080e7Smrj 			 * new window we just moved to is set to sync.
342212f080e7Smrj 			 */
342312f080e7Smrj 			if (cookie->dmac_type & ROOTNEX_USES_COPYBUF) {
342412f080e7Smrj 				window->wd_dosync = B_TRUE;
342512f080e7Smrj 			}
3426ef4ab52fSFrank Van Der Linden 			ROOTNEX_DPROBE1(rootnex__maxxfer__window, dev_info_t *,
342712f080e7Smrj 			    dma->dp_dip);
342812f080e7Smrj 
342912f080e7Smrj 		/* else this cookie fits in the current window */
343012f080e7Smrj 		} else {
343112f080e7Smrj 			window->wd_cookie_cnt++;
343212f080e7Smrj 			window->wd_size += dmac_size;
343312f080e7Smrj 		}
343412f080e7Smrj 
343512f080e7Smrj 		/* track our offset into the buffer, go to the next cookie */
343650200e77SFrank Van Der Linden 		ASSERT(dmac_size <= dmao->dmao_size);
343712f080e7Smrj 		ASSERT(cookie->dmac_size <= dmac_size);
343812f080e7Smrj 		cur_offset += dmac_size;
343912f080e7Smrj 		cookie++;
344012f080e7Smrj 	}
344112f080e7Smrj 
344212f080e7Smrj 	/* if we ended up with a zero sized window in the end, clean it up */
344312f080e7Smrj 	if (window->wd_size == 0) {
344412f080e7Smrj 		hp->dmai_nwin--;
344512f080e7Smrj 		window--;
344612f080e7Smrj 	}
344712f080e7Smrj 
344812f080e7Smrj 	ASSERT(window->wd_trim.tr_trim_last == B_FALSE);
344912f080e7Smrj 
345012f080e7Smrj 	if (!partial) {
345112f080e7Smrj 		return (DDI_DMA_MAPPED);
345212f080e7Smrj 	}
345312f080e7Smrj 
345412f080e7Smrj 	ASSERT(dma->dp_partial_required);
345512f080e7Smrj 	return (DDI_DMA_PARTIAL_MAP);
345612f080e7Smrj }
345712f080e7Smrj 
345812f080e7Smrj /*
345912f080e7Smrj  * rootnex_setup_copybuf()
346012f080e7Smrj  *    Called in bind slowpath. Figures out if we're going to use the copy
346112f080e7Smrj  *    buffer, and if we do, sets up the basic state to handle it.
346212f080e7Smrj  */
346312f080e7Smrj static int
rootnex_setup_copybuf(ddi_dma_impl_t * hp,struct ddi_dma_req * dmareq,rootnex_dma_t * dma,ddi_dma_attr_t * attr)346412f080e7Smrj rootnex_setup_copybuf(ddi_dma_impl_t *hp, struct ddi_dma_req *dmareq,
346512f080e7Smrj     rootnex_dma_t *dma, ddi_dma_attr_t *attr)
346612f080e7Smrj {
346712f080e7Smrj 	rootnex_sglinfo_t *sinfo;
346812f080e7Smrj 	ddi_dma_attr_t lattr;
346912f080e7Smrj 	size_t max_copybuf;
347012f080e7Smrj 	int cansleep;
347112f080e7Smrj 	int e;
347212f080e7Smrj #if !defined(__amd64)
347312f080e7Smrj 	int vmflag;
347412f080e7Smrj #endif
347512f080e7Smrj 
347650200e77SFrank Van Der Linden 	ASSERT(!dma->dp_dvma_used);
347712f080e7Smrj 
347812f080e7Smrj 	sinfo = &dma->dp_sglinfo;
347912f080e7Smrj 
348036945f79Smrj 	/* read this first so it's consistent through the routine  */
348136945f79Smrj 	max_copybuf = i_ddi_copybuf_size() & MMU_PAGEMASK;
348212f080e7Smrj 
348312f080e7Smrj 	/* We need to call into the rootnex on ddi_dma_sync() */
348412f080e7Smrj 	hp->dmai_rflags &= ~DMP_NOSYNC;
348512f080e7Smrj 
348612f080e7Smrj 	/* make sure the copybuf size <= the max size */
348712f080e7Smrj 	dma->dp_copybuf_size = MIN(sinfo->si_copybuf_req, max_copybuf);
348812f080e7Smrj 	ASSERT((dma->dp_copybuf_size & MMU_PAGEOFFSET) == 0);
348912f080e7Smrj 
349012f080e7Smrj #if !defined(__amd64)
349112f080e7Smrj 	/*
349212f080e7Smrj 	 * if we don't have kva space to copy to/from, allocate the KVA space
349312f080e7Smrj 	 * now. We only do this for the 32-bit kernel. We use seg kpm space for
349412f080e7Smrj 	 * the 64-bit kernel.
349512f080e7Smrj 	 */
349612f080e7Smrj 	if ((dmareq->dmar_object.dmao_type == DMA_OTYP_PAGES) ||
349712f080e7Smrj 	    (dmareq->dmar_object.dmao_obj.virt_obj.v_as != NULL)) {
349812f080e7Smrj 
349912f080e7Smrj 		/* convert the sleep flags */
350012f080e7Smrj 		if (dmareq->dmar_fp == DDI_DMA_SLEEP) {
350112f080e7Smrj 			vmflag = VM_SLEEP;
350212f080e7Smrj 		} else {
350312f080e7Smrj 			vmflag = VM_NOSLEEP;
350412f080e7Smrj 		}
350512f080e7Smrj 
350612f080e7Smrj 		/* allocate Kernel VA space that we can bcopy to/from */
350712f080e7Smrj 		dma->dp_kva = vmem_alloc(heap_arena, dma->dp_copybuf_size,
350812f080e7Smrj 		    vmflag);
350912f080e7Smrj 		if (dma->dp_kva == NULL) {
351012f080e7Smrj 			return (DDI_DMA_NORESOURCES);
351112f080e7Smrj 		}
351212f080e7Smrj 	}
351312f080e7Smrj #endif
351412f080e7Smrj 
351512f080e7Smrj 	/* convert the sleep flags */
351612f080e7Smrj 	if (dmareq->dmar_fp == DDI_DMA_SLEEP) {
351712f080e7Smrj 		cansleep = 1;
351812f080e7Smrj 	} else {
351912f080e7Smrj 		cansleep = 0;
352012f080e7Smrj 	}
352112f080e7Smrj 
352212f080e7Smrj 	/*
3523d21b39ddSmrj 	 * Allocate the actual copy buffer. This needs to fit within the DMA
3524d21b39ddSmrj 	 * engine limits, so we can't use kmem_alloc... We don't need
3525d21b39ddSmrj 	 * contiguous memory (sgllen) since we will be forcing windows on
3526d21b39ddSmrj 	 * sgllen anyway.
352712f080e7Smrj 	 */
352812f080e7Smrj 	lattr = *attr;
352912f080e7Smrj 	lattr.dma_attr_align = MMU_PAGESIZE;
3530877f730dSGarrett D'Amore 	lattr.dma_attr_sgllen = -1;	/* no limit */
353107c6692fSMark Johnson 	/*
353207c6692fSMark Johnson 	 * if we're using the copy buffer because of seg, use that for our
353307c6692fSMark Johnson 	 * upper address limit.
353407c6692fSMark Johnson 	 */
353507c6692fSMark Johnson 	if (sinfo->si_bounce_on_seg) {
353607c6692fSMark Johnson 		lattr.dma_attr_addr_hi = lattr.dma_attr_seg;
353707c6692fSMark Johnson 	}
353812f080e7Smrj 	e = i_ddi_mem_alloc(dma->dp_dip, &lattr, dma->dp_copybuf_size, cansleep,
353912f080e7Smrj 	    0, NULL, &dma->dp_cbaddr, &dma->dp_cbsize, NULL);
354012f080e7Smrj 	if (e != DDI_SUCCESS) {
354112f080e7Smrj #if !defined(__amd64)
354212f080e7Smrj 		if (dma->dp_kva != NULL) {
354312f080e7Smrj 			vmem_free(heap_arena, dma->dp_kva,
354412f080e7Smrj 			    dma->dp_copybuf_size);
354512f080e7Smrj 		}
354612f080e7Smrj #endif
354712f080e7Smrj 		return (DDI_DMA_NORESOURCES);
354812f080e7Smrj 	}
354912f080e7Smrj 
3550ef4ab52fSFrank Van Der Linden 	ROOTNEX_DPROBE2(rootnex__alloc__copybuf, dev_info_t *, dma->dp_dip,
355112f080e7Smrj 	    size_t, dma->dp_copybuf_size);
355212f080e7Smrj 
355312f080e7Smrj 	return (DDI_SUCCESS);
355412f080e7Smrj }
355512f080e7Smrj 
355612f080e7Smrj 
355712f080e7Smrj /*
355812f080e7Smrj  * rootnex_setup_windows()
355912f080e7Smrj  *    Called in bind slowpath to setup the window state. We always have windows
356012f080e7Smrj  *    in the slowpath. Even if the window count = 1.
356112f080e7Smrj  */
356212f080e7Smrj static int
rootnex_setup_windows(ddi_dma_impl_t * hp,rootnex_dma_t * dma,ddi_dma_attr_t * attr,ddi_dma_obj_t * dmao,int kmflag)356312f080e7Smrj rootnex_setup_windows(ddi_dma_impl_t *hp, rootnex_dma_t *dma,
356450200e77SFrank Van Der Linden     ddi_dma_attr_t *attr, ddi_dma_obj_t *dmao, int kmflag)
356512f080e7Smrj {
356612f080e7Smrj 	rootnex_window_t *windowp;
356712f080e7Smrj 	rootnex_sglinfo_t *sinfo;
356812f080e7Smrj 	size_t copy_state_size;
356912f080e7Smrj 	size_t win_state_size;
357012f080e7Smrj 	size_t state_available;
357112f080e7Smrj 	size_t space_needed;
357212f080e7Smrj 	uint_t copybuf_win;
357312f080e7Smrj 	uint_t maxxfer_win;
357412f080e7Smrj 	size_t space_used;
357512f080e7Smrj 	uint_t sglwin;
357612f080e7Smrj 
357712f080e7Smrj 
357812f080e7Smrj 	sinfo = &dma->dp_sglinfo;
357912f080e7Smrj 
358012f080e7Smrj 	dma->dp_current_win = 0;
358112f080e7Smrj 	hp->dmai_nwin = 0;
358212f080e7Smrj 
358312f080e7Smrj 	/* If we don't need to do a partial, we only have one window */
358412f080e7Smrj 	if (!dma->dp_partial_required) {
358512f080e7Smrj 		dma->dp_max_win = 1;
358612f080e7Smrj 
358712f080e7Smrj 	/*
358812f080e7Smrj 	 * we need multiple windows, need to figure out the worse case number
358912f080e7Smrj 	 * of windows.
359012f080e7Smrj 	 */
35917c478bd9Sstevel@tonic-gate 	} else {
35927c478bd9Sstevel@tonic-gate 		/*
359312f080e7Smrj 		 * if we need windows because we need more copy buffer that
359412f080e7Smrj 		 * we allow, the worse case number of windows we could need
359512f080e7Smrj 		 * here would be (copybuf space required / copybuf space that
359612f080e7Smrj 		 * we have) plus one for remainder, and plus 2 to handle the
359712f080e7Smrj 		 * extra pages on the trim for the first and last pages of the
359812f080e7Smrj 		 * buffer (a page is the minimum window size so under the right
359912f080e7Smrj 		 * attr settings, you could have a window for each page).
360012f080e7Smrj 		 * The last page will only be hit here if the size is not a
360112f080e7Smrj 		 * multiple of the granularity (which theoretically shouldn't
360212f080e7Smrj 		 * be the case but never has been enforced, so we could have
360312f080e7Smrj 		 * broken things without it).
36047c478bd9Sstevel@tonic-gate 		 */
360512f080e7Smrj 		if (sinfo->si_copybuf_req > dma->dp_copybuf_size) {
360612f080e7Smrj 			ASSERT(dma->dp_copybuf_size > 0);
360712f080e7Smrj 			copybuf_win = (sinfo->si_copybuf_req /
360812f080e7Smrj 			    dma->dp_copybuf_size) + 1 + 2;
36097c478bd9Sstevel@tonic-gate 		} else {
361012f080e7Smrj 			copybuf_win = 0;
36117c478bd9Sstevel@tonic-gate 		}
361212f080e7Smrj 
361312f080e7Smrj 		/*
361412f080e7Smrj 		 * if we need windows because we have more cookies than the H/W
361512f080e7Smrj 		 * can handle, the number of windows we would need here would
3616b57cd2d3SMark Johnson 		 * be (cookie count / cookies count H/W supports minus 1[for
3617b57cd2d3SMark Johnson 		 * trim]) plus one for remainder.
361812f080e7Smrj 		 */
3619877f730dSGarrett D'Amore 		if ((unsigned)attr->dma_attr_sgllen < sinfo->si_sgl_size) {
3620b57cd2d3SMark Johnson 			sglwin = (sinfo->si_sgl_size /
3621b57cd2d3SMark Johnson 			    (attr->dma_attr_sgllen - 1)) + 1;
36227c478bd9Sstevel@tonic-gate 		} else {
362312f080e7Smrj 			sglwin = 0;
36247c478bd9Sstevel@tonic-gate 		}
362512f080e7Smrj 
362612f080e7Smrj 		/*
362712f080e7Smrj 		 * if we need windows because we're binding more memory than the
362812f080e7Smrj 		 * H/W can transfer at once, the number of windows we would need
362912f080e7Smrj 		 * here would be (xfer count / max xfer H/W supports) plus one
363012f080e7Smrj 		 * for remainder, and plus 2 to handle the extra pages on the
363112f080e7Smrj 		 * trim (see above comment about trim)
363212f080e7Smrj 		 */
363350200e77SFrank Van Der Linden 		if (dmao->dmao_size > dma->dp_maxxfer) {
363450200e77SFrank Van Der Linden 			maxxfer_win = (dmao->dmao_size /
363512f080e7Smrj 			    dma->dp_maxxfer) + 1 + 2;
363612f080e7Smrj 		} else {
363712f080e7Smrj 			maxxfer_win = 0;
36387c478bd9Sstevel@tonic-gate 		}
363912f080e7Smrj 		dma->dp_max_win =  copybuf_win + sglwin + maxxfer_win;
364012f080e7Smrj 		ASSERT(dma->dp_max_win > 0);
364112f080e7Smrj 	}
364212f080e7Smrj 	win_state_size = dma->dp_max_win * sizeof (rootnex_window_t);
364312f080e7Smrj 
364412f080e7Smrj 	/*
364512f080e7Smrj 	 * Get space for window and potential copy buffer state. Before we
364612f080e7Smrj 	 * go and allocate memory, see if we can get away with using what's
364712f080e7Smrj 	 * left in the pre-allocted state or the dynamically allocated sgl.
364812f080e7Smrj 	 */
364912f080e7Smrj 	space_used = (uintptr_t)(sinfo->si_sgl_size *
365012f080e7Smrj 	    sizeof (ddi_dma_cookie_t));
365112f080e7Smrj 
365212f080e7Smrj 	/* if we dynamically allocated space for the cookies */
365312f080e7Smrj 	if (dma->dp_need_to_free_cookie) {
365412f080e7Smrj 		/* if we have more space in the pre-allocted buffer, use it */
365512f080e7Smrj 		ASSERT(space_used <= dma->dp_cookie_size);
365612f080e7Smrj 		if ((dma->dp_cookie_size - space_used) <=
365712f080e7Smrj 		    rootnex_state->r_prealloc_size) {
365812f080e7Smrj 			state_available = rootnex_state->r_prealloc_size;
365912f080e7Smrj 			windowp = (rootnex_window_t *)dma->dp_prealloc_buffer;
366012f080e7Smrj 
366112f080e7Smrj 		/*
366212f080e7Smrj 		 * else, we have more free space in the dynamically allocated
366312f080e7Smrj 		 * buffer, i.e. the buffer wasn't worse case fragmented so we
366412f080e7Smrj 		 * didn't need a lot of cookies.
366512f080e7Smrj 		 */
366612f080e7Smrj 		} else {
366712f080e7Smrj 			state_available = dma->dp_cookie_size - space_used;
366812f080e7Smrj 			windowp = (rootnex_window_t *)
366912f080e7Smrj 			    &dma->dp_cookies[sinfo->si_sgl_size];
367012f080e7Smrj 		}
367112f080e7Smrj 
367212f080e7Smrj 	/* we used the pre-alloced buffer */
367312f080e7Smrj 	} else {
367412f080e7Smrj 		ASSERT(space_used <= rootnex_state->r_prealloc_size);
367512f080e7Smrj 		state_available = rootnex_state->r_prealloc_size - space_used;
367612f080e7Smrj 		windowp = (rootnex_window_t *)
367712f080e7Smrj 		    &dma->dp_cookies[sinfo->si_sgl_size];
367812f080e7Smrj 	}
367912f080e7Smrj 
368012f080e7Smrj 	/*
368112f080e7Smrj 	 * figure out how much state we need to track the copy buffer. Add an
368212f080e7Smrj 	 * addition 8 bytes for pointer alignemnt later.
368312f080e7Smrj 	 */
368412f080e7Smrj 	if (dma->dp_copybuf_size > 0) {
368512f080e7Smrj 		copy_state_size = sinfo->si_max_pages *
368612f080e7Smrj 		    sizeof (rootnex_pgmap_t);
368712f080e7Smrj 	} else {
368812f080e7Smrj 		copy_state_size = 0;
368912f080e7Smrj 	}
369012f080e7Smrj 	/* add an additional 8 bytes for pointer alignment */
369112f080e7Smrj 	space_needed = win_state_size + copy_state_size + 0x8;
369212f080e7Smrj 
369312f080e7Smrj 	/* if we have enough space already, use it */
369412f080e7Smrj 	if (state_available >= space_needed) {
369512f080e7Smrj 		dma->dp_window = windowp;
369612f080e7Smrj 		dma->dp_need_to_free_window = B_FALSE;
369712f080e7Smrj 
369812f080e7Smrj 	/* not enough space, need to allocate more. */
369912f080e7Smrj 	} else {
370012f080e7Smrj 		dma->dp_window = kmem_alloc(space_needed, kmflag);
370112f080e7Smrj 		if (dma->dp_window == NULL) {
370212f080e7Smrj 			return (DDI_DMA_NORESOURCES);
370312f080e7Smrj 		}
370412f080e7Smrj 		dma->dp_need_to_free_window = B_TRUE;
370512f080e7Smrj 		dma->dp_window_size = space_needed;
3706ef4ab52fSFrank Van Der Linden 		ROOTNEX_DPROBE2(rootnex__bind__sp__alloc, dev_info_t *,
370712f080e7Smrj 		    dma->dp_dip, size_t, space_needed);
370812f080e7Smrj 	}
370912f080e7Smrj 
371012f080e7Smrj 	/*
371112f080e7Smrj 	 * we allocate copy buffer state and window state at the same time.
371212f080e7Smrj 	 * setup our copy buffer state pointers. Make sure it's aligned.
371312f080e7Smrj 	 */
371412f080e7Smrj 	if (dma->dp_copybuf_size > 0) {
371512f080e7Smrj 		dma->dp_pgmap = (rootnex_pgmap_t *)(((uintptr_t)
371612f080e7Smrj 		    &dma->dp_window[dma->dp_max_win] + 0x7) & ~0x7);
371712f080e7Smrj 
371812f080e7Smrj #if !defined(__amd64)
371912f080e7Smrj 		/*
372012f080e7Smrj 		 * make sure all pm_mapped, pm_vaddr, and pm_pp are set to
372112f080e7Smrj 		 * false/NULL. Should be quicker to bzero vs loop and set.
372212f080e7Smrj 		 */
372312f080e7Smrj 		bzero(dma->dp_pgmap, copy_state_size);
372412f080e7Smrj #endif
372512f080e7Smrj 	} else {
372612f080e7Smrj 		dma->dp_pgmap = NULL;
372712f080e7Smrj 	}
372812f080e7Smrj 
372912f080e7Smrj 	return (DDI_SUCCESS);
373012f080e7Smrj }
373112f080e7Smrj 
373212f080e7Smrj 
373312f080e7Smrj /*
373412f080e7Smrj  * rootnex_teardown_copybuf()
373512f080e7Smrj  *    cleans up after rootnex_setup_copybuf()
373612f080e7Smrj  */
373712f080e7Smrj static void
rootnex_teardown_copybuf(rootnex_dma_t * dma)373812f080e7Smrj rootnex_teardown_copybuf(rootnex_dma_t *dma)
373912f080e7Smrj {
374012f080e7Smrj #if !defined(__amd64)
374112f080e7Smrj 	int i;
374212f080e7Smrj 
374312f080e7Smrj 	/*
374412f080e7Smrj 	 * if we allocated kernel heap VMEM space, go through all the pages and
374512f080e7Smrj 	 * map out any of the ones that we're mapped into the kernel heap VMEM
374612f080e7Smrj 	 * arena. Then free the VMEM space.
374712f080e7Smrj 	 */
374812f080e7Smrj 	if (dma->dp_kva != NULL) {
374912f080e7Smrj 		for (i = 0; i < dma->dp_sglinfo.si_max_pages; i++) {
375012f080e7Smrj 			if (dma->dp_pgmap[i].pm_mapped) {
375112f080e7Smrj 				hat_unload(kas.a_hat, dma->dp_pgmap[i].pm_kaddr,
375212f080e7Smrj 				    MMU_PAGESIZE, HAT_UNLOAD);
375312f080e7Smrj 				dma->dp_pgmap[i].pm_mapped = B_FALSE;
375412f080e7Smrj 			}
375512f080e7Smrj 		}
375612f080e7Smrj 
375712f080e7Smrj 		vmem_free(heap_arena, dma->dp_kva, dma->dp_copybuf_size);
375812f080e7Smrj 	}
375912f080e7Smrj 
376012f080e7Smrj #endif
376112f080e7Smrj 
376212f080e7Smrj 	/* if we allocated a copy buffer, free it */
376312f080e7Smrj 	if (dma->dp_cbaddr != NULL) {
37647b93957cSeota 		i_ddi_mem_free(dma->dp_cbaddr, NULL);
376512f080e7Smrj 	}
376612f080e7Smrj }
376712f080e7Smrj 
376812f080e7Smrj 
376912f080e7Smrj /*
377012f080e7Smrj  * rootnex_teardown_windows()
377112f080e7Smrj  *    cleans up after rootnex_setup_windows()
377212f080e7Smrj  */
377312f080e7Smrj static void
rootnex_teardown_windows(rootnex_dma_t * dma)377412f080e7Smrj rootnex_teardown_windows(rootnex_dma_t *dma)
377512f080e7Smrj {
377612f080e7Smrj 	/*
377712f080e7Smrj 	 * if we had to allocate window state on the last bind (because we
377812f080e7Smrj 	 * didn't have enough pre-allocated space in the handle), free it.
377912f080e7Smrj 	 */
378012f080e7Smrj 	if (dma->dp_need_to_free_window) {
378112f080e7Smrj 		kmem_free(dma->dp_window, dma->dp_window_size);
378212f080e7Smrj 	}
378312f080e7Smrj }
378412f080e7Smrj 
378512f080e7Smrj 
378612f080e7Smrj /*
378712f080e7Smrj  * rootnex_init_win()
378812f080e7Smrj  *    Called in bind slow path during creation of a new window. Initializes
378912f080e7Smrj  *    window state to default values.
379012f080e7Smrj  */
379112f080e7Smrj /*ARGSUSED*/
379212f080e7Smrj static void
rootnex_init_win(ddi_dma_impl_t * hp,rootnex_dma_t * dma,rootnex_window_t * window,ddi_dma_cookie_t * cookie,off_t cur_offset)379312f080e7Smrj rootnex_init_win(ddi_dma_impl_t *hp, rootnex_dma_t *dma,
379412f080e7Smrj     rootnex_window_t *window, ddi_dma_cookie_t *cookie, off_t cur_offset)
379512f080e7Smrj {
379612f080e7Smrj 	hp->dmai_nwin++;
379712f080e7Smrj 	window->wd_dosync = B_FALSE;
379812f080e7Smrj 	window->wd_offset = cur_offset;
379912f080e7Smrj 	window->wd_size = 0;
380012f080e7Smrj 	window->wd_first_cookie = cookie;
380112f080e7Smrj 	window->wd_cookie_cnt = 0;
380212f080e7Smrj 	window->wd_trim.tr_trim_first = B_FALSE;
380312f080e7Smrj 	window->wd_trim.tr_trim_last = B_FALSE;
380412f080e7Smrj 	window->wd_trim.tr_first_copybuf_win = B_FALSE;
380512f080e7Smrj 	window->wd_trim.tr_last_copybuf_win = B_FALSE;
380612f080e7Smrj #if !defined(__amd64)
380712f080e7Smrj 	window->wd_remap_copybuf = dma->dp_cb_remaping;
380812f080e7Smrj #endif
380912f080e7Smrj }
381012f080e7Smrj 
381112f080e7Smrj 
381212f080e7Smrj /*
381312f080e7Smrj  * rootnex_setup_cookie()
381412f080e7Smrj  *    Called in the bind slow path when the sgl uses the copy buffer. If any of
381512f080e7Smrj  *    the sgl uses the copy buffer, we need to go through each cookie, figure
381612f080e7Smrj  *    out if it uses the copy buffer, and if it does, save away everything we'll
381712f080e7Smrj  *    need during sync.
381812f080e7Smrj  */
381912f080e7Smrj static void
rootnex_setup_cookie(ddi_dma_obj_t * dmar_object,rootnex_dma_t * dma,ddi_dma_cookie_t * cookie,off_t cur_offset,size_t * copybuf_used,page_t ** cur_pp)382012f080e7Smrj rootnex_setup_cookie(ddi_dma_obj_t *dmar_object, rootnex_dma_t *dma,
382112f080e7Smrj     ddi_dma_cookie_t *cookie, off_t cur_offset, size_t *copybuf_used,
382212f080e7Smrj     page_t **cur_pp)
382312f080e7Smrj {
382412f080e7Smrj 	boolean_t copybuf_sz_power_2;
382512f080e7Smrj 	rootnex_sglinfo_t *sinfo;
3826843e1988Sjohnlev 	paddr_t paddr;
382712f080e7Smrj 	uint_t pidx;
382812f080e7Smrj 	uint_t pcnt;
382912f080e7Smrj 	off_t poff;
383012f080e7Smrj #if defined(__amd64)
383112f080e7Smrj 	pfn_t pfn;
383212f080e7Smrj #else
383312f080e7Smrj 	page_t **pplist;
383412f080e7Smrj #endif
383512f080e7Smrj 
383650200e77SFrank Van Der Linden 	ASSERT(dmar_object->dmao_type != DMA_OTYP_DVADDR);
383750200e77SFrank Van Der Linden 
383812f080e7Smrj 	sinfo = &dma->dp_sglinfo;
383912f080e7Smrj 
384012f080e7Smrj 	/*
384112f080e7Smrj 	 * Calculate the page index relative to the start of the buffer. The
384212f080e7Smrj 	 * index to the current page for our buffer is the offset into the
384312f080e7Smrj 	 * first page of the buffer plus our current offset into the buffer
384412f080e7Smrj 	 * itself, shifted of course...
384512f080e7Smrj 	 */
384612f080e7Smrj 	pidx = (sinfo->si_buf_offset + cur_offset) >> MMU_PAGESHIFT;
384712f080e7Smrj 	ASSERT(pidx < sinfo->si_max_pages);
384812f080e7Smrj 
384912f080e7Smrj 	/* if this cookie uses the copy buffer */
385012f080e7Smrj 	if (cookie->dmac_type & ROOTNEX_USES_COPYBUF) {
385112f080e7Smrj 		/*
385212f080e7Smrj 		 * NOTE: we know that since this cookie uses the copy buffer, it
385312f080e7Smrj 		 * is <= MMU_PAGESIZE.
385412f080e7Smrj 		 */
385512f080e7Smrj 
385612f080e7Smrj 		/*
385712f080e7Smrj 		 * get the offset into the page. For the 64-bit kernel, get the
385812f080e7Smrj 		 * pfn which we'll use with seg kpm.
385912f080e7Smrj 		 */
3860843e1988Sjohnlev 		poff = cookie->dmac_laddress & MMU_PAGEOFFSET;
386112f080e7Smrj #if defined(__amd64)
3862843e1988Sjohnlev 		/* mfn_to_pfn() is a NOP on i86pc */
3863843e1988Sjohnlev 		pfn = mfn_to_pfn(cookie->dmac_laddress >> MMU_PAGESHIFT);
3864843e1988Sjohnlev #endif /* __amd64 */
386512f080e7Smrj 
386612f080e7Smrj 		/* figure out if the copybuf size is a power of 2 */
3867de710d24SJosef 'Jeff' Sipek 		if (!ISP2(dma->dp_copybuf_size)) {
386812f080e7Smrj 			copybuf_sz_power_2 = B_FALSE;
386912f080e7Smrj 		} else {
387012f080e7Smrj 			copybuf_sz_power_2 = B_TRUE;
387112f080e7Smrj 		}
387212f080e7Smrj 
387312f080e7Smrj 		/* This page uses the copy buffer */
387412f080e7Smrj 		dma->dp_pgmap[pidx].pm_uses_copybuf = B_TRUE;
387512f080e7Smrj 
387612f080e7Smrj 		/*
387712f080e7Smrj 		 * save the copy buffer KVA that we'll use with this page.
387812f080e7Smrj 		 * if we still fit within the copybuf, it's a simple add.
387912f080e7Smrj 		 * otherwise, we need to wrap over using & or % accordingly.
388012f080e7Smrj 		 */
388112f080e7Smrj 		if ((*copybuf_used + MMU_PAGESIZE) <= dma->dp_copybuf_size) {
388212f080e7Smrj 			dma->dp_pgmap[pidx].pm_cbaddr = dma->dp_cbaddr +
388312f080e7Smrj 			    *copybuf_used;
388412f080e7Smrj 		} else {
388512f080e7Smrj 			if (copybuf_sz_power_2) {
388612f080e7Smrj 				dma->dp_pgmap[pidx].pm_cbaddr = (caddr_t)(
388712f080e7Smrj 				    (uintptr_t)dma->dp_cbaddr +
388812f080e7Smrj 				    (*copybuf_used &
388912f080e7Smrj 				    (dma->dp_copybuf_size - 1)));
389012f080e7Smrj 			} else {
389112f080e7Smrj 				dma->dp_pgmap[pidx].pm_cbaddr = (caddr_t)(
389212f080e7Smrj 				    (uintptr_t)dma->dp_cbaddr +
389312f080e7Smrj 				    (*copybuf_used % dma->dp_copybuf_size));
389412f080e7Smrj 			}
389512f080e7Smrj 		}
389612f080e7Smrj 
389712f080e7Smrj 		/*
389812f080e7Smrj 		 * over write the cookie physical address with the address of
389912f080e7Smrj 		 * the physical address of the copy buffer page that we will
390012f080e7Smrj 		 * use.
390112f080e7Smrj 		 */
3902843e1988Sjohnlev 		paddr = pfn_to_pa(hat_getpfnum(kas.a_hat,
390312f080e7Smrj 		    dma->dp_pgmap[pidx].pm_cbaddr)) + poff;
390412f080e7Smrj 
390550200e77SFrank Van Der Linden 		cookie->dmac_laddress = ROOTNEX_PADDR_TO_RBASE(paddr);
3906843e1988Sjohnlev 
390712f080e7Smrj 		/* if we have a kernel VA, it's easy, just save that address */
390812f080e7Smrj 		if ((dmar_object->dmao_type != DMA_OTYP_PAGES) &&
390912f080e7Smrj 		    (sinfo->si_asp == &kas)) {
391012f080e7Smrj 			/*
391112f080e7Smrj 			 * save away the page aligned virtual address of the
391212f080e7Smrj 			 * driver buffer. Offsets are handled in the sync code.
391312f080e7Smrj 			 */
391412f080e7Smrj 			dma->dp_pgmap[pidx].pm_kaddr = (caddr_t)(((uintptr_t)
391512f080e7Smrj 			    dmar_object->dmao_obj.virt_obj.v_addr + cur_offset)
391612f080e7Smrj 			    & MMU_PAGEMASK);
391712f080e7Smrj #if !defined(__amd64)
391812f080e7Smrj 			/*
391912f080e7Smrj 			 * we didn't need to, and will never need to map this
392012f080e7Smrj 			 * page.
392112f080e7Smrj 			 */
392212f080e7Smrj 			dma->dp_pgmap[pidx].pm_mapped = B_FALSE;
392312f080e7Smrj #endif
392412f080e7Smrj 
392512f080e7Smrj 		/* we don't have a kernel VA. We need one for the bcopy. */
392612f080e7Smrj 		} else {
392712f080e7Smrj #if defined(__amd64)
392812f080e7Smrj 			/*
392912f080e7Smrj 			 * for the 64-bit kernel, it's easy. We use seg kpm to
393012f080e7Smrj 			 * get a Kernel VA for the corresponding pfn.
393112f080e7Smrj 			 */
393212f080e7Smrj 			dma->dp_pgmap[pidx].pm_kaddr = hat_kpm_pfn2va(pfn);
393312f080e7Smrj #else
393412f080e7Smrj 			/*
393512f080e7Smrj 			 * for the 32-bit kernel, this is a pain. First we'll
393612f080e7Smrj 			 * save away the page_t or user VA for this page. This
393712f080e7Smrj 			 * is needed in rootnex_dma_win() when we switch to a
393812f080e7Smrj 			 * new window which requires us to re-map the copy
393912f080e7Smrj 			 * buffer.
394012f080e7Smrj 			 */
394112f080e7Smrj 			pplist = dmar_object->dmao_obj.virt_obj.v_priv;
394212f080e7Smrj 			if (dmar_object->dmao_type == DMA_OTYP_PAGES) {
394312f080e7Smrj 				dma->dp_pgmap[pidx].pm_pp = *cur_pp;
394412f080e7Smrj 				dma->dp_pgmap[pidx].pm_vaddr = NULL;
394512f080e7Smrj 			} else if (pplist != NULL) {
394612f080e7Smrj 				dma->dp_pgmap[pidx].pm_pp = pplist[pidx];
394712f080e7Smrj 				dma->dp_pgmap[pidx].pm_vaddr = NULL;
394812f080e7Smrj 			} else {
394912f080e7Smrj 				dma->dp_pgmap[pidx].pm_pp = NULL;
395012f080e7Smrj 				dma->dp_pgmap[pidx].pm_vaddr = (caddr_t)
395112f080e7Smrj 				    (((uintptr_t)
395212f080e7Smrj 				    dmar_object->dmao_obj.virt_obj.v_addr +
395312f080e7Smrj 				    cur_offset) & MMU_PAGEMASK);
395412f080e7Smrj 			}
395512f080e7Smrj 
395612f080e7Smrj 			/*
395712f080e7Smrj 			 * save away the page aligned virtual address which was
395812f080e7Smrj 			 * allocated from the kernel heap arena (taking into
395912f080e7Smrj 			 * account if we need more copy buffer than we alloced
396012f080e7Smrj 			 * and use multiple windows to handle this, i.e. &,%).
396112f080e7Smrj 			 * NOTE: there isn't and physical memory backing up this
396212f080e7Smrj 			 * virtual address space currently.
396312f080e7Smrj 			 */
396412f080e7Smrj 			if ((*copybuf_used + MMU_PAGESIZE) <=
396512f080e7Smrj 			    dma->dp_copybuf_size) {
396612f080e7Smrj 				dma->dp_pgmap[pidx].pm_kaddr = (caddr_t)
396712f080e7Smrj 				    (((uintptr_t)dma->dp_kva + *copybuf_used) &
396812f080e7Smrj 				    MMU_PAGEMASK);
396912f080e7Smrj 			} else {
397012f080e7Smrj 				if (copybuf_sz_power_2) {
397112f080e7Smrj 					dma->dp_pgmap[pidx].pm_kaddr = (caddr_t)
397212f080e7Smrj 					    (((uintptr_t)dma->dp_kva +
397312f080e7Smrj 					    (*copybuf_used &
397412f080e7Smrj 					    (dma->dp_copybuf_size - 1))) &
397512f080e7Smrj 					    MMU_PAGEMASK);
397612f080e7Smrj 				} else {
397712f080e7Smrj 					dma->dp_pgmap[pidx].pm_kaddr = (caddr_t)
397812f080e7Smrj 					    (((uintptr_t)dma->dp_kva +
397912f080e7Smrj 					    (*copybuf_used %
398012f080e7Smrj 					    dma->dp_copybuf_size)) &
398112f080e7Smrj 					    MMU_PAGEMASK);
398212f080e7Smrj 				}
398312f080e7Smrj 			}
398412f080e7Smrj 
398512f080e7Smrj 			/*
398612f080e7Smrj 			 * if we haven't used up the available copy buffer yet,
398712f080e7Smrj 			 * map the kva to the physical page.
398812f080e7Smrj 			 */
398912f080e7Smrj 			if (!dma->dp_cb_remaping && ((*copybuf_used +
399012f080e7Smrj 			    MMU_PAGESIZE) <= dma->dp_copybuf_size)) {
399112f080e7Smrj 				dma->dp_pgmap[pidx].pm_mapped = B_TRUE;
399212f080e7Smrj 				if (dma->dp_pgmap[pidx].pm_pp != NULL) {
399312f080e7Smrj 					i86_pp_map(dma->dp_pgmap[pidx].pm_pp,
399412f080e7Smrj 					    dma->dp_pgmap[pidx].pm_kaddr);
399512f080e7Smrj 				} else {
399612f080e7Smrj 					i86_va_map(dma->dp_pgmap[pidx].pm_vaddr,
399712f080e7Smrj 					    sinfo->si_asp,
399812f080e7Smrj 					    dma->dp_pgmap[pidx].pm_kaddr);
399912f080e7Smrj 				}
400012f080e7Smrj 
400112f080e7Smrj 			/*
400212f080e7Smrj 			 * we've used up the available copy buffer, this page
400312f080e7Smrj 			 * will have to be mapped during rootnex_dma_win() when
400412f080e7Smrj 			 * we switch to a new window which requires a re-map
400512f080e7Smrj 			 * the copy buffer. (32-bit kernel only)
400612f080e7Smrj 			 */
400712f080e7Smrj 			} else {
400812f080e7Smrj 				dma->dp_pgmap[pidx].pm_mapped = B_FALSE;
400912f080e7Smrj 			}
401012f080e7Smrj #endif
401112f080e7Smrj 			/* go to the next page_t */
401212f080e7Smrj 			if (dmar_object->dmao_type == DMA_OTYP_PAGES) {
401312f080e7Smrj 				*cur_pp = (*cur_pp)->p_next;
401412f080e7Smrj 			}
401512f080e7Smrj 		}
401612f080e7Smrj 
401712f080e7Smrj 		/* add to the copy buffer count */
401812f080e7Smrj 		*copybuf_used += MMU_PAGESIZE;
401912f080e7Smrj 
402012f080e7Smrj 	/*
402112f080e7Smrj 	 * This cookie doesn't use the copy buffer. Walk through the pages this
402212f080e7Smrj 	 * cookie occupies to reflect this.
402312f080e7Smrj 	 */
402412f080e7Smrj 	} else {
402512f080e7Smrj 		/*
402612f080e7Smrj 		 * figure out how many pages the cookie occupies. We need to
402712f080e7Smrj 		 * use the original page offset of the buffer and the cookies
402812f080e7Smrj 		 * offset in the buffer to do this.
402912f080e7Smrj 		 */
403012f080e7Smrj 		poff = (sinfo->si_buf_offset + cur_offset) & MMU_PAGEOFFSET;
403112f080e7Smrj 		pcnt = mmu_btopr(cookie->dmac_size + poff);
403212f080e7Smrj 
403312f080e7Smrj 		while (pcnt > 0) {
403412f080e7Smrj #if !defined(__amd64)
403512f080e7Smrj 			/*
403612f080e7Smrj 			 * the 32-bit kernel doesn't have seg kpm, so we need
403712f080e7Smrj 			 * to map in the driver buffer (if it didn't come down
403812f080e7Smrj 			 * with a kernel VA) on the fly. Since this page doesn't
403912f080e7Smrj 			 * use the copy buffer, it's not, or will it ever, have
404012f080e7Smrj 			 * to be mapped in.
404112f080e7Smrj 			 */
404212f080e7Smrj 			dma->dp_pgmap[pidx].pm_mapped = B_FALSE;
404312f080e7Smrj #endif
404412f080e7Smrj 			dma->dp_pgmap[pidx].pm_uses_copybuf = B_FALSE;
404512f080e7Smrj 
404612f080e7Smrj 			/*
404712f080e7Smrj 			 * we need to update pidx and cur_pp or we'll loose
404812f080e7Smrj 			 * track of where we are.
404912f080e7Smrj 			 */
405012f080e7Smrj 			if (dmar_object->dmao_type == DMA_OTYP_PAGES) {
405112f080e7Smrj 				*cur_pp = (*cur_pp)->p_next;
405212f080e7Smrj 			}
405312f080e7Smrj 			pidx++;
405412f080e7Smrj 			pcnt--;
405512f080e7Smrj 		}
405612f080e7Smrj 	}
405712f080e7Smrj }
405812f080e7Smrj 
405912f080e7Smrj 
406012f080e7Smrj /*
406112f080e7Smrj  * rootnex_sgllen_window_boundary()
406212f080e7Smrj  *    Called in the bind slow path when the next cookie causes us to exceed (in
406312f080e7Smrj  *    this case == since we start at 0 and sgllen starts at 1) the maximum sgl
406412f080e7Smrj  *    length supported by the DMA H/W.
406512f080e7Smrj  */
406612f080e7Smrj static int
rootnex_sgllen_window_boundary(ddi_dma_impl_t * hp,rootnex_dma_t * dma,rootnex_window_t ** windowp,ddi_dma_cookie_t * cookie,ddi_dma_attr_t * attr,off_t cur_offset)406712f080e7Smrj rootnex_sgllen_window_boundary(ddi_dma_impl_t *hp, rootnex_dma_t *dma,
406812f080e7Smrj     rootnex_window_t **windowp, ddi_dma_cookie_t *cookie, ddi_dma_attr_t *attr,
406912f080e7Smrj     off_t cur_offset)
407012f080e7Smrj {
407112f080e7Smrj 	off_t new_offset;
407212f080e7Smrj 	size_t trim_sz;
407312f080e7Smrj 	off_t coffset;
407412f080e7Smrj 
407512f080e7Smrj 
407612f080e7Smrj 	/*
407712f080e7Smrj 	 * if we know we'll never have to trim, it's pretty easy. Just move to
407812f080e7Smrj 	 * the next window and init it. We're done.
407912f080e7Smrj 	 */
408012f080e7Smrj 	if (!dma->dp_trim_required) {
408112f080e7Smrj 		(*windowp)++;
408212f080e7Smrj 		rootnex_init_win(hp, dma, *windowp, cookie, cur_offset);
408312f080e7Smrj 		(*windowp)->wd_cookie_cnt++;
408412f080e7Smrj 		(*windowp)->wd_size = cookie->dmac_size;
408512f080e7Smrj 		return (DDI_SUCCESS);
408612f080e7Smrj 	}
408712f080e7Smrj 
408812f080e7Smrj 	/* figure out how much we need to trim from the window */
408912f080e7Smrj 	ASSERT(attr->dma_attr_granular != 0);
409012f080e7Smrj 	if (dma->dp_granularity_power_2) {
409112f080e7Smrj 		trim_sz = (*windowp)->wd_size & (attr->dma_attr_granular - 1);
409212f080e7Smrj 	} else {
409312f080e7Smrj 		trim_sz = (*windowp)->wd_size % attr->dma_attr_granular;
409412f080e7Smrj 	}
409512f080e7Smrj 
409612f080e7Smrj 	/* The window's a whole multiple of granularity. We're done */
409712f080e7Smrj 	if (trim_sz == 0) {
409812f080e7Smrj 		(*windowp)++;
409912f080e7Smrj 		rootnex_init_win(hp, dma, *windowp, cookie, cur_offset);
410012f080e7Smrj 		(*windowp)->wd_cookie_cnt++;
410112f080e7Smrj 		(*windowp)->wd_size = cookie->dmac_size;
410212f080e7Smrj 		return (DDI_SUCCESS);
410312f080e7Smrj 	}
410412f080e7Smrj 
410512f080e7Smrj 	/*
410612f080e7Smrj 	 * The window's not a whole multiple of granularity, since we know this
410712f080e7Smrj 	 * is due to the sgllen, we need to go back to the last cookie and trim
410812f080e7Smrj 	 * that one, add the left over part of the old cookie into the new
410912f080e7Smrj 	 * window, and then add in the new cookie into the new window.
411012f080e7Smrj 	 */
411112f080e7Smrj 
411212f080e7Smrj 	/*
411312f080e7Smrj 	 * make sure the driver isn't making us do something bad... Trimming and
411412f080e7Smrj 	 * sgllen == 1 don't go together.
411512f080e7Smrj 	 */
411612f080e7Smrj 	if (attr->dma_attr_sgllen == 1) {
411712f080e7Smrj 		return (DDI_DMA_NOMAPPING);
411812f080e7Smrj 	}
411912f080e7Smrj 
412012f080e7Smrj 	/*
412112f080e7Smrj 	 * first, setup the current window to account for the trim. Need to go
412212f080e7Smrj 	 * back to the last cookie for this.
412312f080e7Smrj 	 */
412412f080e7Smrj 	cookie--;
412512f080e7Smrj 	(*windowp)->wd_trim.tr_trim_last = B_TRUE;
412612f080e7Smrj 	(*windowp)->wd_trim.tr_last_cookie = cookie;
4127843e1988Sjohnlev 	(*windowp)->wd_trim.tr_last_paddr = cookie->dmac_laddress;
412812f080e7Smrj 	ASSERT(cookie->dmac_size > trim_sz);
412912f080e7Smrj 	(*windowp)->wd_trim.tr_last_size = cookie->dmac_size - trim_sz;
413012f080e7Smrj 	(*windowp)->wd_size -= trim_sz;
413112f080e7Smrj 
413212f080e7Smrj 	/* save the buffer offsets for the next window */
413312f080e7Smrj 	coffset = cookie->dmac_size - trim_sz;
413412f080e7Smrj 	new_offset = (*windowp)->wd_offset + (*windowp)->wd_size;
413512f080e7Smrj 
413612f080e7Smrj 	/*
413712f080e7Smrj 	 * set this now in case this is the first window. all other cases are
413812f080e7Smrj 	 * set in dma_win()
413912f080e7Smrj 	 */
414012f080e7Smrj 	cookie->dmac_size = (*windowp)->wd_trim.tr_last_size;
414112f080e7Smrj 
414212f080e7Smrj 	/*
414312f080e7Smrj 	 * initialize the next window using what's left over in the previous
414412f080e7Smrj 	 * cookie.
414512f080e7Smrj 	 */
414612f080e7Smrj 	(*windowp)++;
414712f080e7Smrj 	rootnex_init_win(hp, dma, *windowp, cookie, new_offset);
414812f080e7Smrj 	(*windowp)->wd_cookie_cnt++;
414912f080e7Smrj 	(*windowp)->wd_trim.tr_trim_first = B_TRUE;
4150843e1988Sjohnlev 	(*windowp)->wd_trim.tr_first_paddr = cookie->dmac_laddress + coffset;
415112f080e7Smrj 	(*windowp)->wd_trim.tr_first_size = trim_sz;
415212f080e7Smrj 	if (cookie->dmac_type & ROOTNEX_USES_COPYBUF) {
415312f080e7Smrj 		(*windowp)->wd_dosync = B_TRUE;
415412f080e7Smrj 	}
415512f080e7Smrj 
415612f080e7Smrj 	/*
415712f080e7Smrj 	 * now go back to the current cookie and add it to the new window. set
415812f080e7Smrj 	 * the new window size to the what was left over from the previous
415912f080e7Smrj 	 * cookie and what's in the current cookie.
416012f080e7Smrj 	 */
416112f080e7Smrj 	cookie++;
416212f080e7Smrj 	(*windowp)->wd_cookie_cnt++;
416312f080e7Smrj 	(*windowp)->wd_size = trim_sz + cookie->dmac_size;
416412f080e7Smrj 
416512f080e7Smrj 	/*
416612f080e7Smrj 	 * trim plus the next cookie could put us over maxxfer (a cookie can be
416712f080e7Smrj 	 * a max size of maxxfer). Handle that case.
416812f080e7Smrj 	 */
416912f080e7Smrj 	if ((*windowp)->wd_size > dma->dp_maxxfer) {
417012f080e7Smrj 		/*
417112f080e7Smrj 		 * maxxfer is already a whole multiple of granularity, and this
417212f080e7Smrj 		 * trim will be <= the previous trim (since a cookie can't be
417312f080e7Smrj 		 * larger than maxxfer). Make things simple here.
417412f080e7Smrj 		 */
417512f080e7Smrj 		trim_sz = (*windowp)->wd_size - dma->dp_maxxfer;
417612f080e7Smrj 		(*windowp)->wd_trim.tr_trim_last = B_TRUE;
417712f080e7Smrj 		(*windowp)->wd_trim.tr_last_cookie = cookie;
4178843e1988Sjohnlev 		(*windowp)->wd_trim.tr_last_paddr = cookie->dmac_laddress;
417912f080e7Smrj 		(*windowp)->wd_trim.tr_last_size = cookie->dmac_size - trim_sz;
418012f080e7Smrj 		(*windowp)->wd_size -= trim_sz;
418112f080e7Smrj 		ASSERT((*windowp)->wd_size == dma->dp_maxxfer);
418212f080e7Smrj 
418312f080e7Smrj 		/* save the buffer offsets for the next window */
418412f080e7Smrj 		coffset = cookie->dmac_size - trim_sz;
418512f080e7Smrj 		new_offset = (*windowp)->wd_offset + (*windowp)->wd_size;
418612f080e7Smrj 
418712f080e7Smrj 		/* setup the next window */
418812f080e7Smrj 		(*windowp)++;
418912f080e7Smrj 		rootnex_init_win(hp, dma, *windowp, cookie, new_offset);
419012f080e7Smrj 		(*windowp)->wd_cookie_cnt++;
419112f080e7Smrj 		(*windowp)->wd_trim.tr_trim_first = B_TRUE;
4192843e1988Sjohnlev 		(*windowp)->wd_trim.tr_first_paddr = cookie->dmac_laddress +
419312f080e7Smrj 		    coffset;
419412f080e7Smrj 		(*windowp)->wd_trim.tr_first_size = trim_sz;
419512f080e7Smrj 	}
419612f080e7Smrj 
419712f080e7Smrj 	return (DDI_SUCCESS);
419812f080e7Smrj }
419912f080e7Smrj 
420012f080e7Smrj 
420112f080e7Smrj /*
420212f080e7Smrj  * rootnex_copybuf_window_boundary()
420312f080e7Smrj  *    Called in bind slowpath when we get to a window boundary because we used
420412f080e7Smrj  *    up all the copy buffer that we have.
420512f080e7Smrj  */
420612f080e7Smrj static int
rootnex_copybuf_window_boundary(ddi_dma_impl_t * hp,rootnex_dma_t * dma,rootnex_window_t ** windowp,ddi_dma_cookie_t * cookie,off_t cur_offset,size_t * copybuf_used)420712f080e7Smrj rootnex_copybuf_window_boundary(ddi_dma_impl_t *hp, rootnex_dma_t *dma,
420812f080e7Smrj     rootnex_window_t **windowp, ddi_dma_cookie_t *cookie, off_t cur_offset,
420912f080e7Smrj     size_t *copybuf_used)
421012f080e7Smrj {
421112f080e7Smrj 	rootnex_sglinfo_t *sinfo;
421212f080e7Smrj 	off_t new_offset;
421312f080e7Smrj 	size_t trim_sz;
4214843e1988Sjohnlev 	paddr_t paddr;
421512f080e7Smrj 	off_t coffset;
421612f080e7Smrj 	uint_t pidx;
421712f080e7Smrj 	off_t poff;
421812f080e7Smrj 
421912f080e7Smrj 
422012f080e7Smrj 	sinfo = &dma->dp_sglinfo;
422112f080e7Smrj 
422212f080e7Smrj 	/*
422312f080e7Smrj 	 * the copy buffer should be a whole multiple of page size. We know that
422412f080e7Smrj 	 * this cookie is <= MMU_PAGESIZE.
422512f080e7Smrj 	 */
422612f080e7Smrj 	ASSERT(cookie->dmac_size <= MMU_PAGESIZE);
422712f080e7Smrj 
422812f080e7Smrj 	/*
422912f080e7Smrj 	 * from now on, all new windows in this bind need to be re-mapped during
423012f080e7Smrj 	 * ddi_dma_getwin() (32-bit kernel only). i.e. we ran out out copybuf
423112f080e7Smrj 	 * space...
423212f080e7Smrj 	 */
423312f080e7Smrj #if !defined(__amd64)
423412f080e7Smrj 	dma->dp_cb_remaping = B_TRUE;
423512f080e7Smrj #endif
423612f080e7Smrj 
423712f080e7Smrj 	/* reset copybuf used */
423812f080e7Smrj 	*copybuf_used = 0;
423912f080e7Smrj 
424012f080e7Smrj 	/*
424112f080e7Smrj 	 * if we don't have to trim (since granularity is set to 1), go to the
424212f080e7Smrj 	 * next window and add the current cookie to it. We know the current
424312f080e7Smrj 	 * cookie uses the copy buffer since we're in this code path.
424412f080e7Smrj 	 */
424512f080e7Smrj 	if (!dma->dp_trim_required) {
424612f080e7Smrj 		(*windowp)++;
424712f080e7Smrj 		rootnex_init_win(hp, dma, *windowp, cookie, cur_offset);
424812f080e7Smrj 
424912f080e7Smrj 		/* Add this cookie to the new window */
425012f080e7Smrj 		(*windowp)->wd_cookie_cnt++;
425112f080e7Smrj 		(*windowp)->wd_size += cookie->dmac_size;
425212f080e7Smrj 		*copybuf_used += MMU_PAGESIZE;
425312f080e7Smrj 		return (DDI_SUCCESS);
425412f080e7Smrj 	}
425512f080e7Smrj 
425612f080e7Smrj 	/*
425712f080e7Smrj 	 * *** may need to trim, figure it out.
425812f080e7Smrj 	 */
425912f080e7Smrj 
426012f080e7Smrj 	/* figure out how much we need to trim from the window */
426112f080e7Smrj 	if (dma->dp_granularity_power_2) {
426212f080e7Smrj 		trim_sz = (*windowp)->wd_size &
426312f080e7Smrj 		    (hp->dmai_attr.dma_attr_granular - 1);
426412f080e7Smrj 	} else {
426512f080e7Smrj 		trim_sz = (*windowp)->wd_size % hp->dmai_attr.dma_attr_granular;
426612f080e7Smrj 	}
426712f080e7Smrj 
426812f080e7Smrj 	/*
426912f080e7Smrj 	 * if the window's a whole multiple of granularity, go to the next
427012f080e7Smrj 	 * window, init it, then add in the current cookie. We know the current
427112f080e7Smrj 	 * cookie uses the copy buffer since we're in this code path.
427212f080e7Smrj 	 */
427312f080e7Smrj 	if (trim_sz == 0) {
427412f080e7Smrj 		(*windowp)++;
427512f080e7Smrj 		rootnex_init_win(hp, dma, *windowp, cookie, cur_offset);
427612f080e7Smrj 
427712f080e7Smrj 		/* Add this cookie to the new window */
427812f080e7Smrj 		(*windowp)->wd_cookie_cnt++;
427912f080e7Smrj 		(*windowp)->wd_size += cookie->dmac_size;
428012f080e7Smrj 		*copybuf_used += MMU_PAGESIZE;
428112f080e7Smrj 		return (DDI_SUCCESS);
428212f080e7Smrj 	}
428312f080e7Smrj 
428412f080e7Smrj 	/*
428512f080e7Smrj 	 * *** We figured it out, we definitly need to trim
428612f080e7Smrj 	 */
428712f080e7Smrj 
428812f080e7Smrj 	/*
428912f080e7Smrj 	 * make sure the driver isn't making us do something bad...
429012f080e7Smrj 	 * Trimming and sgllen == 1 don't go together.
429112f080e7Smrj 	 */
429212f080e7Smrj 	if (hp->dmai_attr.dma_attr_sgllen == 1) {
429312f080e7Smrj 		return (DDI_DMA_NOMAPPING);
429412f080e7Smrj 	}
429512f080e7Smrj 
429612f080e7Smrj 	/*
429712f080e7Smrj 	 * first, setup the current window to account for the trim. Need to go
429812f080e7Smrj 	 * back to the last cookie for this. Some of the last cookie will be in
429912f080e7Smrj 	 * the current window, and some of the last cookie will be in the new
430012f080e7Smrj 	 * window. All of the current cookie will be in the new window.
430112f080e7Smrj 	 */
430212f080e7Smrj 	cookie--;
430312f080e7Smrj 	(*windowp)->wd_trim.tr_trim_last = B_TRUE;
430412f080e7Smrj 	(*windowp)->wd_trim.tr_last_cookie = cookie;
4305843e1988Sjohnlev 	(*windowp)->wd_trim.tr_last_paddr = cookie->dmac_laddress;
430612f080e7Smrj 	ASSERT(cookie->dmac_size > trim_sz);
430712f080e7Smrj 	(*windowp)->wd_trim.tr_last_size = cookie->dmac_size - trim_sz;
430812f080e7Smrj 	(*windowp)->wd_size -= trim_sz;
430912f080e7Smrj 
431012f080e7Smrj 	/*
431112f080e7Smrj 	 * we're trimming the last cookie (not the current cookie). So that
431212f080e7Smrj 	 * last cookie may have or may not have been using the copy buffer (
431312f080e7Smrj 	 * we know the cookie passed in uses the copy buffer since we're in
431412f080e7Smrj 	 * this code path).
431512f080e7Smrj 	 *
431612f080e7Smrj 	 * If the last cookie doesn't use the copy buffer, nothing special to
431712f080e7Smrj 	 * do. However, if it does uses the copy buffer, it will be both the
431812f080e7Smrj 	 * last page in the current window and the first page in the next
431912f080e7Smrj 	 * window. Since we are reusing the copy buffer (and KVA space on the
432012f080e7Smrj 	 * 32-bit kernel), this page will use the end of the copy buffer in the
432112f080e7Smrj 	 * current window, and the start of the copy buffer in the next window.
432212f080e7Smrj 	 * Track that info... The cookie physical address was already set to
432312f080e7Smrj 	 * the copy buffer physical address in setup_cookie..
432412f080e7Smrj 	 */
432512f080e7Smrj 	if (cookie->dmac_type & ROOTNEX_USES_COPYBUF) {
432612f080e7Smrj 		pidx = (sinfo->si_buf_offset + (*windowp)->wd_offset +
432712f080e7Smrj 		    (*windowp)->wd_size) >> MMU_PAGESHIFT;
432812f080e7Smrj 		(*windowp)->wd_trim.tr_last_copybuf_win = B_TRUE;
432912f080e7Smrj 		(*windowp)->wd_trim.tr_last_pidx = pidx;
433012f080e7Smrj 		(*windowp)->wd_trim.tr_last_cbaddr =
433112f080e7Smrj 		    dma->dp_pgmap[pidx].pm_cbaddr;
433212f080e7Smrj #if !defined(__amd64)
433312f080e7Smrj 		(*windowp)->wd_trim.tr_last_kaddr =
433412f080e7Smrj 		    dma->dp_pgmap[pidx].pm_kaddr;
433512f080e7Smrj #endif
433612f080e7Smrj 	}
433712f080e7Smrj 
433812f080e7Smrj 	/* save the buffer offsets for the next window */
433912f080e7Smrj 	coffset = cookie->dmac_size - trim_sz;
434012f080e7Smrj 	new_offset = (*windowp)->wd_offset + (*windowp)->wd_size;
434112f080e7Smrj 
434212f080e7Smrj 	/*
434312f080e7Smrj 	 * set this now in case this is the first window. all other cases are
434412f080e7Smrj 	 * set in dma_win()
434512f080e7Smrj 	 */
434612f080e7Smrj 	cookie->dmac_size = (*windowp)->wd_trim.tr_last_size;
434712f080e7Smrj 
434812f080e7Smrj 	/*
434912f080e7Smrj 	 * initialize the next window using what's left over in the previous
435012f080e7Smrj 	 * cookie.
435112f080e7Smrj 	 */
435212f080e7Smrj 	(*windowp)++;
435312f080e7Smrj 	rootnex_init_win(hp, dma, *windowp, cookie, new_offset);
435412f080e7Smrj 	(*windowp)->wd_cookie_cnt++;
435512f080e7Smrj 	(*windowp)->wd_trim.tr_trim_first = B_TRUE;
4356843e1988Sjohnlev 	(*windowp)->wd_trim.tr_first_paddr = cookie->dmac_laddress + coffset;
435712f080e7Smrj 	(*windowp)->wd_trim.tr_first_size = trim_sz;
435812f080e7Smrj 
435912f080e7Smrj 	/*
436012f080e7Smrj 	 * again, we're tracking if the last cookie uses the copy buffer.
436112f080e7Smrj 	 * read the comment above for more info on why we need to track
436212f080e7Smrj 	 * additional state.
436312f080e7Smrj 	 *
436412f080e7Smrj 	 * For the first cookie in the new window, we need reset the physical
436512f080e7Smrj 	 * address to DMA into to the start of the copy buffer plus any
436612f080e7Smrj 	 * initial page offset which may be present.
436712f080e7Smrj 	 */
436812f080e7Smrj 	if (cookie->dmac_type & ROOTNEX_USES_COPYBUF) {
436912f080e7Smrj 		(*windowp)->wd_dosync = B_TRUE;
437012f080e7Smrj 		(*windowp)->wd_trim.tr_first_copybuf_win = B_TRUE;
437112f080e7Smrj 		(*windowp)->wd_trim.tr_first_pidx = pidx;
437212f080e7Smrj 		(*windowp)->wd_trim.tr_first_cbaddr = dma->dp_cbaddr;
437312f080e7Smrj 		poff = (*windowp)->wd_trim.tr_first_paddr & MMU_PAGEOFFSET;
4374843e1988Sjohnlev 
4375843e1988Sjohnlev 		paddr = pfn_to_pa(hat_getpfnum(kas.a_hat, dma->dp_cbaddr)) +
4376843e1988Sjohnlev 		    poff;
4377843e1988Sjohnlev 		(*windowp)->wd_trim.tr_first_paddr =
437850200e77SFrank Van Der Linden 		    ROOTNEX_PADDR_TO_RBASE(paddr);
4379843e1988Sjohnlev 
438012f080e7Smrj #if !defined(__amd64)
438112f080e7Smrj 		(*windowp)->wd_trim.tr_first_kaddr = dma->dp_kva;
438212f080e7Smrj #endif
438312f080e7Smrj 		/* account for the cookie copybuf usage in the new window */
438412f080e7Smrj 		*copybuf_used += MMU_PAGESIZE;
438512f080e7Smrj 
438612f080e7Smrj 		/*
438712f080e7Smrj 		 * every piece of code has to have a hack, and here is this
438812f080e7Smrj 		 * ones :-)
438912f080e7Smrj 		 *
439012f080e7Smrj 		 * There is a complex interaction between setup_cookie and the
439112f080e7Smrj 		 * copybuf window boundary. The complexity had to be in either
439212f080e7Smrj 		 * the maxxfer window, or the copybuf window, and I chose the
439312f080e7Smrj 		 * copybuf code.
439412f080e7Smrj 		 *
439512f080e7Smrj 		 * So in this code path, we have taken the last cookie,
439612f080e7Smrj 		 * virtually broken it in half due to the trim, and it happens
439712f080e7Smrj 		 * to use the copybuf which further complicates life. At the
439812f080e7Smrj 		 * same time, we have already setup the current cookie, which
439912f080e7Smrj 		 * is now wrong. More background info: the current cookie uses
440012f080e7Smrj 		 * the copybuf, so it is only a page long max. So we need to
440112f080e7Smrj 		 * fix the current cookies copy buffer address, physical
440212f080e7Smrj 		 * address, and kva for the 32-bit kernel. We due this by
440312f080e7Smrj 		 * bumping them by page size (of course, we can't due this on
440412f080e7Smrj 		 * the physical address since the copy buffer may not be
440512f080e7Smrj 		 * physically contiguous).
440612f080e7Smrj 		 */
440712f080e7Smrj 		cookie++;
440812f080e7Smrj 		dma->dp_pgmap[pidx + 1].pm_cbaddr += MMU_PAGESIZE;
4409843e1988Sjohnlev 		poff = cookie->dmac_laddress & MMU_PAGEOFFSET;
4410843e1988Sjohnlev 
4411843e1988Sjohnlev 		paddr = pfn_to_pa(hat_getpfnum(kas.a_hat,
441212f080e7Smrj 		    dma->dp_pgmap[pidx + 1].pm_cbaddr)) + poff;
441350200e77SFrank Van Der Linden 		cookie->dmac_laddress = ROOTNEX_PADDR_TO_RBASE(paddr);
4414843e1988Sjohnlev 
441512f080e7Smrj #if !defined(__amd64)
441612f080e7Smrj 		ASSERT(dma->dp_pgmap[pidx + 1].pm_mapped == B_FALSE);
441712f080e7Smrj 		dma->dp_pgmap[pidx + 1].pm_kaddr += MMU_PAGESIZE;
441812f080e7Smrj #endif
441912f080e7Smrj 	} else {
442012f080e7Smrj 		/* go back to the current cookie */
442112f080e7Smrj 		cookie++;
442212f080e7Smrj 	}
442312f080e7Smrj 
442412f080e7Smrj 	/*
442512f080e7Smrj 	 * add the current cookie to the new window. set the new window size to
442612f080e7Smrj 	 * the what was left over from the previous cookie and what's in the
442712f080e7Smrj 	 * current cookie.
442812f080e7Smrj 	 */
442912f080e7Smrj 	(*windowp)->wd_cookie_cnt++;
443012f080e7Smrj 	(*windowp)->wd_size = trim_sz + cookie->dmac_size;
443112f080e7Smrj 	ASSERT((*windowp)->wd_size < dma->dp_maxxfer);
443212f080e7Smrj 
443312f080e7Smrj 	/*
443412f080e7Smrj 	 * we know that the cookie passed in always uses the copy buffer. We
443512f080e7Smrj 	 * wouldn't be here if it didn't.
443612f080e7Smrj 	 */
443712f080e7Smrj 	*copybuf_used += MMU_PAGESIZE;
443812f080e7Smrj 
443912f080e7Smrj 	return (DDI_SUCCESS);
444012f080e7Smrj }
444112f080e7Smrj 
444212f080e7Smrj 
444312f080e7Smrj /*
444412f080e7Smrj  * rootnex_maxxfer_window_boundary()
444512f080e7Smrj  *    Called in bind slowpath when we get to a window boundary because we will
444612f080e7Smrj  *    go over maxxfer.
444712f080e7Smrj  */
444812f080e7Smrj static int
rootnex_maxxfer_window_boundary(ddi_dma_impl_t * hp,rootnex_dma_t * dma,rootnex_window_t ** windowp,ddi_dma_cookie_t * cookie)444912f080e7Smrj rootnex_maxxfer_window_boundary(ddi_dma_impl_t *hp, rootnex_dma_t *dma,
445012f080e7Smrj     rootnex_window_t **windowp, ddi_dma_cookie_t *cookie)
445112f080e7Smrj {
445212f080e7Smrj 	size_t dmac_size;
445312f080e7Smrj 	off_t new_offset;
445412f080e7Smrj 	size_t trim_sz;
445512f080e7Smrj 	off_t coffset;
445612f080e7Smrj 
445712f080e7Smrj 
445812f080e7Smrj 	/*
445912f080e7Smrj 	 * calculate how much we have to trim off of the current cookie to equal
446012f080e7Smrj 	 * maxxfer. We don't have to account for granularity here since our
446112f080e7Smrj 	 * maxxfer already takes that into account.
446212f080e7Smrj 	 */
446312f080e7Smrj 	trim_sz = ((*windowp)->wd_size + cookie->dmac_size) - dma->dp_maxxfer;
446412f080e7Smrj 	ASSERT(trim_sz <= cookie->dmac_size);
446512f080e7Smrj 	ASSERT(trim_sz <= dma->dp_maxxfer);
446612f080e7Smrj 
446712f080e7Smrj 	/* save cookie size since we need it later and we might change it */
446812f080e7Smrj 	dmac_size = cookie->dmac_size;
446912f080e7Smrj 
447012f080e7Smrj 	/*
447112f080e7Smrj 	 * if we're not trimming the entire cookie, setup the current window to
447212f080e7Smrj 	 * account for the trim.
447312f080e7Smrj 	 */
447412f080e7Smrj 	if (trim_sz < cookie->dmac_size) {
447512f080e7Smrj 		(*windowp)->wd_cookie_cnt++;
447612f080e7Smrj 		(*windowp)->wd_trim.tr_trim_last = B_TRUE;
447712f080e7Smrj 		(*windowp)->wd_trim.tr_last_cookie = cookie;
4478843e1988Sjohnlev 		(*windowp)->wd_trim.tr_last_paddr = cookie->dmac_laddress;
447912f080e7Smrj 		(*windowp)->wd_trim.tr_last_size = cookie->dmac_size - trim_sz;
448012f080e7Smrj 		(*windowp)->wd_size = dma->dp_maxxfer;
448112f080e7Smrj 
448212f080e7Smrj 		/*
448312f080e7Smrj 		 * set the adjusted cookie size now in case this is the first
448412f080e7Smrj 		 * window. All other windows are taken care of in get win
448512f080e7Smrj 		 */
448612f080e7Smrj 		cookie->dmac_size = (*windowp)->wd_trim.tr_last_size;
448712f080e7Smrj 	}
448812f080e7Smrj 
448912f080e7Smrj 	/*
449012f080e7Smrj 	 * coffset is the current offset within the cookie, new_offset is the
449112f080e7Smrj 	 * current offset with the entire buffer.
449212f080e7Smrj 	 */
449312f080e7Smrj 	coffset = dmac_size - trim_sz;
449412f080e7Smrj 	new_offset = (*windowp)->wd_offset + (*windowp)->wd_size;
449512f080e7Smrj 
449612f080e7Smrj 	/* initialize the next window */
449712f080e7Smrj 	(*windowp)++;
449812f080e7Smrj 	rootnex_init_win(hp, dma, *windowp, cookie, new_offset);
449912f080e7Smrj 	(*windowp)->wd_cookie_cnt++;
450012f080e7Smrj 	(*windowp)->wd_size = trim_sz;
450112f080e7Smrj 	if (trim_sz < dmac_size) {
450212f080e7Smrj 		(*windowp)->wd_trim.tr_trim_first = B_TRUE;
4503843e1988Sjohnlev 		(*windowp)->wd_trim.tr_first_paddr = cookie->dmac_laddress +
450412f080e7Smrj 		    coffset;
450512f080e7Smrj 		(*windowp)->wd_trim.tr_first_size = trim_sz;
450612f080e7Smrj 	}
450712f080e7Smrj 
450812f080e7Smrj 	return (DDI_SUCCESS);
450912f080e7Smrj }
451012f080e7Smrj 
451112f080e7Smrj 
451212f080e7Smrj /*ARGSUSED*/
451312f080e7Smrj static int
rootnex_coredma_sync(dev_info_t * dip,dev_info_t * rdip,ddi_dma_handle_t handle,off_t off,size_t len,uint_t cache_flags)451420906b23SVikram Hegde rootnex_coredma_sync(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle,
451512f080e7Smrj     off_t off, size_t len, uint_t cache_flags)
451612f080e7Smrj {
451712f080e7Smrj 	rootnex_sglinfo_t *sinfo;
451812f080e7Smrj 	rootnex_pgmap_t *cbpage;
451912f080e7Smrj 	rootnex_window_t *win;
452012f080e7Smrj 	ddi_dma_impl_t *hp;
452112f080e7Smrj 	rootnex_dma_t *dma;
452212f080e7Smrj 	caddr_t fromaddr;
452312f080e7Smrj 	caddr_t toaddr;
452412f080e7Smrj 	uint_t psize;
452512f080e7Smrj 	off_t offset;
452612f080e7Smrj 	uint_t pidx;
452712f080e7Smrj 	size_t size;
452812f080e7Smrj 	off_t poff;
452912f080e7Smrj 	int e;
453012f080e7Smrj 
453112f080e7Smrj 
453212f080e7Smrj 	hp = (ddi_dma_impl_t *)handle;
453312f080e7Smrj 	dma = (rootnex_dma_t *)hp->dmai_private;
453412f080e7Smrj 	sinfo = &dma->dp_sglinfo;
453512f080e7Smrj 
453612f080e7Smrj 	/*
453712f080e7Smrj 	 * if we don't have any windows, we don't need to sync. A copybuf
453812f080e7Smrj 	 * will cause us to have at least one window.
453912f080e7Smrj 	 */
454012f080e7Smrj 	if (dma->dp_window == NULL) {
454112f080e7Smrj 		return (DDI_SUCCESS);
454212f080e7Smrj 	}
454312f080e7Smrj 
454412f080e7Smrj 	/* This window may not need to be sync'd */
454512f080e7Smrj 	win = &dma->dp_window[dma->dp_current_win];
454612f080e7Smrj 	if (!win->wd_dosync) {
454712f080e7Smrj 		return (DDI_SUCCESS);
454812f080e7Smrj 	}
454912f080e7Smrj 
455012f080e7Smrj 	/* handle off and len special cases */
455112f080e7Smrj 	if ((off == 0) || (rootnex_sync_ignore_params)) {
455212f080e7Smrj 		offset = win->wd_offset;
455312f080e7Smrj 	} else {
455412f080e7Smrj 		offset = off;
455512f080e7Smrj 	}
455612f080e7Smrj 	if ((len == 0) || (rootnex_sync_ignore_params)) {
455712f080e7Smrj 		size = win->wd_size;
455812f080e7Smrj 	} else {
455912f080e7Smrj 		size = len;
456012f080e7Smrj 	}
456112f080e7Smrj 
456212f080e7Smrj 	/* check the sync args to make sure they make a little sense */
456312f080e7Smrj 	if (rootnex_sync_check_parms) {
456412f080e7Smrj 		e = rootnex_valid_sync_parms(hp, win, offset, size,
456512f080e7Smrj 		    cache_flags);
456612f080e7Smrj 		if (e != DDI_SUCCESS) {
4567ef4ab52fSFrank Van Der Linden 			ROOTNEX_DPROF_INC(&rootnex_cnt[ROOTNEX_CNT_SYNC_FAIL]);
456812f080e7Smrj 			return (DDI_FAILURE);
456912f080e7Smrj 		}
457012f080e7Smrj 	}
457112f080e7Smrj 
457212f080e7Smrj 	/*
457312f080e7Smrj 	 * special case the first page to handle the offset into the page. The
457412f080e7Smrj 	 * offset to the current page for our buffer is the offset into the
457512f080e7Smrj 	 * first page of the buffer plus our current offset into the buffer
457612f080e7Smrj 	 * itself, masked of course.
457712f080e7Smrj 	 */
457812f080e7Smrj 	poff = (sinfo->si_buf_offset + offset) & MMU_PAGEOFFSET;
457912f080e7Smrj 	psize = MIN((MMU_PAGESIZE - poff), size);
458012f080e7Smrj 
458112f080e7Smrj 	/* go through all the pages that we want to sync */
458212f080e7Smrj 	while (size > 0) {
458312f080e7Smrj 		/*
458412f080e7Smrj 		 * Calculate the page index relative to the start of the buffer.
458512f080e7Smrj 		 * The index to the current page for our buffer is the offset
458612f080e7Smrj 		 * into the first page of the buffer plus our current offset
458712f080e7Smrj 		 * into the buffer itself, shifted of course...
458812f080e7Smrj 		 */
458912f080e7Smrj 		pidx = (sinfo->si_buf_offset + offset) >> MMU_PAGESHIFT;
459012f080e7Smrj 		ASSERT(pidx < sinfo->si_max_pages);
459112f080e7Smrj 
459212f080e7Smrj 		/*
459312f080e7Smrj 		 * if this page uses the copy buffer, we need to sync it,
459412f080e7Smrj 		 * otherwise, go on to the next page.
459512f080e7Smrj 		 */
459612f080e7Smrj 		cbpage = &dma->dp_pgmap[pidx];
459712f080e7Smrj 		ASSERT((cbpage->pm_uses_copybuf == B_TRUE) ||
459812f080e7Smrj 		    (cbpage->pm_uses_copybuf == B_FALSE));
459912f080e7Smrj 		if (cbpage->pm_uses_copybuf) {
460012f080e7Smrj 			/* cbaddr and kaddr should be page aligned */
460112f080e7Smrj 			ASSERT(((uintptr_t)cbpage->pm_cbaddr &
460212f080e7Smrj 			    MMU_PAGEOFFSET) == 0);
460312f080e7Smrj 			ASSERT(((uintptr_t)cbpage->pm_kaddr &
460412f080e7Smrj 			    MMU_PAGEOFFSET) == 0);
460512f080e7Smrj 
460612f080e7Smrj 			/*
460712f080e7Smrj 			 * if we're copying for the device, we are going to
460812f080e7Smrj 			 * copy from the drivers buffer and to the rootnex
460912f080e7Smrj 			 * allocated copy buffer.
461012f080e7Smrj 			 */
461112f080e7Smrj 			if (cache_flags == DDI_DMA_SYNC_FORDEV) {
461212f080e7Smrj 				fromaddr = cbpage->pm_kaddr + poff;
461312f080e7Smrj 				toaddr = cbpage->pm_cbaddr + poff;
4614ef4ab52fSFrank Van Der Linden 				ROOTNEX_DPROBE2(rootnex__sync__dev,
461512f080e7Smrj 				    dev_info_t *, dma->dp_dip, size_t, psize);
461612f080e7Smrj 
461712f080e7Smrj 			/*
461812f080e7Smrj 			 * if we're copying for the cpu/kernel, we are going to
461912f080e7Smrj 			 * copy from the rootnex allocated copy buffer to the
462012f080e7Smrj 			 * drivers buffer.
462112f080e7Smrj 			 */
462212f080e7Smrj 			} else {
462312f080e7Smrj 				fromaddr = cbpage->pm_cbaddr + poff;
462412f080e7Smrj 				toaddr = cbpage->pm_kaddr + poff;
4625ef4ab52fSFrank Van Der Linden 				ROOTNEX_DPROBE2(rootnex__sync__cpu,
462612f080e7Smrj 				    dev_info_t *, dma->dp_dip, size_t, psize);
462712f080e7Smrj 			}
462812f080e7Smrj 
462912f080e7Smrj 			bcopy(fromaddr, toaddr, psize);
463012f080e7Smrj 		}
463112f080e7Smrj 
463212f080e7Smrj 		/*
463312f080e7Smrj 		 * decrement size until we're done, update our offset into the
463412f080e7Smrj 		 * buffer, and get the next page size.
463512f080e7Smrj 		 */
463612f080e7Smrj 		size -= psize;
463712f080e7Smrj 		offset += psize;
463812f080e7Smrj 		psize = MIN(MMU_PAGESIZE, size);
463912f080e7Smrj 
464012f080e7Smrj 		/* page offset is zero for the rest of this loop */
464112f080e7Smrj 		poff = 0;
464212f080e7Smrj 	}
464312f080e7Smrj 
464412f080e7Smrj 	return (DDI_SUCCESS);
464512f080e7Smrj }
464612f080e7Smrj 
464720906b23SVikram Hegde /*
464820906b23SVikram Hegde  * rootnex_dma_sync()
464920906b23SVikram Hegde  *    called from ddi_dma_sync() if DMP_NOSYNC is not set in hp->dmai_rflags.
465020906b23SVikram Hegde  *    We set DMP_NOSYNC if we're not using the copy buffer. If DMP_NOSYNC
465120906b23SVikram Hegde  *    is set, ddi_dma_sync() returns immediately passing back success.
465220906b23SVikram Hegde  */
465320906b23SVikram Hegde /*ARGSUSED*/
465420906b23SVikram Hegde static int
rootnex_dma_sync(dev_info_t * dip,dev_info_t * rdip,ddi_dma_handle_t handle,off_t off,size_t len,uint_t cache_flags)465520906b23SVikram Hegde rootnex_dma_sync(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle,
465620906b23SVikram Hegde     off_t off, size_t len, uint_t cache_flags)
465720906b23SVikram Hegde {
46583a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
4659b51bbbf5SVikram Hegde 	if (IOMMU_USED(rdip)) {
466020906b23SVikram Hegde 		return (iommulib_nexdma_sync(dip, rdip, handle, off, len,
466120906b23SVikram Hegde 		    cache_flags));
466220906b23SVikram Hegde 	}
466320906b23SVikram Hegde #endif
466420906b23SVikram Hegde 	return (rootnex_coredma_sync(dip, rdip, handle, off, len,
466520906b23SVikram Hegde 	    cache_flags));
466620906b23SVikram Hegde }
466712f080e7Smrj 
466812f080e7Smrj /*
466912f080e7Smrj  * rootnex_valid_sync_parms()
467012f080e7Smrj  *    checks the parameters passed to sync to verify they are correct.
467112f080e7Smrj  */
467212f080e7Smrj static int
rootnex_valid_sync_parms(ddi_dma_impl_t * hp,rootnex_window_t * win,off_t offset,size_t size,uint_t cache_flags)467312f080e7Smrj rootnex_valid_sync_parms(ddi_dma_impl_t *hp, rootnex_window_t *win,
467412f080e7Smrj     off_t offset, size_t size, uint_t cache_flags)
467512f080e7Smrj {
467612f080e7Smrj 	off_t woffset;
467712f080e7Smrj 
467812f080e7Smrj 
467912f080e7Smrj 	/*
468012f080e7Smrj 	 * the first part of the test to make sure the offset passed in is
468112f080e7Smrj 	 * within the window.
468212f080e7Smrj 	 */
468312f080e7Smrj 	if (offset < win->wd_offset) {
468412f080e7Smrj 		return (DDI_FAILURE);
468512f080e7Smrj 	}
468612f080e7Smrj 
468712f080e7Smrj 	/*
468812f080e7Smrj 	 * second and last part of the test to make sure the offset and length
468912f080e7Smrj 	 * passed in is within the window.
469012f080e7Smrj 	 */
469112f080e7Smrj 	woffset = offset - win->wd_offset;
469212f080e7Smrj 	if ((woffset + size) > win->wd_size) {
469312f080e7Smrj 		return (DDI_FAILURE);
469412f080e7Smrj 	}
469512f080e7Smrj 
469612f080e7Smrj 	/*
469712f080e7Smrj 	 * if we are sync'ing for the device, the DDI_DMA_WRITE flag should
469812f080e7Smrj 	 * be set too.
469912f080e7Smrj 	 */
470012f080e7Smrj 	if ((cache_flags == DDI_DMA_SYNC_FORDEV) &&
470112f080e7Smrj 	    (hp->dmai_rflags & DDI_DMA_WRITE)) {
470212f080e7Smrj 		return (DDI_SUCCESS);
470312f080e7Smrj 	}
470412f080e7Smrj 
470512f080e7Smrj 	/*
470612f080e7Smrj 	 * at this point, either DDI_DMA_SYNC_FORCPU or DDI_DMA_SYNC_FORKERNEL
470712f080e7Smrj 	 * should be set. Also DDI_DMA_READ should be set in the flags.
470812f080e7Smrj 	 */
470912f080e7Smrj 	if (((cache_flags == DDI_DMA_SYNC_FORCPU) ||
471012f080e7Smrj 	    (cache_flags == DDI_DMA_SYNC_FORKERNEL)) &&
471112f080e7Smrj 	    (hp->dmai_rflags & DDI_DMA_READ)) {
471212f080e7Smrj 		return (DDI_SUCCESS);
471312f080e7Smrj 	}
471412f080e7Smrj 
471512f080e7Smrj 	return (DDI_FAILURE);
471612f080e7Smrj }
471712f080e7Smrj 
471812f080e7Smrj 
471912f080e7Smrj /*ARGSUSED*/
472012f080e7Smrj static int
rootnex_coredma_win(dev_info_t * dip,dev_info_t * rdip,ddi_dma_handle_t handle,uint_t win,off_t * offp,size_t * lenp,ddi_dma_cookie_t * cookiep,uint_t * ccountp)472120906b23SVikram Hegde rootnex_coredma_win(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle,
472212f080e7Smrj     uint_t win, off_t *offp, size_t *lenp, ddi_dma_cookie_t *cookiep,
472312f080e7Smrj     uint_t *ccountp)
472412f080e7Smrj {
472512f080e7Smrj 	rootnex_window_t *window;
472612f080e7Smrj 	rootnex_trim_t *trim;
472712f080e7Smrj 	ddi_dma_impl_t *hp;
472812f080e7Smrj 	rootnex_dma_t *dma;
472950200e77SFrank Van Der Linden 	ddi_dma_obj_t *dmao;
473012f080e7Smrj #if !defined(__amd64)
473112f080e7Smrj 	rootnex_sglinfo_t *sinfo;
473212f080e7Smrj 	rootnex_pgmap_t *pmap;
473312f080e7Smrj 	uint_t pidx;
473412f080e7Smrj 	uint_t pcnt;
473512f080e7Smrj 	off_t poff;
473612f080e7Smrj 	int i;
473712f080e7Smrj #endif
473812f080e7Smrj 
473912f080e7Smrj 
474012f080e7Smrj 	hp = (ddi_dma_impl_t *)handle;
474112f080e7Smrj 	dma = (rootnex_dma_t *)hp->dmai_private;
474212f080e7Smrj #if !defined(__amd64)
474312f080e7Smrj 	sinfo = &dma->dp_sglinfo;
474412f080e7Smrj #endif
474512f080e7Smrj 
474612f080e7Smrj 	/* If we try and get a window which doesn't exist, return failure */
474712f080e7Smrj 	if (win >= hp->dmai_nwin) {
4748ef4ab52fSFrank Van Der Linden 		ROOTNEX_DPROF_INC(&rootnex_cnt[ROOTNEX_CNT_GETWIN_FAIL]);
474912f080e7Smrj 		return (DDI_FAILURE);
475012f080e7Smrj 	}
475112f080e7Smrj 
4752bd155b87SGarrett D'Amore 	dmao = dma->dp_dvma_used ? &dma->dp_dvma : &dma->dp_dma;
475350200e77SFrank Van Der Linden 
475412f080e7Smrj 	/*
475512f080e7Smrj 	 * if we don't have any windows, and they're asking for the first
475612f080e7Smrj 	 * window, setup the cookie pointer to the first cookie in the bind.
475712f080e7Smrj 	 * setup our return values, then increment the cookie since we return
475812f080e7Smrj 	 * the first cookie on the stack.
475912f080e7Smrj 	 */
476012f080e7Smrj 	if (dma->dp_window == NULL) {
476112f080e7Smrj 		if (win != 0) {
4762ef4ab52fSFrank Van Der Linden 			ROOTNEX_DPROF_INC(
4763ef4ab52fSFrank Van Der Linden 			    &rootnex_cnt[ROOTNEX_CNT_GETWIN_FAIL]);
476412f080e7Smrj 			return (DDI_FAILURE);
476512f080e7Smrj 		}
476612f080e7Smrj 		hp->dmai_cookie = dma->dp_cookies;
476712f080e7Smrj 		*offp = 0;
476850200e77SFrank Van Der Linden 		*lenp = dmao->dmao_size;
476912f080e7Smrj 		*ccountp = dma->dp_sglinfo.si_sgl_size;
477012f080e7Smrj 		*cookiep = hp->dmai_cookie[0];
477112f080e7Smrj 		hp->dmai_cookie++;
477212f080e7Smrj 		return (DDI_SUCCESS);
477312f080e7Smrj 	}
477412f080e7Smrj 
477512f080e7Smrj 	/* sync the old window before moving on to the new one */
477612f080e7Smrj 	window = &dma->dp_window[dma->dp_current_win];
477712f080e7Smrj 	if ((window->wd_dosync) && (hp->dmai_rflags & DDI_DMA_READ)) {
477894f1124eSVikram Hegde 		(void) rootnex_coredma_sync(dip, rdip, handle, 0, 0,
477912f080e7Smrj 		    DDI_DMA_SYNC_FORCPU);
478012f080e7Smrj 	}
478112f080e7Smrj 
478212f080e7Smrj #if !defined(__amd64)
478312f080e7Smrj 	/*
478412f080e7Smrj 	 * before we move to the next window, if we need to re-map, unmap all
478512f080e7Smrj 	 * the pages in this window.
478612f080e7Smrj 	 */
478712f080e7Smrj 	if (dma->dp_cb_remaping) {
478812f080e7Smrj 		/*
478912f080e7Smrj 		 * If we switch to this window again, we'll need to map in
479012f080e7Smrj 		 * on the fly next time.
479112f080e7Smrj 		 */
479212f080e7Smrj 		window->wd_remap_copybuf = B_TRUE;
479312f080e7Smrj 
479412f080e7Smrj 		/*
479512f080e7Smrj 		 * calculate the page index into the buffer where this window
479612f080e7Smrj 		 * starts, and the number of pages this window takes up.
479712f080e7Smrj 		 */
479812f080e7Smrj 		pidx = (sinfo->si_buf_offset + window->wd_offset) >>
479912f080e7Smrj 		    MMU_PAGESHIFT;
480012f080e7Smrj 		poff = (sinfo->si_buf_offset + window->wd_offset) &
480112f080e7Smrj 		    MMU_PAGEOFFSET;
480212f080e7Smrj 		pcnt = mmu_btopr(window->wd_size + poff);
480312f080e7Smrj 		ASSERT((pidx + pcnt) <= sinfo->si_max_pages);
480412f080e7Smrj 
480512f080e7Smrj 		/* unmap pages which are currently mapped in this window */
480612f080e7Smrj 		for (i = 0; i < pcnt; i++) {
480712f080e7Smrj 			if (dma->dp_pgmap[pidx].pm_mapped) {
480812f080e7Smrj 				hat_unload(kas.a_hat,
480912f080e7Smrj 				    dma->dp_pgmap[pidx].pm_kaddr, MMU_PAGESIZE,
481012f080e7Smrj 				    HAT_UNLOAD);
481112f080e7Smrj 				dma->dp_pgmap[pidx].pm_mapped = B_FALSE;
481212f080e7Smrj 			}
481312f080e7Smrj 			pidx++;
481412f080e7Smrj 		}
481512f080e7Smrj 	}
481612f080e7Smrj #endif
481712f080e7Smrj 
481812f080e7Smrj 	/*
481912f080e7Smrj 	 * Move to the new window.
482012f080e7Smrj 	 * NOTE: current_win must be set for sync to work right
482112f080e7Smrj 	 */
482212f080e7Smrj 	dma->dp_current_win = win;
482312f080e7Smrj 	window = &dma->dp_window[win];
482412f080e7Smrj 
482512f080e7Smrj 	/* if needed, adjust the first and/or last cookies for trim */
482612f080e7Smrj 	trim = &window->wd_trim;
482712f080e7Smrj 	if (trim->tr_trim_first) {
4828843e1988Sjohnlev 		window->wd_first_cookie->dmac_laddress = trim->tr_first_paddr;
482912f080e7Smrj 		window->wd_first_cookie->dmac_size = trim->tr_first_size;
483012f080e7Smrj #if !defined(__amd64)
483112f080e7Smrj 		window->wd_first_cookie->dmac_type =
483212f080e7Smrj 		    (window->wd_first_cookie->dmac_type &
483312f080e7Smrj 		    ROOTNEX_USES_COPYBUF) + window->wd_offset;
483412f080e7Smrj #endif
483512f080e7Smrj 		if (trim->tr_first_copybuf_win) {
483612f080e7Smrj 			dma->dp_pgmap[trim->tr_first_pidx].pm_cbaddr =
483712f080e7Smrj 			    trim->tr_first_cbaddr;
483812f080e7Smrj #if !defined(__amd64)
483912f080e7Smrj 			dma->dp_pgmap[trim->tr_first_pidx].pm_kaddr =
484012f080e7Smrj 			    trim->tr_first_kaddr;
484112f080e7Smrj #endif
484212f080e7Smrj 		}
484312f080e7Smrj 	}
484412f080e7Smrj 	if (trim->tr_trim_last) {
4845843e1988Sjohnlev 		trim->tr_last_cookie->dmac_laddress = trim->tr_last_paddr;
484612f080e7Smrj 		trim->tr_last_cookie->dmac_size = trim->tr_last_size;
484712f080e7Smrj 		if (trim->tr_last_copybuf_win) {
484812f080e7Smrj 			dma->dp_pgmap[trim->tr_last_pidx].pm_cbaddr =
484912f080e7Smrj 			    trim->tr_last_cbaddr;
485012f080e7Smrj #if !defined(__amd64)
485112f080e7Smrj 			dma->dp_pgmap[trim->tr_last_pidx].pm_kaddr =
485212f080e7Smrj 			    trim->tr_last_kaddr;
485312f080e7Smrj #endif
485412f080e7Smrj 		}
485512f080e7Smrj 	}
485612f080e7Smrj 
485712f080e7Smrj 	/*
485812f080e7Smrj 	 * setup the cookie pointer to the first cookie in the window. setup
485912f080e7Smrj 	 * our return values, then increment the cookie since we return the
486012f080e7Smrj 	 * first cookie on the stack.
486112f080e7Smrj 	 */
486212f080e7Smrj 	hp->dmai_cookie = window->wd_first_cookie;
486312f080e7Smrj 	*offp = window->wd_offset;
486412f080e7Smrj 	*lenp = window->wd_size;
486512f080e7Smrj 	*ccountp = window->wd_cookie_cnt;
486612f080e7Smrj 	*cookiep = hp->dmai_cookie[0];
486712f080e7Smrj 	hp->dmai_cookie++;
486812f080e7Smrj 
486912f080e7Smrj #if !defined(__amd64)
487012f080e7Smrj 	/* re-map copybuf if required for this window */
487112f080e7Smrj 	if (dma->dp_cb_remaping) {
487212f080e7Smrj 		/*
487312f080e7Smrj 		 * calculate the page index into the buffer where this
487412f080e7Smrj 		 * window starts.
487512f080e7Smrj 		 */
487612f080e7Smrj 		pidx = (sinfo->si_buf_offset + window->wd_offset) >>
487712f080e7Smrj 		    MMU_PAGESHIFT;
487812f080e7Smrj 		ASSERT(pidx < sinfo->si_max_pages);
487912f080e7Smrj 
488012f080e7Smrj 		/*
488112f080e7Smrj 		 * the first page can get unmapped if it's shared with the
488212f080e7Smrj 		 * previous window. Even if the rest of this window is already
488312f080e7Smrj 		 * mapped in, we need to still check this one.
488412f080e7Smrj 		 */
488512f080e7Smrj 		pmap = &dma->dp_pgmap[pidx];
488612f080e7Smrj 		if ((pmap->pm_uses_copybuf) && (pmap->pm_mapped == B_FALSE)) {
488712f080e7Smrj 			if (pmap->pm_pp != NULL) {
488812f080e7Smrj 				pmap->pm_mapped = B_TRUE;
488912f080e7Smrj 				i86_pp_map(pmap->pm_pp, pmap->pm_kaddr);
489012f080e7Smrj 			} else if (pmap->pm_vaddr != NULL) {
489112f080e7Smrj 				pmap->pm_mapped = B_TRUE;
489212f080e7Smrj 				i86_va_map(pmap->pm_vaddr, sinfo->si_asp,
489312f080e7Smrj 				    pmap->pm_kaddr);
489412f080e7Smrj 			}
489512f080e7Smrj 		}
489612f080e7Smrj 		pidx++;
489712f080e7Smrj 
489812f080e7Smrj 		/* map in the rest of the pages if required */
489912f080e7Smrj 		if (window->wd_remap_copybuf) {
490012f080e7Smrj 			window->wd_remap_copybuf = B_FALSE;
490112f080e7Smrj 
490212f080e7Smrj 			/* figure out many pages this window takes up */
490312f080e7Smrj 			poff = (sinfo->si_buf_offset + window->wd_offset) &
490412f080e7Smrj 			    MMU_PAGEOFFSET;
490512f080e7Smrj 			pcnt = mmu_btopr(window->wd_size + poff);
490612f080e7Smrj 			ASSERT(((pidx - 1) + pcnt) <= sinfo->si_max_pages);
490712f080e7Smrj 
490812f080e7Smrj 			/* map pages which require it */
490912f080e7Smrj 			for (i = 1; i < pcnt; i++) {
491012f080e7Smrj 				pmap = &dma->dp_pgmap[pidx];
491112f080e7Smrj 				if (pmap->pm_uses_copybuf) {
491212f080e7Smrj 					ASSERT(pmap->pm_mapped == B_FALSE);
491312f080e7Smrj 					if (pmap->pm_pp != NULL) {
491412f080e7Smrj 						pmap->pm_mapped = B_TRUE;
491512f080e7Smrj 						i86_pp_map(pmap->pm_pp,
491612f080e7Smrj 						    pmap->pm_kaddr);
491712f080e7Smrj 					} else if (pmap->pm_vaddr != NULL) {
491812f080e7Smrj 						pmap->pm_mapped = B_TRUE;
491912f080e7Smrj 						i86_va_map(pmap->pm_vaddr,
492012f080e7Smrj 						    sinfo->si_asp,
492112f080e7Smrj 						    pmap->pm_kaddr);
492212f080e7Smrj 					}
492312f080e7Smrj 				}
492412f080e7Smrj 				pidx++;
492512f080e7Smrj 			}
492612f080e7Smrj 		}
492712f080e7Smrj 	}
492812f080e7Smrj #endif
492912f080e7Smrj 
493012f080e7Smrj 	/* if the new window uses the copy buffer, sync it for the device */
493112f080e7Smrj 	if ((window->wd_dosync) && (hp->dmai_rflags & DDI_DMA_WRITE)) {
493294f1124eSVikram Hegde 		(void) rootnex_coredma_sync(dip, rdip, handle, 0, 0,
493312f080e7Smrj 		    DDI_DMA_SYNC_FORDEV);
493412f080e7Smrj 	}
493512f080e7Smrj 
493612f080e7Smrj 	return (DDI_SUCCESS);
493712f080e7Smrj }
493812f080e7Smrj 
493920906b23SVikram Hegde /*
494020906b23SVikram Hegde  * rootnex_dma_win()
494120906b23SVikram Hegde  *    called from ddi_dma_getwin()
494220906b23SVikram Hegde  */
494320906b23SVikram Hegde /*ARGSUSED*/
494420906b23SVikram Hegde static int
rootnex_dma_win(dev_info_t * dip,dev_info_t * rdip,ddi_dma_handle_t handle,uint_t win,off_t * offp,size_t * lenp,ddi_dma_cookie_t * cookiep,uint_t * ccountp)494520906b23SVikram Hegde rootnex_dma_win(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle,
494620906b23SVikram Hegde     uint_t win, off_t *offp, size_t *lenp, ddi_dma_cookie_t *cookiep,
494720906b23SVikram Hegde     uint_t *ccountp)
494820906b23SVikram Hegde {
49493a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
4950b51bbbf5SVikram Hegde 	if (IOMMU_USED(rdip)) {
495120906b23SVikram Hegde 		return (iommulib_nexdma_win(dip, rdip, handle, win, offp, lenp,
495220906b23SVikram Hegde 		    cookiep, ccountp));
495320906b23SVikram Hegde 	}
495420906b23SVikram Hegde #endif
495512f080e7Smrj 
495620906b23SVikram Hegde 	return (rootnex_coredma_win(dip, rdip, handle, win, offp, lenp,
495720906b23SVikram Hegde 	    cookiep, ccountp));
495820906b23SVikram Hegde }
495912f080e7Smrj 
4960ee7536e8SFrank Van Der Linden #if defined(__amd64) && !defined(__xpv)
496150200e77SFrank Van Der Linden /*ARGSUSED*/
496250200e77SFrank Van Der Linden static int
rootnex_coredma_hdl_setprivate(dev_info_t * dip,dev_info_t * rdip,ddi_dma_handle_t handle,void * v)496350200e77SFrank Van Der Linden rootnex_coredma_hdl_setprivate(dev_info_t *dip, dev_info_t *rdip,
496450200e77SFrank Van Der Linden     ddi_dma_handle_t handle, void *v)
496550200e77SFrank Van Der Linden {
496650200e77SFrank Van Der Linden 	ddi_dma_impl_t *hp;
496750200e77SFrank Van Der Linden 	rootnex_dma_t *dma;
496850200e77SFrank Van Der Linden 
496950200e77SFrank Van Der Linden 	hp = (ddi_dma_impl_t *)handle;
497050200e77SFrank Van Der Linden 	dma = (rootnex_dma_t *)hp->dmai_private;
497150200e77SFrank Van Der Linden 	dma->dp_iommu_private = v;
497250200e77SFrank Van Der Linden 
497350200e77SFrank Van Der Linden 	return (DDI_SUCCESS);
497450200e77SFrank Van Der Linden }
497550200e77SFrank Van Der Linden 
497650200e77SFrank Van Der Linden /*ARGSUSED*/
497750200e77SFrank Van Der Linden static void *
rootnex_coredma_hdl_getprivate(dev_info_t * dip,dev_info_t * rdip,ddi_dma_handle_t handle)497850200e77SFrank Van Der Linden rootnex_coredma_hdl_getprivate(dev_info_t *dip, dev_info_t *rdip,
497950200e77SFrank Van Der Linden     ddi_dma_handle_t handle)
498050200e77SFrank Van Der Linden {
498150200e77SFrank Van Der Linden 	ddi_dma_impl_t *hp;
498250200e77SFrank Van Der Linden 	rootnex_dma_t *dma;
498350200e77SFrank Van Der Linden 
498450200e77SFrank Van Der Linden 	hp = (ddi_dma_impl_t *)handle;
498550200e77SFrank Van Der Linden 	dma = (rootnex_dma_t *)hp->dmai_private;
498650200e77SFrank Van Der Linden 
498750200e77SFrank Van Der Linden 	return (dma->dp_iommu_private);
498850200e77SFrank Van Der Linden }
4989ee7536e8SFrank Van Der Linden #endif
499050200e77SFrank Van Der Linden 
499112f080e7Smrj /*
499212f080e7Smrj  * ************************
499312f080e7Smrj  *  obsoleted dma routines
499412f080e7Smrj  * ************************
499512f080e7Smrj  */
499612f080e7Smrj 
4997b51bbbf5SVikram Hegde /*
499812f080e7Smrj  * rootnex_dma_mctl()
499912f080e7Smrj  *
5000cd21e7c5SGarrett D'Amore  * We don't support this legacy interface any more on x86.
500112f080e7Smrj  */
500212f080e7Smrj /* ARGSUSED */
500312f080e7Smrj static int
rootnex_dma_mctl(dev_info_t * dip,dev_info_t * rdip,ddi_dma_handle_t handle,enum ddi_dma_ctlops request,off_t * offp,size_t * lenp,caddr_t * objpp,uint_t cache_flags)5004b51bbbf5SVikram Hegde rootnex_dma_mctl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle,
500512f080e7Smrj     enum ddi_dma_ctlops request, off_t *offp, size_t *lenp, caddr_t *objpp,
500612f080e7Smrj     uint_t cache_flags)
500712f080e7Smrj {
500812f080e7Smrj 	/*
5009cd21e7c5SGarrett D'Amore 	 * The only thing dma_mctl is usef for anymore is legacy SPARC
5010cd21e7c5SGarrett D'Amore 	 * dvma and sbus-specific routines.
501112f080e7Smrj 	 */
501212f080e7Smrj 	return (DDI_FAILURE);
50137c478bd9Sstevel@tonic-gate }
50147aec1d6eScindi 
501520906b23SVikram Hegde /*
501600d0963fSdilpreet  * *********
501700d0963fSdilpreet  *  FMA Code
501800d0963fSdilpreet  * *********
501900d0963fSdilpreet  */
502000d0963fSdilpreet 
502100d0963fSdilpreet /*
502200d0963fSdilpreet  * rootnex_fm_init()
502300d0963fSdilpreet  *    FMA init busop
502400d0963fSdilpreet  */
50257aec1d6eScindi /* ARGSUSED */
50267aec1d6eScindi static int
rootnex_fm_init(dev_info_t * dip,dev_info_t * tdip,int tcap,ddi_iblock_cookie_t * ibc)502700d0963fSdilpreet rootnex_fm_init(dev_info_t *dip, dev_info_t *tdip, int tcap,
502800d0963fSdilpreet     ddi_iblock_cookie_t *ibc)
50297aec1d6eScindi {
503000d0963fSdilpreet 	*ibc = rootnex_state->r_err_ibc;
503100d0963fSdilpreet 
503200d0963fSdilpreet 	return (ddi_system_fmcap);
503300d0963fSdilpreet }
503400d0963fSdilpreet 
503500d0963fSdilpreet /*
503600d0963fSdilpreet  * rootnex_dma_check()
503700d0963fSdilpreet  *    Function called after a dma fault occurred to find out whether the
503800d0963fSdilpreet  *    fault address is associated with a driver that is able to handle faults
503900d0963fSdilpreet  *    and recover from faults.
504000d0963fSdilpreet  */
504100d0963fSdilpreet /* ARGSUSED */
504200d0963fSdilpreet static int
rootnex_dma_check(dev_info_t * dip,const void * handle,const void * addr,const void * not_used)504300d0963fSdilpreet rootnex_dma_check(dev_info_t *dip, const void *handle, const void *addr,
504400d0963fSdilpreet     const void *not_used)
504500d0963fSdilpreet {
504600d0963fSdilpreet 	rootnex_window_t *window;
504700d0963fSdilpreet 	uint64_t start_addr;
504800d0963fSdilpreet 	uint64_t fault_addr;
504900d0963fSdilpreet 	ddi_dma_impl_t *hp;
505000d0963fSdilpreet 	rootnex_dma_t *dma;
505100d0963fSdilpreet 	uint64_t end_addr;
505200d0963fSdilpreet 	size_t csize;
505300d0963fSdilpreet 	int i;
505400d0963fSdilpreet 	int j;
505500d0963fSdilpreet 
505600d0963fSdilpreet 
505700d0963fSdilpreet 	/* The driver has to set DDI_DMA_FLAGERR to recover from dma faults */
505800d0963fSdilpreet 	hp = (ddi_dma_impl_t *)handle;
505900d0963fSdilpreet 	ASSERT(hp);
506000d0963fSdilpreet 
506100d0963fSdilpreet 	dma = (rootnex_dma_t *)hp->dmai_private;
506200d0963fSdilpreet 
506300d0963fSdilpreet 	/* Get the address that we need to search for */
506400d0963fSdilpreet 	fault_addr = *(uint64_t *)addr;
506500d0963fSdilpreet 
506600d0963fSdilpreet 	/*
506700d0963fSdilpreet 	 * if we don't have any windows, we can just walk through all the
506800d0963fSdilpreet 	 * cookies.
506900d0963fSdilpreet 	 */
507000d0963fSdilpreet 	if (dma->dp_window == NULL) {
507100d0963fSdilpreet 		/* for each cookie */
507200d0963fSdilpreet 		for (i = 0; i < dma->dp_sglinfo.si_sgl_size; i++) {
507300d0963fSdilpreet 			/*
507400d0963fSdilpreet 			 * if the faulted address is within the physical address
507500d0963fSdilpreet 			 * range of the cookie, return DDI_FM_NONFATAL.
507600d0963fSdilpreet 			 */
507700d0963fSdilpreet 			if ((fault_addr >= dma->dp_cookies[i].dmac_laddress) &&
507800d0963fSdilpreet 			    (fault_addr <= (dma->dp_cookies[i].dmac_laddress +
507900d0963fSdilpreet 			    dma->dp_cookies[i].dmac_size))) {
508000d0963fSdilpreet 				return (DDI_FM_NONFATAL);
508100d0963fSdilpreet 			}
508200d0963fSdilpreet 		}
508300d0963fSdilpreet 
508400d0963fSdilpreet 		/* fault_addr not within this DMA handle */
508500d0963fSdilpreet 		return (DDI_FM_UNKNOWN);
508600d0963fSdilpreet 	}
508700d0963fSdilpreet 
508800d0963fSdilpreet 	/* we have mutiple windows, walk through each window */
508900d0963fSdilpreet 	for (i = 0; i < hp->dmai_nwin; i++) {
509000d0963fSdilpreet 		window = &dma->dp_window[i];
509100d0963fSdilpreet 
509200d0963fSdilpreet 		/* Go through all the cookies in the window */
509300d0963fSdilpreet 		for (j = 0; j < window->wd_cookie_cnt; j++) {
509400d0963fSdilpreet 
509500d0963fSdilpreet 			start_addr = window->wd_first_cookie[j].dmac_laddress;
509600d0963fSdilpreet 			csize = window->wd_first_cookie[j].dmac_size;
509700d0963fSdilpreet 
509800d0963fSdilpreet 			/*
509900d0963fSdilpreet 			 * if we are trimming the first cookie in the window,
510000d0963fSdilpreet 			 * and this is the first cookie, adjust the start
510100d0963fSdilpreet 			 * address and size of the cookie to account for the
510200d0963fSdilpreet 			 * trim.
510300d0963fSdilpreet 			 */
510400d0963fSdilpreet 			if (window->wd_trim.tr_trim_first && (j == 0)) {
510500d0963fSdilpreet 				start_addr = window->wd_trim.tr_first_paddr;
510600d0963fSdilpreet 				csize = window->wd_trim.tr_first_size;
510700d0963fSdilpreet 			}
510800d0963fSdilpreet 
510900d0963fSdilpreet 			/*
511000d0963fSdilpreet 			 * if we are trimming the last cookie in the window,
511100d0963fSdilpreet 			 * and this is the last cookie, adjust the start
511200d0963fSdilpreet 			 * address and size of the cookie to account for the
511300d0963fSdilpreet 			 * trim.
511400d0963fSdilpreet 			 */
511500d0963fSdilpreet 			if (window->wd_trim.tr_trim_last &&
511600d0963fSdilpreet 			    (j == (window->wd_cookie_cnt - 1))) {
511700d0963fSdilpreet 				start_addr = window->wd_trim.tr_last_paddr;
511800d0963fSdilpreet 				csize = window->wd_trim.tr_last_size;
511900d0963fSdilpreet 			}
512000d0963fSdilpreet 
512100d0963fSdilpreet 			end_addr = start_addr + csize;
512200d0963fSdilpreet 
512300d0963fSdilpreet 			/*
51243a634bfcSVikram Hegde 			 * if the faulted address is within the physical
51253a634bfcSVikram Hegde 			 * address of the cookie, return DDI_FM_NONFATAL.
512600d0963fSdilpreet 			 */
512700d0963fSdilpreet 			if ((fault_addr >= start_addr) &&
512800d0963fSdilpreet 			    (fault_addr <= end_addr)) {
512900d0963fSdilpreet 				return (DDI_FM_NONFATAL);
513000d0963fSdilpreet 			}
513100d0963fSdilpreet 		}
513200d0963fSdilpreet 	}
513300d0963fSdilpreet 
513400d0963fSdilpreet 	/* fault_addr not within this DMA handle */
513500d0963fSdilpreet 	return (DDI_FM_UNKNOWN);
51367aec1d6eScindi }
51373a634bfcSVikram Hegde 
51383a634bfcSVikram Hegde /*ARGSUSED*/
51393a634bfcSVikram Hegde static int
rootnex_quiesce(dev_info_t * dip)51403a634bfcSVikram Hegde rootnex_quiesce(dev_info_t *dip)
51413a634bfcSVikram Hegde {
51423a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
51433a634bfcSVikram Hegde 	return (immu_quiesce());
51443a634bfcSVikram Hegde #else
51453a634bfcSVikram Hegde 	return (DDI_SUCCESS);
51463a634bfcSVikram Hegde #endif
51473a634bfcSVikram Hegde }
51483a634bfcSVikram Hegde 
51493a634bfcSVikram Hegde #if defined(__xpv)
51503a634bfcSVikram Hegde void
immu_init(void)51513a634bfcSVikram Hegde immu_init(void)
51523a634bfcSVikram Hegde {
51533a634bfcSVikram Hegde 	;
51543a634bfcSVikram Hegde }
51553a634bfcSVikram Hegde 
51563a634bfcSVikram Hegde void
immu_startup(void)51573a634bfcSVikram Hegde immu_startup(void)
51583a634bfcSVikram Hegde {
51593a634bfcSVikram Hegde 	;
51603a634bfcSVikram Hegde }
51613a634bfcSVikram Hegde /*ARGSUSED*/
51623a634bfcSVikram Hegde void
immu_physmem_update(uint64_t addr,uint64_t size)51633a634bfcSVikram Hegde immu_physmem_update(uint64_t addr, uint64_t size)
51643a634bfcSVikram Hegde {
51653a634bfcSVikram Hegde 	;
51663a634bfcSVikram Hegde }
51673a634bfcSVikram Hegde #endif
5168