xref: /onnv-gate/usr/src/uts/sun4u/io/pci/pcisch.c (revision 2358:cf7d70658cc6)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51865Sdilpreet  * Common Development and Distribution License (the "License").
61865Sdilpreet  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
221295Sdanice  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * Schizo specifics implementation:
300Sstevel@tonic-gate  *	interrupt mapping register
310Sstevel@tonic-gate  *	PBM configuration
320Sstevel@tonic-gate  *	ECC and PBM error handling
330Sstevel@tonic-gate  *	Iommu mapping handling
340Sstevel@tonic-gate  *	Streaming Cache flushing
350Sstevel@tonic-gate  */
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include <sys/types.h>
380Sstevel@tonic-gate #include <sys/kmem.h>
390Sstevel@tonic-gate #include <sys/sysmacros.h>
400Sstevel@tonic-gate #include <sys/async.h>
410Sstevel@tonic-gate #include <sys/ivintr.h>
420Sstevel@tonic-gate #include <sys/systm.h>
430Sstevel@tonic-gate #include <sys/intr.h>
440Sstevel@tonic-gate #include <sys/machsystm.h>	/* lddphys() */
450Sstevel@tonic-gate #include <sys/machsystm.h>	/* lddphys, intr_dist_add */
460Sstevel@tonic-gate #include <sys/iommutsb.h>
470Sstevel@tonic-gate #include <sys/promif.h>		/* prom_printf */
480Sstevel@tonic-gate #include <sys/map.h>
490Sstevel@tonic-gate #include <sys/ddi.h>
500Sstevel@tonic-gate #include <sys/sunddi.h>
510Sstevel@tonic-gate #include <sys/sunndi.h>
520Sstevel@tonic-gate #include <sys/spl.h>
530Sstevel@tonic-gate #include <sys/fm/util.h>
540Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
550Sstevel@tonic-gate #include <sys/fm/protocol.h>
560Sstevel@tonic-gate #include <sys/fm/io/sun4upci.h>
570Sstevel@tonic-gate #include <sys/fm/io/ddi.h>
580Sstevel@tonic-gate #include <sys/fm/io/pci.h>
590Sstevel@tonic-gate #include <sys/pci/pci_obj.h>
600Sstevel@tonic-gate #include <sys/pci/pcisch.h>
610Sstevel@tonic-gate #include <sys/pci/pcisch_asm.h>
620Sstevel@tonic-gate #include <sys/x_call.h>		/* XCALL_PIL */
630Sstevel@tonic-gate 
640Sstevel@tonic-gate /*LINTLIBRARY*/
650Sstevel@tonic-gate 
660Sstevel@tonic-gate extern uint8_t ldstub(uint8_t *);
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #define	IOMMU_CTX_BITMAP_SIZE	(1 << (12 - 3))
690Sstevel@tonic-gate static void iommu_ctx_free(iommu_t *);
700Sstevel@tonic-gate static int iommu_tlb_scrub(iommu_t *, int);
710Sstevel@tonic-gate static uint32_t pci_identity_init(pci_t *);
720Sstevel@tonic-gate 
730Sstevel@tonic-gate static void pci_cb_clear_error(cb_t *, cb_errstate_t *);
740Sstevel@tonic-gate static void pci_clear_error(pci_t *, pbm_errstate_t *);
750Sstevel@tonic-gate static uint32_t pci_identity_init(pci_t *pci_p);
760Sstevel@tonic-gate static int pci_intr_setup(pci_t *pci_p);
770Sstevel@tonic-gate static void iommu_ereport_post(dev_info_t *, uint64_t, pbm_errstate_t *);
780Sstevel@tonic-gate static void cb_ereport_post(dev_info_t *, uint64_t, cb_errstate_t *);
790Sstevel@tonic-gate static void pcix_ereport_post(dev_info_t *, uint64_t, pbm_errstate_t *);
800Sstevel@tonic-gate static void pci_format_ecc_addr(dev_info_t *dip, uint64_t *afar,
810Sstevel@tonic-gate 		ecc_region_t region);
820Sstevel@tonic-gate static void pci_pbm_errstate_get(pci_t *pci_p, pbm_errstate_t *pbm_err_p);
830Sstevel@tonic-gate static void tm_vmem_free(ddi_dma_impl_t *mp, iommu_t *iommu_p,
840Sstevel@tonic-gate 		dvma_addr_t dvma_pg, int npages);
850Sstevel@tonic-gate 
860Sstevel@tonic-gate static int pcix_ma_behind_bridge(pbm_errstate_t *pbm_err_p);
870Sstevel@tonic-gate 
880Sstevel@tonic-gate static pci_ksinfo_t	*pci_name_kstat;
890Sstevel@tonic-gate static pci_ksinfo_t	*saf_name_kstat;
900Sstevel@tonic-gate 
910Sstevel@tonic-gate extern void pcix_set_cmd_reg(dev_info_t *child, uint16_t value);
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /* called by pci_attach() DDI_ATTACH to initialize pci objects */
940Sstevel@tonic-gate int
950Sstevel@tonic-gate pci_obj_setup(pci_t *pci_p)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate 	pci_common_t *cmn_p;
980Sstevel@tonic-gate 	uint32_t chip_id = pci_identity_init(pci_p);
990Sstevel@tonic-gate 	uint32_t cmn_id = PCI_CMN_ID(ID_CHIP_TYPE(chip_id), pci_p->pci_id);
1000Sstevel@tonic-gate 	int ret;
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	/* Perform allocations first to avoid delicate unwinding. */
1030Sstevel@tonic-gate 	if (pci_alloc_tsb(pci_p) != DDI_SUCCESS)
1040Sstevel@tonic-gate 		return (DDI_FAILURE);
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	mutex_enter(&pci_global_mutex);
1070Sstevel@tonic-gate 	cmn_p = get_pci_common_soft_state(cmn_id);
1080Sstevel@tonic-gate 	if (cmn_p == NULL) {
1090Sstevel@tonic-gate 		if (alloc_pci_common_soft_state(cmn_id) != DDI_SUCCESS) {
1100Sstevel@tonic-gate 			mutex_exit(&pci_global_mutex);
1110Sstevel@tonic-gate 			pci_free_tsb(pci_p);
1120Sstevel@tonic-gate 			return (DDI_FAILURE);
1130Sstevel@tonic-gate 		}
1140Sstevel@tonic-gate 		cmn_p = get_pci_common_soft_state(cmn_id);
1150Sstevel@tonic-gate 		cmn_p->pci_common_id = cmn_id;
1160Sstevel@tonic-gate 		cmn_p->pci_common_tsb_cookie = IOMMU_TSB_COOKIE_NONE;
1170Sstevel@tonic-gate 	}
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	ASSERT((pci_p->pci_side == 0) || (pci_p->pci_side == 1));
1200Sstevel@tonic-gate 	if (cmn_p->pci_p[pci_p->pci_side]) {
1210Sstevel@tonic-gate 		/* second side attach */
1220Sstevel@tonic-gate 		pci_p->pci_side = PCI_OTHER_SIDE(pci_p->pci_side);
1230Sstevel@tonic-gate 		ASSERT(cmn_p->pci_p[pci_p->pci_side] == NULL);
1240Sstevel@tonic-gate 	}
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	cmn_p->pci_p[pci_p->pci_side] = pci_p;
1270Sstevel@tonic-gate 	pci_p->pci_common_p = cmn_p;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	if (cmn_p->pci_common_refcnt == 0)
1300Sstevel@tonic-gate 		cmn_p->pci_chip_id = chip_id;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	ib_create(pci_p);
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	/*
1350Sstevel@tonic-gate 	 * The initialization of cb internal interrupts depends on ib
1360Sstevel@tonic-gate 	 */
1370Sstevel@tonic-gate 	if (cmn_p->pci_common_refcnt == 0) {
1380Sstevel@tonic-gate 		cb_create(pci_p);
1390Sstevel@tonic-gate 		cmn_p->pci_common_cb_p = pci_p->pci_cb_p;
1400Sstevel@tonic-gate 	} else
1410Sstevel@tonic-gate 		pci_p->pci_cb_p = cmn_p->pci_common_cb_p;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	iommu_create(pci_p);
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	if (cmn_p->pci_common_refcnt == 0) {
1460Sstevel@tonic-gate 		ecc_create(pci_p);
1470Sstevel@tonic-gate 		cmn_p->pci_common_ecc_p = pci_p->pci_ecc_p;
1480Sstevel@tonic-gate 	} else
1490Sstevel@tonic-gate 		pci_p->pci_ecc_p = cmn_p->pci_common_ecc_p;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	pbm_create(pci_p);
1520Sstevel@tonic-gate 	sc_create(pci_p);
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	pci_fm_create(pci_p);
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	if ((ret = pci_intr_setup(pci_p)) != DDI_SUCCESS)
1570Sstevel@tonic-gate 		goto done;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	pci_kstat_create(pci_p);
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	cmn_p->pci_common_attachcnt++;
1620Sstevel@tonic-gate 	cmn_p->pci_common_refcnt++;
1630Sstevel@tonic-gate done:
1640Sstevel@tonic-gate 	mutex_exit(&pci_global_mutex);
1650Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
1660Sstevel@tonic-gate 		cmn_err(CE_WARN, "pci_obj_setup failed %x", ret);
1670Sstevel@tonic-gate 	return (ret);
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate /* called by pci_detach() DDI_DETACH to destroy pci objects */
1710Sstevel@tonic-gate void
1720Sstevel@tonic-gate pci_obj_destroy(pci_t *pci_p)
1730Sstevel@tonic-gate {
1740Sstevel@tonic-gate 	pci_common_t *cmn_p;
1750Sstevel@tonic-gate 	mutex_enter(&pci_global_mutex);
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	cmn_p = pci_p->pci_common_p;
1780Sstevel@tonic-gate 	cmn_p->pci_common_refcnt--;
1790Sstevel@tonic-gate 	cmn_p->pci_common_attachcnt--;
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	pci_kstat_destroy(pci_p);
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	/* schizo non-shared objects */
1840Sstevel@tonic-gate 	pci_fm_destroy(pci_p);
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	sc_destroy(pci_p);
1870Sstevel@tonic-gate 	pbm_destroy(pci_p);
1880Sstevel@tonic-gate 	iommu_destroy(pci_p);
1890Sstevel@tonic-gate 	ib_destroy(pci_p);
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	if (cmn_p->pci_common_refcnt != 0) {
1920Sstevel@tonic-gate 		pci_intr_teardown(pci_p);
1930Sstevel@tonic-gate 		cmn_p->pci_p[pci_p->pci_side] = NULL;
1940Sstevel@tonic-gate 		mutex_exit(&pci_global_mutex);
1950Sstevel@tonic-gate 		return;
1960Sstevel@tonic-gate 	}
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	/* schizo shared objects - uses cmn_p, must be destroyed before cmn */
1990Sstevel@tonic-gate 	ecc_destroy(pci_p);
2000Sstevel@tonic-gate 	cb_destroy(pci_p);
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	free_pci_common_soft_state(cmn_p->pci_common_id);
2030Sstevel@tonic-gate 	pci_intr_teardown(pci_p);
2040Sstevel@tonic-gate 	mutex_exit(&pci_global_mutex);
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate /* called by pci_attach() DDI_RESUME to (re)initialize pci objects */
2080Sstevel@tonic-gate void
2090Sstevel@tonic-gate pci_obj_resume(pci_t *pci_p)
2100Sstevel@tonic-gate {
2110Sstevel@tonic-gate 	pci_common_t *cmn_p = pci_p->pci_common_p;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	mutex_enter(&pci_global_mutex);
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	ib_configure(pci_p->pci_ib_p);
2160Sstevel@tonic-gate 	iommu_configure(pci_p->pci_iommu_p);
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	if (cmn_p->pci_common_attachcnt == 0)
2190Sstevel@tonic-gate 		ecc_configure(pci_p);
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	ib_resume(pci_p->pci_ib_p);
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	pbm_configure(pci_p->pci_pbm_p);
2240Sstevel@tonic-gate 	sc_configure(pci_p->pci_sc_p);
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	if (cmn_p->pci_common_attachcnt == 0)
2270Sstevel@tonic-gate 		cb_resume(pci_p->pci_cb_p);
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	pbm_resume(pci_p->pci_pbm_p);
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	cmn_p->pci_common_attachcnt++;
2320Sstevel@tonic-gate 	mutex_exit(&pci_global_mutex);
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate /* called by pci_detach() DDI_SUSPEND to suspend pci objects */
2360Sstevel@tonic-gate void
2370Sstevel@tonic-gate pci_obj_suspend(pci_t *pci_p)
2380Sstevel@tonic-gate {
2390Sstevel@tonic-gate 	mutex_enter(&pci_global_mutex);
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	pbm_suspend(pci_p->pci_pbm_p);
2420Sstevel@tonic-gate 	ib_suspend(pci_p->pci_ib_p);
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	if (!--pci_p->pci_common_p->pci_common_attachcnt)
2450Sstevel@tonic-gate 		cb_suspend(pci_p->pci_cb_p);
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	mutex_exit(&pci_global_mutex);
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate /*
2510Sstevel@tonic-gate  * add an additional 0x35 or 0x36 ino interrupt on platforms don't have them
2520Sstevel@tonic-gate  * This routine has multiple places that assumes interrupt takes one cell
2530Sstevel@tonic-gate  * each and cell size is same as integer size.
2540Sstevel@tonic-gate  */
2550Sstevel@tonic-gate static int
2560Sstevel@tonic-gate pci_intr_setup(pci_t *pci_p)
2570Sstevel@tonic-gate {
2580Sstevel@tonic-gate 	dev_info_t *dip = pci_p->pci_dip;
2590Sstevel@tonic-gate 	pbm_t *pbm_p = pci_p->pci_pbm_p;
2600Sstevel@tonic-gate 	cb_t *cb_p = pci_p->pci_cb_p;
2610Sstevel@tonic-gate 	uint32_t *intr_buf, *new_intr_buf;
2620Sstevel@tonic-gate 	int intr_len, intr_cnt, ret;
2630Sstevel@tonic-gate 
264506Scth 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
2650Sstevel@tonic-gate 		"interrupts", (caddr_t)&intr_buf, &intr_len) != DDI_SUCCESS)
2660Sstevel@tonic-gate 		cmn_err(CE_PANIC, "%s%d: no interrupts property\n",
2670Sstevel@tonic-gate 			ddi_driver_name(dip), ddi_get_instance(dip));
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	intr_cnt = BYTES_TO_1275_CELLS(intr_len);
2700Sstevel@tonic-gate 	if (intr_cnt < CBNINTR_CDMA)	/* CBNINTR_CDMA is 0 based */
2710Sstevel@tonic-gate 		cmn_err(CE_PANIC, "%s%d: <%d interrupts", ddi_driver_name(dip),
2720Sstevel@tonic-gate 			ddi_get_instance(dip), CBNINTR_CDMA);
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	if (intr_cnt == CBNINTR_CDMA)
2750Sstevel@tonic-gate 		intr_cnt++;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	new_intr_buf = kmem_alloc(CELLS_1275_TO_BYTES(intr_cnt), KM_SLEEP);
2780Sstevel@tonic-gate 	bcopy(intr_buf, new_intr_buf, intr_len);
2790Sstevel@tonic-gate 	kmem_free(intr_buf, intr_len);
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	new_intr_buf[CBNINTR_CDMA] = PBM_CDMA_INO_BASE + pci_p->pci_side;
2820Sstevel@tonic-gate 	pci_p->pci_inos = new_intr_buf;
2830Sstevel@tonic-gate 	pci_p->pci_inos_len = CELLS_1275_TO_BYTES(intr_cnt);
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	if (ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "interrupts",
2860Sstevel@tonic-gate 		(int *)new_intr_buf, intr_cnt))
2870Sstevel@tonic-gate 		cmn_err(CE_PANIC, "%s%d: cannot update interrupts property\n",
2880Sstevel@tonic-gate 			ddi_driver_name(dip), ddi_get_instance(dip));
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	if (pci_p->pci_common_p->pci_common_refcnt == 0) {
2910Sstevel@tonic-gate 		cb_p->cb_no_of_inos = intr_cnt;
2920Sstevel@tonic-gate 		if (ret = cb_register_intr(pci_p))
2930Sstevel@tonic-gate 			goto teardown;
2940Sstevel@tonic-gate 		if (ret = ecc_register_intr(pci_p))
2950Sstevel@tonic-gate 			goto teardown;
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 		intr_dist_add(cb_intr_dist, cb_p);
2980Sstevel@tonic-gate 		cb_enable_intr(pci_p);
2990Sstevel@tonic-gate 		ecc_enable_intr(pci_p);
3000Sstevel@tonic-gate 	}
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	if (CHIP_TYPE(pci_p) != PCI_CHIP_SCHIZO)
3030Sstevel@tonic-gate 		pbm_p->pbm_sync_ino = pci_p->pci_inos[CBNINTR_PBM];
3040Sstevel@tonic-gate 	if (ret = pbm_register_intr(pbm_p)) {
3050Sstevel@tonic-gate 		if (pci_p->pci_common_p->pci_common_refcnt == 0)
3060Sstevel@tonic-gate 			intr_dist_rem(cb_intr_dist, cb_p);
3070Sstevel@tonic-gate 		goto teardown;
3080Sstevel@tonic-gate 	}
3090Sstevel@tonic-gate 	intr_dist_add(pbm_intr_dist, pbm_p);
3100Sstevel@tonic-gate 	ib_intr_enable(pci_p, pci_p->pci_inos[CBNINTR_PBM]);
3110Sstevel@tonic-gate 	ib_intr_enable(pci_p, pci_p->pci_inos[CBNINTR_CDMA]);
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	intr_dist_add_weighted(ib_intr_dist_all, pci_p->pci_ib_p);
3140Sstevel@tonic-gate 	return (DDI_SUCCESS);
3150Sstevel@tonic-gate teardown:
3160Sstevel@tonic-gate 	pci_intr_teardown(pci_p);
3170Sstevel@tonic-gate 	return (ret);
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate uint64_t
3210Sstevel@tonic-gate pci_sc_configure(pci_t *pci_p)
3220Sstevel@tonic-gate {
3230Sstevel@tonic-gate 	int instance;
3240Sstevel@tonic-gate 	dev_info_t *dip = pci_p->pci_dip;
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
3270Sstevel@tonic-gate 	if ((pci_xmits_sc_max_prf & (1 << instance)) &&
3280Sstevel@tonic-gate 	    (CHIP_TYPE(pci_p) == PCI_CHIP_XMITS))
3290Sstevel@tonic-gate 		return (XMITS_SC_MAX_PRF);
3300Sstevel@tonic-gate 	else
3310Sstevel@tonic-gate 		return (0);
3320Sstevel@tonic-gate }
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate static void
3350Sstevel@tonic-gate pci_schizo_cdma_sync(pbm_t *pbm_p)
3360Sstevel@tonic-gate {
3370Sstevel@tonic-gate 	pci_t *pci_p = pbm_p->pbm_pci_p;
3380Sstevel@tonic-gate 	hrtime_t start_time;
3390Sstevel@tonic-gate 	volatile uint64_t *clr_p = ib_clear_intr_reg_addr(pci_p->pci_ib_p,
3400Sstevel@tonic-gate 		pci_p->pci_inos[CBNINTR_CDMA]);
3410Sstevel@tonic-gate 	uint32_t fail_cnt = pci_cdma_intr_count;
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	mutex_enter(&pbm_p->pbm_sync_mutex);
3440Sstevel@tonic-gate #ifdef PBM_CDMA_DEBUG
3450Sstevel@tonic-gate 	pbm_p->pbm_cdma_req_cnt++;
3460Sstevel@tonic-gate #endif /* PBM_CDMA_DEBUG */
3470Sstevel@tonic-gate 	pbm_p->pbm_cdma_flag = PBM_CDMA_PEND;
3480Sstevel@tonic-gate 	IB_INO_INTR_TRIG(clr_p);
3490Sstevel@tonic-gate wait:
3500Sstevel@tonic-gate 	start_time = gethrtime();
3510Sstevel@tonic-gate 	while (pbm_p->pbm_cdma_flag != PBM_CDMA_DONE) {
3520Sstevel@tonic-gate 		if (gethrtime() - start_time <= pci_cdma_intr_timeout)
3530Sstevel@tonic-gate 			continue;
3540Sstevel@tonic-gate 		if (--fail_cnt > 0)
3550Sstevel@tonic-gate 			goto wait;
3560Sstevel@tonic-gate 		if (pbm_p->pbm_cdma_flag == PBM_CDMA_DONE)
3570Sstevel@tonic-gate 			break;
3580Sstevel@tonic-gate 		cmn_err(CE_PANIC, "%s (%s): consistent dma sync timeout",
3590Sstevel@tonic-gate 		    pbm_p->pbm_nameinst_str, pbm_p->pbm_nameaddr_str);
3600Sstevel@tonic-gate 	}
3610Sstevel@tonic-gate #ifdef PBM_CDMA_DEBUG
3620Sstevel@tonic-gate 	if (pbm_p->pbm_cdma_flag != PBM_CDMA_DONE)
3630Sstevel@tonic-gate 		pbm_p->pbm_cdma_to_cnt++;
3640Sstevel@tonic-gate 	else {
3650Sstevel@tonic-gate 		start_time = gethrtime() - start_time;
3660Sstevel@tonic-gate 		pbm_p->pbm_cdma_success_cnt++;
3670Sstevel@tonic-gate 		pbm_p->pbm_cdma_latency_sum += start_time;
3680Sstevel@tonic-gate 		if (start_time > pbm_p->pbm_cdma_latency_max)
3690Sstevel@tonic-gate 			pbm_p->pbm_cdma_latency_max = start_time;
3700Sstevel@tonic-gate 	}
3710Sstevel@tonic-gate #endif /* PBM_CDMA_DEBUG */
3720Sstevel@tonic-gate 	mutex_exit(&pbm_p->pbm_sync_mutex);
3730Sstevel@tonic-gate }
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate #if !defined(lint)
3760Sstevel@tonic-gate #include <sys/cpuvar.h>
3770Sstevel@tonic-gate #endif
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate #define	SYNC_HW_BUSY(pa, mask)	(lddphysio(pa) & (mask))
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate /*
3820Sstevel@tonic-gate  * Consistent DMA Sync/Flush
3830Sstevel@tonic-gate  *
3840Sstevel@tonic-gate  * XMITS and Tomatillo use multi-threaded sync/flush register.
3850Sstevel@tonic-gate  * Called from interrupt wrapper: the associated ino is used to index
3860Sstevel@tonic-gate  *	the distinctive register bit.
3870Sstevel@tonic-gate  * Called from pci_dma_sync(): the bit belongs to PBM is shared
3880Sstevel@tonic-gate  *	for all calls from pci_dma_sync(). Xmits requires serialization
3890Sstevel@tonic-gate  *	while Tomatillo does not.
3900Sstevel@tonic-gate  */
3910Sstevel@tonic-gate void
3920Sstevel@tonic-gate pci_pbm_dma_sync(pbm_t *pbm_p, ib_ino_t ino)
3930Sstevel@tonic-gate {
3940Sstevel@tonic-gate 	pci_t *pci_p = pbm_p->pbm_pci_p;
3950Sstevel@tonic-gate 	hrtime_t start_time;
3960Sstevel@tonic-gate 	uint64_t ino_mask, sync_reg_pa;
3970Sstevel@tonic-gate 	volatile uint64_t flag_val;
3980Sstevel@tonic-gate 	uint32_t locked, chip_type = CHIP_TYPE(pci_p);
3990Sstevel@tonic-gate 	int	i;
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 	if (chip_type == PCI_CHIP_SCHIZO) {
4020Sstevel@tonic-gate 		pci_schizo_cdma_sync(pbm_p);
4030Sstevel@tonic-gate 		return;
4040Sstevel@tonic-gate 	}
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	sync_reg_pa = pbm_p->pbm_sync_reg_pa;
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	locked = 0;
4090Sstevel@tonic-gate 	if (((chip_type == PCI_CHIP_XMITS) && (ino == pbm_p->pbm_sync_ino)) ||
4100Sstevel@tonic-gate 	    pci_sync_lock) {
4110Sstevel@tonic-gate 		locked = 1;
4120Sstevel@tonic-gate 		mutex_enter(&pbm_p->pbm_sync_mutex);
4130Sstevel@tonic-gate 	}
4140Sstevel@tonic-gate 	ino_mask = 1ull << ino;
4150Sstevel@tonic-gate 	stdphysio(sync_reg_pa, ino_mask);
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	for (i = 0; i < 5; i++) {
4180Sstevel@tonic-gate 		if ((flag_val = SYNC_HW_BUSY(sync_reg_pa, ino_mask)) == 0)
4190Sstevel@tonic-gate 			goto done;
4200Sstevel@tonic-gate 	}
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	start_time = gethrtime();
4230Sstevel@tonic-gate 	for (; (flag_val = SYNC_HW_BUSY(sync_reg_pa, ino_mask)) != 0; i++) {
4240Sstevel@tonic-gate 		if (gethrtime() - start_time > pci_sync_buf_timeout)
4250Sstevel@tonic-gate 			break;
4260Sstevel@tonic-gate 	}
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	if (flag_val && SYNC_HW_BUSY(sync_reg_pa, ino_mask) && !panicstr)
429946Smathue 		cmn_err(CE_PANIC, "%s: pbm dma sync %lx,%lx timeout!",
4300Sstevel@tonic-gate 			pbm_p->pbm_nameaddr_str, sync_reg_pa, flag_val);
4310Sstevel@tonic-gate done:
4320Sstevel@tonic-gate 	/* optional: stdphysio(sync_reg_pa - 8, ino_mask); */
4330Sstevel@tonic-gate 	if (locked)
4340Sstevel@tonic-gate 		mutex_exit(&pbm_p->pbm_sync_mutex);
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	if (tomatillo_store_store_wrka) {
4370Sstevel@tonic-gate #if !defined(lint)
4380Sstevel@tonic-gate 		kpreempt_disable();
4390Sstevel@tonic-gate #endif
4400Sstevel@tonic-gate 		tomatillo_store_store_order();
4410Sstevel@tonic-gate #if !defined(lint)
4420Sstevel@tonic-gate 		kpreempt_enable();
4430Sstevel@tonic-gate #endif
4440Sstevel@tonic-gate 	}
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate }
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate /*ARGSUSED*/
4490Sstevel@tonic-gate void
4500Sstevel@tonic-gate pci_fix_ranges(pci_ranges_t *rng_p, int rng_entries)
4510Sstevel@tonic-gate {
4520Sstevel@tonic-gate }
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate /*
4550Sstevel@tonic-gate  * map_pci_registers
4560Sstevel@tonic-gate  *
4570Sstevel@tonic-gate  * This function is called from the attach routine to map the registers
4580Sstevel@tonic-gate  * accessed by this driver.
4590Sstevel@tonic-gate  *
4600Sstevel@tonic-gate  * used by: pci_attach()
4610Sstevel@tonic-gate  *
4620Sstevel@tonic-gate  * return value: DDI_FAILURE on failure
4630Sstevel@tonic-gate  */
4640Sstevel@tonic-gate int
4650Sstevel@tonic-gate map_pci_registers(pci_t *pci_p, dev_info_t *dip)
4660Sstevel@tonic-gate {
4670Sstevel@tonic-gate 	ddi_device_acc_attr_t attr;
4680Sstevel@tonic-gate 	int len;
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
4710Sstevel@tonic-gate 	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	attr.devacc_attr_endian_flags = DDI_NEVERSWAP_ACC;
4741865Sdilpreet 
4750Sstevel@tonic-gate 	/*
4760Sstevel@tonic-gate 	 * Register set 0 is PCI CSR Base
4770Sstevel@tonic-gate 	 */
4780Sstevel@tonic-gate 	if (ddi_regs_map_setup(dip, 0, &pci_p->pci_address[0], 0, 0,
4790Sstevel@tonic-gate 	    &attr, &pci_p->pci_ac[0]) != DDI_SUCCESS) {
4800Sstevel@tonic-gate 		len = 0;
4810Sstevel@tonic-gate 		goto fail;
4820Sstevel@tonic-gate 	}
4830Sstevel@tonic-gate 	/*
4840Sstevel@tonic-gate 	 * Register set 1 is Schizo CSR Base
4850Sstevel@tonic-gate 	 */
4860Sstevel@tonic-gate 	if (ddi_regs_map_setup(dip, 1, &pci_p->pci_address[1], 0, 0,
4870Sstevel@tonic-gate 	    &attr, &pci_p->pci_ac[1]) != DDI_SUCCESS) {
4880Sstevel@tonic-gate 		len = 1;
4890Sstevel@tonic-gate 		goto fail;
4900Sstevel@tonic-gate 	}
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	/*
4930Sstevel@tonic-gate 	 * The third register set contains the bridge's configuration
4940Sstevel@tonic-gate 	 * header.  This header is at the very beginning of the bridge's
4950Sstevel@tonic-gate 	 * configuration space.  This space has litte-endian byte order.
4960Sstevel@tonic-gate 	 */
4970Sstevel@tonic-gate 	attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
4980Sstevel@tonic-gate 	if (ddi_regs_map_setup(dip, 2, &pci_p->pci_address[2], 0,
4990Sstevel@tonic-gate 	    PCI_CONF_HDR_SIZE, &attr, &pci_p->pci_ac[2]) != DDI_SUCCESS) {
5000Sstevel@tonic-gate 		len = 2;
5010Sstevel@tonic-gate 		goto fail;
5020Sstevel@tonic-gate 	}
5030Sstevel@tonic-gate 
504506Scth 	if (ddi_getproplen(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
5050Sstevel@tonic-gate 	    "reg", &len) || (len / sizeof (pci_nexus_regspec_t) < 4))
5060Sstevel@tonic-gate 		goto done;
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	/*
5090Sstevel@tonic-gate 	 * The optional fourth register bank points to the
5100Sstevel@tonic-gate 	 * interrupt concentrator registers.
5110Sstevel@tonic-gate 	 */
5120Sstevel@tonic-gate 	attr.devacc_attr_endian_flags = DDI_NEVERSWAP_ACC;
5130Sstevel@tonic-gate 	if (ddi_regs_map_setup(dip, 3, &pci_p->pci_address[3], 0,
5140Sstevel@tonic-gate 	    0, &attr, &pci_p->pci_ac[3]) != DDI_SUCCESS) {
5150Sstevel@tonic-gate 		len = 3;
5160Sstevel@tonic-gate 		goto fail;
5170Sstevel@tonic-gate 	}
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate done:
5200Sstevel@tonic-gate 	DEBUG4(DBG_ATTACH, dip, "address (%p,%p,%p,%p)\n",
5210Sstevel@tonic-gate 	    pci_p->pci_address[0], pci_p->pci_address[1],
5220Sstevel@tonic-gate 	    pci_p->pci_address[2], pci_p->pci_address[3]);
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 	return (DDI_SUCCESS);
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate fail:
5280Sstevel@tonic-gate 	cmn_err(CE_WARN, "%s%d: unable to map reg entry %d\n",
5290Sstevel@tonic-gate 		ddi_driver_name(dip), ddi_get_instance(dip), len);
5300Sstevel@tonic-gate 	for (; len--; ddi_regs_map_free(&pci_p->pci_ac[len]));
5310Sstevel@tonic-gate 	return (DDI_FAILURE);
5320Sstevel@tonic-gate }
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate /*
5350Sstevel@tonic-gate  * unmap_pci_registers:
5360Sstevel@tonic-gate  *
5370Sstevel@tonic-gate  * This routine unmap the registers mapped by map_pci_registers.
5380Sstevel@tonic-gate  *
5390Sstevel@tonic-gate  * used by: pci_detach()
5400Sstevel@tonic-gate  *
5410Sstevel@tonic-gate  * return value: none
5420Sstevel@tonic-gate  */
5430Sstevel@tonic-gate void
5440Sstevel@tonic-gate unmap_pci_registers(pci_t *pci_p)
5450Sstevel@tonic-gate {
5460Sstevel@tonic-gate 	int i;
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	for (i = 0; i < 4; i++) {
5490Sstevel@tonic-gate 		if (pci_p->pci_ac[i])
5500Sstevel@tonic-gate 			ddi_regs_map_free(&pci_p->pci_ac[i]);
5510Sstevel@tonic-gate 	}
5520Sstevel@tonic-gate }
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate uint64_t
5550Sstevel@tonic-gate ib_get_map_reg(ib_mondo_t mondo, uint32_t cpu_id)
5560Sstevel@tonic-gate {
5570Sstevel@tonic-gate 	uint32_t agent_id;
5580Sstevel@tonic-gate 	uint32_t node_id;
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	/* ensure that cpu_id is only 10 bits. */
5610Sstevel@tonic-gate 	ASSERT((cpu_id & ~0x3ff) == 0);
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	agent_id = cpu_id & 0x1f;
5640Sstevel@tonic-gate 	node_id = (cpu_id >> 5) & 0x1f;
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	return ((mondo) | (agent_id << COMMON_INTR_MAP_REG_TID_SHIFT) |
5670Sstevel@tonic-gate 	    (node_id << SCHIZO_INTR_MAP_REG_NID_SHIFT) |
5680Sstevel@tonic-gate 	    COMMON_INTR_MAP_REG_VALID);
5690Sstevel@tonic-gate }
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate uint32_t
5720Sstevel@tonic-gate ib_map_reg_get_cpu(volatile uint64_t reg)
5730Sstevel@tonic-gate {
5740Sstevel@tonic-gate 	return (((reg & COMMON_INTR_MAP_REG_TID) >>
5750Sstevel@tonic-gate 		COMMON_INTR_MAP_REG_TID_SHIFT) |
5760Sstevel@tonic-gate 			((reg & SCHIZO_INTR_MAP_REG_NID) >>
5770Sstevel@tonic-gate 			(SCHIZO_INTR_MAP_REG_NID_SHIFT-5)));
5780Sstevel@tonic-gate }
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate uint64_t *
5810Sstevel@tonic-gate ib_intr_map_reg_addr(ib_t *ib_p, ib_ino_t ino)
5820Sstevel@tonic-gate {
5830Sstevel@tonic-gate 	/*
5840Sstevel@tonic-gate 	 * Schizo maps all interrupts in one contiguous area.
5850Sstevel@tonic-gate 	 * (PCI_CSRBase + 0x00.1000 + INO * 8).
5860Sstevel@tonic-gate 	 */
5870Sstevel@tonic-gate 	return ((uint64_t *)(ib_p->ib_intr_map_regs) + (ino & 0x3f));
5880Sstevel@tonic-gate }
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate uint64_t *
5910Sstevel@tonic-gate ib_clear_intr_reg_addr(ib_t *ib_p, ib_ino_t ino)	/* XXX - needs work */
5920Sstevel@tonic-gate {
5930Sstevel@tonic-gate 	/*
5940Sstevel@tonic-gate 	 * Schizo maps clear intr. registers in contiguous area.
5950Sstevel@tonic-gate 	 * (PCI_CSRBase + 0x00.1400 + INO * 8).
5960Sstevel@tonic-gate 	 */
5970Sstevel@tonic-gate 	return ((uint64_t *)(ib_p->ib_slot_clear_intr_regs) + (ino & 0x3f));
5980Sstevel@tonic-gate }
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate /*
6010Sstevel@tonic-gate  * schizo does not have mapping register per slot, so no sharing
6020Sstevel@tonic-gate  * is done.
6030Sstevel@tonic-gate  */
6040Sstevel@tonic-gate /*ARGSUSED*/
6050Sstevel@tonic-gate void
6060Sstevel@tonic-gate ib_ino_map_reg_share(ib_t *ib_p, ib_ino_t ino, ib_ino_info_t *ino_p)
6070Sstevel@tonic-gate {
6080Sstevel@tonic-gate }
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate /*
6110Sstevel@tonic-gate  * return true if there are interrupts using this mapping register
6120Sstevel@tonic-gate  */
6130Sstevel@tonic-gate /*ARGSUSED*/
6140Sstevel@tonic-gate int
6150Sstevel@tonic-gate ib_ino_map_reg_unshare(ib_t *ib_p, ib_ino_t ino, ib_ino_info_t *ino_p)
6160Sstevel@tonic-gate {
6170Sstevel@tonic-gate 	return (ino_p->ino_ih_size);
6180Sstevel@tonic-gate }
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate void
6210Sstevel@tonic-gate pci_pbm_intr_dist(pbm_t *pbm_p)
6220Sstevel@tonic-gate {
6230Sstevel@tonic-gate 	pci_t *pci_p = pbm_p->pbm_pci_p;
6240Sstevel@tonic-gate 	ib_t *ib_p = pci_p->pci_ib_p;
6250Sstevel@tonic-gate 	ib_ino_t ino = IB_MONDO_TO_INO(pci_p->pci_inos[CBNINTR_CDMA]);
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	mutex_enter(&pbm_p->pbm_sync_mutex);
6280Sstevel@tonic-gate 	ib_intr_dist_nintr(ib_p, ino, ib_intr_map_reg_addr(ib_p, ino));
6290Sstevel@tonic-gate 	mutex_exit(&pbm_p->pbm_sync_mutex);
6300Sstevel@tonic-gate }
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate uint32_t
6330Sstevel@tonic-gate pci_xlate_intr(dev_info_t *dip, dev_info_t *rdip, ib_t *ib_p, uint32_t intr)
6340Sstevel@tonic-gate {
6350Sstevel@tonic-gate 	return (IB_INO_TO_MONDO(ib_p, intr));
6360Sstevel@tonic-gate }
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate /*
6400Sstevel@tonic-gate  * Return the cpuid to to be used for an ino.  We have no special cpu
6410Sstevel@tonic-gate  * assignment constraints for this nexus, so just call intr_dist_cpuid().
6420Sstevel@tonic-gate  */
6430Sstevel@tonic-gate /* ARGSUSED */
6440Sstevel@tonic-gate uint32_t
6450Sstevel@tonic-gate pci_intr_dist_cpuid(ib_t *ib_p, ib_ino_info_t *ino_p)
6460Sstevel@tonic-gate {
6470Sstevel@tonic-gate 	return (intr_dist_cpuid());
6480Sstevel@tonic-gate }
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate void
6510Sstevel@tonic-gate pci_cb_teardown(pci_t *pci_p)
6520Sstevel@tonic-gate {
6530Sstevel@tonic-gate 	cb_t 	*cb_p = pci_p->pci_cb_p;
6540Sstevel@tonic-gate 	uint32_t mondo;
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 	if (!pci_buserr_interrupt)
6570Sstevel@tonic-gate 		return;
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	mondo = ((pci_p->pci_cb_p->cb_ign  << PCI_INO_BITS) |
6600Sstevel@tonic-gate 	    pci_p->pci_inos[CBNINTR_BUS_ERROR]);
6610Sstevel@tonic-gate 	mondo = CB_MONDO_TO_XMONDO(pci_p->pci_cb_p, mondo);
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 	cb_disable_nintr(cb_p, CBNINTR_BUS_ERROR, IB_INTR_WAIT);
6640Sstevel@tonic-gate 	rem_ivintr(mondo, NULL);
6650Sstevel@tonic-gate }
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate int
6680Sstevel@tonic-gate cb_register_intr(pci_t *pci_p)
6690Sstevel@tonic-gate {
6700Sstevel@tonic-gate 	uint32_t mondo;
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	if (!pci_buserr_interrupt)
6730Sstevel@tonic-gate 		return (DDI_SUCCESS);
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 	mondo = ((pci_p->pci_cb_p->cb_ign << PCI_INO_BITS) |
6760Sstevel@tonic-gate 	    pci_p->pci_inos[CBNINTR_BUS_ERROR]);
6770Sstevel@tonic-gate 	mondo = CB_MONDO_TO_XMONDO(pci_p->pci_cb_p, mondo);
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	VERIFY(add_ivintr(mondo, pci_pil[CBNINTR_BUS_ERROR],
6800Sstevel@tonic-gate 	    cb_buserr_intr, (caddr_t)pci_p->pci_cb_p, NULL) == 0);
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 	return (PCI_ATTACH_RETCODE(PCI_CB_OBJ, PCI_OBJ_INTR_ADD, DDI_SUCCESS));
6830Sstevel@tonic-gate }
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate void
6860Sstevel@tonic-gate cb_enable_intr(pci_t *pci_p)
6870Sstevel@tonic-gate {
6880Sstevel@tonic-gate 	if (pci_buserr_interrupt)
6890Sstevel@tonic-gate 		cb_enable_nintr(pci_p, CBNINTR_BUS_ERROR);
6900Sstevel@tonic-gate }
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate uint64_t
6930Sstevel@tonic-gate cb_ino_to_map_pa(cb_t *cb_p, ib_ino_t ino)
6940Sstevel@tonic-gate {
6950Sstevel@tonic-gate 	return (cb_p->cb_map_pa + (ino << 3));
6960Sstevel@tonic-gate }
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate uint64_t
6990Sstevel@tonic-gate cb_ino_to_clr_pa(cb_t *cb_p, ib_ino_t ino)
7000Sstevel@tonic-gate {
7010Sstevel@tonic-gate 	return (cb_p->cb_clr_pa + (ino << 3));
7020Sstevel@tonic-gate }
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate /*
7050Sstevel@tonic-gate  * Useful on psycho only.
7060Sstevel@tonic-gate  */
7070Sstevel@tonic-gate int
7080Sstevel@tonic-gate cb_remove_xintr(pci_t *pci_p, dev_info_t *dip, dev_info_t *rdip, ib_ino_t ino,
7090Sstevel@tonic-gate ib_mondo_t mondo)
7100Sstevel@tonic-gate {
7110Sstevel@tonic-gate 	return (DDI_FAILURE);
7120Sstevel@tonic-gate }
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate void
7150Sstevel@tonic-gate pbm_configure(pbm_t *pbm_p)
7160Sstevel@tonic-gate {
7170Sstevel@tonic-gate 	pci_t *pci_p = pbm_p->pbm_pci_p;
7180Sstevel@tonic-gate 	dev_info_t *dip = pbm_p->pbm_pci_p->pci_dip;
7190Sstevel@tonic-gate 	int instance = ddi_get_instance(dip);
7200Sstevel@tonic-gate 	uint64_t l;
7210Sstevel@tonic-gate 	uint64_t mask = 1ll << instance;
7220Sstevel@tonic-gate 	ushort_t s = 0;
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 	l = *pbm_p->pbm_ctrl_reg;	/* save control register state */
7250Sstevel@tonic-gate 	DEBUG1(DBG_ATTACH, dip, "pbm_configure: ctrl reg=%llx\n", l);
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 	/*
7280Sstevel@tonic-gate 	 * See if any SERR# signals are asserted.  We'll clear them later.
7290Sstevel@tonic-gate 	 */
7300Sstevel@tonic-gate 	if (l & COMMON_PCI_CTRL_SERR)
7310Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: SERR asserted on pci bus\n",
7320Sstevel@tonic-gate 		    ddi_driver_name(dip), instance);
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 	/*
7350Sstevel@tonic-gate 	 * Determine if PCI bus is running at 33 or 66 mhz.
7360Sstevel@tonic-gate 	 */
7370Sstevel@tonic-gate 	if (l & COMMON_PCI_CTRL_SPEED)
7380Sstevel@tonic-gate 		pbm_p->pbm_speed = PBM_SPEED_66MHZ;
7390Sstevel@tonic-gate 	else
7400Sstevel@tonic-gate 		pbm_p->pbm_speed = PBM_SPEED_33MHZ;
7410Sstevel@tonic-gate 	DEBUG1(DBG_ATTACH, dip, "pbm_configure: %d mhz\n",
7420Sstevel@tonic-gate 	    pbm_p->pbm_speed  == PBM_SPEED_66MHZ ? 66 : 33);
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	if (pci_set_dto_value & mask) {
7450Sstevel@tonic-gate 		l &= ~(3ull << SCHIZO_PCI_CTRL_PTO_SHIFT);
7460Sstevel@tonic-gate 		l |= pci_dto_value << SCHIZO_PCI_CTRL_PTO_SHIFT;
7470Sstevel@tonic-gate 	} else if (PCI_CHIP_ID(pci_p) >= TOMATILLO_VER_21) {
7480Sstevel@tonic-gate 		l |= (3ull << SCHIZO_PCI_CTRL_PTO_SHIFT);
7490Sstevel@tonic-gate 	}
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	/*
7520Sstevel@tonic-gate 	 * Enable error interrupts.
7530Sstevel@tonic-gate 	 */
7540Sstevel@tonic-gate 	if (pci_error_intr_enable & mask)
7550Sstevel@tonic-gate 		l |= SCHIZO_PCI_CTRL_ERR_INT_EN;
7560Sstevel@tonic-gate 	else
7570Sstevel@tonic-gate 		l &= ~SCHIZO_PCI_CTRL_ERR_INT_EN;
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	/*
7600Sstevel@tonic-gate 	 * Enable pci streaming byte errors and error interrupts.
7610Sstevel@tonic-gate 	 */
7620Sstevel@tonic-gate 	if (pci_sbh_error_intr_enable & mask)
7630Sstevel@tonic-gate 		l |= SCHIZO_PCI_CTRL_SBH_INT_EN;
7640Sstevel@tonic-gate 	else
7650Sstevel@tonic-gate 		l &= ~SCHIZO_PCI_CTRL_SBH_INT_EN;
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 	/*
7680Sstevel@tonic-gate 	 * Enable pci discard timeout error interrupt.
7690Sstevel@tonic-gate 	 */
7700Sstevel@tonic-gate 	if (pci_mmu_error_intr_enable & mask)
7710Sstevel@tonic-gate 		l |= SCHIZO_PCI_CTRL_MMU_INT_EN;
7720Sstevel@tonic-gate 	else
7730Sstevel@tonic-gate 		l &= ~SCHIZO_PCI_CTRL_MMU_INT_EN;
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	/*
7760Sstevel@tonic-gate 	 * Enable PCI-X error interrupts.
7770Sstevel@tonic-gate 	 */
7780Sstevel@tonic-gate 	if (CHIP_TYPE(pci_p) == PCI_CHIP_XMITS) {
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 		if (xmits_error_intr_enable & mask)
7810Sstevel@tonic-gate 			l |= XMITS_PCI_CTRL_X_ERRINT_EN;
7820Sstevel@tonic-gate 		else
7830Sstevel@tonic-gate 			l &= ~XMITS_PCI_CTRL_X_ERRINT_EN;
7840Sstevel@tonic-gate 		/*
7850Sstevel@tonic-gate 		 * Panic if older XMITS hardware is found.
7860Sstevel@tonic-gate 		 */
7870Sstevel@tonic-gate 		if (*pbm_p->pbm_ctrl_reg & XMITS_PCI_CTRL_X_MODE)
7880Sstevel@tonic-gate 			if (PCI_CHIP_ID(pci_p) <= XMITS_VER_10)
7890Sstevel@tonic-gate 				cmn_err(CE_PANIC, "%s (%s): PCIX mode "
7900Sstevel@tonic-gate 				"unsupported on XMITS version %d\n",
7910Sstevel@tonic-gate 				    pbm_p->pbm_nameinst_str,
7920Sstevel@tonic-gate 				    pbm_p->pbm_nameaddr_str, CHIP_VER(pci_p));
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 		if (xmits_perr_recov_int_enable) {
7950Sstevel@tonic-gate 			if (PCI_CHIP_ID(pci_p) >= XMITS_VER_30) {
7960Sstevel@tonic-gate 				uint64_t pcix_err;
7970Sstevel@tonic-gate 				/*
7980Sstevel@tonic-gate 				 * Enable interrupt on PERR
7990Sstevel@tonic-gate 				 */
8000Sstevel@tonic-gate 				pcix_err = *pbm_p->pbm_pcix_err_stat_reg;
8010Sstevel@tonic-gate 				pcix_err |= XMITS_PCIX_STAT_PERR_RECOV_INT_EN;
8020Sstevel@tonic-gate 				pcix_err &= ~XMITS_PCIX_STAT_SERR_ON_PERR;
8030Sstevel@tonic-gate 				*pbm_p->pbm_pcix_err_stat_reg = pcix_err;
8040Sstevel@tonic-gate 			}
8050Sstevel@tonic-gate 		}
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 		/*
8080Sstevel@tonic-gate 		 * Enable parity error detection on internal memories
8090Sstevel@tonic-gate 		 */
8100Sstevel@tonic-gate 		*pbm_p->pbm_pci_ped_ctrl = 0x3fff;
8110Sstevel@tonic-gate 	}
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 	/*
8140Sstevel@tonic-gate 	 * Enable/disable bus parking.
8150Sstevel@tonic-gate 	 */
8160Sstevel@tonic-gate 	if ((pci_bus_parking_enable & mask) &&
8170Sstevel@tonic-gate 	    !ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
8180Sstevel@tonic-gate 	    "no-bus-parking"))
8190Sstevel@tonic-gate 		l |= SCHIZO_PCI_CTRL_ARB_PARK;
8200Sstevel@tonic-gate 	else
8210Sstevel@tonic-gate 		l &= ~SCHIZO_PCI_CTRL_ARB_PARK;
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 	/*
8240Sstevel@tonic-gate 	 * Enable arbitration.
8250Sstevel@tonic-gate 	 */
8260Sstevel@tonic-gate 	l |= PCI_CHIP_ID(pci_p) == XMITS_VER_10 ? XMITS10_PCI_CTRL_ARB_EN_MASK :
8270Sstevel@tonic-gate 		SCHIZO_PCI_CTRL_ARB_EN_MASK;
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	/*
8300Sstevel@tonic-gate 	 * Make sure SERR is clear
8310Sstevel@tonic-gate 	 */
8320Sstevel@tonic-gate 	l |= COMMON_PCI_CTRL_SERR;
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	/*
8360Sstevel@tonic-gate 	 * Enable DTO interrupt, if desired.
8370Sstevel@tonic-gate 	 */
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 	if (PCI_CHIP_ID(pci_p) <= TOMATILLO_VER_20 || (pci_dto_intr_enable &
8400Sstevel@tonic-gate 	    mask))
8410Sstevel@tonic-gate 		l |=	 (TOMATILLO_PCI_CTRL_DTO_INT_EN);
8420Sstevel@tonic-gate 	else
8430Sstevel@tonic-gate 		l &=	 ~(TOMATILLO_PCI_CTRL_DTO_INT_EN);
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 	l |= TOMATILLO_PCI_CTRL_PEN_RD_MLTPL |
8460Sstevel@tonic-gate 		TOMATILLO_PCI_CTRL_PEN_RD_ONE |
8470Sstevel@tonic-gate 		TOMATILLO_PCI_CTRL_PEN_RD_LINE;
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 	/*
8500Sstevel@tonic-gate 	 * Now finally write the control register with the appropriate value.
8510Sstevel@tonic-gate 	 */
8520Sstevel@tonic-gate 	DEBUG1(DBG_ATTACH, dip, "pbm_configure: ctrl reg=%llx\n", l);
8530Sstevel@tonic-gate 	*pbm_p->pbm_ctrl_reg = l;
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 	/*
8560Sstevel@tonic-gate 	 * Enable IO Prefetch on Tomatillo
8570Sstevel@tonic-gate 	 */
8580Sstevel@tonic-gate 	if (CHIP_TYPE(pci_p) == PCI_CHIP_TOMATILLO) {
8590Sstevel@tonic-gate 		volatile uint64_t *ioc_csr_p = pbm_p->pbm_ctrl_reg +
8600Sstevel@tonic-gate 			((TOMATILLO_IOC_CSR_OFF -
8610Sstevel@tonic-gate 			SCHIZO_PCI_CTRL_REG_OFFSET) >> 3);
8620Sstevel@tonic-gate 		*ioc_csr_p = TOMATILLO_WRT_PEN |
8630Sstevel@tonic-gate 			(1 << TOMATILLO_POFFSET_SHIFT) |
8640Sstevel@tonic-gate 			TOMATILLO_C_PEN_RD_MLTPL |
8650Sstevel@tonic-gate 			TOMATILLO_C_PEN_RD_ONE |
8660Sstevel@tonic-gate 			TOMATILLO_C_PEN_RD_LINE;
8670Sstevel@tonic-gate 	}
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate 	/*
8700Sstevel@tonic-gate 	 * Allow DMA write parity errors to generate an interrupt.
8710Sstevel@tonic-gate 	 * This is implemented on Schizo 2.5 and greater and XMITS 3.0
8720Sstevel@tonic-gate 	 * and greater.  Setting this on earlier versions of XMITS 3.0
8730Sstevel@tonic-gate 	 * has no affect.
8740Sstevel@tonic-gate 	 */
8750Sstevel@tonic-gate 	if (((CHIP_TYPE(pci_p) == PCI_CHIP_SCHIZO) &&
8760Sstevel@tonic-gate 	    PCI_CHIP_ID(pci_p) >= SCHIZO_VER_25) ||
8770Sstevel@tonic-gate 	    (CHIP_TYPE(pci_p) == PCI_CHIP_XMITS)) {
8780Sstevel@tonic-gate 		volatile uint64_t *pbm_icd = pbm_p->pbm_ctrl_reg +
8790Sstevel@tonic-gate 		    ((SCHIZO_PERF_PCI_ICD_OFFSET -
8800Sstevel@tonic-gate 		    SCHIZO_PCI_CTRL_REG_OFFSET) >> 3);
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 		*pbm_icd |= SCHIZO_PERF_PCI_ICD_DMAW_PARITY_INT_ENABLE;
8830Sstevel@tonic-gate 	}
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 	/*
8860Sstevel@tonic-gate 	 * Clear any PBM errors.
8870Sstevel@tonic-gate 	 */
8880Sstevel@tonic-gate 	l = (SCHIZO_PCI_AFSR_E_MASK << SCHIZO_PCI_AFSR_PE_SHIFT) |
8890Sstevel@tonic-gate 		(SCHIZO_PCI_AFSR_E_MASK << SCHIZO_PCI_AFSR_SE_SHIFT);
8900Sstevel@tonic-gate 	*pbm_p->pbm_async_flt_status_reg = l;
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 	/*
8930Sstevel@tonic-gate 	 * Allow the diag register to be set based upon variable that
8940Sstevel@tonic-gate 	 * can be configured via /etc/system.
8950Sstevel@tonic-gate 	 */
8960Sstevel@tonic-gate 	l = *pbm_p->pbm_diag_reg;
8970Sstevel@tonic-gate 	DEBUG1(DBG_ATTACH, dip, "pbm_configure: PCI diag reg=%llx\n", l);
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 	/*
9000Sstevel@tonic-gate 	 * Enable/disable retry limit.
9010Sstevel@tonic-gate 	 */
9020Sstevel@tonic-gate 	if (pci_retry_disable & mask)
9030Sstevel@tonic-gate 		l |= COMMON_PCI_DIAG_DIS_RETRY;
9040Sstevel@tonic-gate 	else
9050Sstevel@tonic-gate 		l &= ~COMMON_PCI_DIAG_DIS_RETRY;
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 	/*
9080Sstevel@tonic-gate 	 * Enable/disable DMA write/interrupt synchronization.
9090Sstevel@tonic-gate 	 */
9100Sstevel@tonic-gate 	if (pci_intsync_disable & mask)
9110Sstevel@tonic-gate 		l |= COMMON_PCI_DIAG_DIS_INTSYNC;
9120Sstevel@tonic-gate 	else
9130Sstevel@tonic-gate 		l &= ~COMMON_PCI_DIAG_DIS_INTSYNC;
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 	/*
9160Sstevel@tonic-gate 	 * Enable/disable retry arbitration priority.
9170Sstevel@tonic-gate 	 */
9180Sstevel@tonic-gate 	if (pci_enable_retry_arb & mask)
9190Sstevel@tonic-gate 		l &= ~SCHIZO_PCI_DIAG_DIS_RTRY_ARB;
9200Sstevel@tonic-gate 	else
9210Sstevel@tonic-gate 		l |= SCHIZO_PCI_DIAG_DIS_RTRY_ARB;
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate 	DEBUG1(DBG_ATTACH, dip, "pbm_configure: PCI diag reg=%llx\n", l);
9240Sstevel@tonic-gate 	*pbm_p->pbm_diag_reg = l;
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 	/*
9270Sstevel@tonic-gate 	 * Enable SERR# and parity reporting via command register.
9280Sstevel@tonic-gate 	 */
9290Sstevel@tonic-gate 	s = pci_perr_enable & mask ? PCI_COMM_PARITY_DETECT : 0;
9300Sstevel@tonic-gate 	s |= pci_serr_enable & mask ? PCI_COMM_SERR_ENABLE : 0;
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 	DEBUG1(DBG_ATTACH, dip, "pbm_configure: conf command reg=%x\n", s);
9330Sstevel@tonic-gate 	pbm_p->pbm_config_header->ch_command_reg = s;
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 	/*
9360Sstevel@tonic-gate 	 * Clear error bits in configuration status register.
9370Sstevel@tonic-gate 	 */
9380Sstevel@tonic-gate 	s = PCI_STAT_PERROR | PCI_STAT_S_PERROR |
9390Sstevel@tonic-gate 		PCI_STAT_R_MAST_AB | PCI_STAT_R_TARG_AB |
9400Sstevel@tonic-gate 		PCI_STAT_S_TARG_AB | PCI_STAT_S_PERROR;
9410Sstevel@tonic-gate 	DEBUG1(DBG_ATTACH, dip, "pbm_configure: conf status reg=%x\n", s);
9420Sstevel@tonic-gate 	pbm_p->pbm_config_header->ch_status_reg = s;
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 	/*
9450Sstevel@tonic-gate 	 * The current versions of the obp are suppose to set the latency
9460Sstevel@tonic-gate 	 * timer register but do not.  Bug 1234181 is open against this
9470Sstevel@tonic-gate 	 * problem.  Until this bug is fixed we check to see if the obp
9480Sstevel@tonic-gate 	 * has attempted to set the latency timer register by checking
9490Sstevel@tonic-gate 	 * for the existence of a "latency-timer" property.
9500Sstevel@tonic-gate 	 */
9510Sstevel@tonic-gate 	if (pci_set_latency_timer_register) {
9520Sstevel@tonic-gate 		DEBUG1(DBG_ATTACH, dip,
9530Sstevel@tonic-gate 		    "pbm_configure: set schizo latency timer to %x\n",
9540Sstevel@tonic-gate 			pci_latency_timer);
9550Sstevel@tonic-gate 		pbm_p->pbm_config_header->ch_latency_timer_reg =
9560Sstevel@tonic-gate 			pci_latency_timer;
9570Sstevel@tonic-gate 	}
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_ANY, dip, "latency-timer",
9600Sstevel@tonic-gate 		(int)pbm_p->pbm_config_header->ch_latency_timer_reg);
9611295Sdanice 
9621295Sdanice 	/*
9631295Sdanice 	 * Adjust xmits_upper_retry_counter if set in /etc/system
9641295Sdanice 	 *
9651295Sdanice 	 * NOTE: current implementation resets UPPR_RTRY counter for
9661295Sdanice 	 * _all_ XMITS' PBMs and does not support tuning per PBM.
9671295Sdanice 	 */
9681295Sdanice 	if (CHIP_TYPE(pci_p) == PCI_CHIP_XMITS) {
9691295Sdanice 		uint_t xurc = xmits_upper_retry_counter &
9701295Sdanice 		    XMITS_UPPER_RETRY_MASK;
9711295Sdanice 
9721295Sdanice 		if (xurc) {
9731295Sdanice 			*pbm_p->pbm_upper_retry_counter_reg = (uint64_t)xurc;
9741295Sdanice 			DEBUG1(DBG_ATTACH, dip, "pbm_configure: Setting XMITS"
9751295Sdanice 			    " uppr_rtry counter = 0x%lx\n",
9761295Sdanice 			    *pbm_p->pbm_upper_retry_counter_reg);
9771295Sdanice 		}
9781295Sdanice 	}
9790Sstevel@tonic-gate }
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate uint_t
9820Sstevel@tonic-gate pbm_disable_pci_errors(pbm_t *pbm_p)
9830Sstevel@tonic-gate {
9840Sstevel@tonic-gate 	pci_t *pci_p = pbm_p->pbm_pci_p;
9850Sstevel@tonic-gate 	ib_t *ib_p = pci_p->pci_ib_p;
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 	/*
9880Sstevel@tonic-gate 	 * Disable error and streaming byte hole interrupts via the
9890Sstevel@tonic-gate 	 * PBM control register.
9900Sstevel@tonic-gate 	 */
9910Sstevel@tonic-gate 	*pbm_p->pbm_ctrl_reg &=
9920Sstevel@tonic-gate 		~(SCHIZO_PCI_CTRL_ERR_INT_EN | SCHIZO_PCI_CTRL_SBH_INT_EN |
9930Sstevel@tonic-gate 		SCHIZO_PCI_CTRL_MMU_INT_EN);
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate 	/*
9960Sstevel@tonic-gate 	 * Disable error interrupts via the interrupt mapping register.
9970Sstevel@tonic-gate 	 */
9980Sstevel@tonic-gate 	ib_intr_disable(ib_p, pci_p->pci_inos[CBNINTR_PBM], IB_INTR_NOWAIT);
9990Sstevel@tonic-gate 	return (BF_NONE);
10000Sstevel@tonic-gate }
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate /*
10030Sstevel@tonic-gate  * Layout of the dvma context bucket bitmap entry:
10040Sstevel@tonic-gate  *
10050Sstevel@tonic-gate  *	63 - 56		55 - 0
10060Sstevel@tonic-gate  *	8-bit lock	56-bit, each represent one context
10070Sstevel@tonic-gate  *	DCB_LOCK_BITS	DCB_BMAP_BITS
10080Sstevel@tonic-gate  */
10090Sstevel@tonic-gate #define	DCB_LOCK_BITS	8
10100Sstevel@tonic-gate #define	DCB_BMAP_BITS	(64 - DCB_LOCK_BITS)
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate dvma_context_t
10130Sstevel@tonic-gate pci_iommu_get_dvma_context(iommu_t *iommu_p, dvma_addr_t dvma_pg_index)
10140Sstevel@tonic-gate {
10150Sstevel@tonic-gate 	dvma_context_t ctx;
10160Sstevel@tonic-gate 	int i = (dvma_pg_index >> 6) & 0x1f;	/* 5 bit index within bucket */
10170Sstevel@tonic-gate 	uint64_t ctx_mask, test = 1ull << i;
10180Sstevel@tonic-gate 	uint32_t bucket_no = dvma_pg_index & 0x3f;
10190Sstevel@tonic-gate 	uint64_t *bucket_ptr = iommu_p->iommu_ctx_bitmap + bucket_no;
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 	uint32_t spl = ddi_enter_critical();	/* block interrupts */
10220Sstevel@tonic-gate 	if (ldstub((uint8_t *)bucket_ptr)) {	/* try lock */
10230Sstevel@tonic-gate 		ddi_exit_critical(spl);		/* unblock interrupt */
10240Sstevel@tonic-gate 		pci_iommu_ctx_lock_failure++;
10250Sstevel@tonic-gate 		return (0);
10260Sstevel@tonic-gate 	}
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 	/* clear lock bits */
10290Sstevel@tonic-gate 	ctx_mask = (*bucket_ptr << DCB_LOCK_BITS) >> DCB_LOCK_BITS;
10300Sstevel@tonic-gate 	ASSERT(*bucket_ptr >> DCB_BMAP_BITS == 0xff);
10310Sstevel@tonic-gate 	ASSERT(ctx_mask >> DCB_BMAP_BITS == 0);
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate 	if (ctx_mask & test)			/* quick check i bit */
10340Sstevel@tonic-gate 		for (i = 0, test = 1ull; test & ctx_mask; test <<= 1, i++);
10350Sstevel@tonic-gate 	if (i < DCB_BMAP_BITS)
10360Sstevel@tonic-gate 		ctx_mask |= test;
10370Sstevel@tonic-gate 	*bucket_ptr = ctx_mask;			/* unlock */
10380Sstevel@tonic-gate 	ddi_exit_critical(spl);			/* unblock interrupts */
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate 	ctx = i < DCB_BMAP_BITS ? (bucket_no << 6) | i : 0;
10410Sstevel@tonic-gate 	DEBUG3(DBG_DMA_MAP, iommu_p->iommu_pci_p->pci_dip,
10420Sstevel@tonic-gate 		"get_dvma_context: ctx_mask=0x%x.%x ctx=0x%x\n",
10430Sstevel@tonic-gate 		(uint32_t)(ctx_mask >> 32), (uint32_t)ctx_mask, ctx);
10440Sstevel@tonic-gate 	return (ctx);
10450Sstevel@tonic-gate }
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate void
10480Sstevel@tonic-gate pci_iommu_free_dvma_context(iommu_t *iommu_p, dvma_context_t ctx)
10490Sstevel@tonic-gate {
10500Sstevel@tonic-gate 	uint64_t ctx_mask;
10510Sstevel@tonic-gate 	uint32_t spl, bucket_no = ctx >> 6;
10520Sstevel@tonic-gate 	int bit_no = ctx & 0x3f;
10530Sstevel@tonic-gate 	uint64_t *bucket_ptr = iommu_p->iommu_ctx_bitmap + bucket_no;
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 	DEBUG1(DBG_DMA_MAP, iommu_p->iommu_pci_p->pci_dip,
10560Sstevel@tonic-gate 		"free_dvma_context: ctx=0x%x\n", ctx);
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate 	spl = ddi_enter_critical();			/* block interrupts */
10590Sstevel@tonic-gate 	while (ldstub((uint8_t *)bucket_ptr));		/* spin lock */
10600Sstevel@tonic-gate 	ctx_mask = (*bucket_ptr << DCB_LOCK_BITS) >> DCB_LOCK_BITS;
10610Sstevel@tonic-gate 							/* clear lock bits */
10620Sstevel@tonic-gate 	ASSERT(ctx_mask & (1ull << bit_no));
10630Sstevel@tonic-gate 	*bucket_ptr = ctx_mask ^ (1ull << bit_no);	/* clear & unlock */
10640Sstevel@tonic-gate 	ddi_exit_critical(spl);				/* unblock interrupt */
10650Sstevel@tonic-gate }
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate int
10680Sstevel@tonic-gate pci_sc_ctx_inv(dev_info_t *dip, sc_t *sc_p, ddi_dma_impl_t *mp)
10690Sstevel@tonic-gate {
10700Sstevel@tonic-gate 	dvma_context_t ctx = MP2CTX(mp);
10710Sstevel@tonic-gate 	volatile uint64_t *reg_addr = sc_p->sc_ctx_match_reg + ctx;
10720Sstevel@tonic-gate 	uint64_t matchreg;
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 	if (!*reg_addr) {
10750Sstevel@tonic-gate 		DEBUG1(DBG_SC, dip, "ctx=%x no match\n", ctx);
10760Sstevel@tonic-gate 		return (DDI_SUCCESS);
10770Sstevel@tonic-gate 	}
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 	*sc_p->sc_ctx_invl_reg = ctx;	/* 1st flush write */
10800Sstevel@tonic-gate 	matchreg = *reg_addr;		/* re-fetch after 1st flush */
10810Sstevel@tonic-gate 	if (!matchreg)
10820Sstevel@tonic-gate 		return (DDI_SUCCESS);
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate 	matchreg = (matchreg << SC_ENT_SHIFT) >> SC_ENT_SHIFT;	/* low 16-bit */
10850Sstevel@tonic-gate 	do {
10860Sstevel@tonic-gate 		if (matchreg & 1)
10870Sstevel@tonic-gate 			*sc_p->sc_ctx_invl_reg = ctx;
10880Sstevel@tonic-gate 		matchreg >>= 1;
10890Sstevel@tonic-gate 	} while (matchreg);
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 	if (pci_ctx_no_compat || !*reg_addr)	/* compat: active ctx flush */
10920Sstevel@tonic-gate 		return (DDI_SUCCESS);
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate 	pci_ctx_unsuccess_count++;
10950Sstevel@tonic-gate 	if (pci_ctx_flush_warn)
10960Sstevel@tonic-gate 		cmn_err(pci_ctx_flush_warn, "%s%d: ctx flush unsuccessful\n",
10970Sstevel@tonic-gate 			NAMEINST(dip));
10980Sstevel@tonic-gate 	return (DDI_FAILURE);
10990Sstevel@tonic-gate }
11000Sstevel@tonic-gate 
11010Sstevel@tonic-gate void
11020Sstevel@tonic-gate pci_cb_setup(pci_t *pci_p)
11030Sstevel@tonic-gate {
11040Sstevel@tonic-gate 	dev_info_t *dip = pci_p->pci_dip;
11050Sstevel@tonic-gate 	cb_t *cb_p = pci_p->pci_cb_p;
11060Sstevel@tonic-gate 	uint64_t pa;
11070Sstevel@tonic-gate 	uint32_t chip_id = PCI_CHIP_ID(pci_p);
11080Sstevel@tonic-gate 	DEBUG1(DBG_ATTACH, dip, "cb_create: chip id %d\n", chip_id);
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate 	if (CHIP_TYPE(pci_p) == PCI_CHIP_TOMATILLO) {
11110Sstevel@tonic-gate 		if ((!tm_mtlb_gc_manual) &&
11120Sstevel@tonic-gate 		    (PCI_CHIP_ID(pci_p) <= TOMATILLO_VER_24))
11130Sstevel@tonic-gate 			tm_mtlb_gc = 1;
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 		if (PCI_CHIP_ID(pci_p) <= TOMATILLO_VER_23) {
11160Sstevel@tonic-gate 			extern int ignore_invalid_vecintr;
11170Sstevel@tonic-gate 			ignore_invalid_vecintr = 1;
11180Sstevel@tonic-gate 			tomatillo_store_store_wrka = 1;
11190Sstevel@tonic-gate 			tomatillo_disallow_bypass = 1;
11200Sstevel@tonic-gate 			if (pci_spurintr_msgs == PCI_SPURINTR_MSG_DEFAULT)
11210Sstevel@tonic-gate 				pci_spurintr_msgs = 0;
11220Sstevel@tonic-gate 		}
11230Sstevel@tonic-gate 	}
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate 	if (chip_id == TOMATILLO_VER_20 || chip_id == TOMATILLO_VER_21)
11260Sstevel@tonic-gate 		cmn_err(CE_WARN, "Unsupported Tomatillo rev (%x)", chip_id);
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 	if (chip_id < SCHIZO_VER_23)
11290Sstevel@tonic-gate 		pci_ctx_no_active_flush = 1;
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 	cb_p->cb_node_id = PCI_ID_TO_NODEID(pci_p->pci_id);
11320Sstevel@tonic-gate 	cb_p->cb_ign	 = PCI_ID_TO_IGN(pci_p->pci_id);
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 	/*
11350Sstevel@tonic-gate 	 * schizo control status reg bank is on the 2nd "reg" property entry
11360Sstevel@tonic-gate 	 * interrupt mapping/clear/state regs are on the 1st "reg" entry.
11370Sstevel@tonic-gate 	 *
11380Sstevel@tonic-gate 	 * ALL internal interrupts except pbm interrupts are shared by both
11390Sstevel@tonic-gate 	 * sides, 1st-side-attached is used as *the* owner.
11400Sstevel@tonic-gate 	 */
11410Sstevel@tonic-gate 	pa = (uint64_t)hat_getpfnum(kas.a_hat, pci_p->pci_address[1]);
11420Sstevel@tonic-gate 	cb_p->cb_base_pa = pa << MMU_PAGESHIFT;
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 	pa = pci_p->pci_address[3] ?
11450Sstevel@tonic-gate 		(uint64_t)hat_getpfnum(kas.a_hat, pci_p->pci_address[3]) : 0;
11460Sstevel@tonic-gate 	cb_p->cb_icbase_pa = (pa == PFN_INVALID) ? 0 : pa << MMU_PAGESHIFT;
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate 	pa = (uint64_t)hat_getpfnum(kas.a_hat, pci_p->pci_address[0])
11490Sstevel@tonic-gate 		<< MMU_PAGESHIFT;
11500Sstevel@tonic-gate 	cb_p->cb_map_pa = pa + SCHIZO_IB_INTR_MAP_REG_OFFSET;
11510Sstevel@tonic-gate 	cb_p->cb_clr_pa = pa + SCHIZO_IB_CLEAR_INTR_REG_OFFSET;
11520Sstevel@tonic-gate 	cb_p->cb_obsta_pa = pa + COMMON_IB_OBIO_INTR_STATE_DIAG_REG;
11530Sstevel@tonic-gate }
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate void
11560Sstevel@tonic-gate pci_ecc_setup(ecc_t *ecc_p)
11570Sstevel@tonic-gate {
11580Sstevel@tonic-gate 	ecc_p->ecc_ue.ecc_errpndg_mask = SCHIZO_ECC_UE_AFSR_ERRPNDG;
11590Sstevel@tonic-gate 	ecc_p->ecc_ue.ecc_offset_mask = SCHIZO_ECC_UE_AFSR_QW_OFFSET;
11600Sstevel@tonic-gate 	ecc_p->ecc_ue.ecc_offset_shift = SCHIZO_ECC_UE_AFSR_QW_OFFSET_SHIFT;
11610Sstevel@tonic-gate 	ecc_p->ecc_ue.ecc_size_log2 = 4;
11620Sstevel@tonic-gate 
11630Sstevel@tonic-gate 	ecc_p->ecc_ce.ecc_errpndg_mask = SCHIZO_ECC_CE_AFSR_ERRPNDG;
11640Sstevel@tonic-gate 	ecc_p->ecc_ce.ecc_offset_mask = SCHIZO_ECC_CE_AFSR_QW_OFFSET;
11650Sstevel@tonic-gate 	ecc_p->ecc_ce.ecc_offset_shift = SCHIZO_ECC_CE_AFSR_QW_OFFSET_SHIFT;
11660Sstevel@tonic-gate 	ecc_p->ecc_ce.ecc_size_log2 = 4;
11670Sstevel@tonic-gate }
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate ushort_t
11700Sstevel@tonic-gate pci_ecc_get_synd(uint64_t afsr)
11710Sstevel@tonic-gate {
11720Sstevel@tonic-gate 	return ((ushort_t)((afsr & SCHIZO_ECC_CE_AFSR_SYND) >>
11730Sstevel@tonic-gate 	    SCHIZO_ECC_CE_AFSR_SYND_SHIFT));
11740Sstevel@tonic-gate }
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate /*
11770Sstevel@tonic-gate  * overwrite dvma end address (only on virtual-dma systems)
11780Sstevel@tonic-gate  * initialize tsb size
11790Sstevel@tonic-gate  * reset context bits
11800Sstevel@tonic-gate  * return: IOMMU CSR bank base address (VA)
11810Sstevel@tonic-gate  */
11820Sstevel@tonic-gate 
11830Sstevel@tonic-gate uintptr_t
11840Sstevel@tonic-gate pci_iommu_setup(iommu_t *iommu_p)
11850Sstevel@tonic-gate {
11860Sstevel@tonic-gate 	pci_dvma_range_prop_t *dvma_prop;
11870Sstevel@tonic-gate 	int dvma_prop_len;
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate 	uintptr_t a;
11900Sstevel@tonic-gate 	pci_t *pci_p = iommu_p->iommu_pci_p;
11910Sstevel@tonic-gate 	dev_info_t *dip = pci_p->pci_dip;
11920Sstevel@tonic-gate 	uint_t tsb_size = iommu_tsb_cookie_to_size(pci_p->pci_tsb_cookie);
1193566Ssuha 	uint_t tsb_size_prop;
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 	/*
11960Sstevel@tonic-gate 	 * Initializations for Tomatillo's micro TLB bug. errata #82
11970Sstevel@tonic-gate 	 */
11980Sstevel@tonic-gate 	if (tm_mtlb_gc) {
11990Sstevel@tonic-gate 		iommu_p->iommu_mtlb_nreq = 0;
12000Sstevel@tonic-gate 		iommu_p->iommu_mtlb_npgs = 0;
12010Sstevel@tonic-gate 		iommu_p->iommu_mtlb_maxpgs = tm_mtlb_maxpgs;
12020Sstevel@tonic-gate 		iommu_p->iommu_mtlb_req_p = (dvma_unbind_req_t *)
12030Sstevel@tonic-gate 		    kmem_zalloc(sizeof (dvma_unbind_req_t) *
12040Sstevel@tonic-gate 		    (tm_mtlb_maxpgs + 1), KM_SLEEP);
12050Sstevel@tonic-gate 		mutex_init(&iommu_p->iommu_mtlb_lock, NULL, MUTEX_DRIVER, NULL);
12060Sstevel@tonic-gate 	}
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
12090Sstevel@tonic-gate 		"virtual-dma", (caddr_t)&dvma_prop, &dvma_prop_len) !=
12100Sstevel@tonic-gate 		DDI_PROP_SUCCESS)
12110Sstevel@tonic-gate 		goto tsb_done;
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 	if (dvma_prop_len != sizeof (pci_dvma_range_prop_t)) {
12140Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: invalid virtual-dma property",
12150Sstevel@tonic-gate 			ddi_driver_name(dip), ddi_get_instance(dip));
12160Sstevel@tonic-gate 		goto tsb_end;
12170Sstevel@tonic-gate 	}
12180Sstevel@tonic-gate 	iommu_p->iommu_dvma_end = dvma_prop->dvma_base +
12190Sstevel@tonic-gate 		(dvma_prop->dvma_len - 1);
1220566Ssuha 	tsb_size_prop = IOMMU_BTOP(dvma_prop->dvma_len) * sizeof (uint64_t);
1221566Ssuha 	tsb_size = MIN(tsb_size_prop, tsb_size);
12220Sstevel@tonic-gate tsb_end:
12230Sstevel@tonic-gate 	kmem_free(dvma_prop, dvma_prop_len);
12240Sstevel@tonic-gate tsb_done:
12250Sstevel@tonic-gate 	iommu_p->iommu_tsb_size = iommu_tsb_size_encode(tsb_size);
12260Sstevel@tonic-gate 	iommu_p->iommu_ctx_bitmap =
12270Sstevel@tonic-gate 		kmem_zalloc(IOMMU_CTX_BITMAP_SIZE, KM_SLEEP);
12280Sstevel@tonic-gate 	*iommu_p->iommu_ctx_bitmap = 1ull;	/* reserve context 0 */
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate 	/*
12310Sstevel@tonic-gate 	 * Determine the virtual address of the register block
12320Sstevel@tonic-gate 	 * containing the iommu control registers and determine
12330Sstevel@tonic-gate 	 * the virtual address of schizo specific iommu registers.
12340Sstevel@tonic-gate 	 */
12350Sstevel@tonic-gate 	a = (uintptr_t)pci_p->pci_address[0];
12360Sstevel@tonic-gate 	iommu_p->iommu_flush_ctx_reg =
12370Sstevel@tonic-gate 		(uint64_t *)(a + SCHIZO_IOMMU_FLUSH_CTX_REG_OFFSET);
12380Sstevel@tonic-gate 	if (CHIP_TYPE(pci_p) == PCI_CHIP_TOMATILLO)
12390Sstevel@tonic-gate 		iommu_p->iommu_tfar_reg =
12400Sstevel@tonic-gate 			(uint64_t *)(a + TOMATILLO_IOMMU_ERR_TFAR_OFFSET);
12410Sstevel@tonic-gate 	return (a);	/* PCICSRBase */
12420Sstevel@tonic-gate }
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate void
12450Sstevel@tonic-gate pci_iommu_teardown(iommu_t *iommu_p)
12460Sstevel@tonic-gate {
12470Sstevel@tonic-gate 	if (pci_use_contexts)
12480Sstevel@tonic-gate 		iommu_ctx_free(iommu_p);
12490Sstevel@tonic-gate 	if (iommu_p->iommu_mtlb_req_p) {
12500Sstevel@tonic-gate 		kmem_free(iommu_p->iommu_mtlb_req_p,
12510Sstevel@tonic-gate 		    sizeof (dvma_unbind_req_t) * (tm_mtlb_maxpgs + 1));
12520Sstevel@tonic-gate 		mutex_destroy(&iommu_p->iommu_mtlb_lock);
12530Sstevel@tonic-gate 		iommu_p->iommu_mtlb_req_p = NULL;
12540Sstevel@tonic-gate 		iommu_p->iommu_mtlb_nreq = 0;
12550Sstevel@tonic-gate 		iommu_p->iommu_mtlb_npgs = iommu_p->iommu_mtlb_maxpgs = 0;
12560Sstevel@tonic-gate 	}
12570Sstevel@tonic-gate }
12580Sstevel@tonic-gate 
12590Sstevel@tonic-gate uintptr_t
12600Sstevel@tonic-gate get_pbm_reg_base(pci_t *pci_p)
12610Sstevel@tonic-gate {
12620Sstevel@tonic-gate 	return ((uintptr_t)
12630Sstevel@tonic-gate 		(pci_p->pci_address[0] + SCHIZO_PCI_CTRL_REG_OFFSET));
12640Sstevel@tonic-gate }
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate /* ARGSUSED */
12670Sstevel@tonic-gate static boolean_t
12680Sstevel@tonic-gate pci_pbm_panic_callb(void *arg, int code)
12690Sstevel@tonic-gate {
12700Sstevel@tonic-gate 	pbm_t *pbm_p = (pbm_t *)arg;
12710Sstevel@tonic-gate 	volatile uint64_t *ctrl_reg_p;
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate 	if (pbm_p->pbm_quiesce_count > 0) {
12740Sstevel@tonic-gate 		ctrl_reg_p = pbm_p->pbm_ctrl_reg;
12750Sstevel@tonic-gate 		*ctrl_reg_p = pbm_p->pbm_saved_ctrl_reg;
12760Sstevel@tonic-gate 	}
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 	return (B_TRUE);
12790Sstevel@tonic-gate }
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate static boolean_t
12820Sstevel@tonic-gate pci_pbm_debug_callb(void *arg, int code)
12830Sstevel@tonic-gate {
12840Sstevel@tonic-gate 	pbm_t *pbm_p = (pbm_t *)arg;
12850Sstevel@tonic-gate 	volatile uint64_t *ctrl_reg_p;
12860Sstevel@tonic-gate 	uint64_t ctrl_reg;
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 	if (pbm_p->pbm_quiesce_count > 0) {
12890Sstevel@tonic-gate 		ctrl_reg_p = pbm_p->pbm_ctrl_reg;
12900Sstevel@tonic-gate 		if (code == 0) {
12910Sstevel@tonic-gate 			*ctrl_reg_p = pbm_p->pbm_saved_ctrl_reg;
12920Sstevel@tonic-gate 		} else {
12930Sstevel@tonic-gate 			ctrl_reg = pbm_p->pbm_saved_ctrl_reg;
12940Sstevel@tonic-gate 			ctrl_reg &= ~(SCHIZO_PCI_CTRL_ARB_EN_MASK |
12950Sstevel@tonic-gate 			    SCHIZO_PCI_CTRL_ARB_PARK);
12960Sstevel@tonic-gate 			*ctrl_reg_p = ctrl_reg;
12970Sstevel@tonic-gate 		}
12980Sstevel@tonic-gate 	}
12990Sstevel@tonic-gate 
13000Sstevel@tonic-gate 	return (B_TRUE);
13010Sstevel@tonic-gate }
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate void
13040Sstevel@tonic-gate pci_pbm_setup(pbm_t *pbm_p)
13050Sstevel@tonic-gate {
13060Sstevel@tonic-gate 	pci_t *pci_p = pbm_p->pbm_pci_p;
13070Sstevel@tonic-gate 	caddr_t a = pci_p->pci_address[0]; /* PBM block base VA */
13080Sstevel@tonic-gate 	uint64_t pa = va_to_pa(a);
13090Sstevel@tonic-gate 	extern int segkmem_reloc;
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate 	mutex_init(&pbm_p->pbm_sync_mutex, NULL, MUTEX_DRIVER,
13120Sstevel@tonic-gate 	    (void *)ipltospl(XCALL_PIL));
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate 	pbm_p->pbm_config_header = (config_header_t *)pci_p->pci_address[2];
13150Sstevel@tonic-gate 	pbm_p->pbm_ctrl_reg = (uint64_t *)(a + SCHIZO_PCI_CTRL_REG_OFFSET);
13160Sstevel@tonic-gate 	pbm_p->pbm_diag_reg = (uint64_t *)(a + SCHIZO_PCI_DIAG_REG_OFFSET);
13170Sstevel@tonic-gate 	pbm_p->pbm_async_flt_status_reg =
13180Sstevel@tonic-gate 		(uint64_t *)(a + SCHIZO_PCI_ASYNC_FLT_STATUS_REG_OFFSET);
13190Sstevel@tonic-gate 	pbm_p->pbm_async_flt_addr_reg =
13200Sstevel@tonic-gate 		(uint64_t *)(a + SCHIZO_PCI_ASYNC_FLT_ADDR_REG_OFFSET);
13210Sstevel@tonic-gate 	pbm_p->pbm_estar_reg = (uint64_t *)(a + SCHIZO_PCI_ESTAR_REG_OFFSET);
13220Sstevel@tonic-gate 	pbm_p->pbm_pcix_err_stat_reg = (uint64_t *)(a +
13230Sstevel@tonic-gate 	    XMITS_PCI_X_ERROR_STATUS_REG_OFFSET);
13240Sstevel@tonic-gate 	pbm_p->pbm_pci_ped_ctrl = (uint64_t *)(a +
13250Sstevel@tonic-gate 	    XMITS_PARITY_DETECT_REG_OFFSET);
13260Sstevel@tonic-gate 
13270Sstevel@tonic-gate 	/*
13280Sstevel@tonic-gate 	 * Create a property to indicate that this node supports DVMA
13290Sstevel@tonic-gate 	 * page relocation.
13300Sstevel@tonic-gate 	 */
13310Sstevel@tonic-gate 	if (CHIP_TYPE(pci_p) != PCI_CHIP_TOMATILLO && segkmem_reloc != 0) {
13320Sstevel@tonic-gate 		pci_dvma_remap_enabled = 1;
13330Sstevel@tonic-gate 		(void) ndi_prop_create_boolean(DDI_DEV_T_NONE,
13340Sstevel@tonic-gate 		    pci_p->pci_dip, "dvma-remap-supported");
13350Sstevel@tonic-gate 	}
13360Sstevel@tonic-gate 
13370Sstevel@tonic-gate 	/*
13380Sstevel@tonic-gate 	 * Register a panic callback so we can unquiesce this bus
13390Sstevel@tonic-gate 	 * if it has been placed in the quiesced state.
13400Sstevel@tonic-gate 	 */
13410Sstevel@tonic-gate 	pbm_p->pbm_panic_cb_id = callb_add(pci_pbm_panic_callb,
13420Sstevel@tonic-gate 	    (void *)pbm_p, CB_CL_PANIC, "pci_panic");
13430Sstevel@tonic-gate 	pbm_p->pbm_debug_cb_id = callb_add(pci_pbm_panic_callb,
13440Sstevel@tonic-gate 	    (void *)pbm_p, CB_CL_ENTER_DEBUGGER, "pci_debug_enter");
13450Sstevel@tonic-gate 
13460Sstevel@tonic-gate 	if (CHIP_TYPE(pci_p) != PCI_CHIP_SCHIZO)
13470Sstevel@tonic-gate 		goto non_schizo;
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate 	if (PCI_CHIP_ID(pci_p) >= SCHIZO_VER_23) {
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate 		pbm_p->pbm_sync_reg_pa = pa + SCHIZO_PBM_DMA_SYNC_REG_OFFSET;
13520Sstevel@tonic-gate 
13530Sstevel@tonic-gate 		/*
13540Sstevel@tonic-gate 		 * This is a software workaround to fix schizo hardware bug.
13550Sstevel@tonic-gate 		 * Create a boolean property and its existence means consistent
13560Sstevel@tonic-gate 		 * dma sync should not be done while in prom. The usb polled
13570Sstevel@tonic-gate 		 * code (OHCI,EHCI) will check for this property and will not
13580Sstevel@tonic-gate 		 * do dma sync if this property exist.
13590Sstevel@tonic-gate 		 */
13600Sstevel@tonic-gate 		(void) ndi_prop_create_boolean(DDI_DEV_T_NONE,
13610Sstevel@tonic-gate 		    pci_p->pci_dip, "no-prom-cdma-sync");
13620Sstevel@tonic-gate 	}
13630Sstevel@tonic-gate 	return;
13640Sstevel@tonic-gate non_schizo:
13650Sstevel@tonic-gate 	if (CHIP_TYPE(pci_p) == PCI_CHIP_TOMATILLO) {
13660Sstevel@tonic-gate 		pci_dvma_sync_before_unmap = 1;
13670Sstevel@tonic-gate 		pa = pci_p->pci_cb_p->cb_icbase_pa;
13680Sstevel@tonic-gate 	}
13691295Sdanice 	if (CHIP_TYPE(pci_p) == PCI_CHIP_XMITS)
13701295Sdanice 		pbm_p->pbm_upper_retry_counter_reg =
13711295Sdanice 		    (uint64_t *)(a + XMITS_UPPER_RETRY_COUNTER_REG_OFFSET);
13721295Sdanice 
13730Sstevel@tonic-gate 	pbm_p->pbm_sync_reg_pa = pa + PBM_DMA_SYNC_PEND_REG_OFFSET;
13740Sstevel@tonic-gate }
13750Sstevel@tonic-gate 
13760Sstevel@tonic-gate void
13770Sstevel@tonic-gate pci_pbm_teardown(pbm_t *pbm_p)
13780Sstevel@tonic-gate {
13790Sstevel@tonic-gate 	(void) callb_delete(pbm_p->pbm_panic_cb_id);
13800Sstevel@tonic-gate 	(void) callb_delete(pbm_p->pbm_debug_cb_id);
13810Sstevel@tonic-gate }
13820Sstevel@tonic-gate 
13830Sstevel@tonic-gate uintptr_t
13840Sstevel@tonic-gate pci_ib_setup(ib_t *ib_p)
13850Sstevel@tonic-gate {
13860Sstevel@tonic-gate 	/*
13870Sstevel@tonic-gate 	 * Determine virtual addresses of bridge specific registers,
13880Sstevel@tonic-gate 	 */
13890Sstevel@tonic-gate 	pci_t *pci_p = ib_p->ib_pci_p;
13900Sstevel@tonic-gate 	uintptr_t a = (uintptr_t)pci_p->pci_address[0];
13910Sstevel@tonic-gate 
13920Sstevel@tonic-gate 	ib_p->ib_ign = PCI_ID_TO_IGN(pci_p->pci_id);
13930Sstevel@tonic-gate 	ib_p->ib_max_ino = SCHIZO_MAX_INO;
13940Sstevel@tonic-gate 	ib_p->ib_slot_intr_map_regs = a + SCHIZO_IB_SLOT_INTR_MAP_REG_OFFSET;
13950Sstevel@tonic-gate 	ib_p->ib_intr_map_regs = a + SCHIZO_IB_INTR_MAP_REG_OFFSET;
13960Sstevel@tonic-gate 	ib_p->ib_slot_clear_intr_regs =
13970Sstevel@tonic-gate 		a + SCHIZO_IB_CLEAR_INTR_REG_OFFSET;
13980Sstevel@tonic-gate 	return (a);
13990Sstevel@tonic-gate }
14000Sstevel@tonic-gate 
14010Sstevel@tonic-gate void
14020Sstevel@tonic-gate pci_sc_setup(sc_t *sc_p)
14030Sstevel@tonic-gate {
14040Sstevel@tonic-gate 	pci_t *pci_p = sc_p->sc_pci_p;
14050Sstevel@tonic-gate 	uintptr_t a;
14060Sstevel@tonic-gate 
14070Sstevel@tonic-gate 	/*
14080Sstevel@tonic-gate 	 * Determine the virtual addresses of the stream cache
14090Sstevel@tonic-gate 	 * control/status and flush registers.
14100Sstevel@tonic-gate 	 */
14110Sstevel@tonic-gate 	a = (uintptr_t)pci_p->pci_address[0];	/* PCICSRBase */
14120Sstevel@tonic-gate 	sc_p->sc_ctrl_reg = (uint64_t *)(a + SCHIZO_SC_CTRL_REG_OFFSET);
14130Sstevel@tonic-gate 	sc_p->sc_invl_reg = (uint64_t *)(a + SCHIZO_SC_INVL_REG_OFFSET);
14140Sstevel@tonic-gate 	sc_p->sc_sync_reg = (uint64_t *)(a + SCHIZO_SC_SYNC_REG_OFFSET);
14150Sstevel@tonic-gate 	sc_p->sc_ctx_invl_reg = (uint64_t *)(a + SCHIZO_SC_CTX_INVL_REG_OFFSET);
14160Sstevel@tonic-gate 	sc_p->sc_ctx_match_reg =
14170Sstevel@tonic-gate 		(uint64_t *)(a + SCHIZO_SC_CTX_MATCH_REG_OFFSET);
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 	/*
14200Sstevel@tonic-gate 	 * Determine the virtual addresses of the streaming cache
14210Sstevel@tonic-gate 	 * diagnostic access registers.
14220Sstevel@tonic-gate 	 */
14230Sstevel@tonic-gate 	sc_p->sc_data_diag_acc = (uint64_t *)(a + SCHIZO_SC_DATA_DIAG_OFFSET);
14240Sstevel@tonic-gate 	sc_p->sc_tag_diag_acc = (uint64_t *)(a + SCHIZO_SC_TAG_DIAG_OFFSET);
14250Sstevel@tonic-gate 	sc_p->sc_ltag_diag_acc = (uint64_t *)(a + SCHIZO_SC_LTAG_DIAG_OFFSET);
14260Sstevel@tonic-gate }
14270Sstevel@tonic-gate 
14280Sstevel@tonic-gate /*ARGSUSED*/
14290Sstevel@tonic-gate int
14300Sstevel@tonic-gate pci_get_numproxy(dev_info_t *dip)
14310Sstevel@tonic-gate {
14320Sstevel@tonic-gate 	/*
14330Sstevel@tonic-gate 	 * Schizo does not support interrupt proxies.
14340Sstevel@tonic-gate 	 */
14350Sstevel@tonic-gate 	return (0);
14360Sstevel@tonic-gate }
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate /*
14390Sstevel@tonic-gate  * pcisch error handling 101:
14400Sstevel@tonic-gate  *
14410Sstevel@tonic-gate  * The various functions below are responsible for error handling. Given
14420Sstevel@tonic-gate  * a particular error, they must gather the appropriate state, report all
14430Sstevel@tonic-gate  * errors with correct payload, and attempt recovery where ever possible.
14440Sstevel@tonic-gate  *
14450Sstevel@tonic-gate  * Recovery in the context of this driver is being able notify a leaf device
14460Sstevel@tonic-gate  * of the failed transaction. This leaf device may either be the master or
14470Sstevel@tonic-gate  * target for this transaction and may have already received an error
14480Sstevel@tonic-gate  * notification via a PCI interrupt. Notification is done via DMA and access
14490Sstevel@tonic-gate  * handles. If we capture an address for the transaction then we can map it
14500Sstevel@tonic-gate  * to a handle(if the leaf device is fma-compliant) and fault the handle as
14510Sstevel@tonic-gate  * well as call the device driver registered callback.
14520Sstevel@tonic-gate  *
14530Sstevel@tonic-gate  * The hardware can either interrupt or trap upon detection of an error, in
14540Sstevel@tonic-gate  * some rare cases it also causes a fatal reset.
14550Sstevel@tonic-gate  *
14560Sstevel@tonic-gate  * cb_buserr_intr() is responsible for handling control block
14570Sstevel@tonic-gate  * errors(errors which stem from the host bus side of the bridge). Since
14580Sstevel@tonic-gate  * we support multiple chips and host bus standards, cb_buserr_intr will
14590Sstevel@tonic-gate  * call a bus specific error handler to report and handle the detected
14600Sstevel@tonic-gate  * error. Since this error can either affect or orginate from either of the
14610Sstevel@tonic-gate  * two PCI busses which are connected to the bridge, we need to call
14620Sstevel@tonic-gate  * pci_pbm_err_handler() for each bus as well to report their errors. We
14630Sstevel@tonic-gate  * also need to gather possible errors which have been detected by their
14640Sstevel@tonic-gate  * compliant children(via ndi_fm_handler_dispatch()).
14650Sstevel@tonic-gate  *
14660Sstevel@tonic-gate  * pbm_error_intr() and ecc_intr() are responsible for PCI Block Module
14670Sstevel@tonic-gate  * errors(generic PCI + bridge specific) and ECC errors, respectively. They
14680Sstevel@tonic-gate  * are common between pcisch and pcipsy and therefore exist in pci_pbm.c and
14690Sstevel@tonic-gate  * pci_ecc.c. To support error handling certain chip specific handlers
14700Sstevel@tonic-gate  * must exist and they are defined below.
14710Sstevel@tonic-gate  *
14720Sstevel@tonic-gate  * cpu_deferred_error() and cpu_async_error(), handle the traps that may
14730Sstevel@tonic-gate  * have originated from IO space. They call into the registered IO callbacks
14740Sstevel@tonic-gate  * to report and handle errors that may have caused the trap.
14750Sstevel@tonic-gate  *
14760Sstevel@tonic-gate  * pci_pbm_err_handler() is called by pbm_error_intr() or pci_err_callback()
14770Sstevel@tonic-gate  * (generic fma callback for pcipsy/pcisch, pci_fm.c). pci_err_callback() is
14780Sstevel@tonic-gate  * called when the CPU has trapped because of a possible IO error(TO/BERR/UE).
14790Sstevel@tonic-gate  * It will call pci_pbm_err_handler() to report and handle all PCI/PBM/IOMMU
14800Sstevel@tonic-gate  * related errors which are detected by the chip.
14810Sstevel@tonic-gate  *
14820Sstevel@tonic-gate  * pci_pbm_err_handler() calls a generic interface pbm_afsr_report()(pci_pbm.c)
14830Sstevel@tonic-gate  * to report the pbm specific errors and attempt to map the failed address
14840Sstevel@tonic-gate  * (if captured) to a device instance. pbm_afsr_report() calls a chip specific
14850Sstevel@tonic-gate  * interface to interpret the afsr bits pci_pbm_classify()(pcisch.c/pcipsy.c).
14860Sstevel@tonic-gate  * pci_pbm_err_handler() also calls iommu_err_handler() to handle IOMMU related
14870Sstevel@tonic-gate  * errors.
14880Sstevel@tonic-gate  *
14890Sstevel@tonic-gate  * iommu_err_handler() can recover from most errors, as long as the requesting
14900Sstevel@tonic-gate  * device is notified and the iommu can be flushed. If an IOMMU error occurs
14910Sstevel@tonic-gate  * due to a UE then it will be passed on to the ecc_err_handler() for
14920Sstevel@tonic-gate  * subsequent handling.
14930Sstevel@tonic-gate  *
14940Sstevel@tonic-gate  * ecc_err_handler()(pci_ecc.c) also calls a chip specific interface to
14950Sstevel@tonic-gate  * interpret the afsr, pci_ecc_classify(). ecc_err_handler() also calls
14960Sstevel@tonic-gate  * pci_pbm_err_handler() to report any pbm errors detected.
14970Sstevel@tonic-gate  *
14980Sstevel@tonic-gate  * To make sure that the trap code and the interrupt code are not going
14990Sstevel@tonic-gate  * to step on each others toes we have a per chip pci_fm_mutex. This also
15000Sstevel@tonic-gate  * makes it necessary for us to be caution while we are at a high PIL, so
15010Sstevel@tonic-gate  * that we do not cause a subsequent trap that causes us to hang.
15020Sstevel@tonic-gate  *
15030Sstevel@tonic-gate  * The attempt to commonize code was meant to keep in line with the current
15040Sstevel@tonic-gate  * pci driver implementation and it was not meant to confuse. If you are
15050Sstevel@tonic-gate  * confused then don't worry, I was too.
15060Sstevel@tonic-gate  *
15070Sstevel@tonic-gate  */
15080Sstevel@tonic-gate static void
15090Sstevel@tonic-gate pci_cb_errstate_get(cb_t *cb_p, cb_errstate_t *cb_err_p)
15100Sstevel@tonic-gate {
15110Sstevel@tonic-gate 	uint64_t pa = cb_p->cb_base_pa;
15120Sstevel@tonic-gate 	int	i;
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate 	bzero(cb_err_p, sizeof (cb_errstate_t));
15150Sstevel@tonic-gate 
15160Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cb_p->cb_pci_cmn_p->pci_fm_mutex));
15170Sstevel@tonic-gate 
15180Sstevel@tonic-gate 	cb_err_p->cb_bridge_type = PCI_BRIDGE_TYPE(cb_p->cb_pci_cmn_p);
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 	cb_err_p->cb_csr = lddphysio(pa + SCHIZO_CB_CSR_OFFSET);
15210Sstevel@tonic-gate 	cb_err_p->cb_err = lddphysio(pa + SCHIZO_CB_ERRCTRL_OFFSET);
15220Sstevel@tonic-gate 	cb_err_p->cb_intr = lddphysio(pa + SCHIZO_CB_INTCTRL_OFFSET);
15230Sstevel@tonic-gate 	cb_err_p->cb_elog = lddphysio(pa + SCHIZO_CB_ERRLOG_OFFSET);
15240Sstevel@tonic-gate 	cb_err_p->cb_ecc = lddphysio(pa + SCHIZO_CB_ECCCTRL_OFFSET);
15250Sstevel@tonic-gate 	cb_err_p->cb_ue_afsr = lddphysio(pa + SCHIZO_CB_UEAFSR_OFFSET);
15260Sstevel@tonic-gate 	cb_err_p->cb_ue_afar = lddphysio(pa + SCHIZO_CB_UEAFAR_OFFSET);
15270Sstevel@tonic-gate 	cb_err_p->cb_ce_afsr = lddphysio(pa + SCHIZO_CB_CEAFSR_OFFSET);
15280Sstevel@tonic-gate 	cb_err_p->cb_ce_afar = lddphysio(pa + SCHIZO_CB_CEAFAR_OFFSET);
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate 	if ((CB_CHIP_TYPE((cb_t *)cb_p)) == PCI_CHIP_XMITS) {
15310Sstevel@tonic-gate 		cb_err_p->cb_first_elog = lddphysio(pa +
15320Sstevel@tonic-gate 				XMITS_CB_FIRST_ERROR_LOG);
15330Sstevel@tonic-gate 		cb_err_p->cb_first_eaddr = lddphysio(pa +
15340Sstevel@tonic-gate 				XMITS_CB_FIRST_ERROR_ADDR);
15350Sstevel@tonic-gate 		cb_err_p->cb_leaf_status = lddphysio(pa +
15360Sstevel@tonic-gate 				XMITS_CB_FIRST_ERROR_ADDR);
15370Sstevel@tonic-gate 	}
15380Sstevel@tonic-gate 
15390Sstevel@tonic-gate 	/* Gather PBM state information for both sides of this chip */
15400Sstevel@tonic-gate 	for (i = 0; i < 2; i++) {
15410Sstevel@tonic-gate 		if (cb_p->cb_pci_cmn_p->pci_p[i] == NULL)
15420Sstevel@tonic-gate 			continue;
15430Sstevel@tonic-gate 		pci_pbm_errstate_get(((cb_t *)cb_p)->cb_pci_cmn_p->
15440Sstevel@tonic-gate 					    pci_p[i], &cb_err_p->cb_pbm[i]);
15450Sstevel@tonic-gate 	}
15460Sstevel@tonic-gate }
15470Sstevel@tonic-gate 
15480Sstevel@tonic-gate static void
15490Sstevel@tonic-gate pci_cb_clear_error(cb_t *cb_p, cb_errstate_t *cb_err_p)
15500Sstevel@tonic-gate {
15510Sstevel@tonic-gate 	uint64_t pa = ((cb_t *)cb_p)->cb_base_pa;
15520Sstevel@tonic-gate 
15530Sstevel@tonic-gate 	stdphysio(pa + SCHIZO_CB_ERRLOG_OFFSET, cb_err_p->cb_elog);
15540Sstevel@tonic-gate }
15550Sstevel@tonic-gate 
15560Sstevel@tonic-gate static cb_fm_err_t safari_err_tbl[] = {
15570Sstevel@tonic-gate 	SAFARI_BAD_CMD,		SCHIZO_CB_ELOG_BAD_CMD,		CB_FATAL,
15580Sstevel@tonic-gate 	SAFARI_SSM_DIS,		SCHIZO_CB_ELOG_SSM_DIS,		CB_FATAL,
15590Sstevel@tonic-gate 	SAFARI_BAD_CMD_PCIA, 	SCHIZO_CB_ELOG_BAD_CMD_PCIA,	CB_FATAL,
15600Sstevel@tonic-gate 	SAFARI_BAD_CMD_PCIB, 	SCHIZO_CB_ELOG_BAD_CMD_PCIB,	CB_FATAL,
15610Sstevel@tonic-gate 	SAFARI_PAR_ERR_INT_PCIB, XMITS_CB_ELOG_PAR_ERR_INT_PCIB, CB_FATAL,
15620Sstevel@tonic-gate 	SAFARI_PAR_ERR_INT_PCIA, XMITS_CB_ELOG_PAR_ERR_INT_PCIA, CB_FATAL,
15630Sstevel@tonic-gate 	SAFARI_PAR_ERR_INT_SAF,	XMITS_CB_ELOG_PAR_ERR_INT_SAF,	CB_FATAL,
15640Sstevel@tonic-gate 	SAFARI_PLL_ERR_PCIB,	XMITS_CB_ELOG_PLL_ERR_PCIB,	CB_FATAL,
15650Sstevel@tonic-gate 	SAFARI_PLL_ERR_PCIA,	XMITS_CB_ELOG_PLL_ERR_PCIA,	CB_FATAL,
15660Sstevel@tonic-gate 	SAFARI_PLL_ERR_SAF,	XMITS_CB_ELOG_PLL_ERR_SAF,	CB_FATAL,
15670Sstevel@tonic-gate 	SAFARI_SAF_CIQ_TO,	SCHIZO_CB_ELOG_SAF_CIQ_TO,	CB_FATAL,
15680Sstevel@tonic-gate 	SAFARI_SAF_LPQ_TO,	SCHIZO_CB_ELOG_SAF_LPQ_TO,	CB_FATAL,
15690Sstevel@tonic-gate 	SAFARI_SAF_SFPQ_TO,	SCHIZO_CB_ELOG_SAF_SFPQ_TO,	CB_FATAL,
15700Sstevel@tonic-gate 	SAFARI_APERR,		SCHIZO_CB_ELOG_ADDR_PAR_ERR,	CB_FATAL,
15710Sstevel@tonic-gate 	SAFARI_UNMAP_ERR,	SCHIZO_CB_ELOG_UNMAP_ERR,	CB_FATAL,
15720Sstevel@tonic-gate 	SAFARI_BUS_ERR,		SCHIZO_CB_ELOG_BUS_ERR,		CB_FATAL,
15730Sstevel@tonic-gate 	SAFARI_TO_ERR,		SCHIZO_CB_ELOG_TO_ERR,		CB_FATAL,
15740Sstevel@tonic-gate 	SAFARI_DSTAT_ERR,	SCHIZO_CB_ELOG_DSTAT_ERR,	CB_FATAL,
15750Sstevel@tonic-gate 	SAFARI_SAF_UFPQ_TO,	SCHIZO_CB_ELOG_SAF_UFPQ_TO,	CB_FATAL,
15760Sstevel@tonic-gate 	SAFARI_CPU0_PAR_SINGLE,	SCHIZO_CB_ELOG_CPU0_PAR_SINGLE,	CB_FATAL,
15770Sstevel@tonic-gate 	SAFARI_CPU0_PAR_BIDI,	SCHIZO_CB_ELOG_CPU0_PAR_BIDI,	CB_FATAL,
15780Sstevel@tonic-gate 	SAFARI_CPU1_PAR_SINGLE,	SCHIZO_CB_ELOG_CPU1_PAR_SINGLE,	CB_FATAL,
15790Sstevel@tonic-gate 	SAFARI_CPU1_PAR_BIDI,	SCHIZO_CB_ELOG_CPU1_PAR_BIDI,	CB_FATAL,
15800Sstevel@tonic-gate 	NULL,			NULL,				NULL,
15810Sstevel@tonic-gate };
15820Sstevel@tonic-gate 
15830Sstevel@tonic-gate /*
15840Sstevel@tonic-gate  * Function used to handle and log Safari bus errors.
15850Sstevel@tonic-gate  */
15860Sstevel@tonic-gate static int
15870Sstevel@tonic-gate safari_err_handler(dev_info_t *dip, uint64_t fme_ena,
15880Sstevel@tonic-gate 		cb_errstate_t *cb_err_p)
15890Sstevel@tonic-gate {
15900Sstevel@tonic-gate 	int	i;
15910Sstevel@tonic-gate 	int	fatal = 0;
15920Sstevel@tonic-gate 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
15930Sstevel@tonic-gate 	pci_common_t *cmn_p = pci_p->pci_common_p;
15940Sstevel@tonic-gate 
15950Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cmn_p->pci_fm_mutex));
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate 	for (i = 0; safari_err_tbl[i].cb_err_class != NULL; i++) {
15980Sstevel@tonic-gate 		if (cb_err_p->cb_elog & safari_err_tbl[i].cb_reg_bit) {
15990Sstevel@tonic-gate 			cb_err_p->cb_err_class = safari_err_tbl[i].cb_err_class;
16000Sstevel@tonic-gate 			cb_ereport_post(dip, fme_ena, cb_err_p);
16010Sstevel@tonic-gate 			fatal += safari_err_tbl[i].cb_fatal;
16020Sstevel@tonic-gate 		}
16030Sstevel@tonic-gate 	}
16040Sstevel@tonic-gate 
16050Sstevel@tonic-gate 	if (fatal)
16060Sstevel@tonic-gate 		return (DDI_FM_FATAL);
16070Sstevel@tonic-gate 	return (DDI_FM_OK);
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate }
16100Sstevel@tonic-gate 
16110Sstevel@tonic-gate /*
16120Sstevel@tonic-gate  * Check pbm va log register for captured errant address, and fail handle
16130Sstevel@tonic-gate  * if in per device cache.
16140Sstevel@tonic-gate  * Called from jbus_err_handler.
16150Sstevel@tonic-gate  */
16160Sstevel@tonic-gate static int
16170Sstevel@tonic-gate jbus_check_va_log(cb_t *cb_p, uint64_t fme_ena,
16180Sstevel@tonic-gate     cb_errstate_t *cb_err_p)
16190Sstevel@tonic-gate {
16200Sstevel@tonic-gate 	int i;
16210Sstevel@tonic-gate 	int ret = DDI_FM_FATAL;
16220Sstevel@tonic-gate 	pci_common_t *cmn_p = cb_p->cb_pci_cmn_p;
16230Sstevel@tonic-gate 
16240Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cmn_p->pci_fm_mutex));
16250Sstevel@tonic-gate 	/*
16260Sstevel@tonic-gate 	 * Check VA log register for address associated with error,
16270Sstevel@tonic-gate 	 * if no address is registered then return failure
16280Sstevel@tonic-gate 	 */
16290Sstevel@tonic-gate 	for (i = 0; i < 2; i++) {
16300Sstevel@tonic-gate 
16310Sstevel@tonic-gate 		if (cb_p->cb_pci_cmn_p->pci_p[i] == NULL)
16320Sstevel@tonic-gate 			continue;
16330Sstevel@tonic-gate 		/*
16340Sstevel@tonic-gate 		 * Look up and fault handle associated with
16350Sstevel@tonic-gate 		 * logged DMA address
16360Sstevel@tonic-gate 		 */
16370Sstevel@tonic-gate 		if (cb_err_p->cb_pbm[i].pbm_va_log) {
16381865Sdilpreet 			void *addr = (void *)&cb_err_p->cb_pbm[i].pbm_va_log;
16391865Sdilpreet 			ret = ndi_fmc_error(cb_p->cb_pci_cmn_p->pci_p[i]->
16401865Sdilpreet 					pci_dip, NULL, DMA_HANDLE, fme_ena,
16411865Sdilpreet 					(void *)addr);
16420Sstevel@tonic-gate 			if (ret == DDI_FM_NONFATAL)
16430Sstevel@tonic-gate 				break;
16440Sstevel@tonic-gate 		}
16450Sstevel@tonic-gate 	}
16460Sstevel@tonic-gate 	return (ret);
16470Sstevel@tonic-gate }
16480Sstevel@tonic-gate 
16490Sstevel@tonic-gate static cb_fm_err_t jbus_err_tbl[] = {
16500Sstevel@tonic-gate 	JBUS_APERR,		SCHIZO_CB_ELOG_ADDR_PAR_ERR,	CB_FATAL,
16510Sstevel@tonic-gate 	JBUS_PWR_DATA_PERR,	TOMATILLO_CB_ELOG_WR_DATA_PAR_ERR, CB_FATAL,
16520Sstevel@tonic-gate 	JBUS_DRD_DATA_PERR,	TOMATILLO_CB_ELOG_RD_DATA_PAR_ERR, CB_NONFATAL,
16530Sstevel@tonic-gate 	JBUS_CTL_PERR,		TOMATILLO_CB_ELOG_CTL_PAR_ERR,	CB_FATAL,
16540Sstevel@tonic-gate 	JBUS_ILL_BYTE_EN,	TOMATILLO_CB_ELOG_ILL_BYTE_EN,	CB_FATAL,
16550Sstevel@tonic-gate 	JBUS_ILL_COH_IN,	TOMATILLO_CB_ELOG_ILL_COH_IN,	CB_FATAL,
16560Sstevel@tonic-gate 	JBUS_SNOOP_ERR_RD,	TOMATILLO_CB_ELOG_SNOOP_ERR_RD,	CB_FATAL,
16570Sstevel@tonic-gate 	JBUS_SNOOP_ERR_RDS,	TOMATILLO_CB_ELOG_SNOOP_ERR_RDS, CB_FATAL,
16580Sstevel@tonic-gate 	JBUS_SNOOP_ERR_RDSA,	TOMATILLO_CB_ELOG_SNOOP_ERR_RDSA, CB_FATAL,
16590Sstevel@tonic-gate 	JBUS_SNOOP_ERR_OWN,	TOMATILLO_CB_ELOG_SNOOP_ERR_OWN, CB_FATAL,
16600Sstevel@tonic-gate 	JBUS_SNOOP_ERR_RDO,	TOMATILLO_CB_ELOG_SNOOP_ERR_RDO, CB_FATAL,
16610Sstevel@tonic-gate 	JBUS_SNOOP_ERR_PCI,	TOMATILLO_CB_ELOG_SNOOP_ERR_PCI, CB_FATAL,
16620Sstevel@tonic-gate 	JBUS_SNOOP_ERR_GR,	TOMATILLO_CB_ELOG_SNOOP_ERR_GR,	CB_FATAL,
16630Sstevel@tonic-gate 	JBUS_SNOOP_ERR,		TOMATILLO_CB_ELOG_SNOOP_ERR,	CB_FATAL,
16640Sstevel@tonic-gate 	JBUS_BAD_CMD,		SCHIZO_CB_ELOG_BAD_CMD,		CB_FATAL,
16650Sstevel@tonic-gate 	JBUS_UNMAP_ERR,		SCHIZO_CB_ELOG_UNMAP_ERR,	CB_NONFATAL,
16660Sstevel@tonic-gate 	JBUS_TO_EXP_ERR,	TOMATILLO_CB_ELOG_TO_EXP_ERR,	CB_NONFATAL,
16670Sstevel@tonic-gate 	JBUS_TO_ERR,		SCHIZO_CB_ELOG_TO_ERR,		CB_NONFATAL,
16680Sstevel@tonic-gate 	JBUS_BUS_ERR,		SCHIZO_CB_ELOG_BUS_ERR,		CB_NONFATAL,
16690Sstevel@tonic-gate 	NULL,			NULL,				NULL,
16700Sstevel@tonic-gate };
16710Sstevel@tonic-gate 
16720Sstevel@tonic-gate /*
16730Sstevel@tonic-gate  * Function used to handle and log Jbus errors.
16740Sstevel@tonic-gate  */
16750Sstevel@tonic-gate static int
16760Sstevel@tonic-gate jbus_err_handler(dev_info_t *dip, uint64_t fme_ena,
16770Sstevel@tonic-gate     cb_errstate_t *cb_err_p)
16780Sstevel@tonic-gate {
16790Sstevel@tonic-gate 	int	fatal = 0;
16800Sstevel@tonic-gate 	int	nonfatal = 0;
16810Sstevel@tonic-gate 	int	i;
16820Sstevel@tonic-gate 	pci_t	*pci_p = get_pci_soft_state(ddi_get_instance(dip));
16830Sstevel@tonic-gate 	cb_t	*cb_p = pci_p->pci_cb_p;
16840Sstevel@tonic-gate 
16850Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pci_p->pci_common_p->pci_fm_mutex));
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate 	for (i = 0; jbus_err_tbl[i].cb_err_class != NULL; i++) {
16880Sstevel@tonic-gate 		if (!(cb_err_p->cb_elog & jbus_err_tbl[i].cb_reg_bit))
16890Sstevel@tonic-gate 			continue;
16900Sstevel@tonic-gate 		cb_err_p->cb_err_class = jbus_err_tbl[i].cb_err_class;
16910Sstevel@tonic-gate 		if (jbus_err_tbl[i].cb_fatal) {
16920Sstevel@tonic-gate 			fatal += jbus_err_tbl[i].cb_fatal;
16930Sstevel@tonic-gate 			continue;
16940Sstevel@tonic-gate 		}
16950Sstevel@tonic-gate 		if (jbus_check_va_log(cb_p, fme_ena, cb_err_p)
16960Sstevel@tonic-gate 				!= DDI_FM_NONFATAL) {
16970Sstevel@tonic-gate 			fatal++;
16980Sstevel@tonic-gate 		}
16990Sstevel@tonic-gate 		cb_ereport_post(dip, fme_ena, cb_err_p);
17000Sstevel@tonic-gate 	}
17010Sstevel@tonic-gate 
17020Sstevel@tonic-gate 	return (fatal ? DDI_FM_FATAL : (nonfatal ? DDI_FM_NONFATAL :
17030Sstevel@tonic-gate 				DDI_FM_OK));
17040Sstevel@tonic-gate }
17050Sstevel@tonic-gate 
17060Sstevel@tonic-gate /*
17070Sstevel@tonic-gate  * Control Block error interrupt handler.
17080Sstevel@tonic-gate  */
17090Sstevel@tonic-gate uint_t
17100Sstevel@tonic-gate cb_buserr_intr(caddr_t a)
17110Sstevel@tonic-gate {
17120Sstevel@tonic-gate 	cb_t *cb_p = (cb_t *)a;
17130Sstevel@tonic-gate 	pci_common_t *cmn_p = cb_p->cb_pci_cmn_p;
17140Sstevel@tonic-gate 	pci_t *pci_p = cmn_p->pci_p[0];
17150Sstevel@tonic-gate 	cb_errstate_t cb_err;
17160Sstevel@tonic-gate 	ddi_fm_error_t derr;
17170Sstevel@tonic-gate 	int ret = DDI_FM_FATAL;
17180Sstevel@tonic-gate 	int i;
17190Sstevel@tonic-gate 
17200Sstevel@tonic-gate 	if (pci_p == NULL)
17210Sstevel@tonic-gate 		pci_p = cmn_p->pci_p[1];
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate 	bzero(&derr, sizeof (ddi_fm_error_t));
17240Sstevel@tonic-gate 	derr.fme_version = DDI_FME_VERSION;
17250Sstevel@tonic-gate 	derr.fme_ena = fm_ena_generate(0, FM_ENA_FMT1);
17260Sstevel@tonic-gate 
17270Sstevel@tonic-gate 	mutex_enter(&cmn_p->pci_fm_mutex);
17280Sstevel@tonic-gate 
17290Sstevel@tonic-gate 	pci_cb_errstate_get(cb_p, &cb_err);
17300Sstevel@tonic-gate 
17310Sstevel@tonic-gate 	if (CB_CHIP_TYPE(cb_p) == PCI_CHIP_TOMATILLO)
17320Sstevel@tonic-gate 		ret = jbus_err_handler(pci_p->pci_dip, derr.fme_ena, &cb_err);
17330Sstevel@tonic-gate 	else if ((CB_CHIP_TYPE(cb_p) == PCI_CHIP_SCHIZO) ||
17340Sstevel@tonic-gate 			(CB_CHIP_TYPE(cb_p) == PCI_CHIP_XMITS))
17350Sstevel@tonic-gate 		ret = safari_err_handler(pci_p->pci_dip, derr.fme_ena,
17360Sstevel@tonic-gate 		    &cb_err);
17370Sstevel@tonic-gate 
17380Sstevel@tonic-gate 	/*
17390Sstevel@tonic-gate 	 * Check for related errors in PBM and IOMMU. The IOMMU could cause
17400Sstevel@tonic-gate 	 * a timeout on the jbus due to an IOMMU miss, so we need to check and
17410Sstevel@tonic-gate 	 * log the IOMMU error registers.
17420Sstevel@tonic-gate 	 */
17430Sstevel@tonic-gate 	for (i = 0; i < 2; i++) {
17440Sstevel@tonic-gate 		if (cmn_p->pci_p[i] == NULL)
17450Sstevel@tonic-gate 			continue;
17460Sstevel@tonic-gate 		if (pci_pbm_err_handler(cmn_p->pci_p[i]->pci_dip, &derr,
17470Sstevel@tonic-gate 		    (void *)cmn_p->pci_p[i], PCI_CB_CALL) == DDI_FM_FATAL)
17480Sstevel@tonic-gate 			ret = DDI_FM_FATAL;
17490Sstevel@tonic-gate 	}
17500Sstevel@tonic-gate 
17510Sstevel@tonic-gate 	/* Cleanup and reset error bits */
17520Sstevel@tonic-gate 	(void) pci_cb_clear_error(cb_p, &cb_err);
17530Sstevel@tonic-gate 	mutex_exit(&cmn_p->pci_fm_mutex);
17540Sstevel@tonic-gate 
17550Sstevel@tonic-gate 	if (ret == DDI_FM_FATAL) {
17560Sstevel@tonic-gate 		fm_panic("Fatal System Bus Error has occurred\n");
17570Sstevel@tonic-gate 	}
17580Sstevel@tonic-gate 
17590Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
17600Sstevel@tonic-gate }
17610Sstevel@tonic-gate 
17620Sstevel@tonic-gate static ecc_fm_err_t ecc_err_tbl[] = {
17630Sstevel@tonic-gate 	PCI_ECC_PIO_UE, COMMON_ECC_UE_AFSR_E_PIO, CBNINTR_UE,
17640Sstevel@tonic-gate 	PBM_PRIMARY, SCHIZO_ECC_AFAR_PIOW_UPA64S, SCH_REG_UPA,
17650Sstevel@tonic-gate 	ACC_HANDLE,
17660Sstevel@tonic-gate 
17670Sstevel@tonic-gate 	PCI_ECC_PIO_UE, COMMON_ECC_UE_AFSR_E_PIO, CBNINTR_UE,
17680Sstevel@tonic-gate 	PBM_PRIMARY, SCHIZO_ECC_AFAR_PIOW_PCIA_REG, SCH_REG_PCIA_REG,
17690Sstevel@tonic-gate 	ACC_HANDLE,
17700Sstevel@tonic-gate 
17710Sstevel@tonic-gate 	PCI_ECC_PIO_UE, COMMON_ECC_UE_AFSR_E_PIO, CBNINTR_UE,
17720Sstevel@tonic-gate 	PBM_PRIMARY, SCHIZO_ECC_AFAR_PIOW_PCIA_MEM, SCH_REG_PCIA_MEM,
17730Sstevel@tonic-gate 	ACC_HANDLE,
17740Sstevel@tonic-gate 
17750Sstevel@tonic-gate 	PCI_ECC_PIO_UE, COMMON_ECC_UE_AFSR_E_PIO, CBNINTR_UE,
17760Sstevel@tonic-gate 	PBM_PRIMARY, SCHIZO_ECC_AFAR_PIOW_PCIA_CFGIO, SCH_REG_PCIA_CFGIO,
17770Sstevel@tonic-gate 	ACC_HANDLE,
17780Sstevel@tonic-gate 
17790Sstevel@tonic-gate 	PCI_ECC_PIO_UE, COMMON_ECC_UE_AFSR_E_PIO, CBNINTR_UE,
17800Sstevel@tonic-gate 	PBM_PRIMARY, SCHIZO_ECC_AFAR_PIOW_PCIB_REG, SCH_REG_PCIB_REG,
17810Sstevel@tonic-gate 	ACC_HANDLE,
17820Sstevel@tonic-gate 
17830Sstevel@tonic-gate 	PCI_ECC_PIO_UE, COMMON_ECC_UE_AFSR_E_PIO, CBNINTR_UE,
17840Sstevel@tonic-gate 	PBM_PRIMARY, SCHIZO_ECC_AFAR_PIOW_PCIB_MEM, SCH_REG_PCIB_MEM,
17850Sstevel@tonic-gate 	ACC_HANDLE,
17860Sstevel@tonic-gate 
17870Sstevel@tonic-gate 	PCI_ECC_PIO_UE, COMMON_ECC_UE_AFSR_E_PIO, CBNINTR_UE,
17880Sstevel@tonic-gate 	PBM_PRIMARY, SCHIZO_ECC_AFAR_PIOW_PCIB_CFGIO, SCH_REG_PCIB_CFGIO,
17890Sstevel@tonic-gate 	ACC_HANDLE,
17900Sstevel@tonic-gate 
17910Sstevel@tonic-gate 	PCI_ECC_PIO_UE, COMMON_ECC_UE_AFSR_E_PIO, CBNINTR_UE,
17920Sstevel@tonic-gate 	PBM_PRIMARY, SCHIZO_ECC_AFAR_PIOW_SAFARI_REGS, SCH_REG_SAFARI_REGS,
17930Sstevel@tonic-gate 	ACC_HANDLE,
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate 	PCI_ECC_SEC_PIO_UE, COMMON_ECC_UE_AFSR_E_PIO,  CBNINTR_UE,
17960Sstevel@tonic-gate 	PBM_SECONDARY, NULL, NULL, ACC_HANDLE,
17970Sstevel@tonic-gate 
17980Sstevel@tonic-gate 	PCI_ECC_PIO_CE, COMMON_ECC_UE_AFSR_E_PIO,  CBNINTR_CE,
17990Sstevel@tonic-gate 	PBM_PRIMARY, NULL, NULL, ACC_HANDLE,
18000Sstevel@tonic-gate 
18010Sstevel@tonic-gate 	PCI_ECC_SEC_PIO_CE, COMMON_ECC_UE_AFSR_E_PIO,  CBNINTR_CE,
18020Sstevel@tonic-gate 	PBM_SECONDARY, NULL, NULL, ACC_HANDLE,
18030Sstevel@tonic-gate 
18040Sstevel@tonic-gate 	PCI_ECC_DRD_UE, COMMON_ECC_UE_AFSR_E_DRD, CBNINTR_UE,
18050Sstevel@tonic-gate 	PBM_PRIMARY, NULL, NULL, DMA_HANDLE,
18060Sstevel@tonic-gate 
18070Sstevel@tonic-gate 	PCI_ECC_SEC_DRD_UE, COMMON_ECC_UE_AFSR_E_DRD, CBNINTR_UE,
18080Sstevel@tonic-gate 	PBM_SECONDARY, NULL, NULL, DMA_HANDLE,
18090Sstevel@tonic-gate 
18100Sstevel@tonic-gate 	PCI_ECC_DRD_CE, COMMON_ECC_UE_AFSR_E_DRD, CBNINTR_CE,
18110Sstevel@tonic-gate 	PBM_PRIMARY, NULL, NULL, DMA_HANDLE,
18120Sstevel@tonic-gate 
18130Sstevel@tonic-gate 	PCI_ECC_SEC_DRD_CE, COMMON_ECC_UE_AFSR_E_DRD, CBNINTR_CE,
18140Sstevel@tonic-gate 	PBM_SECONDARY, NULL, NULL, DMA_HANDLE,
18150Sstevel@tonic-gate 
18160Sstevel@tonic-gate 	PCI_ECC_DWR_UE, COMMON_ECC_UE_AFSR_E_DWR, CBNINTR_UE,
18170Sstevel@tonic-gate 	PBM_PRIMARY, NULL, NULL, DMA_HANDLE,
18180Sstevel@tonic-gate 
18190Sstevel@tonic-gate 	PCI_ECC_SEC_DWR_UE, COMMON_ECC_UE_AFSR_E_DWR, CBNINTR_UE,
18200Sstevel@tonic-gate 	PBM_SECONDARY, NULL, NULL, DMA_HANDLE,
18210Sstevel@tonic-gate 
18220Sstevel@tonic-gate 	PCI_ECC_DWR_CE, COMMON_ECC_UE_AFSR_E_DWR, CBNINTR_CE,
18230Sstevel@tonic-gate 	PBM_PRIMARY, NULL, NULL, DMA_HANDLE,
18240Sstevel@tonic-gate 
18250Sstevel@tonic-gate 	PCI_ECC_SEC_DWR_CE, COMMON_ECC_UE_AFSR_E_DWR, CBNINTR_CE,
18260Sstevel@tonic-gate 	PBM_SECONDARY, NULL, NULL, DMA_HANDLE,
18270Sstevel@tonic-gate 
18280Sstevel@tonic-gate 	NULL, NULL, NULL, NULL, NULL, NULL,
18290Sstevel@tonic-gate };
18300Sstevel@tonic-gate 
18310Sstevel@tonic-gate /*
18320Sstevel@tonic-gate  * pci_ecc_classify, called by ecc_handler to classify ecc errors
18330Sstevel@tonic-gate  * and determine if we should panic or not.
18340Sstevel@tonic-gate  */
18350Sstevel@tonic-gate void
18360Sstevel@tonic-gate pci_ecc_classify(uint64_t err, ecc_errstate_t *ecc_err_p)
18370Sstevel@tonic-gate {
18380Sstevel@tonic-gate 	struct async_flt *ecc_p = &ecc_err_p->ecc_aflt;
18390Sstevel@tonic-gate 	uint64_t region, afar = ecc_p->flt_addr;
18400Sstevel@tonic-gate 	int i, j, ret = 0;
18410Sstevel@tonic-gate 	int flag, fatal = 0;
18420Sstevel@tonic-gate 	pci_common_t *cmn_p = ecc_err_p->ecc_ii_p.ecc_p->ecc_pci_cmn_p;
18430Sstevel@tonic-gate 	pci_t *pci_p = cmn_p->pci_p[0];
18440Sstevel@tonic-gate 
18450Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cmn_p->pci_fm_mutex));
18460Sstevel@tonic-gate 
18470Sstevel@tonic-gate 	ecc_err_p->ecc_bridge_type = PCI_BRIDGE_TYPE(cmn_p);
18480Sstevel@tonic-gate 
18490Sstevel@tonic-gate 	if (pci_p == NULL)
18500Sstevel@tonic-gate 		pci_p = cmn_p->pci_p[1];
18510Sstevel@tonic-gate 
18520Sstevel@tonic-gate 	ecc_err_p->ecc_ctrl = lddphysio(ecc_err_p->ecc_ii_p.ecc_p->ecc_csr_pa);
18530Sstevel@tonic-gate 	ecc_err_p->ecc_err_addr = afar;
18540Sstevel@tonic-gate 	region = afar & SCHIZO_ECC_AFAR_PIOW_MASK;
18550Sstevel@tonic-gate 
18560Sstevel@tonic-gate 	for (i = 0; ecc_err_tbl[i].ecc_err_class != NULL; i++) {
18570Sstevel@tonic-gate 		if (!(err & ecc_err_tbl[i].ecc_reg_bit) ||
18580Sstevel@tonic-gate 			(ecc_err_p->ecc_ii_p.ecc_type !=
18590Sstevel@tonic-gate 			    ecc_err_tbl[i].ecc_type) ||
18600Sstevel@tonic-gate 			(ecc_err_p->ecc_pri != ecc_err_tbl[i].ecc_pri))
18610Sstevel@tonic-gate 			continue;
18620Sstevel@tonic-gate 
18630Sstevel@tonic-gate 		ecc_p->flt_erpt_class = ecc_err_tbl[i].ecc_err_class;
18640Sstevel@tonic-gate 		flag = ecc_err_tbl[i].ecc_flag;
18650Sstevel@tonic-gate 
18660Sstevel@tonic-gate 		if (!ecc_err_tbl[i].ecc_pri ||
18670Sstevel@tonic-gate 				(ecc_err_tbl[i].ecc_type == CBNINTR_CE)) {
18680Sstevel@tonic-gate 			fatal += (ecc_err_tbl[i].ecc_type == CBNINTR_UE) ?
18690Sstevel@tonic-gate 				1 : 0;
18700Sstevel@tonic-gate 			break;
18710Sstevel@tonic-gate 		}
18720Sstevel@tonic-gate 
18730Sstevel@tonic-gate 		if (flag == ACC_HANDLE &&
18740Sstevel@tonic-gate 			(region & ecc_err_tbl[i].ecc_region_bits)) {
18750Sstevel@tonic-gate 			ecc_err_p->ecc_region = ecc_err_tbl[i].ecc_region;
18760Sstevel@tonic-gate 			pci_format_ecc_addr(pci_p->pci_dip,
18770Sstevel@tonic-gate 					&ecc_err_p->ecc_err_addr,
18780Sstevel@tonic-gate 					ecc_err_p->ecc_region);
18790Sstevel@tonic-gate 		}
18800Sstevel@tonic-gate 
18810Sstevel@tonic-gate 		/*
18820Sstevel@tonic-gate 		 * Lookup and fault errant handle
18830Sstevel@tonic-gate 		 */
18840Sstevel@tonic-gate 		for (j = 0; j < 2; ++j) {
18850Sstevel@tonic-gate 			ret = DDI_FM_UNKNOWN;
18860Sstevel@tonic-gate 			if (cmn_p->pci_p[j] == NULL)
18870Sstevel@tonic-gate 				continue;
18881865Sdilpreet 			ret = ndi_fmc_error(cmn_p->pci_p[j]->pci_dip, NULL,
18890Sstevel@tonic-gate 					flag, ecc_err_p->ecc_ena,
18900Sstevel@tonic-gate 					(void *)&ecc_err_p->ecc_err_addr);
18910Sstevel@tonic-gate 			if (ret == DDI_FM_NONFATAL) {
18920Sstevel@tonic-gate 				fatal = 0;
18930Sstevel@tonic-gate 				break;
18940Sstevel@tonic-gate 			} else
18950Sstevel@tonic-gate 				fatal++;
18960Sstevel@tonic-gate 		}
18970Sstevel@tonic-gate 		break;
18980Sstevel@tonic-gate 	}
18990Sstevel@tonic-gate 
19000Sstevel@tonic-gate 	if (fatal)
19010Sstevel@tonic-gate 		ecc_p->flt_panic = 1;
19020Sstevel@tonic-gate 	else if (flag != ACC_HANDLE)
19030Sstevel@tonic-gate 		ecc_err_p->ecc_pg_ret = 1;
19040Sstevel@tonic-gate }
19050Sstevel@tonic-gate 
19060Sstevel@tonic-gate /*
19070Sstevel@tonic-gate  * Tables to define PCI-X Split Completion errors
19080Sstevel@tonic-gate  */
19090Sstevel@tonic-gate 
19100Sstevel@tonic-gate pcix_err_msg_rec_t pcix_completer_errs[] = {
19110Sstevel@tonic-gate 	{PCIX_CPLT_OUT_OF_RANGE,	"pcix", "oor"	},
19120Sstevel@tonic-gate };
19130Sstevel@tonic-gate 
19140Sstevel@tonic-gate pcix_err_tbl_t pcix_split_errs_tbl[] = {
19150Sstevel@tonic-gate 	{PCIX_CLASS_CPLT,
19160Sstevel@tonic-gate 		sizeof (pcix_completer_errs)/sizeof (pcix_err_msg_rec_t),
19170Sstevel@tonic-gate 		pcix_completer_errs		},
19180Sstevel@tonic-gate };
19190Sstevel@tonic-gate 
19200Sstevel@tonic-gate /*
19210Sstevel@tonic-gate  * Tables for the PCI-X error status messages
19220Sstevel@tonic-gate  */
19230Sstevel@tonic-gate pcix_err_msg_rec_t pcix_stat_errs[] = {
19240Sstevel@tonic-gate 	{XMITS_PCIX_STAT_SC_DSCRD,	"pcix", "discard"  	},
19250Sstevel@tonic-gate 	{XMITS_PCIX_STAT_SC_TTO,	"xmits.pbmx", "tato" 	},
19260Sstevel@tonic-gate 	{XMITS_PCIX_STAT_SMMU,		"xmits.pbmx", "stmmu"	},
19270Sstevel@tonic-gate 	{XMITS_PCIX_STAT_SDSTAT,	"xmits.pbmx", "stdst"	},
19280Sstevel@tonic-gate 	{XMITS_PCIX_STAT_CMMU,		"xmits.pbmx", "cnmmu"	},
19290Sstevel@tonic-gate 	{XMITS_PCIX_STAT_CDSTAT,	"xmits.pbmx", "cndst"	}
19300Sstevel@tonic-gate };
19310Sstevel@tonic-gate 
19320Sstevel@tonic-gate pcix_err_tbl_t pcix_stat_errs_tbl =
19330Sstevel@tonic-gate 	{PCIX_NO_CLASS,
19340Sstevel@tonic-gate 		sizeof (pcix_stat_errs)/sizeof (pcix_err_msg_rec_t),
19350Sstevel@tonic-gate 		pcix_stat_errs		};
19360Sstevel@tonic-gate 
19370Sstevel@tonic-gate 
19380Sstevel@tonic-gate /*
19390Sstevel@tonic-gate  * walk thru a table of error messages, printing as appropriate
19400Sstevel@tonic-gate  *
19410Sstevel@tonic-gate  * t - the table of messages to parse
19420Sstevel@tonic-gate  * err - the error to match against
19430Sstevel@tonic-gate  * multi - flag, sometimes multiple error bits may be set/desired
19440Sstevel@tonic-gate  */
19450Sstevel@tonic-gate static int
19460Sstevel@tonic-gate pcix_lookup_err_msgs(dev_info_t *dip, uint64_t ena, pcix_err_tbl_t t,
19470Sstevel@tonic-gate 		pbm_errstate_t *pbm_err_p)
19480Sstevel@tonic-gate {
19490Sstevel@tonic-gate 	uint32_t err_bits  = pbm_err_p->pbm_err & XMITS_PCIX_MSG_INDEX_MASK;
19500Sstevel@tonic-gate 	int nerr = 0;
19510Sstevel@tonic-gate 	int j;
19520Sstevel@tonic-gate 	char buf[FM_MAX_CLASS];
19530Sstevel@tonic-gate 
19540Sstevel@tonic-gate 	for (j = 0; j < t.err_rec_num; j++)  {
19550Sstevel@tonic-gate 		uint32_t msg_key = t.err_msg_tbl[j].msg_key;
19560Sstevel@tonic-gate 		if (pbm_err_p->pbm_multi ? !(err_bits & msg_key) : err_bits
19570Sstevel@tonic-gate 				!= msg_key)
19580Sstevel@tonic-gate 			continue;
19590Sstevel@tonic-gate 
19600Sstevel@tonic-gate 		(void) snprintf(buf, FM_MAX_CLASS, "%s.%s%s",
19610Sstevel@tonic-gate 		    t.err_msg_tbl[j].msg_class,
19620Sstevel@tonic-gate 		    pbm_err_p->pbm_pri ? "" : PCIX_SECONDARY,
19630Sstevel@tonic-gate 		    t.err_msg_tbl[j].msg_str);
19640Sstevel@tonic-gate 
19650Sstevel@tonic-gate 		pbm_err_p->pbm_err_class = buf;
19660Sstevel@tonic-gate 		pcix_ereport_post(dip, ena, pbm_err_p);
19670Sstevel@tonic-gate 		nerr++;
19680Sstevel@tonic-gate 	}
19690Sstevel@tonic-gate 	return (nerr ? DDI_FM_FATAL : DDI_FM_OK);
19700Sstevel@tonic-gate }
19710Sstevel@tonic-gate 
19720Sstevel@tonic-gate /*
19730Sstevel@tonic-gate  * Decodes primary(bit 27-24) or secondary(bit 15-12) PCI-X split
19740Sstevel@tonic-gate  * completion error message class and index in PBM AFSR.
19750Sstevel@tonic-gate  */
19760Sstevel@tonic-gate static void
19770Sstevel@tonic-gate pcix_log_split_err(dev_info_t *dip, uint64_t ena, pbm_errstate_t *pbm_err_p)
19780Sstevel@tonic-gate {
19790Sstevel@tonic-gate 	uint32_t class  = pbm_err_p->pbm_err & XMITS_PCIX_MSG_CLASS_MASK;
19800Sstevel@tonic-gate 	uint32_t num_classes = sizeof (pcix_split_errs_tbl) /
19810Sstevel@tonic-gate 	    sizeof (struct pcix_err_tbl);
19820Sstevel@tonic-gate 	int i;
19830Sstevel@tonic-gate 
19840Sstevel@tonic-gate 	for (i = 0; i < num_classes; i++) {
19850Sstevel@tonic-gate 		if (class == pcix_split_errs_tbl[i].err_class) {
19860Sstevel@tonic-gate 			pbm_err_p->pbm_multi = PCIX_SINGLE_ERR;
19870Sstevel@tonic-gate 			(void) pcix_lookup_err_msgs(dip, ena,
19880Sstevel@tonic-gate 			    pcix_split_errs_tbl[i], pbm_err_p);
19890Sstevel@tonic-gate 			break;
19900Sstevel@tonic-gate 		}
19910Sstevel@tonic-gate 	}
19920Sstevel@tonic-gate }
19930Sstevel@tonic-gate 
19940Sstevel@tonic-gate /*
19950Sstevel@tonic-gate  * Report PBM PCI-X Error Status Register if in PCI-X mode
19960Sstevel@tonic-gate  *
19970Sstevel@tonic-gate  * Once a PCI-X fault tree is constructed, the code below may need to
19980Sstevel@tonic-gate  * change.
19990Sstevel@tonic-gate  */
20000Sstevel@tonic-gate static int
20010Sstevel@tonic-gate pcix_log_pbm(pci_t *pci_p, uint64_t ena, pbm_errstate_t *pbm_err_p)
20020Sstevel@tonic-gate {
20030Sstevel@tonic-gate 	int fatal = 0;
20040Sstevel@tonic-gate 	int nonfatal = 0;
20050Sstevel@tonic-gate 	uint32_t e;
20060Sstevel@tonic-gate 
20070Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pci_p->pci_common_p->pci_fm_mutex));
20080Sstevel@tonic-gate 
20090Sstevel@tonic-gate 	DEBUG3(DBG_ERR_INTR, pci_p->pci_dip, "pcix_log_pbm: chip_type=%d "
20100Sstevel@tonic-gate 	    "ctr_stat=%lx afsr = 0x%lx", CHIP_TYPE(pci_p),
20110Sstevel@tonic-gate 	    pbm_err_p->pbm_ctl_stat, pbm_err_p->pbm_afsr);
20120Sstevel@tonic-gate 
20130Sstevel@tonic-gate 	if (!(CHIP_TYPE(pci_p) == PCI_CHIP_XMITS) ||
20140Sstevel@tonic-gate 	    !(pbm_err_p->pbm_ctl_stat & XMITS_PCI_CTRL_X_MODE))
20150Sstevel@tonic-gate 		return (DDI_FM_OK);
20160Sstevel@tonic-gate 
20170Sstevel@tonic-gate 	if (pbm_err_p->pbm_afsr & XMITS_PCI_X_AFSR_P_SC_ERR) {
20180Sstevel@tonic-gate 		pbm_err_p->pbm_err = PBM_AFSR_TO_PRISPLIT(pbm_err_p->pbm_afsr);
20190Sstevel@tonic-gate 		pbm_err_p->pbm_pri = PBM_PRIMARY;
20200Sstevel@tonic-gate 		pcix_log_split_err(pci_p->pci_dip, ena, pbm_err_p);
20210Sstevel@tonic-gate 		nonfatal++;
20220Sstevel@tonic-gate 	}
20230Sstevel@tonic-gate 	if (pbm_err_p->pbm_afsr & XMITS_PCI_X_AFSR_S_SC_ERR) {
20240Sstevel@tonic-gate 		pbm_err_p->pbm_err = PBM_AFSR_TO_PRISPLIT(pbm_err_p->pbm_afsr);
20250Sstevel@tonic-gate 		pbm_err_p->pbm_pri = PBM_PRIMARY;
20260Sstevel@tonic-gate 		pcix_log_split_err(pci_p->pci_dip, ena, pbm_err_p);
20270Sstevel@tonic-gate 		nonfatal++;
20280Sstevel@tonic-gate 	}
20290Sstevel@tonic-gate 
20300Sstevel@tonic-gate 	e = PBM_PCIX_TO_PRIERR(pbm_err_p->pbm_pcix_stat);
20310Sstevel@tonic-gate 	if (e) {
20320Sstevel@tonic-gate 		pbm_err_p->pbm_pri = PBM_PRIMARY;
20330Sstevel@tonic-gate 		pbm_err_p->pbm_err = e;
20340Sstevel@tonic-gate 		pbm_err_p->pbm_multi = PCIX_MULTI_ERR;
20350Sstevel@tonic-gate 		if (pcix_lookup_err_msgs(pci_p->pci_dip, ena,
20360Sstevel@tonic-gate 		    pcix_stat_errs_tbl, pbm_err_p) == DDI_FM_FATAL)
20370Sstevel@tonic-gate 			fatal++;
20380Sstevel@tonic-gate 		else
20390Sstevel@tonic-gate 			nonfatal++;
20400Sstevel@tonic-gate 	}
20410Sstevel@tonic-gate 
20420Sstevel@tonic-gate 	e = PBM_PCIX_TO_SECERR(pbm_err_p->pbm_pcix_stat);
20430Sstevel@tonic-gate 	if (e) {
20440Sstevel@tonic-gate 		pbm_err_p->pbm_pri = PBM_SECONDARY;
20450Sstevel@tonic-gate 		pbm_err_p->pbm_err = e;
20460Sstevel@tonic-gate 		pbm_err_p->pbm_multi = PCIX_MULTI_ERR;
20470Sstevel@tonic-gate 		if (pcix_lookup_err_msgs(pci_p->pci_dip, ena,
20480Sstevel@tonic-gate 		    pcix_stat_errs_tbl, pbm_err_p) == DDI_FM_FATAL)
20490Sstevel@tonic-gate 			fatal++;
20500Sstevel@tonic-gate 		else
20510Sstevel@tonic-gate 			nonfatal++;
20520Sstevel@tonic-gate 	}
20530Sstevel@tonic-gate 
20540Sstevel@tonic-gate 	if (!fatal && !nonfatal)
20550Sstevel@tonic-gate 		return (DDI_FM_OK);
20560Sstevel@tonic-gate 	else if (fatal)
20570Sstevel@tonic-gate 		return (DDI_FM_FATAL);
20580Sstevel@tonic-gate 	return (DDI_FM_NONFATAL);
20590Sstevel@tonic-gate }
20600Sstevel@tonic-gate 
20610Sstevel@tonic-gate static pbm_fm_err_t pbm_err_tbl[] = {
20620Sstevel@tonic-gate 	PCI_MA,			SCHIZO_PCI_AFSR_E_MA,	PBM_PRIMARY,
20630Sstevel@tonic-gate 	FM_LOG_PCI,	PCI_TARG_MA,
20640Sstevel@tonic-gate 
20650Sstevel@tonic-gate 	PCI_SEC_MA,		SCHIZO_PCI_AFSR_E_MA,	PBM_SECONDARY,
20660Sstevel@tonic-gate 	FM_LOG_PBM,	NULL,
20670Sstevel@tonic-gate 
20680Sstevel@tonic-gate 	PCI_REC_TA,		SCHIZO_PCI_AFSR_E_TA,	PBM_PRIMARY,
20690Sstevel@tonic-gate 	FM_LOG_PCI,	PCI_TARG_REC_TA,
20700Sstevel@tonic-gate 
20710Sstevel@tonic-gate 	PCI_SEC_REC_TA,		SCHIZO_PCI_AFSR_E_TA,	PBM_SECONDARY,
20720Sstevel@tonic-gate 	FM_LOG_PBM,	NULL,
20730Sstevel@tonic-gate 
20740Sstevel@tonic-gate 	PCI_PBM_RETRY,		SCHIZO_PCI_AFSR_E_RTRY,	PBM_PRIMARY,
20750Sstevel@tonic-gate 	FM_LOG_PBM,	PCI_PBM_TARG_RETRY,
20760Sstevel@tonic-gate 
20770Sstevel@tonic-gate 	PCI_SEC_PBM_RETRY,	SCHIZO_PCI_AFSR_E_RTRY,	PBM_SECONDARY,
20780Sstevel@tonic-gate 	FM_LOG_PBM,	NULL,
20790Sstevel@tonic-gate 
20800Sstevel@tonic-gate 	PCI_MDPE,		SCHIZO_PCI_AFSR_E_PERR,	PBM_PRIMARY,
20810Sstevel@tonic-gate 	FM_LOG_PCI,	PCI_TARG_MDPE,
20820Sstevel@tonic-gate 
20830Sstevel@tonic-gate 	PCI_SEC_MDPE,		SCHIZO_PCI_AFSR_E_PERR,	PBM_SECONDARY,
20840Sstevel@tonic-gate 	FM_LOG_PBM,	NULL,
20850Sstevel@tonic-gate 
20860Sstevel@tonic-gate 	PCI_PBM_TTO,		SCHIZO_PCI_AFSR_E_TTO,	PBM_PRIMARY,
20870Sstevel@tonic-gate 	FM_LOG_PBM,	PCI_PBM_TARG_TTO,
20880Sstevel@tonic-gate 
20890Sstevel@tonic-gate 	PCI_SEC_PBM_TTO,	SCHIZO_PCI_AFSR_E_TTO,	PBM_SECONDARY,
20900Sstevel@tonic-gate 	FM_LOG_PBM,	NULL,
20910Sstevel@tonic-gate 
20920Sstevel@tonic-gate 	PCI_SCH_BUS_UNUSABLE_ERR, SCHIZO_PCI_AFSR_E_UNUSABLE, PBM_PRIMARY,
20930Sstevel@tonic-gate 	FM_LOG_PBM,	NULL,
20940Sstevel@tonic-gate 
20950Sstevel@tonic-gate 	PCI_SEC_SCH_BUS_UNUSABLE_ERR, SCHIZO_PCI_AFSR_E_UNUSABLE, PBM_SECONDARY,
20960Sstevel@tonic-gate 	FM_LOG_PBM,	NULL,
20970Sstevel@tonic-gate 
20980Sstevel@tonic-gate 	NULL,			NULL,			NULL,
20990Sstevel@tonic-gate 	NULL,		NULL,
21000Sstevel@tonic-gate };
21010Sstevel@tonic-gate 
21020Sstevel@tonic-gate 
21030Sstevel@tonic-gate /*
21040Sstevel@tonic-gate  * pci_pbm_classify, called by pbm_afsr_report to classify piow afsr.
21050Sstevel@tonic-gate  */
21060Sstevel@tonic-gate int
21070Sstevel@tonic-gate pci_pbm_classify(pbm_errstate_t *pbm_err_p)
21080Sstevel@tonic-gate {
21090Sstevel@tonic-gate 	uint32_t err;
21100Sstevel@tonic-gate 	int nerr = 0;
21110Sstevel@tonic-gate 	int i;
21120Sstevel@tonic-gate 
21130Sstevel@tonic-gate 	err = pbm_err_p->pbm_pri ? PBM_AFSR_TO_PRIERR(pbm_err_p->pbm_afsr):
21140Sstevel@tonic-gate 		PBM_AFSR_TO_SECERR(pbm_err_p->pbm_afsr);
21150Sstevel@tonic-gate 
21160Sstevel@tonic-gate 	for (i = 0; pbm_err_tbl[i].pbm_err_class != NULL; i++) {
21170Sstevel@tonic-gate 		if ((err & pbm_err_tbl[i].pbm_reg_bit) &&
21180Sstevel@tonic-gate 		    (pbm_err_p->pbm_pri == pbm_err_tbl[i].pbm_pri)) {
21190Sstevel@tonic-gate 			if (pbm_err_tbl[i].pbm_flag == FM_LOG_PCI)
21200Sstevel@tonic-gate 				pbm_err_p->pbm_pci.pci_err_class =
21210Sstevel@tonic-gate 					pbm_err_tbl[i].pbm_err_class;
21220Sstevel@tonic-gate 			else
21230Sstevel@tonic-gate 				pbm_err_p->pbm_err_class =
21240Sstevel@tonic-gate 				    pbm_err_tbl[i].pbm_err_class;
21250Sstevel@tonic-gate 
21260Sstevel@tonic-gate 			pbm_err_p->pbm_terr_class =
21270Sstevel@tonic-gate 			    pbm_err_tbl[i].pbm_terr_class;
21280Sstevel@tonic-gate 			pbm_err_p->pbm_log = pbm_err_tbl[i].pbm_flag;
21290Sstevel@tonic-gate 			nerr++;
21300Sstevel@tonic-gate 			break;
21310Sstevel@tonic-gate 		}
21320Sstevel@tonic-gate 	}
21330Sstevel@tonic-gate 
21340Sstevel@tonic-gate 	return (nerr);
21350Sstevel@tonic-gate }
21360Sstevel@tonic-gate 
21370Sstevel@tonic-gate /*
21380Sstevel@tonic-gate  * Function used to handle and log IOMMU errors. Called by pci_pbm_err_handler,
21390Sstevel@tonic-gate  * with pci_fm_mutex held.
21400Sstevel@tonic-gate  */
21410Sstevel@tonic-gate static int
21420Sstevel@tonic-gate iommu_err_handler(dev_info_t *dip, uint64_t ena, pbm_errstate_t *pbm_err_p)
21430Sstevel@tonic-gate {
21440Sstevel@tonic-gate 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
21450Sstevel@tonic-gate 	iommu_t *iommu_p = pci_p->pci_iommu_p;
21460Sstevel@tonic-gate 	ecc_t *ecc_p = pci_p->pci_ecc_p;
21470Sstevel@tonic-gate 	uint64_t stat;
21480Sstevel@tonic-gate 	ushort_t ta_signalled;
21490Sstevel@tonic-gate 	int err = 0;
21500Sstevel@tonic-gate 	int fatal = 0;
21510Sstevel@tonic-gate 	int nonfatal = 0;
21520Sstevel@tonic-gate 	int ret;
21530Sstevel@tonic-gate 
21540Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ecc_p->ecc_pci_cmn_p->pci_fm_mutex));
21550Sstevel@tonic-gate 	if (!((stat = *iommu_p->iommu_ctrl_reg) & TOMATILLO_IOMMU_ERR)) {
21560Sstevel@tonic-gate 		pbm_err_p->pbm_err_class = PCI_SCH_MMU_ERR;
21570Sstevel@tonic-gate 		iommu_ereport_post(dip, ena, pbm_err_p);
21580Sstevel@tonic-gate 		return (DDI_FM_NONFATAL);
21590Sstevel@tonic-gate 	}
21600Sstevel@tonic-gate 
21610Sstevel@tonic-gate 	/*
21620Sstevel@tonic-gate 	 * Need to make sure a Target Abort was signalled to the device if
21630Sstevel@tonic-gate 	 * we have any hope of recovering. Tomatillo does not send a TA for
21640Sstevel@tonic-gate 	 * DMA Writes that result in a Translation Error, thus fooling the
21650Sstevel@tonic-gate 	 * device into believing everything is as it expects. Ignorance
21660Sstevel@tonic-gate 	 * is bliss, but knowledge is power.
21670Sstevel@tonic-gate 	 */
21680Sstevel@tonic-gate 	ta_signalled = pbm_err_p->pbm_pci.pci_cfg_stat &
21690Sstevel@tonic-gate 		PCI_STAT_S_TARG_AB;
21700Sstevel@tonic-gate 
21710Sstevel@tonic-gate 	if (stat & TOMATILLO_IOMMU_ERR_ILLTSBTBW) {
21720Sstevel@tonic-gate 		pbm_err_p->pbm_err_class = PCI_TOM_MMU_BAD_TSBTBW;
21730Sstevel@tonic-gate 		err = 1;
21740Sstevel@tonic-gate 		iommu_ereport_post(dip, ena, pbm_err_p);
21750Sstevel@tonic-gate 		if (!ta_signalled)
21760Sstevel@tonic-gate 			fatal++;
21770Sstevel@tonic-gate 		else
21780Sstevel@tonic-gate 			nonfatal++;
21790Sstevel@tonic-gate 	}
21800Sstevel@tonic-gate 
21810Sstevel@tonic-gate 	if (stat & TOMATILLO_IOMMU_ERR_BAD_VA) {
21820Sstevel@tonic-gate 		pbm_err_p->pbm_err_class = PCI_TOM_MMU_BAD_VA;
21830Sstevel@tonic-gate 		err = 1;
21840Sstevel@tonic-gate 		iommu_ereport_post(dip, ena, pbm_err_p);
21850Sstevel@tonic-gate 		if (!ta_signalled)
21860Sstevel@tonic-gate 			fatal++;
21870Sstevel@tonic-gate 		else
21880Sstevel@tonic-gate 			nonfatal++;
21890Sstevel@tonic-gate 	}
21900Sstevel@tonic-gate 
21910Sstevel@tonic-gate 	if (!err) {
21920Sstevel@tonic-gate 		stat = ((stat & TOMATILLO_IOMMU_ERRSTS) >>
21930Sstevel@tonic-gate 		    TOMATILLO_IOMMU_ERRSTS_SHIFT);
21940Sstevel@tonic-gate 		switch (stat) {
21950Sstevel@tonic-gate 		case TOMATILLO_IOMMU_PROTECTION_ERR:
21960Sstevel@tonic-gate 			pbm_err_p->pbm_err_class = PCI_TOM_MMU_PROT_ERR;
21970Sstevel@tonic-gate 			iommu_ereport_post(dip, ena, pbm_err_p);
21980Sstevel@tonic-gate 			fatal++;
21990Sstevel@tonic-gate 			break;
22000Sstevel@tonic-gate 		case TOMATILLO_IOMMU_INVALID_ERR:
22010Sstevel@tonic-gate 			pbm_err_p->pbm_err_class = PCI_TOM_MMU_INVAL_ERR;
22020Sstevel@tonic-gate 			/*
22030Sstevel@tonic-gate 			 * Fault the address in iommu_tfar
22040Sstevel@tonic-gate 			 * register to inform target driver of error
22050Sstevel@tonic-gate 			 */
22061865Sdilpreet 			ret = ndi_fmc_error(pci_p->pci_dip, NULL, DMA_HANDLE,
22070Sstevel@tonic-gate 				ena, (void *)&pbm_err_p->pbm_iommu.iommu_tfar);
22080Sstevel@tonic-gate 
22091865Sdilpreet 			if (ret != DDI_FM_NONFATAL)
22100Sstevel@tonic-gate 				if (ta_signalled)
22110Sstevel@tonic-gate 					nonfatal++;
22120Sstevel@tonic-gate 				else
22130Sstevel@tonic-gate 					fatal++;
22140Sstevel@tonic-gate 			else
22151865Sdilpreet 				nonfatal++;
22161865Sdilpreet 
22170Sstevel@tonic-gate 			iommu_ereport_post(dip, ena, pbm_err_p);
22180Sstevel@tonic-gate 			break;
22190Sstevel@tonic-gate 		case TOMATILLO_IOMMU_TIMEOUT_ERR:
22200Sstevel@tonic-gate 			pbm_err_p->pbm_err_class = PCI_TOM_MMU_TO_ERR;
22210Sstevel@tonic-gate 			fatal++;
22220Sstevel@tonic-gate 			iommu_ereport_post(dip, ena, pbm_err_p);
22230Sstevel@tonic-gate 			break;
22240Sstevel@tonic-gate 		case TOMATILLO_IOMMU_ECC_ERR:
22250Sstevel@tonic-gate 			pbm_err_p->pbm_err_class = PCI_TOM_MMU_UE;
22260Sstevel@tonic-gate 			iommu_ereport_post(dip, ena, pbm_err_p);
22270Sstevel@tonic-gate 			break;
22280Sstevel@tonic-gate 		}
22290Sstevel@tonic-gate 	}
22300Sstevel@tonic-gate 
22310Sstevel@tonic-gate 	if (fatal)
22320Sstevel@tonic-gate 		return (DDI_FM_FATAL);
22330Sstevel@tonic-gate 	else if (nonfatal)
22340Sstevel@tonic-gate 		return (DDI_FM_NONFATAL);
22350Sstevel@tonic-gate 
22360Sstevel@tonic-gate 	return (DDI_FM_OK);
22370Sstevel@tonic-gate }
22380Sstevel@tonic-gate 
22390Sstevel@tonic-gate int
22400Sstevel@tonic-gate pci_check_error(pci_t *pci_p)
22410Sstevel@tonic-gate {
22420Sstevel@tonic-gate 	pbm_t *pbm_p = pci_p->pci_pbm_p;
22430Sstevel@tonic-gate 	uint16_t pci_cfg_stat;
22440Sstevel@tonic-gate 	uint64_t pbm_ctl_stat, pbm_afsr, pbm_pcix_stat;
22450Sstevel@tonic-gate 	caddr_t a = pci_p->pci_address[0];
22460Sstevel@tonic-gate 	uint64_t *pbm_pcix_stat_reg;
22470Sstevel@tonic-gate 
22480Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pci_p->pci_common_p->pci_fm_mutex));
22490Sstevel@tonic-gate 
22500Sstevel@tonic-gate 	pci_cfg_stat = pbm_p->pbm_config_header->ch_status_reg;
22510Sstevel@tonic-gate 	pbm_ctl_stat = *pbm_p->pbm_ctrl_reg;
22520Sstevel@tonic-gate 	pbm_afsr = *pbm_p->pbm_async_flt_status_reg;
22530Sstevel@tonic-gate 
22540Sstevel@tonic-gate 	if ((pci_cfg_stat & (PCI_STAT_S_PERROR | PCI_STAT_S_TARG_AB |
22550Sstevel@tonic-gate 				PCI_STAT_R_TARG_AB | PCI_STAT_R_MAST_AB |
22560Sstevel@tonic-gate 				PCI_STAT_S_SYSERR | PCI_STAT_PERROR)) ||
22570Sstevel@tonic-gate 			(pbm_ctl_stat & (SCHIZO_PCI_CTRL_BUS_UNUSABLE |
22580Sstevel@tonic-gate 				TOMATILLO_PCI_CTRL_PCI_DTO_ERR |
22590Sstevel@tonic-gate 				SCHIZO_PCI_CTRL_PCI_TTO_ERR |
22600Sstevel@tonic-gate 				SCHIZO_PCI_CTRL_PCI_RTRY_ERR |
22610Sstevel@tonic-gate 				SCHIZO_PCI_CTRL_PCI_MMU_ERR |
22620Sstevel@tonic-gate 				COMMON_PCI_CTRL_SBH_ERR |
22630Sstevel@tonic-gate 				COMMON_PCI_CTRL_SERR)) ||
22640Sstevel@tonic-gate 			(PBM_AFSR_TO_PRIERR(pbm_afsr)))
22650Sstevel@tonic-gate 		return (1);
22660Sstevel@tonic-gate 
22670Sstevel@tonic-gate 	if ((CHIP_TYPE(pci_p) == PCI_CHIP_XMITS) &&
22680Sstevel@tonic-gate 			(pbm_ctl_stat & XMITS_PCI_CTRL_X_MODE)) {
22690Sstevel@tonic-gate 
22700Sstevel@tonic-gate 		pbm_pcix_stat_reg = (uint64_t *)(a +
22710Sstevel@tonic-gate 		    XMITS_PCI_X_ERROR_STATUS_REG_OFFSET);
22720Sstevel@tonic-gate 
22730Sstevel@tonic-gate 		pbm_pcix_stat = *pbm_pcix_stat_reg;
22740Sstevel@tonic-gate 
22750Sstevel@tonic-gate 		if (PBM_PCIX_TO_PRIERR(pbm_pcix_stat))
22760Sstevel@tonic-gate 			return (1);
22770Sstevel@tonic-gate 
22780Sstevel@tonic-gate 		if (pbm_pcix_stat & XMITS_PCIX_STAT_PERR_RECOV_INT)
22790Sstevel@tonic-gate 			return (1);
22800Sstevel@tonic-gate 	}
22810Sstevel@tonic-gate 
22820Sstevel@tonic-gate 	return (0);
22830Sstevel@tonic-gate 
22840Sstevel@tonic-gate }
22850Sstevel@tonic-gate 
22860Sstevel@tonic-gate static pbm_fm_err_t pci_pbm_err_tbl[] = {
22870Sstevel@tonic-gate 	PCI_PBM_RETRY,			SCHIZO_PCI_CTRL_PCI_RTRY_ERR,
22880Sstevel@tonic-gate 	NULL,	PBM_NONFATAL,	PCI_PBM_TARG_RETRY,
22890Sstevel@tonic-gate 
22900Sstevel@tonic-gate 	PCI_PBM_TTO,			SCHIZO_PCI_CTRL_PCI_TTO_ERR,
22910Sstevel@tonic-gate 	NULL,	PBM_NONFATAL,	PCI_PBM_TARG_TTO,
22920Sstevel@tonic-gate 
22930Sstevel@tonic-gate 	PCI_SCH_BUS_UNUSABLE_ERR,	SCHIZO_PCI_CTRL_BUS_UNUSABLE,
22940Sstevel@tonic-gate 	NULL,	PBM_NONFATAL,	NULL,
22950Sstevel@tonic-gate 
22960Sstevel@tonic-gate 	NULL,				NULL,
22970Sstevel@tonic-gate 	NULL,	NULL,		NULL
22980Sstevel@tonic-gate };
22990Sstevel@tonic-gate 
23000Sstevel@tonic-gate /*
23010Sstevel@tonic-gate  * Function used to log all PCI/PBM/IOMMU errors found in the system.
23020Sstevel@tonic-gate  * It is called by the pbm_error_intr as well as the pci_err_callback(trap
23030Sstevel@tonic-gate  * callback). To protect access we hold the pci_fm_mutex when calling
23040Sstevel@tonic-gate  * this function.
23050Sstevel@tonic-gate  */
23060Sstevel@tonic-gate int
23070Sstevel@tonic-gate pci_pbm_err_handler(dev_info_t *dip, ddi_fm_error_t *derr,
23080Sstevel@tonic-gate 		const void *impl_data, int caller)
23090Sstevel@tonic-gate {
23100Sstevel@tonic-gate 	int fatal = 0;
23110Sstevel@tonic-gate 	int nonfatal = 0;
23120Sstevel@tonic-gate 	int unknown = 0;
23130Sstevel@tonic-gate 	uint32_t prierr, secerr;
23140Sstevel@tonic-gate 	pbm_errstate_t pbm_err;
23150Sstevel@tonic-gate 	char buf[FM_MAX_CLASS];
23160Sstevel@tonic-gate 	pci_t *pci_p = (pci_t *)impl_data;
23170Sstevel@tonic-gate 	pbm_t *pbm_p = pci_p->pci_pbm_p;
23180Sstevel@tonic-gate 	int i, ret = 0;
23190Sstevel@tonic-gate 
23200Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pci_p->pci_common_p->pci_fm_mutex));
23210Sstevel@tonic-gate 	pci_pbm_errstate_get(pci_p, &pbm_err);
23220Sstevel@tonic-gate 
23230Sstevel@tonic-gate 	derr->fme_ena = derr->fme_ena ? derr->fme_ena :
23240Sstevel@tonic-gate 	    fm_ena_generate(0, FM_ENA_FMT1);
23250Sstevel@tonic-gate 
23260Sstevel@tonic-gate 	prierr = PBM_AFSR_TO_PRIERR(pbm_err.pbm_afsr);
23270Sstevel@tonic-gate 	secerr = PBM_AFSR_TO_SECERR(pbm_err.pbm_afsr);
23280Sstevel@tonic-gate 
23290Sstevel@tonic-gate 	if (derr->fme_flag == DDI_FM_ERR_EXPECTED) {
23300Sstevel@tonic-gate 		if (caller == PCI_TRAP_CALL) {
23310Sstevel@tonic-gate 			/*
23320Sstevel@tonic-gate 			 * For ddi_caut_get treat all events as nonfatal.
23330Sstevel@tonic-gate 			 * The trampoline will set err_ena = 0, err_status =
23340Sstevel@tonic-gate 			 * NONFATAL. We only really call this function so that
23350Sstevel@tonic-gate 			 * pci_clear_error() and ndi_fm_handler_dispatch() will
23360Sstevel@tonic-gate 			 * get called.
23370Sstevel@tonic-gate 			 */
23380Sstevel@tonic-gate 			derr->fme_status = DDI_FM_NONFATAL;
23390Sstevel@tonic-gate 			nonfatal++;
23400Sstevel@tonic-gate 			goto done;
23410Sstevel@tonic-gate 		} else {
23420Sstevel@tonic-gate 			/*
23430Sstevel@tonic-gate 			 * For ddi_caut_put treat all events as nonfatal. Here
23440Sstevel@tonic-gate 			 * we have the handle and can call ndi_fm_acc_err_set().
23450Sstevel@tonic-gate 			 */
23460Sstevel@tonic-gate 			derr->fme_status = DDI_FM_NONFATAL;
23470Sstevel@tonic-gate 			ndi_fm_acc_err_set(pbm_p->pbm_excl_handle, derr);
23480Sstevel@tonic-gate 			nonfatal++;
23490Sstevel@tonic-gate 			goto done;
23500Sstevel@tonic-gate 		}
23510Sstevel@tonic-gate 	} else if (derr->fme_flag == DDI_FM_ERR_PEEK) {
23520Sstevel@tonic-gate 		/*
23530Sstevel@tonic-gate 		 * For ddi_peek treat all events as nonfatal. We only
23540Sstevel@tonic-gate 		 * really call this function so that pci_clear_error()
23550Sstevel@tonic-gate 		 * and ndi_fm_handler_dispatch() will get called.
23560Sstevel@tonic-gate 		 */
23570Sstevel@tonic-gate 		nonfatal++;
23580Sstevel@tonic-gate 		goto done;
23590Sstevel@tonic-gate 	} else if (derr->fme_flag == DDI_FM_ERR_POKE) {
23600Sstevel@tonic-gate 		/*
23610Sstevel@tonic-gate 		 * For ddi_poke we can treat as nonfatal if the
23620Sstevel@tonic-gate 		 * following conditions are met :
23630Sstevel@tonic-gate 		 * 1. Make sure only primary error is MA/TA
23640Sstevel@tonic-gate 		 * 2. Make sure no secondary error bits set
23650Sstevel@tonic-gate 		 * 3. check pci config header stat reg to see MA/TA is
23660Sstevel@tonic-gate 		 *    logged. We cannot verify only MA/TA is recorded
23670Sstevel@tonic-gate 		 *    since it gets much more complicated when a
23680Sstevel@tonic-gate 		 *    PCI-to-PCI bridge is present.
23690Sstevel@tonic-gate 		 */
23700Sstevel@tonic-gate 		if ((prierr == SCHIZO_PCI_AFSR_E_MA) && !secerr &&
23710Sstevel@tonic-gate 		    (pbm_err.pbm_pci.pci_cfg_stat & PCI_STAT_R_MAST_AB)) {
23720Sstevel@tonic-gate 			nonfatal++;
23730Sstevel@tonic-gate 			goto done;
23740Sstevel@tonic-gate 		} else if ((*pbm_p->pbm_ctrl_reg & XMITS_PCI_CTRL_X_MODE) &&
23750Sstevel@tonic-gate 		    pcix_ma_behind_bridge(&pbm_err)) {
23760Sstevel@tonic-gate 			/*
23770Sstevel@tonic-gate 			 * MAs behind a PCI-X bridge get sent back to
23780Sstevel@tonic-gate 			 * the host as a Split Completion Error Message.
23790Sstevel@tonic-gate 			 * We handle this the same as the above check.
23800Sstevel@tonic-gate 			 */
23810Sstevel@tonic-gate 			nonfatal++;
23820Sstevel@tonic-gate 			goto done;
23830Sstevel@tonic-gate 		}
23840Sstevel@tonic-gate 		if ((prierr == SCHIZO_PCI_AFSR_E_TA) && !secerr &&
23850Sstevel@tonic-gate 		    (pbm_err.pbm_pci.pci_cfg_stat & PCI_STAT_R_TARG_AB)) {
23860Sstevel@tonic-gate 			nonfatal++;
23870Sstevel@tonic-gate 			goto done;
23880Sstevel@tonic-gate 		}
23890Sstevel@tonic-gate 	}
23900Sstevel@tonic-gate 
23910Sstevel@tonic-gate 	DEBUG2(DBG_ERR_INTR, dip, "pci_pbm_err_handler: prierr=0x%x "
23920Sstevel@tonic-gate 	    "secerr=0x%x", prierr, secerr);
23930Sstevel@tonic-gate 
23940Sstevel@tonic-gate 	if (prierr || secerr) {
23950Sstevel@tonic-gate 		ret = pbm_afsr_report(dip, derr->fme_ena, &pbm_err);
23960Sstevel@tonic-gate 		if (ret == DDI_FM_FATAL)
23970Sstevel@tonic-gate 			fatal++;
23980Sstevel@tonic-gate 		else
23990Sstevel@tonic-gate 			nonfatal++;
24000Sstevel@tonic-gate 	}
24010Sstevel@tonic-gate 	if ((ret = pcix_log_pbm(pci_p, derr->fme_ena, &pbm_err))
24020Sstevel@tonic-gate 			== DDI_FM_FATAL)
24030Sstevel@tonic-gate 		fatal++;
24040Sstevel@tonic-gate 	else if (ret == DDI_FM_NONFATAL)
24050Sstevel@tonic-gate 		nonfatal++;
24060Sstevel@tonic-gate 
24070Sstevel@tonic-gate 	if ((ret = pci_cfg_report(dip, derr, &pbm_err.pbm_pci, caller, prierr))
24080Sstevel@tonic-gate 			== DDI_FM_FATAL)
24090Sstevel@tonic-gate 		fatal++;
24100Sstevel@tonic-gate 	else if (ret == DDI_FM_NONFATAL)
24110Sstevel@tonic-gate 		nonfatal++;
24120Sstevel@tonic-gate 
24130Sstevel@tonic-gate 	for (i = 0; pci_pbm_err_tbl[i].pbm_err_class != NULL; i++) {
24140Sstevel@tonic-gate 		if ((pbm_err.pbm_ctl_stat & pci_pbm_err_tbl[i].pbm_reg_bit) &&
24150Sstevel@tonic-gate 		    !prierr) {
24160Sstevel@tonic-gate 			pbm_err.pbm_err_class =
24170Sstevel@tonic-gate 				pci_pbm_err_tbl[i].pbm_err_class;
24180Sstevel@tonic-gate 			pbm_ereport_post(dip, derr->fme_ena, &pbm_err);
24190Sstevel@tonic-gate 			if (pci_pbm_err_tbl[i].pbm_flag)
24200Sstevel@tonic-gate 				fatal++;
24210Sstevel@tonic-gate 			else
24220Sstevel@tonic-gate 				nonfatal++;
24230Sstevel@tonic-gate 			if (caller == PCI_TRAP_CALL &&
24241865Sdilpreet 			    pci_pbm_err_tbl[i].pbm_terr_class)
24251865Sdilpreet 				pci_target_enqueue(derr->fme_ena,
24261865Sdilpreet 				    pci_pbm_err_tbl[i].pbm_terr_class,
24271865Sdilpreet 				    pbm_err.pbm_bridge_type,
24281865Sdilpreet 				    (uint64_t)derr->fme_bus_specific);
24290Sstevel@tonic-gate 		}
24300Sstevel@tonic-gate 	}
24310Sstevel@tonic-gate 
24320Sstevel@tonic-gate 	if ((pbm_err.pbm_ctl_stat & COMMON_PCI_CTRL_SBH_ERR) &&
24330Sstevel@tonic-gate 	    (CHIP_TYPE(pci_p) != PCI_CHIP_TOMATILLO)) {
24340Sstevel@tonic-gate 		pbm_err.pbm_err_class = PCI_SCH_SBH;
24350Sstevel@tonic-gate 		pbm_ereport_post(dip, derr->fme_ena, &pbm_err);
24360Sstevel@tonic-gate 		if (pci_panic_on_sbh_errors)
24370Sstevel@tonic-gate 			fatal++;
24380Sstevel@tonic-gate 		else
24390Sstevel@tonic-gate 			nonfatal++;
24400Sstevel@tonic-gate 	}
24410Sstevel@tonic-gate 
24420Sstevel@tonic-gate 	/*
24430Sstevel@tonic-gate 	 * PBM Received System Error - During any transaction, or
24440Sstevel@tonic-gate 	 * at any point on the bus, some device may detect a critical
24450Sstevel@tonic-gate 	 * error and signal a system error to the system.
24460Sstevel@tonic-gate 	 */
24470Sstevel@tonic-gate 	if (pbm_err.pbm_ctl_stat & COMMON_PCI_CTRL_SERR) {
24480Sstevel@tonic-gate 		/*
24490Sstevel@tonic-gate 		 * may be expected (master abort from pci-pci bridge during
24500Sstevel@tonic-gate 		 * poke will generate SERR)
24510Sstevel@tonic-gate 		 */
24520Sstevel@tonic-gate 		if (derr->fme_flag != DDI_FM_ERR_POKE) {
24530Sstevel@tonic-gate 			DEBUG1(DBG_ERR_INTR, dip, "pci_pbm_err_handler: "
24540Sstevel@tonic-gate 			    "ereport_post: %s", buf);
24550Sstevel@tonic-gate 			(void) snprintf(buf, FM_MAX_CLASS, "%s.%s",
24560Sstevel@tonic-gate 				PCI_ERROR_SUBCLASS, PCI_REC_SERR);
24570Sstevel@tonic-gate 			ddi_fm_ereport_post(dip, buf, derr->fme_ena,
24580Sstevel@tonic-gate 			    DDI_NOSLEEP, FM_VERSION, DATA_TYPE_UINT8, 0,
24590Sstevel@tonic-gate 			    PCI_CONFIG_STATUS, DATA_TYPE_UINT16,
24600Sstevel@tonic-gate 			    pbm_err.pbm_pci.pci_cfg_stat, PCI_CONFIG_COMMAND,
24610Sstevel@tonic-gate 			    DATA_TYPE_UINT16, pbm_err.pbm_pci.pci_cfg_comm,
24620Sstevel@tonic-gate 			    PCI_PA, DATA_TYPE_UINT64, (uint64_t)0, NULL);
24630Sstevel@tonic-gate 		}
24641865Sdilpreet 		unknown++;
24650Sstevel@tonic-gate 	}
24660Sstevel@tonic-gate 
24670Sstevel@tonic-gate 	/*
24680Sstevel@tonic-gate 	 * PCI Retry Timeout - Device fails to retry deferred
24690Sstevel@tonic-gate 	 * transaction within timeout. Only Tomatillo
24700Sstevel@tonic-gate 	 */
24710Sstevel@tonic-gate 	if (pbm_err.pbm_ctl_stat & TOMATILLO_PCI_CTRL_PCI_DTO_ERR) {
24720Sstevel@tonic-gate 		if (pci_dto_fault_warn == CE_PANIC)
24730Sstevel@tonic-gate 			fatal++;
24740Sstevel@tonic-gate 		else
24750Sstevel@tonic-gate 			nonfatal++;
24760Sstevel@tonic-gate 
24770Sstevel@tonic-gate 		(void) snprintf(buf, FM_MAX_CLASS, "%s.%s",
24780Sstevel@tonic-gate 			PCI_ERROR_SUBCLASS, PCI_DTO);
24790Sstevel@tonic-gate 		ddi_fm_ereport_post(dip, buf, derr->fme_ena, DDI_NOSLEEP,
24800Sstevel@tonic-gate 		    FM_VERSION, DATA_TYPE_UINT8, 0,
24810Sstevel@tonic-gate 		    PCI_CONFIG_STATUS, DATA_TYPE_UINT16,
24820Sstevel@tonic-gate 		    pbm_err.pbm_pci.pci_cfg_stat,
24830Sstevel@tonic-gate 		    PCI_CONFIG_COMMAND, DATA_TYPE_UINT16,
24840Sstevel@tonic-gate 		    pbm_err.pbm_pci.pci_cfg_comm,
24850Sstevel@tonic-gate 		    PCI_PA, DATA_TYPE_UINT64, (uint64_t)0, NULL);
24860Sstevel@tonic-gate 	}
24870Sstevel@tonic-gate 
24880Sstevel@tonic-gate 	/*
24890Sstevel@tonic-gate 	 * PBM Detected Data Parity Error - DPE detected during a DMA Write
24900Sstevel@tonic-gate 	 * or PIO Read. Later case is taken care of by cpu_deferred_error
24910Sstevel@tonic-gate 	 * and sent here to be logged.
24920Sstevel@tonic-gate 	 */
24930Sstevel@tonic-gate 	if ((pbm_err.pbm_pci.pci_cfg_stat & PCI_STAT_PERROR) &&
24940Sstevel@tonic-gate 			!(pbm_err.pbm_pci.pci_cfg_stat & PCI_STAT_S_SYSERR)) {
24950Sstevel@tonic-gate 		/*
24960Sstevel@tonic-gate 		 * If we have an address then fault
24970Sstevel@tonic-gate 		 * it, if not probe for errant device
24980Sstevel@tonic-gate 		 */
24990Sstevel@tonic-gate 		ret = DDI_FM_FATAL;
25000Sstevel@tonic-gate 		if (caller != PCI_TRAP_CALL) {
25011865Sdilpreet 			if (pbm_err.pbm_va_log) {
25021865Sdilpreet 				ret = ndi_fmc_error(dip, NULL, DMA_HANDLE,
25031865Sdilpreet 				    derr->fme_ena, (void *)&pbm_err.pbm_va_log);
25041865Sdilpreet 			}
25050Sstevel@tonic-gate 			if (ret == DDI_FM_NONFATAL)
25060Sstevel@tonic-gate 				nonfatal++;
25070Sstevel@tonic-gate 			else
25080Sstevel@tonic-gate 				fatal++;
25090Sstevel@tonic-gate 		} else
25100Sstevel@tonic-gate 			nonfatal++;
25110Sstevel@tonic-gate 
25120Sstevel@tonic-gate 	}
25130Sstevel@tonic-gate 
25140Sstevel@tonic-gate 	/* PBM Detected IOMMU Error */
25150Sstevel@tonic-gate 	if (pbm_err.pbm_ctl_stat & SCHIZO_PCI_CTRL_PCI_MMU_ERR) {
25160Sstevel@tonic-gate 		if (iommu_err_handler(dip, derr->fme_ena, &pbm_err)
25170Sstevel@tonic-gate 				== DDI_FM_FATAL)
25180Sstevel@tonic-gate 			fatal++;
25190Sstevel@tonic-gate 		else
25200Sstevel@tonic-gate 			nonfatal++;
25210Sstevel@tonic-gate 	}
25220Sstevel@tonic-gate 
25230Sstevel@tonic-gate done:
25240Sstevel@tonic-gate 	ret = ndi_fm_handler_dispatch(dip, NULL, derr);
25250Sstevel@tonic-gate 	if (ret == DDI_FM_FATAL) {
25260Sstevel@tonic-gate 		fatal++;
25270Sstevel@tonic-gate 	} else if (ret == DDI_FM_NONFATAL) {
25280Sstevel@tonic-gate 		nonfatal++;
25290Sstevel@tonic-gate 	} else if (ret == DDI_FM_UNKNOWN) {
25300Sstevel@tonic-gate 		unknown++;
25310Sstevel@tonic-gate 	}
25320Sstevel@tonic-gate 
25330Sstevel@tonic-gate 	/*
25340Sstevel@tonic-gate 	 * RSERR not claimed as nonfatal by a child is considered fatal
25350Sstevel@tonic-gate 	 */
25361865Sdilpreet 	if (unknown && !fatal && !nonfatal)
25370Sstevel@tonic-gate 		fatal++;
25380Sstevel@tonic-gate 
25390Sstevel@tonic-gate 	/* Cleanup and reset error bits */
25400Sstevel@tonic-gate 	pci_clear_error(pci_p, &pbm_err);
25410Sstevel@tonic-gate 
25420Sstevel@tonic-gate 	return (fatal ? DDI_FM_FATAL : (nonfatal ? DDI_FM_NONFATAL :
25430Sstevel@tonic-gate 	    (unknown ? DDI_FM_UNKNOWN : DDI_FM_OK)));
25440Sstevel@tonic-gate }
25450Sstevel@tonic-gate 
25460Sstevel@tonic-gate /*
25470Sstevel@tonic-gate  * Function returns TRUE if a Primary error is Split Completion Error
25480Sstevel@tonic-gate  * that indicates a Master Abort occured behind a PCI-X bridge.
25490Sstevel@tonic-gate  * This function should only be called for busses running in PCI-X mode.
25500Sstevel@tonic-gate  */
25510Sstevel@tonic-gate static int
25520Sstevel@tonic-gate pcix_ma_behind_bridge(pbm_errstate_t *pbm_err_p)
25530Sstevel@tonic-gate {
25540Sstevel@tonic-gate 	uint64_t msg;
25550Sstevel@tonic-gate 
25560Sstevel@tonic-gate 	if (pbm_err_p->pbm_afsr & XMITS_PCI_X_AFSR_S_SC_ERR)
25570Sstevel@tonic-gate 		return (0);
25580Sstevel@tonic-gate 
25590Sstevel@tonic-gate 	if (pbm_err_p->pbm_afsr & XMITS_PCI_X_AFSR_P_SC_ERR) {
25600Sstevel@tonic-gate 		msg = (pbm_err_p->pbm_afsr >> XMITS_PCI_X_P_MSG_SHIFT) &
25610Sstevel@tonic-gate 		    XMITS_PCIX_MSG_MASK;
25620Sstevel@tonic-gate 		if (msg & PCIX_CLASS_BRIDGE)
25630Sstevel@tonic-gate 			if (msg & PCIX_BRIDGE_MASTER_ABORT) {
25640Sstevel@tonic-gate 				return (1);
25650Sstevel@tonic-gate 			}
25660Sstevel@tonic-gate 	}
25670Sstevel@tonic-gate 
25680Sstevel@tonic-gate 	return (0);
25690Sstevel@tonic-gate }
25700Sstevel@tonic-gate 
25710Sstevel@tonic-gate /*
25720Sstevel@tonic-gate  * Function used to gather PBM/PCI/IOMMU error state for the
25730Sstevel@tonic-gate  * pci_pbm_err_handler and the cb_buserr_intr. This function must be
25740Sstevel@tonic-gate  * called while pci_fm_mutex is held.
25750Sstevel@tonic-gate  */
25760Sstevel@tonic-gate static void
25770Sstevel@tonic-gate pci_pbm_errstate_get(pci_t *pci_p, pbm_errstate_t *pbm_err_p)
25780Sstevel@tonic-gate {
25790Sstevel@tonic-gate 	pbm_t *pbm_p = pci_p->pci_pbm_p;
25800Sstevel@tonic-gate 	iommu_t *iommu_p = pci_p->pci_iommu_p;
25810Sstevel@tonic-gate 	caddr_t a = pci_p->pci_address[0];
25820Sstevel@tonic-gate 	uint64_t *pbm_pcix_stat_reg;
25830Sstevel@tonic-gate 
25840Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pci_p->pci_common_p->pci_fm_mutex));
25850Sstevel@tonic-gate 	bzero(pbm_err_p, sizeof (pbm_errstate_t));
25860Sstevel@tonic-gate 
25870Sstevel@tonic-gate 	/*
25880Sstevel@tonic-gate 	 * Capture all pbm error state for later logging
25890Sstevel@tonic-gate 	 */
25900Sstevel@tonic-gate 	pbm_err_p->pbm_bridge_type = PCI_BRIDGE_TYPE(pci_p->pci_common_p);
25910Sstevel@tonic-gate 
25920Sstevel@tonic-gate 	pbm_err_p->pbm_pci.pci_cfg_stat =
25930Sstevel@tonic-gate 		pbm_p->pbm_config_header->ch_status_reg;
25940Sstevel@tonic-gate 	pbm_err_p->pbm_ctl_stat = *pbm_p->pbm_ctrl_reg;
25950Sstevel@tonic-gate 	pbm_err_p->pbm_afsr = *pbm_p->pbm_async_flt_status_reg;
25960Sstevel@tonic-gate 	pbm_err_p->pbm_afar = *pbm_p->pbm_async_flt_addr_reg;
25970Sstevel@tonic-gate 	pbm_err_p->pbm_iommu.iommu_stat = *iommu_p->iommu_ctrl_reg;
25980Sstevel@tonic-gate 	pbm_err_p->pbm_pci.pci_cfg_comm =
25990Sstevel@tonic-gate 		pbm_p->pbm_config_header->ch_command_reg;
26000Sstevel@tonic-gate 	pbm_err_p->pbm_pci.pci_pa = *pbm_p->pbm_async_flt_addr_reg;
26010Sstevel@tonic-gate 
26020Sstevel@tonic-gate 	/*
26030Sstevel@tonic-gate 	 * Record errant slot for Xmits and Schizo
26040Sstevel@tonic-gate 	 * Not stored in Tomatillo
26050Sstevel@tonic-gate 	 */
26060Sstevel@tonic-gate 	if (CHIP_TYPE(pci_p) == PCI_CHIP_XMITS ||
26070Sstevel@tonic-gate 			CHIP_TYPE(pci_p) == PCI_CHIP_SCHIZO) {
26080Sstevel@tonic-gate 		pbm_err_p->pbm_err_sl = (pbm_err_p->pbm_ctl_stat &
26090Sstevel@tonic-gate 				SCHIZO_PCI_CTRL_ERR_SLOT) >>
26100Sstevel@tonic-gate 			SCHIZO_PCI_CTRL_ERR_SLOT_SHIFT;
26110Sstevel@tonic-gate 
26120Sstevel@tonic-gate 		/*
26130Sstevel@tonic-gate 		 * The bit 51 on XMITS rev1.0 is same as
26140Sstevel@tonic-gate 		 * SCHIZO_PCI_CTRL_ERR_SLOT_LOCK on schizo2.3. But
26150Sstevel@tonic-gate 		 * this bit needs to be cleared to be able to latch
26160Sstevel@tonic-gate 		 * the slot info on next fault.
26170Sstevel@tonic-gate 		 * But in XMITS Rev2.0, this bit indicates a DMA Write
26180Sstevel@tonic-gate 		 * Parity error.
26190Sstevel@tonic-gate 		 */
26200Sstevel@tonic-gate 		if (pbm_err_p->pbm_ctl_stat & XMITS_PCI_CTRL_DMA_WR_PERR) {
26210Sstevel@tonic-gate 			if ((PCI_CHIP_ID(pci_p) == XMITS_VER_10) ||
26220Sstevel@tonic-gate 				(PCI_CHIP_ID(pci_p) <= SCHIZO_VER_23)) {
26230Sstevel@tonic-gate 				/*
26240Sstevel@tonic-gate 				 * top 32 bits are W1C and we just want to
26250Sstevel@tonic-gate 				 * clear SLOT_LOCK. Leave bottom 32 bits
26260Sstevel@tonic-gate 				 * unchanged
26270Sstevel@tonic-gate 				 */
26280Sstevel@tonic-gate 				*pbm_p->pbm_ctrl_reg =
26290Sstevel@tonic-gate 					pbm_err_p->pbm_ctl_stat &
26300Sstevel@tonic-gate 					(SCHIZO_PCI_CTRL_ERR_SLOT_LOCK |
26310Sstevel@tonic-gate 					0xffffffff);
26320Sstevel@tonic-gate 				pbm_err_p->pbm_ctl_stat =
26330Sstevel@tonic-gate 					*pbm_p->pbm_ctrl_reg;
26340Sstevel@tonic-gate 			}
26350Sstevel@tonic-gate 		}
26360Sstevel@tonic-gate 	}
26370Sstevel@tonic-gate 
26380Sstevel@tonic-gate 	/*
26390Sstevel@tonic-gate 	 * Tomatillo specific registers
26400Sstevel@tonic-gate 	 */
26410Sstevel@tonic-gate 	if (CHIP_TYPE(pci_p) == PCI_CHIP_TOMATILLO) {
2642946Smathue 		pbm_err_p->pbm_va_log = (uint64_t)va_to_pa(
2643946Smathue 		    (void *)(uintptr_t)*(a + TOMATILLO_TGT_ERR_VALOG_OFFSET));
26440Sstevel@tonic-gate 		pbm_err_p->pbm_iommu.iommu_tfar = *iommu_p->iommu_tfar_reg;
26450Sstevel@tonic-gate 	}
26460Sstevel@tonic-gate 
26470Sstevel@tonic-gate 	/*
26480Sstevel@tonic-gate 	 * Xmits PCI-X register
26490Sstevel@tonic-gate 	 */
26500Sstevel@tonic-gate 	if ((CHIP_TYPE(pci_p) == PCI_CHIP_XMITS) &&
26510Sstevel@tonic-gate 			(pbm_err_p->pbm_ctl_stat & XMITS_PCI_CTRL_X_MODE)) {
26520Sstevel@tonic-gate 
26530Sstevel@tonic-gate 		pbm_pcix_stat_reg = (uint64_t *)(a +
26540Sstevel@tonic-gate 		    XMITS_PCI_X_ERROR_STATUS_REG_OFFSET);
26550Sstevel@tonic-gate 
26560Sstevel@tonic-gate 		pbm_err_p->pbm_pcix_stat = *pbm_pcix_stat_reg;
26570Sstevel@tonic-gate 		pbm_err_p->pbm_pcix_pfar = pbm_err_p->pbm_pcix_stat &
26580Sstevel@tonic-gate 				XMITS_PCI_X_STATUS_PFAR_MASK;
26590Sstevel@tonic-gate 	}
26600Sstevel@tonic-gate }
26610Sstevel@tonic-gate 
26620Sstevel@tonic-gate /*
26630Sstevel@tonic-gate  * Function used to clear PBM/PCI/IOMMU error state after error handling
26640Sstevel@tonic-gate  * is complete. Only clearing error bits which have been logged. Called by
26650Sstevel@tonic-gate  * pci_pbm_err_handler and pci_bus_exit.
26660Sstevel@tonic-gate  */
26670Sstevel@tonic-gate static void
26680Sstevel@tonic-gate pci_clear_error(pci_t *pci_p, pbm_errstate_t *pbm_err_p)
26690Sstevel@tonic-gate {
26700Sstevel@tonic-gate 	pbm_t *pbm_p = pci_p->pci_pbm_p;
26710Sstevel@tonic-gate 	iommu_t *iommu_p = pci_p->pci_iommu_p;
26720Sstevel@tonic-gate 
26730Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pbm_p->pbm_pci_p->pci_common_p->pci_fm_mutex));
26740Sstevel@tonic-gate 
26750Sstevel@tonic-gate 	if (*pbm_p->pbm_ctrl_reg & SCHIZO_PCI_CTRL_PCI_MMU_ERR) {
26760Sstevel@tonic-gate 		iommu_tlb_scrub(pci_p->pci_iommu_p, 1);
26770Sstevel@tonic-gate 	}
26780Sstevel@tonic-gate 	pbm_p->pbm_config_header->ch_status_reg =
26790Sstevel@tonic-gate 		pbm_err_p->pbm_pci.pci_cfg_stat;
26800Sstevel@tonic-gate 	*pbm_p->pbm_ctrl_reg = pbm_err_p->pbm_ctl_stat;
26810Sstevel@tonic-gate 	*pbm_p->pbm_async_flt_status_reg = pbm_err_p->pbm_afsr;
26820Sstevel@tonic-gate 	*iommu_p->iommu_ctrl_reg = pbm_err_p->pbm_iommu.iommu_stat;
26830Sstevel@tonic-gate }
26840Sstevel@tonic-gate 
26850Sstevel@tonic-gate void
26860Sstevel@tonic-gate pbm_clear_error(pbm_t *pbm_p)
26870Sstevel@tonic-gate {
26880Sstevel@tonic-gate 	uint64_t pbm_afsr, pbm_ctl_stat;
26890Sstevel@tonic-gate 
26900Sstevel@tonic-gate 	/*
26910Sstevel@tonic-gate 	 * for poke() support - called from POKE_FLUSH. Spin waiting
26920Sstevel@tonic-gate 	 * for MA, TA or SERR to be cleared by a pbm_error_intr().
26930Sstevel@tonic-gate 	 * We have to wait for SERR too in case the device is beyond
26940Sstevel@tonic-gate 	 * a pci-pci bridge.
26950Sstevel@tonic-gate 	 */
26960Sstevel@tonic-gate 	pbm_ctl_stat = *pbm_p->pbm_ctrl_reg;
26970Sstevel@tonic-gate 	pbm_afsr = *pbm_p->pbm_async_flt_status_reg;
26980Sstevel@tonic-gate 	while (((pbm_afsr >> SCHIZO_PCI_AFSR_PE_SHIFT) &
26990Sstevel@tonic-gate 	    (SCHIZO_PCI_AFSR_E_MA | SCHIZO_PCI_AFSR_E_TA)) ||
27000Sstevel@tonic-gate 	    (pbm_ctl_stat & COMMON_PCI_CTRL_SERR)) {
27010Sstevel@tonic-gate 		pbm_ctl_stat = *pbm_p->pbm_ctrl_reg;
27020Sstevel@tonic-gate 		pbm_afsr = *pbm_p->pbm_async_flt_status_reg;
27030Sstevel@tonic-gate 	}
27040Sstevel@tonic-gate }
27050Sstevel@tonic-gate 
27060Sstevel@tonic-gate /*
27070Sstevel@tonic-gate  * Function used to convert the 32 bit captured PCI error address
27080Sstevel@tonic-gate  * to the full Safari or Jbus address. This is so we can look this address
27090Sstevel@tonic-gate  * up in our handle caches.
27100Sstevel@tonic-gate  */
27110Sstevel@tonic-gate void
27120Sstevel@tonic-gate pci_format_addr(dev_info_t *dip, uint64_t *afar, uint64_t afsr)
27130Sstevel@tonic-gate {
27140Sstevel@tonic-gate 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
27150Sstevel@tonic-gate 	pci_ranges_t *io_range, *mem_range;
27160Sstevel@tonic-gate 	uint64_t err_pa = 0;
27170Sstevel@tonic-gate 
27180Sstevel@tonic-gate 	if (afsr & SCHIZO_PCI_AFSR_CONF_SPACE) {
27190Sstevel@tonic-gate 		err_pa |= pci_p->pci_ranges->parent_high;
27200Sstevel@tonic-gate 		err_pa = err_pa << 32;
27210Sstevel@tonic-gate 		err_pa |= pci_p->pci_ranges->parent_low;
27220Sstevel@tonic-gate 	} else if (afsr & SCHIZO_PCI_AFSR_IO_SPACE) {
27230Sstevel@tonic-gate 		io_range = pci_p->pci_ranges + 1;
27240Sstevel@tonic-gate 		err_pa |= io_range->parent_high;
27250Sstevel@tonic-gate 		err_pa = err_pa << 32;
27260Sstevel@tonic-gate 		err_pa |= io_range->parent_low;
27270Sstevel@tonic-gate 	} else if (afsr & SCHIZO_PCI_AFSR_MEM_SPACE) {
27280Sstevel@tonic-gate 		mem_range = pci_p->pci_ranges + 2;
27290Sstevel@tonic-gate 		err_pa |= mem_range->parent_high;
27300Sstevel@tonic-gate 		err_pa = err_pa << 32;
27310Sstevel@tonic-gate 		err_pa |= mem_range->parent_low;
27320Sstevel@tonic-gate 	}
27330Sstevel@tonic-gate 	*afar |= err_pa;
27340Sstevel@tonic-gate }
27350Sstevel@tonic-gate 
27360Sstevel@tonic-gate static ecc_format_t ecc_format_tbl[] = {
27370Sstevel@tonic-gate 	SCH_REG_UPA,		NULL,				NULL,
27380Sstevel@tonic-gate 	SCH_REG_PCIA_REG,	SCHIZO_PCI_AFSR_CONF_SPACE,	PCI_SIDEA,
27390Sstevel@tonic-gate 	SCH_REG_PCIA_MEM,	SCHIZO_PCI_AFSR_MEM_SPACE,	PCI_SIDEA,
27400Sstevel@tonic-gate 	SCH_REG_PCIA_CFGIO,	SCHIZO_PCI_AFSR_IO_SPACE,	PCI_SIDEA,
27410Sstevel@tonic-gate 	SCH_REG_PCIB_REG,	SCHIZO_PCI_AFSR_CONF_SPACE,	PCI_SIDEB,
27420Sstevel@tonic-gate 	SCH_REG_PCIB_MEM,	SCHIZO_PCI_AFSR_MEM_SPACE,	PCI_SIDEB,
27430Sstevel@tonic-gate 	SCH_REG_PCIB_CFGIO,	SCHIZO_PCI_AFSR_IO_SPACE,	PCI_SIDEB,
27440Sstevel@tonic-gate 	SCH_REG_SAFARI_REGS,	NULL,				NULL,
27450Sstevel@tonic-gate 	NULL,			NULL,				NULL,
27460Sstevel@tonic-gate };
27470Sstevel@tonic-gate 
27480Sstevel@tonic-gate /*
27490Sstevel@tonic-gate  * Function used to convert the 32 bit PIO address captured for a
27500Sstevel@tonic-gate  * Safari Bus UE(during PIO Rd/Wr) to a full Safari Bus Address.
27510Sstevel@tonic-gate  */
27520Sstevel@tonic-gate static void
27530Sstevel@tonic-gate pci_format_ecc_addr(dev_info_t *dip, uint64_t *afar, ecc_region_t region)
27540Sstevel@tonic-gate {
27550Sstevel@tonic-gate 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
27560Sstevel@tonic-gate 	pci_common_t *cmn_p = pci_p->pci_common_p;
27570Sstevel@tonic-gate 	cb_t *cb_p = pci_p->pci_cb_p;
27580Sstevel@tonic-gate 	int i, pci_side = 0;
27590Sstevel@tonic-gate 	int swap = 0;
27600Sstevel@tonic-gate 	uint64_t pa = cb_p->cb_base_pa;
27610Sstevel@tonic-gate 	uint64_t flag, schizo_base, pci_csr_base;
27620Sstevel@tonic-gate 
27630Sstevel@tonic-gate 	if (pci_p == NULL)
27640Sstevel@tonic-gate 		return;
27650Sstevel@tonic-gate 
27660Sstevel@tonic-gate 	pci_csr_base = va_to_pa(pci_p->pci_address[0]);
27670Sstevel@tonic-gate 
27680Sstevel@tonic-gate 	/*
27690Sstevel@tonic-gate 	 * Using the csr_base address to determine which side
27700Sstevel@tonic-gate 	 * we are on.
27710Sstevel@tonic-gate 	 */
27720Sstevel@tonic-gate 	if (pci_csr_base & PCI_SIDE_ADDR_MASK)
27730Sstevel@tonic-gate 		pci_side = 1;
27740Sstevel@tonic-gate 	else
27750Sstevel@tonic-gate 		pci_side = 0;
27760Sstevel@tonic-gate 
27770Sstevel@tonic-gate 	schizo_base = pa - PBM_CTRL_OFFSET;
27780Sstevel@tonic-gate 
27790Sstevel@tonic-gate 	for (i = 0; ecc_format_tbl[i].ecc_region != NULL; i++) {
27800Sstevel@tonic-gate 		if (region == ecc_format_tbl[i].ecc_region) {
27810Sstevel@tonic-gate 			flag = ecc_format_tbl[i].ecc_space;
27820Sstevel@tonic-gate 			if (ecc_format_tbl[i].ecc_side != pci_side)
27830Sstevel@tonic-gate 				swap = 1;
27840Sstevel@tonic-gate 			if (region == SCH_REG_SAFARI_REGS)
27850Sstevel@tonic-gate 				*afar |= schizo_base;
27860Sstevel@tonic-gate 			break;
27870Sstevel@tonic-gate 		}
27880Sstevel@tonic-gate 	}
27890Sstevel@tonic-gate 
27900Sstevel@tonic-gate 	if (swap) {
27910Sstevel@tonic-gate 		pci_p = cmn_p->pci_p[PCI_OTHER_SIDE(pci_p->pci_side)];
27920Sstevel@tonic-gate 
27930Sstevel@tonic-gate 		if (pci_p == NULL)
27940Sstevel@tonic-gate 			return;
27950Sstevel@tonic-gate 	}
27960Sstevel@tonic-gate 	pci_format_addr(pci_p->pci_dip, afar, flag);
27970Sstevel@tonic-gate }
27980Sstevel@tonic-gate 
27990Sstevel@tonic-gate /*
28000Sstevel@tonic-gate  * Function used to post control block specific ereports.
28010Sstevel@tonic-gate  */
28020Sstevel@tonic-gate static void
28030Sstevel@tonic-gate cb_ereport_post(dev_info_t *dip, uint64_t ena, cb_errstate_t *cb_err)
28040Sstevel@tonic-gate {
28050Sstevel@tonic-gate 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
28060Sstevel@tonic-gate 	char buf[FM_MAX_CLASS], dev_path[MAXPATHLEN], *ptr;
28070Sstevel@tonic-gate 	struct i_ddi_fmhdl *fmhdl = DEVI(dip)->devi_fmhdl;
28080Sstevel@tonic-gate 	nvlist_t *ereport, *detector;
28090Sstevel@tonic-gate 	errorq_elem_t *eqep;
28100Sstevel@tonic-gate 	nv_alloc_t *nva;
28110Sstevel@tonic-gate 
28120Sstevel@tonic-gate 	DEBUG1(DBG_ATTACH, dip, "cb_ereport_post: elog 0x%lx",
28130Sstevel@tonic-gate 	    cb_err->cb_elog);
28140Sstevel@tonic-gate 
28150Sstevel@tonic-gate 	/*
28160Sstevel@tonic-gate 	 * We do not use ddi_fm_ereport_post because we need to set a
28170Sstevel@tonic-gate 	 * special detector here. Since we do not have a device path for
28180Sstevel@tonic-gate 	 * the bridge chip we use what we think it should be to aid in
28190Sstevel@tonic-gate 	 * diagnosis.
28200Sstevel@tonic-gate 	 */
28210Sstevel@tonic-gate 	(void) snprintf(buf, FM_MAX_CLASS, "%s.%s.%s", DDI_IO_CLASS,
28220Sstevel@tonic-gate 	    cb_err->cb_bridge_type, cb_err->cb_err_class);
28230Sstevel@tonic-gate 
28240Sstevel@tonic-gate 	ena = ena ? ena : fm_ena_generate(0, FM_ENA_FMT1);
28250Sstevel@tonic-gate 
28260Sstevel@tonic-gate 	eqep = errorq_reserve(fmhdl->fh_errorq);
28270Sstevel@tonic-gate 	if (eqep == NULL)
28280Sstevel@tonic-gate 		return;
28290Sstevel@tonic-gate 
28300Sstevel@tonic-gate 	ereport = errorq_elem_nvl(fmhdl->fh_errorq, eqep);
28310Sstevel@tonic-gate 	nva = errorq_elem_nva(fmhdl->fh_errorq, eqep);
28320Sstevel@tonic-gate 	detector = fm_nvlist_create(nva);
28330Sstevel@tonic-gate 
28340Sstevel@tonic-gate 	ASSERT(ereport);
28350Sstevel@tonic-gate 	ASSERT(nva);
28360Sstevel@tonic-gate 	ASSERT(detector);
28370Sstevel@tonic-gate 
28380Sstevel@tonic-gate 	ddi_pathname(dip, dev_path);
28390Sstevel@tonic-gate 	ptr = strrchr(dev_path, (int)',');
28400Sstevel@tonic-gate 
28410Sstevel@tonic-gate 	if (ptr)
28420Sstevel@tonic-gate 		*ptr = '\0';
28430Sstevel@tonic-gate 
28440Sstevel@tonic-gate 	fm_fmri_dev_set(detector, FM_DEV_SCHEME_VERSION, NULL, dev_path, NULL);
28450Sstevel@tonic-gate 
28460Sstevel@tonic-gate 	DEBUG1(DBG_ERR_INTR, dip, "cb_ereport_post: ereport_set: %s", buf);
28470Sstevel@tonic-gate 
28480Sstevel@tonic-gate 	if (CHIP_TYPE(pci_p) == PCI_CHIP_SCHIZO ||
28490Sstevel@tonic-gate 	    CHIP_TYPE(pci_p) == PCI_CHIP_XMITS) {
28500Sstevel@tonic-gate 		fm_ereport_set(ereport, FM_EREPORT_VERSION, buf, ena, detector,
28510Sstevel@tonic-gate 		    SAFARI_CSR, DATA_TYPE_UINT64, cb_err->cb_csr,
28520Sstevel@tonic-gate 		    SAFARI_ERR, DATA_TYPE_UINT64, cb_err->cb_err,
28530Sstevel@tonic-gate 		    SAFARI_INTR, DATA_TYPE_UINT64, cb_err->cb_intr,
28540Sstevel@tonic-gate 		    SAFARI_ELOG, DATA_TYPE_UINT64, cb_err->cb_elog,
28550Sstevel@tonic-gate 		    SAFARI_PCR, DATA_TYPE_UINT64, cb_err->cb_pcr,
28560Sstevel@tonic-gate 		    NULL);
28570Sstevel@tonic-gate 	} else if (CHIP_TYPE(pci_p) == PCI_CHIP_TOMATILLO) {
28580Sstevel@tonic-gate 		fm_ereport_set(ereport, FM_EREPORT_VERSION, buf, ena, detector,
28590Sstevel@tonic-gate 		    JBUS_CSR, DATA_TYPE_UINT64, cb_err->cb_csr,
28600Sstevel@tonic-gate 		    JBUS_ERR, DATA_TYPE_UINT64, cb_err->cb_err,
28610Sstevel@tonic-gate 		    JBUS_INTR, DATA_TYPE_UINT64, cb_err->cb_intr,
28620Sstevel@tonic-gate 		    JBUS_ELOG, DATA_TYPE_UINT64, cb_err->cb_elog,
28630Sstevel@tonic-gate 		    JBUS_PCR, DATA_TYPE_UINT64, cb_err->cb_pcr,
28640Sstevel@tonic-gate 		    NULL);
28650Sstevel@tonic-gate 	}
28660Sstevel@tonic-gate 	errorq_commit(fmhdl->fh_errorq, eqep, ERRORQ_ASYNC);
28670Sstevel@tonic-gate }
28680Sstevel@tonic-gate 
28690Sstevel@tonic-gate /*
28700Sstevel@tonic-gate  * Function used to post IOMMU specific ereports.
28710Sstevel@tonic-gate  */
28720Sstevel@tonic-gate static void
28730Sstevel@tonic-gate iommu_ereport_post(dev_info_t *dip, uint64_t ena, pbm_errstate_t *pbm_err)
28740Sstevel@tonic-gate {
28750Sstevel@tonic-gate 	char buf[FM_MAX_CLASS];
28760Sstevel@tonic-gate 
28770Sstevel@tonic-gate 	(void) snprintf(buf, FM_MAX_CLASS, "%s.%s",
28780Sstevel@tonic-gate 		    pbm_err->pbm_bridge_type, pbm_err->pbm_err_class);
28790Sstevel@tonic-gate 
28800Sstevel@tonic-gate 	ena = ena ? ena : fm_ena_generate(0, FM_ENA_FMT1);
28810Sstevel@tonic-gate 
28820Sstevel@tonic-gate 	DEBUG1(DBG_ERR_INTR, dip, "iommu_ereport_post: ereport_set: %s", buf);
28830Sstevel@tonic-gate 
28840Sstevel@tonic-gate 	ddi_fm_ereport_post(dip, buf, ena, DDI_NOSLEEP,
28850Sstevel@tonic-gate 	    FM_VERSION, DATA_TYPE_UINT8, 0,
28860Sstevel@tonic-gate 	    PCI_CONFIG_STATUS, DATA_TYPE_UINT16, pbm_err->pbm_pci.pci_cfg_stat,
28870Sstevel@tonic-gate 	    PCI_CONFIG_COMMAND, DATA_TYPE_UINT16, pbm_err->pbm_pci.pci_cfg_comm,
28880Sstevel@tonic-gate 	    PCI_PBM_CSR, DATA_TYPE_UINT64, pbm_err->pbm_ctl_stat,
28890Sstevel@tonic-gate 	    PCI_PBM_IOMMU_CTRL, DATA_TYPE_UINT64, pbm_err->pbm_iommu.iommu_stat,
28900Sstevel@tonic-gate 	    PCI_PBM_IOMMU_TFAR, DATA_TYPE_UINT64, pbm_err->pbm_iommu.iommu_tfar,
28910Sstevel@tonic-gate 	    PCI_PBM_SLOT, DATA_TYPE_UINT64, pbm_err->pbm_err_sl,
28920Sstevel@tonic-gate 	    PCI_PBM_VALOG, DATA_TYPE_UINT64, pbm_err->pbm_va_log,
28930Sstevel@tonic-gate 	    NULL);
28940Sstevel@tonic-gate }
28950Sstevel@tonic-gate 
28960Sstevel@tonic-gate /*
28970Sstevel@tonic-gate  * Function used to post PCI-X generic ereports.
28980Sstevel@tonic-gate  * This function needs to be fixed once the Fault Boundary Analysis
28990Sstevel@tonic-gate  * for PCI-X is conducted. The payload should be made more generic.
29000Sstevel@tonic-gate  */
29010Sstevel@tonic-gate static void
29020Sstevel@tonic-gate pcix_ereport_post(dev_info_t *dip, uint64_t ena, pbm_errstate_t *pbm_err)
29030Sstevel@tonic-gate {
29040Sstevel@tonic-gate 	char buf[FM_MAX_CLASS];
29050Sstevel@tonic-gate 
29061865Sdilpreet 	(void) snprintf(buf, FM_MAX_CLASS, "%s.%s",
29071865Sdilpreet 		    pbm_err->pbm_bridge_type, pbm_err->pbm_err_class);
29081865Sdilpreet 
29090Sstevel@tonic-gate 	ena = ena ? ena : fm_ena_generate(0, FM_ENA_FMT1);
29100Sstevel@tonic-gate 
29110Sstevel@tonic-gate 	DEBUG1(DBG_ERR_INTR, dip, "pcix_ereport_post: ereport_post: %s", buf);
29120Sstevel@tonic-gate 
29131865Sdilpreet 	ddi_fm_ereport_post(dip, buf, ena, DDI_NOSLEEP,
29140Sstevel@tonic-gate 	    FM_VERSION, DATA_TYPE_UINT8, 0,
29150Sstevel@tonic-gate 	    PCI_CONFIG_STATUS, DATA_TYPE_UINT16, pbm_err->pbm_pci.pci_cfg_stat,
29160Sstevel@tonic-gate 	    PCI_CONFIG_COMMAND, DATA_TYPE_UINT16, pbm_err->pbm_pci.pci_cfg_comm,
29170Sstevel@tonic-gate 	    PCI_PBM_CSR, DATA_TYPE_UINT64, pbm_err->pbm_ctl_stat,
29180Sstevel@tonic-gate 	    PCI_PBM_AFSR, DATA_TYPE_UINT64, pbm_err->pbm_afsr,
29190Sstevel@tonic-gate 	    PCI_PBM_AFAR, DATA_TYPE_UINT64, pbm_err->pbm_afar,
29200Sstevel@tonic-gate 	    PCI_PBM_SLOT, DATA_TYPE_UINT64, pbm_err->pbm_err_sl,
29210Sstevel@tonic-gate 	    PCIX_STAT, DATA_TYPE_UINT64, pbm_err->pbm_pcix_stat,
29220Sstevel@tonic-gate 	    PCIX_PFAR, DATA_TYPE_UINT32, pbm_err->pbm_pcix_pfar,
29230Sstevel@tonic-gate 	    NULL);
29240Sstevel@tonic-gate }
29250Sstevel@tonic-gate 
29260Sstevel@tonic-gate static void
29270Sstevel@tonic-gate iommu_ctx_free(iommu_t *iommu_p)
29280Sstevel@tonic-gate {
29290Sstevel@tonic-gate 	kmem_free(iommu_p->iommu_ctx_bitmap, IOMMU_CTX_BITMAP_SIZE);
29300Sstevel@tonic-gate }
29310Sstevel@tonic-gate 
29320Sstevel@tonic-gate /*
29330Sstevel@tonic-gate  * iommu_tlb_scrub():
29340Sstevel@tonic-gate  *	Exam TLB entries through TLB diagnostic registers and look for errors.
29350Sstevel@tonic-gate  *	scrub = 1 : cleanup all error bits in tlb, called in FAULT_RESET case
29360Sstevel@tonic-gate  *	scrub = 0 : log all error conditions to console, FAULT_LOG case
29370Sstevel@tonic-gate  *	In both cases, it returns number of errors found in tlb entries.
29380Sstevel@tonic-gate  */
29390Sstevel@tonic-gate static int
29400Sstevel@tonic-gate iommu_tlb_scrub(iommu_t *iommu_p, int scrub)
29410Sstevel@tonic-gate {
29420Sstevel@tonic-gate 	int i, nerr = 0;
29430Sstevel@tonic-gate 	dev_info_t *dip = iommu_p->iommu_pci_p->pci_dip;
29440Sstevel@tonic-gate 	char *neg = "not ";
29450Sstevel@tonic-gate 
29460Sstevel@tonic-gate 	uint64_t base = (uint64_t)iommu_p->iommu_ctrl_reg -
29470Sstevel@tonic-gate 		COMMON_IOMMU_CTRL_REG_OFFSET;
29480Sstevel@tonic-gate 
29490Sstevel@tonic-gate 	volatile uint64_t *tlb_tag = (volatile uint64_t *)
29500Sstevel@tonic-gate 		(base + COMMON_IOMMU_TLB_TAG_DIAG_ACC_OFFSET);
29510Sstevel@tonic-gate 	volatile uint64_t *tlb_data = (volatile uint64_t *)
29520Sstevel@tonic-gate 		(base + COMMON_IOMMU_TLB_DATA_DIAG_ACC_OFFSET);
29530Sstevel@tonic-gate 	for (i = 0; i < IOMMU_TLB_ENTRIES; i++) {
29540Sstevel@tonic-gate 		uint64_t tag = tlb_tag[i];
29550Sstevel@tonic-gate 		uint64_t data = tlb_data[i];
29560Sstevel@tonic-gate 		uint32_t errstat;
29570Sstevel@tonic-gate 		iopfn_t pfn;
29580Sstevel@tonic-gate 
29590Sstevel@tonic-gate 		if (!(tag & TLBTAG_ERR_BIT))
29600Sstevel@tonic-gate 			continue;
29610Sstevel@tonic-gate 
29620Sstevel@tonic-gate 		pfn = (iopfn_t)(data & TLBDATA_MEMPA_BITS);
29630Sstevel@tonic-gate 		errstat = (uint32_t)
29640Sstevel@tonic-gate 			((tag & TLBTAG_ERRSTAT_BITS) >> TLBTAG_ERRSTAT_SHIFT);
29650Sstevel@tonic-gate 		if (errstat == TLBTAG_ERRSTAT_INVALID) {
29660Sstevel@tonic-gate 			if (scrub)
29670Sstevel@tonic-gate 				tlb_tag[i] = tlb_data[i] = 0ull;
29680Sstevel@tonic-gate 		} else
29690Sstevel@tonic-gate 			nerr++;
29700Sstevel@tonic-gate 
29710Sstevel@tonic-gate 		if (scrub)
29720Sstevel@tonic-gate 			continue;
29730Sstevel@tonic-gate 
29740Sstevel@tonic-gate 		cmn_err(CE_CONT, "%s%d: Error %x on IOMMU TLB entry %x:\n"
2975946Smathue 		"\tContext=%lx %sWritable %sStreamable\n"
2976946Smathue 		"\tPCI Page Size=%sk Address in page %lx\n",
29770Sstevel@tonic-gate 			ddi_driver_name(dip), ddi_get_instance(dip), errstat, i,
29780Sstevel@tonic-gate 			(tag & TLBTAG_CONTEXT_BITS) >> TLBTAG_CONTEXT_SHIFT,
29790Sstevel@tonic-gate 			(tag & TLBTAG_WRITABLE_BIT) ? "" : neg,
29800Sstevel@tonic-gate 			(tag & TLBTAG_STREAM_BIT) ? "" : neg,
29810Sstevel@tonic-gate 			(tag & TLBTAG_PGSIZE_BIT) ? "64" : "8",
29820Sstevel@tonic-gate 			(tag & TLBTAG_PCIVPN_BITS) << 13);
2983946Smathue 		cmn_err(CE_CONT, "Memory: %sValid %sCacheable Page Frame=%lx\n",
29840Sstevel@tonic-gate 			(data & TLBDATA_VALID_BIT) ? "" : neg,
29850Sstevel@tonic-gate 			(data & TLBDATA_CACHE_BIT) ? "" : neg, pfn);
29860Sstevel@tonic-gate 	}
29870Sstevel@tonic-gate 	return (nerr);
29880Sstevel@tonic-gate }
29890Sstevel@tonic-gate 
29900Sstevel@tonic-gate /*
29910Sstevel@tonic-gate  * pci_iommu_disp: calculates the displacement needed in tomatillo's
29920Sstevel@tonic-gate  *	iommu control register and modifies the control value template
29930Sstevel@tonic-gate  *	from caller. It also clears any error status bit that are new
29940Sstevel@tonic-gate  *	in tomatillo.
29950Sstevel@tonic-gate  * return value: an 8-bit mask to enable corresponding 512 MB segments
29960Sstevel@tonic-gate  *	suitable for tomatillo's target address register.
29970Sstevel@tonic-gate  *	0x00: no programming is needed, use existing value from prom
29980Sstevel@tonic-gate  *	0x60: use segment 5 and 6 to form a 1GB dvma range
29990Sstevel@tonic-gate  */
30000Sstevel@tonic-gate static uint64_t
30010Sstevel@tonic-gate pci_iommu_disp(iommu_t *iommu_p, uint64_t *ctl_p)
30020Sstevel@tonic-gate {
30030Sstevel@tonic-gate 	uint64_t ctl_old;
30040Sstevel@tonic-gate 	if (CHIP_TYPE(iommu_p->iommu_pci_p) != PCI_CHIP_TOMATILLO)
30050Sstevel@tonic-gate 		return (0);
30060Sstevel@tonic-gate 
30070Sstevel@tonic-gate 	ctl_old = *iommu_p->iommu_ctrl_reg;
30080Sstevel@tonic-gate 	/* iommu ctrl reg error bits are W1C */
30090Sstevel@tonic-gate 	if (ctl_old >> TOMATIILO_IOMMU_ERR_REG_SHIFT) {
3010946Smathue 		cmn_err(CE_WARN, "Tomatillo iommu err: %lx", ctl_old);
30110Sstevel@tonic-gate 		*ctl_p |= (ctl_old >> TOMATIILO_IOMMU_ERR_REG_SHIFT)
30120Sstevel@tonic-gate 		    << TOMATIILO_IOMMU_ERR_REG_SHIFT;
30130Sstevel@tonic-gate 	}
30140Sstevel@tonic-gate 
30150Sstevel@tonic-gate 	if (iommu_p->iommu_tsb_size != TOMATILLO_IOMMU_TSB_MAX)
30160Sstevel@tonic-gate 		return (0);
30170Sstevel@tonic-gate 
30180Sstevel@tonic-gate 	/* Tomatillo 2.0 and later, and 1GB DVMA range */
30190Sstevel@tonic-gate 	*ctl_p |= 1 << TOMATILLO_IOMMU_SEG_DISP_SHIFT;
30200Sstevel@tonic-gate 	return (3 << (iommu_p->iommu_dvma_base >> (32 - 3)));
30210Sstevel@tonic-gate }
30220Sstevel@tonic-gate 
30230Sstevel@tonic-gate void
30240Sstevel@tonic-gate pci_iommu_config(iommu_t *iommu_p, uint64_t iommu_ctl, uint64_t cfgpa)
30250Sstevel@tonic-gate {
30260Sstevel@tonic-gate 	uintptr_t pbm_regbase = get_pbm_reg_base(iommu_p->iommu_pci_p);
30270Sstevel@tonic-gate 	volatile uint64_t *pbm_csr_p = (volatile uint64_t *)pbm_regbase;
30280Sstevel@tonic-gate 	volatile uint64_t *tgt_space_p = (volatile uint64_t *)(pbm_regbase |
30290Sstevel@tonic-gate 		(TOMATILLO_TGT_ADDR_SPACE_OFFSET - SCHIZO_PCI_CTRL_REG_OFFSET));
30300Sstevel@tonic-gate 	volatile uint64_t pbm_ctl = *pbm_csr_p;
30310Sstevel@tonic-gate 
30320Sstevel@tonic-gate 	volatile uint64_t *iommu_ctl_p = iommu_p->iommu_ctrl_reg;
30330Sstevel@tonic-gate 	volatile uint64_t tsb_bar_val = iommu_p->iommu_tsb_paddr;
30340Sstevel@tonic-gate 	volatile uint64_t *tsb_bar_p = iommu_p->iommu_tsb_base_addr_reg;
30350Sstevel@tonic-gate 	uint64_t mask = pci_iommu_disp(iommu_p, &iommu_ctl);
30360Sstevel@tonic-gate 
30370Sstevel@tonic-gate 	DEBUG2(DBG_ATTACH, iommu_p->iommu_pci_p->pci_dip,
30380Sstevel@tonic-gate 		"\npci_iommu_config: pbm_csr_p=%llx pbm_ctl=%llx",
30390Sstevel@tonic-gate 		pbm_csr_p, pbm_ctl);
30400Sstevel@tonic-gate 	DEBUG2(DBG_ATTACH|DBG_CONT, iommu_p->iommu_pci_p->pci_dip,
30410Sstevel@tonic-gate 		"\n\tiommu_ctl_p=%llx iommu_ctl=%llx",
30420Sstevel@tonic-gate 		iommu_ctl_p, iommu_ctl);
30430Sstevel@tonic-gate 	DEBUG4(DBG_ATTACH|DBG_CONT, iommu_p->iommu_pci_p->pci_dip,
30440Sstevel@tonic-gate 		"\n\tcfgpa=%llx tgt_space_p=%llx mask=%x tsb=%llx\n",
30450Sstevel@tonic-gate 		cfgpa, tgt_space_p, mask, tsb_bar_val);
30460Sstevel@tonic-gate 
30470Sstevel@tonic-gate 	if (!cfgpa)
30480Sstevel@tonic-gate 		goto reprog;
30490Sstevel@tonic-gate 
30500Sstevel@tonic-gate 	/* disable PBM arbiters - turn off bits 0-7 */
30510Sstevel@tonic-gate 	*pbm_csr_p = (pbm_ctl >> 8) << 8;
30520Sstevel@tonic-gate 
30530Sstevel@tonic-gate 	/*
30540Sstevel@tonic-gate 	 * For non-XMITS, flush any previous writes. This is only
30550Sstevel@tonic-gate 	 * necessary for host bridges that may have a USB keywboard
30560Sstevel@tonic-gate 	 * attached.  XMITS does not.
30570Sstevel@tonic-gate 	 */
30580Sstevel@tonic-gate 	if (!(CHIP_TYPE(iommu_p->iommu_pci_p) == PCI_CHIP_XMITS))
30590Sstevel@tonic-gate 		(void) ldphysio(cfgpa);
30600Sstevel@tonic-gate 
30610Sstevel@tonic-gate reprog:
30620Sstevel@tonic-gate 	if (mask)
30630Sstevel@tonic-gate 		*tgt_space_p = mask;
30640Sstevel@tonic-gate 
30650Sstevel@tonic-gate 	*tsb_bar_p = tsb_bar_val;
30660Sstevel@tonic-gate 	*iommu_ctl_p = iommu_ctl;
30670Sstevel@tonic-gate 
30680Sstevel@tonic-gate 	*pbm_csr_p = pbm_ctl;	/* re-enable bus arbitration */
30690Sstevel@tonic-gate 	pbm_ctl = *pbm_csr_p;	/* flush all prev writes */
30700Sstevel@tonic-gate }
30710Sstevel@tonic-gate 
30720Sstevel@tonic-gate 
30730Sstevel@tonic-gate int
30740Sstevel@tonic-gate pci_get_portid(dev_info_t *dip)
30750Sstevel@tonic-gate {
30760Sstevel@tonic-gate 	return (ddi_getprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
30770Sstevel@tonic-gate 	    "portid", -1));
30780Sstevel@tonic-gate }
30790Sstevel@tonic-gate 
30800Sstevel@tonic-gate /*
30810Sstevel@tonic-gate  * Schizo Safari Performance Events.
30820Sstevel@tonic-gate  */
30830Sstevel@tonic-gate pci_kev_mask_t
30840Sstevel@tonic-gate schizo_saf_events[] = {
30850Sstevel@tonic-gate 	{"saf_bus_cycles", 0x1},	{"saf_pause_asserted_cycles", 0x2},
30860Sstevel@tonic-gate 	{"saf_frn_coherent_cmds", 0x3},	{"saf_frn_coherent_hits", 0x4},
30870Sstevel@tonic-gate 	{"saf_my_coherent_cmds", 0x5},	{"saf_my_coherent_hits", 0x6},
30880Sstevel@tonic-gate 	{"saf_frn_io_cmds", 0x7}, 	{"saf_frn_io_hits", 0x8},
30890Sstevel@tonic-gate 	{"merge_buffer", 0x9}, 		{"interrupts", 0xa},
30900Sstevel@tonic-gate 	{"csr_pios", 0xc}, 		{"upa_pios", 0xd},
30910Sstevel@tonic-gate 	{"pcia_pios", 0xe}, 		{"pcib_pios", 0xf},
30920Sstevel@tonic-gate 	{"saf_pause_seen_cycles", 0x11}, 	{"dvma_reads", 0x12},
30930Sstevel@tonic-gate 	{"dvma_writes", 0x13},		{"saf_orq_full_cycles", 0x14},
30940Sstevel@tonic-gate 	{"saf_data_in_cycles", 0x15},	{"saf_data_out_cycles", 0x16},
30950Sstevel@tonic-gate 	{"clear_pic", 0x1f}
30960Sstevel@tonic-gate };
30970Sstevel@tonic-gate 
30980Sstevel@tonic-gate 
30990Sstevel@tonic-gate /*
31000Sstevel@tonic-gate  * Schizo PCI Performance Events.
31010Sstevel@tonic-gate  */
31020Sstevel@tonic-gate pci_kev_mask_t
31030Sstevel@tonic-gate schizo_pci_events[] = {
31040Sstevel@tonic-gate 	{"dvma_stream_rd", 0x0}, 	{"dvma_stream_wr", 0x1},
31050Sstevel@tonic-gate 	{"dvma_const_rd", 0x2},		{"dvma_const_wr", 0x3},
31060Sstevel@tonic-gate 	{"dvma_stream_buf_mis", 0x4},	{"dvma_cycles", 0x5},
31070Sstevel@tonic-gate 	{"dvma_wd_xfr", 0x6},		{"pio_cycles", 0x7},
31080Sstevel@tonic-gate 	{"dvma_tlb_misses", 0x10},	{"interrupts", 0x11},
31090Sstevel@tonic-gate 	{"saf_inter_nack", 0x12},	{"pio_reads", 0x13},
31100Sstevel@tonic-gate 	{"pio_writes", 0x14},		{"dvma_rd_buf_timeout", 0x15},
31110Sstevel@tonic-gate 	{"dvma_rd_rtry_stc", 0x16},	{"dvma_wr_rtry_stc", 0x17},
31120Sstevel@tonic-gate 	{"dvma_rd_rtry_nonstc", 0x18},	{"dvma_wr_rtry_nonstc", 0x19},
31130Sstevel@tonic-gate 	{"E*_slow_transitions", 0x1a},	{"E*_slow_cycles_per_64", 0x1b},
31140Sstevel@tonic-gate 	{"clear_pic", 0x1f}
31150Sstevel@tonic-gate };
31160Sstevel@tonic-gate 
31170Sstevel@tonic-gate 
31180Sstevel@tonic-gate /*
31190Sstevel@tonic-gate  * Create the picN kstats for the pci
31200Sstevel@tonic-gate  * and safari events.
31210Sstevel@tonic-gate  */
31220Sstevel@tonic-gate void
31230Sstevel@tonic-gate pci_kstat_init()
31240Sstevel@tonic-gate {
31250Sstevel@tonic-gate 	pci_name_kstat = (pci_ksinfo_t *)kmem_alloc(sizeof (pci_ksinfo_t),
31260Sstevel@tonic-gate 		KM_NOSLEEP);
31270Sstevel@tonic-gate 
31280Sstevel@tonic-gate 	if (pci_name_kstat == NULL) {
31290Sstevel@tonic-gate 		cmn_err(CE_WARN, "pcisch : no space for kstat\n");
31300Sstevel@tonic-gate 	} else {
31310Sstevel@tonic-gate 		pci_name_kstat->pic_no_evs =
31320Sstevel@tonic-gate 			sizeof (schizo_pci_events) / sizeof (pci_kev_mask_t);
31330Sstevel@tonic-gate 		pci_name_kstat->pic_shift[0] = SCHIZO_SHIFT_PIC0;
31340Sstevel@tonic-gate 		pci_name_kstat->pic_shift[1] = SCHIZO_SHIFT_PIC1;
31350Sstevel@tonic-gate 		pci_create_name_kstat("pcis",
31360Sstevel@tonic-gate 			pci_name_kstat, schizo_pci_events);
31370Sstevel@tonic-gate 	}
31380Sstevel@tonic-gate 
31390Sstevel@tonic-gate 	saf_name_kstat = (pci_ksinfo_t *)kmem_alloc(sizeof (pci_ksinfo_t),
31400Sstevel@tonic-gate 		KM_NOSLEEP);
31410Sstevel@tonic-gate 	if (saf_name_kstat == NULL) {
31420Sstevel@tonic-gate 		cmn_err(CE_WARN, "pcisch : no space for kstat\n");
31430Sstevel@tonic-gate 	} else {
31440Sstevel@tonic-gate 		saf_name_kstat->pic_no_evs =
31450Sstevel@tonic-gate 			sizeof (schizo_saf_events) / sizeof (pci_kev_mask_t);
31460Sstevel@tonic-gate 		saf_name_kstat->pic_shift[0] = SCHIZO_SHIFT_PIC0;
31470Sstevel@tonic-gate 		saf_name_kstat->pic_shift[1] = SCHIZO_SHIFT_PIC1;
31480Sstevel@tonic-gate 		pci_create_name_kstat("saf", saf_name_kstat, schizo_saf_events);
31490Sstevel@tonic-gate 	}
31500Sstevel@tonic-gate }
31510Sstevel@tonic-gate 
31520Sstevel@tonic-gate void
31530Sstevel@tonic-gate pci_kstat_fini()
31540Sstevel@tonic-gate {
31550Sstevel@tonic-gate 	if (pci_name_kstat != NULL) {
31560Sstevel@tonic-gate 		pci_delete_name_kstat(pci_name_kstat);
31570Sstevel@tonic-gate 		kmem_free(pci_name_kstat, sizeof (pci_ksinfo_t));
31580Sstevel@tonic-gate 		pci_name_kstat = NULL;
31590Sstevel@tonic-gate 	}
31600Sstevel@tonic-gate 
31610Sstevel@tonic-gate 	if (saf_name_kstat != NULL) {
31620Sstevel@tonic-gate 		pci_delete_name_kstat(saf_name_kstat);
31630Sstevel@tonic-gate 		kmem_free(saf_name_kstat, sizeof (pci_ksinfo_t));
31640Sstevel@tonic-gate 		saf_name_kstat = NULL;
31650Sstevel@tonic-gate 	}
31660Sstevel@tonic-gate }
31670Sstevel@tonic-gate 
31680Sstevel@tonic-gate /*
31690Sstevel@tonic-gate  * Create 'counters' kstat for pci events.
31700Sstevel@tonic-gate  */
31710Sstevel@tonic-gate void
31720Sstevel@tonic-gate pci_add_pci_kstat(pci_t *pci_p)
31730Sstevel@tonic-gate {
31740Sstevel@tonic-gate 	pci_cntr_addr_t *cntr_addr_p = &pci_p->pci_ks_addr;
31750Sstevel@tonic-gate 	uintptr_t regbase = (uintptr_t)pci_p->pci_address[0];
31760Sstevel@tonic-gate 
31770Sstevel@tonic-gate 	cntr_addr_p->pcr_addr = (uint64_t *)
31780Sstevel@tonic-gate 		(regbase + SCHIZO_PERF_PCI_PCR_OFFSET);
31790Sstevel@tonic-gate 	cntr_addr_p->pic_addr = (uint64_t *)
31800Sstevel@tonic-gate 		(regbase + SCHIZO_PERF_PCI_PIC_OFFSET);
31810Sstevel@tonic-gate 
31820Sstevel@tonic-gate 	pci_p->pci_ksp = pci_create_cntr_kstat(pci_p, "pcis",
31830Sstevel@tonic-gate 		NUM_OF_PICS, pci_cntr_kstat_update, cntr_addr_p);
31840Sstevel@tonic-gate 
31850Sstevel@tonic-gate 	if (pci_p->pci_ksp == NULL) {
31860Sstevel@tonic-gate 		cmn_err(CE_WARN, "pcisch : cannot create counter kstat");
31870Sstevel@tonic-gate 	}
31880Sstevel@tonic-gate }
31890Sstevel@tonic-gate 
31900Sstevel@tonic-gate void
31910Sstevel@tonic-gate pci_rem_pci_kstat(pci_t *pci_p)
31920Sstevel@tonic-gate {
31930Sstevel@tonic-gate 	if (pci_p->pci_ksp != NULL)
31940Sstevel@tonic-gate 		kstat_delete(pci_p->pci_ksp);
31950Sstevel@tonic-gate 	pci_p->pci_ksp = NULL;
31960Sstevel@tonic-gate }
31970Sstevel@tonic-gate 
31980Sstevel@tonic-gate void
31990Sstevel@tonic-gate pci_add_upstream_kstat(pci_t *pci_p)
32000Sstevel@tonic-gate {
32010Sstevel@tonic-gate 	pci_common_t	*cmn_p = pci_p->pci_common_p;
32020Sstevel@tonic-gate 	pci_cntr_pa_t	*cntr_pa_p = &cmn_p->pci_cmn_uks_pa;
32030Sstevel@tonic-gate 	uint64_t regbase = va_to_pa(pci_p->pci_address[1]);
32040Sstevel@tonic-gate 
32050Sstevel@tonic-gate 	cntr_pa_p->pcr_pa =
32060Sstevel@tonic-gate 		regbase + SCHIZO_PERF_SAF_PCR_OFFSET;
32070Sstevel@tonic-gate 	cntr_pa_p->pic_pa =
32080Sstevel@tonic-gate 		regbase + SCHIZO_PERF_SAF_PIC_OFFSET;
32090Sstevel@tonic-gate 
32100Sstevel@tonic-gate 	cmn_p->pci_common_uksp = pci_create_cntr_kstat(pci_p, "saf",
32110Sstevel@tonic-gate 		NUM_OF_PICS, pci_cntr_kstat_pa_update, cntr_pa_p);
32120Sstevel@tonic-gate }
32130Sstevel@tonic-gate 
32140Sstevel@tonic-gate /*
32150Sstevel@tonic-gate  * Extract the drivers binding name to identify which chip
32160Sstevel@tonic-gate  * we're binding to.  Whenever a new bus bridge is created, the driver alias
32170Sstevel@tonic-gate  * entry should be added here to identify the device if needed.  If a device
32180Sstevel@tonic-gate  * isn't added, the identity defaults to PCI_CHIP_UNIDENTIFIED.
32190Sstevel@tonic-gate  */
32200Sstevel@tonic-gate static uint32_t
32210Sstevel@tonic-gate pci_identity_init(pci_t *pci_p)
32220Sstevel@tonic-gate {
32230Sstevel@tonic-gate 	dev_info_t *dip = pci_p->pci_dip;
32240Sstevel@tonic-gate 	char *name = ddi_binding_name(dip);
32250Sstevel@tonic-gate 	uint32_t ver = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
32260Sstevel@tonic-gate 		"version#", 0);
32270Sstevel@tonic-gate 
32280Sstevel@tonic-gate 	if (strcmp(name, "pci108e,a801") == 0)
32290Sstevel@tonic-gate 		return (CHIP_ID(PCI_CHIP_TOMATILLO, ver, 0x00));
32300Sstevel@tonic-gate 
32310Sstevel@tonic-gate 	if (strcmp(name, "pci108e,8001") == 0)
32320Sstevel@tonic-gate 		return (CHIP_ID(PCI_CHIP_SCHIZO, ver, 0x00));
32330Sstevel@tonic-gate 
32340Sstevel@tonic-gate 	if (strcmp(name, "pci108e,8002") == 0) {
32350Sstevel@tonic-gate 		uint32_t mod_rev = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
32360Sstevel@tonic-gate 			DDI_PROP_DONTPASS, "module-revision#", 0);
32370Sstevel@tonic-gate 		return (CHIP_ID(PCI_CHIP_XMITS, ver, mod_rev));
32380Sstevel@tonic-gate 	}
32390Sstevel@tonic-gate 
32400Sstevel@tonic-gate 	cmn_err(CE_WARN, "%s%d: Unknown PCI Host bridge %s %x\n",
32410Sstevel@tonic-gate 		ddi_driver_name(dip), ddi_get_instance(dip), name, ver);
32420Sstevel@tonic-gate 
32430Sstevel@tonic-gate 	return (PCI_CHIP_UNIDENTIFIED);
32440Sstevel@tonic-gate }
32450Sstevel@tonic-gate 
32460Sstevel@tonic-gate /*
32470Sstevel@tonic-gate  * Setup a physical pointer to one leaf config space area. This
32480Sstevel@tonic-gate  * is used in several places in order to do a dummy read which
32490Sstevel@tonic-gate  * guarantees the nexus (and not a bus master) has gained control
32500Sstevel@tonic-gate  * of the bus.
32510Sstevel@tonic-gate  */
32520Sstevel@tonic-gate static void
32530Sstevel@tonic-gate pci_setup_cfgpa(pci_t *pci_p)
32540Sstevel@tonic-gate {
32550Sstevel@tonic-gate 	dev_info_t *dip = pci_p->pci_dip;
32560Sstevel@tonic-gate 	dev_info_t *cdip;
32570Sstevel@tonic-gate 	pbm_t *pbm_p = pci_p->pci_pbm_p;
32580Sstevel@tonic-gate 	uint64_t cfgpa = pci_get_cfg_pabase(pci_p);
32590Sstevel@tonic-gate 	uint32_t *reg_p;
32600Sstevel@tonic-gate 	int reg_len;
32610Sstevel@tonic-gate 
32620Sstevel@tonic-gate 	for (cdip = ddi_get_child(dip); cdip != NULL;
32630Sstevel@tonic-gate 	    cdip = ddi_get_next_sibling(cdip)) {
3264506Scth 		if (ddi_getlongprop(DDI_DEV_T_ANY, cdip, DDI_PROP_DONTPASS,
32650Sstevel@tonic-gate 		    "reg", (caddr_t)&reg_p, &reg_len) != DDI_PROP_SUCCESS)
32660Sstevel@tonic-gate 			continue;
32670Sstevel@tonic-gate 		cfgpa += (*reg_p) & (PCI_CONF_ADDR_MASK ^ PCI_REG_REG_M);
32680Sstevel@tonic-gate 		kmem_free(reg_p, reg_len);
32690Sstevel@tonic-gate 		break;
32700Sstevel@tonic-gate 	}
32710Sstevel@tonic-gate 	pbm_p->pbm_anychild_cfgpa = cfgpa;
32720Sstevel@tonic-gate }
32730Sstevel@tonic-gate 
32740Sstevel@tonic-gate void
32750Sstevel@tonic-gate pci_post_init_child(pci_t *pci_p, dev_info_t *child)
32760Sstevel@tonic-gate {
32770Sstevel@tonic-gate 	volatile uint64_t *ctrl_reg_p;
32780Sstevel@tonic-gate 	pbm_t *pbm_p = pci_p->pci_pbm_p;
32790Sstevel@tonic-gate 
32800Sstevel@tonic-gate 	pci_setup_cfgpa(pci_p);
32810Sstevel@tonic-gate 
32820Sstevel@tonic-gate 	/*
32830Sstevel@tonic-gate 	 * This is a hack for skyhawk/casinni combination to address
32840Sstevel@tonic-gate 	 * hardware problems between the request and grant signals which
32850Sstevel@tonic-gate 	 * causes a bus hang.  One workaround, which is applied here,
32860Sstevel@tonic-gate 	 * is to disable bus parking if the child contains the property
32870Sstevel@tonic-gate 	 * pci-req-removal.  Note that if the bus is quiesced we must mask
32880Sstevel@tonic-gate 	 * off the parking bit in the saved control registers, since the
32890Sstevel@tonic-gate 	 * quiesce operation temporarily turns off PCI bus parking.
32900Sstevel@tonic-gate 	 */
32910Sstevel@tonic-gate 	if (ddi_prop_exists(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
32920Sstevel@tonic-gate 		"pci-req-removal") == 1) {
32930Sstevel@tonic-gate 
32940Sstevel@tonic-gate 		if (pbm_p->pbm_quiesce_count > 0) {
32950Sstevel@tonic-gate 			pbm_p->pbm_saved_ctrl_reg &= ~SCHIZO_PCI_CTRL_ARB_PARK;
32960Sstevel@tonic-gate 		} else {
32970Sstevel@tonic-gate 			ctrl_reg_p = pbm_p->pbm_ctrl_reg;
32980Sstevel@tonic-gate 			*ctrl_reg_p &= ~SCHIZO_PCI_CTRL_ARB_PARK;
32990Sstevel@tonic-gate 		}
33000Sstevel@tonic-gate 	}
33010Sstevel@tonic-gate 
33020Sstevel@tonic-gate 	if (CHIP_TYPE(pci_p) == PCI_CHIP_XMITS) {
33030Sstevel@tonic-gate 		if (*pbm_p->pbm_ctrl_reg & XMITS_PCI_CTRL_X_MODE) {
33040Sstevel@tonic-gate 			int value;
33050Sstevel@tonic-gate 
33060Sstevel@tonic-gate 			/*
33070Sstevel@tonic-gate 			 * Due to a XMITS bug, we need to set the outstanding
33080Sstevel@tonic-gate 			 * split transactions to 1 for all PCI-X functions
33090Sstevel@tonic-gate 			 * behind the leaf.
33100Sstevel@tonic-gate 			 */
33110Sstevel@tonic-gate 			value = (xmits_max_transactions << 4) |
33120Sstevel@tonic-gate 			    (xmits_max_read_bytes << 2);
33130Sstevel@tonic-gate 
33140Sstevel@tonic-gate 			DEBUG1(DBG_INIT_CLD, child, "Turning on XMITS NCPQ "
33150Sstevel@tonic-gate 			    "Workaround: value = %x\n", value);
33160Sstevel@tonic-gate 
33170Sstevel@tonic-gate 			pcix_set_cmd_reg(child, value);
33180Sstevel@tonic-gate 
33190Sstevel@tonic-gate 			(void) ndi_prop_update_int(DDI_DEV_T_NONE,
33200Sstevel@tonic-gate 			    child, "pcix-update-cmd-reg", value);
33210Sstevel@tonic-gate 		}
3322*2358Sdanice 
3323*2358Sdanice 		if (PCI_CHIP_ID(pci_p) >= XMITS_VER_30) {
3324*2358Sdanice 			uint64_t *pbm_pcix_diag_reg =
3325*2358Sdanice 			    (uint64_t *)(pci_p->pci_address[0] +
3326*2358Sdanice 			    XMITS_PCI_X_DIAG_REG_OFFSET);
3327*2358Sdanice 			uint64_t bugcntl = (*pbm_pcix_diag_reg >>
3328*2358Sdanice 			    XMITS_PCI_X_DIAG_BUGCNTL_SHIFT) &
3329*2358Sdanice 			    XMITS_PCI_X_DIAG_BUGCNTL_MASK;
3330*2358Sdanice 			uint64_t tunable = (*pbm_p->pbm_ctrl_reg &
3331*2358Sdanice 			    XMITS_PCI_CTRL_X_MODE ?
3332*2358Sdanice 			    xmits_pcix_diag_bugcntl_pcix :
3333*2358Sdanice 			    xmits_pcix_diag_bugcntl_pci)
3334*2358Sdanice 			    & XMITS_PCI_X_DIAG_BUGCNTL_MASK;
3335*2358Sdanice 
3336*2358Sdanice 			DEBUG4(DBG_INIT_CLD, pci_p->pci_dip, "%s: XMITS "
3337*2358Sdanice 			    "pcix diag bugcntl=0x%lx, tunable=0x%lx, mode=%s\n",
3338*2358Sdanice 			    ddi_driver_name(child), bugcntl, tunable,
3339*2358Sdanice 			    ((*pbm_p->pbm_ctrl_reg & XMITS_PCI_CTRL_X_MODE)?
3340*2358Sdanice 			    "PCI-X":"PCI"));
3341*2358Sdanice 
3342*2358Sdanice 			DEBUG2(DBG_INIT_CLD, pci_p->pci_dip, "%s: XMITS "
3343*2358Sdanice 			    "pcix diag reg=0x%lx (CUR)\n",
3344*2358Sdanice 			    ddi_driver_name(child), *pbm_pcix_diag_reg);
3345*2358Sdanice 
3346*2358Sdanice 			/*
3347*2358Sdanice 			 * Due to a XMITS 3.x hw bug, we need to
3348*2358Sdanice 			 * read PBM's xmits pci ctrl status register to
3349*2358Sdanice 			 * determine mode (PCI or PCI-X) and then update
3350*2358Sdanice 			 * PBM's pcix diag register with new BUG_FIX_CNTL
3351*2358Sdanice 			 * bits (47:32) _if_ different from tunable's mode
3352*2358Sdanice 			 * based value. This update is performed only once
3353*2358Sdanice 			 * during the PBM's first child init.
3354*2358Sdanice 			 *
3355*2358Sdanice 			 * Per instructions from xmits hw engineering,
3356*2358Sdanice 			 * non-BUG_FIX_CNTL bits should not be preserved
3357*2358Sdanice 			 * when updating the pcix diag register. Such bits
3358*2358Sdanice 			 * should be written as 0s.
3359*2358Sdanice 			 */
3360*2358Sdanice 
3361*2358Sdanice 			if (bugcntl != tunable) {
3362*2358Sdanice 				*pbm_pcix_diag_reg = tunable <<
3363*2358Sdanice 				    XMITS_PCI_X_DIAG_BUGCNTL_SHIFT;
3364*2358Sdanice 
3365*2358Sdanice 				DEBUG2(DBG_INIT_CLD, pci_p->pci_dip, "%s: XMITS"
3366*2358Sdanice 				    " pcix diag reg=0x%lx (NEW)\n",
3367*2358Sdanice 				    ddi_driver_name(child), *pbm_pcix_diag_reg);
3368*2358Sdanice 			}
3369*2358Sdanice 		}
33700Sstevel@tonic-gate 	}
33710Sstevel@tonic-gate }
33720Sstevel@tonic-gate 
33730Sstevel@tonic-gate void
33740Sstevel@tonic-gate pci_post_uninit_child(pci_t *pci_p)
33750Sstevel@tonic-gate {
33760Sstevel@tonic-gate 	pci_setup_cfgpa(pci_p);
33770Sstevel@tonic-gate }
33780Sstevel@tonic-gate 
33790Sstevel@tonic-gate static int
33800Sstevel@tonic-gate pci_tom_nbintr_op(pci_t *pci_p, uint32_t inum, intrfunc f, caddr_t arg,
33810Sstevel@tonic-gate     int flag)
33820Sstevel@tonic-gate {
33830Sstevel@tonic-gate 	uint32_t ino = pci_p->pci_inos[inum];
33840Sstevel@tonic-gate 	uint32_t mondo = IB_INO_TO_NBMONDO(pci_p->pci_ib_p, ino);
33850Sstevel@tonic-gate 	int ret = DDI_SUCCESS;
33860Sstevel@tonic-gate 
33870Sstevel@tonic-gate 	mondo = CB_MONDO_TO_XMONDO(pci_p->pci_cb_p, mondo); /* no op on tom */
33880Sstevel@tonic-gate 
33890Sstevel@tonic-gate 	switch (flag) {
33900Sstevel@tonic-gate 	case PCI_OBJ_INTR_ADD:
33910Sstevel@tonic-gate 		VERIFY(add_ivintr(mondo, pci_pil[inum], f, arg, NULL) == 0);
33920Sstevel@tonic-gate 		break;
33930Sstevel@tonic-gate 	case PCI_OBJ_INTR_REMOVE:
33940Sstevel@tonic-gate 		rem_ivintr(mondo, NULL);
33950Sstevel@tonic-gate 		break;
33960Sstevel@tonic-gate 	default:
33970Sstevel@tonic-gate 		ret = DDI_FAILURE;
33980Sstevel@tonic-gate 		break;
33990Sstevel@tonic-gate 	}
34000Sstevel@tonic-gate 
34010Sstevel@tonic-gate 	return (ret);
34020Sstevel@tonic-gate }
34030Sstevel@tonic-gate 
34040Sstevel@tonic-gate int
34050Sstevel@tonic-gate pci_ecc_add_intr(pci_t *pci_p, int inum, ecc_intr_info_t *eii_p)
34060Sstevel@tonic-gate {
34070Sstevel@tonic-gate 	uint32_t mondo;
34080Sstevel@tonic-gate 	int	r;
34090Sstevel@tonic-gate 
34100Sstevel@tonic-gate 	mondo = ((pci_p->pci_cb_p->cb_ign << PCI_INO_BITS) |
34110Sstevel@tonic-gate 	    pci_p->pci_inos[inum]);
34120Sstevel@tonic-gate 	mondo = CB_MONDO_TO_XMONDO(pci_p->pci_cb_p, mondo);
34130Sstevel@tonic-gate 
34140Sstevel@tonic-gate 	VERIFY(add_ivintr(mondo, pci_pil[inum], ecc_intr,
34150Sstevel@tonic-gate 	    (caddr_t)eii_p, NULL) == 0);
34160Sstevel@tonic-gate 
34170Sstevel@tonic-gate 	if (CHIP_TYPE(pci_p) != PCI_CHIP_TOMATILLO)
34180Sstevel@tonic-gate 		return (PCI_ATTACH_RETCODE(PCI_ECC_OBJ, PCI_OBJ_INTR_ADD,
34190Sstevel@tonic-gate 		    DDI_SUCCESS));
34200Sstevel@tonic-gate 
34210Sstevel@tonic-gate 	r = pci_tom_nbintr_op(pci_p, inum, ecc_intr,
34220Sstevel@tonic-gate 	    (caddr_t)eii_p, PCI_OBJ_INTR_ADD);
34230Sstevel@tonic-gate 	return (PCI_ATTACH_RETCODE(PCI_ECC_OBJ, PCI_OBJ_INTR_ADD, r));
34240Sstevel@tonic-gate }
34250Sstevel@tonic-gate 
34260Sstevel@tonic-gate void
34270Sstevel@tonic-gate pci_ecc_rem_intr(pci_t *pci_p, int inum, ecc_intr_info_t *eii_p)
34280Sstevel@tonic-gate {
34290Sstevel@tonic-gate 	uint32_t mondo;
34300Sstevel@tonic-gate 
34310Sstevel@tonic-gate 	mondo = ((pci_p->pci_cb_p->cb_ign << PCI_INO_BITS) |
34320Sstevel@tonic-gate 	    pci_p->pci_inos[inum]);
34330Sstevel@tonic-gate 	mondo = CB_MONDO_TO_XMONDO(pci_p->pci_cb_p, mondo);
34340Sstevel@tonic-gate 
34350Sstevel@tonic-gate 	rem_ivintr(mondo, NULL);
34360Sstevel@tonic-gate 
34370Sstevel@tonic-gate 	if (CHIP_TYPE(pci_p) == PCI_CHIP_TOMATILLO)
34380Sstevel@tonic-gate 		pci_tom_nbintr_op(pci_p, inum, ecc_intr,
34390Sstevel@tonic-gate 			(caddr_t)eii_p, PCI_OBJ_INTR_REMOVE);
34400Sstevel@tonic-gate }
34410Sstevel@tonic-gate 
34420Sstevel@tonic-gate static uint_t
34430Sstevel@tonic-gate pci_pbm_cdma_intr(caddr_t a)
34440Sstevel@tonic-gate {
34450Sstevel@tonic-gate 	pbm_t *pbm_p = (pbm_t *)a;
34460Sstevel@tonic-gate 	pbm_p->pbm_cdma_flag = PBM_CDMA_DONE;
34470Sstevel@tonic-gate #ifdef PBM_CDMA_DEBUG
34480Sstevel@tonic-gate 	pbm_p->pbm_cdma_intr_cnt++;
34490Sstevel@tonic-gate #endif /* PBM_CDMA_DEBUG */
34500Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
34510Sstevel@tonic-gate }
34520Sstevel@tonic-gate 
34530Sstevel@tonic-gate int
34540Sstevel@tonic-gate pci_pbm_add_intr(pci_t *pci_p)
34550Sstevel@tonic-gate {
34560Sstevel@tonic-gate 	uint32_t mondo;
34570Sstevel@tonic-gate 
34580Sstevel@tonic-gate 	mondo = IB_INO_TO_MONDO(pci_p->pci_ib_p, pci_p->pci_inos[CBNINTR_CDMA]);
34590Sstevel@tonic-gate 	mondo = CB_MONDO_TO_XMONDO(pci_p->pci_cb_p, mondo);
34600Sstevel@tonic-gate 
34610Sstevel@tonic-gate 	VERIFY(add_ivintr(mondo, pci_pil[CBNINTR_CDMA],
34620Sstevel@tonic-gate 	    pci_pbm_cdma_intr, (caddr_t)pci_p->pci_pbm_p, NULL) == 0);
34630Sstevel@tonic-gate 
34640Sstevel@tonic-gate 	return (DDI_SUCCESS);
34650Sstevel@tonic-gate }
34660Sstevel@tonic-gate 
34670Sstevel@tonic-gate void
34680Sstevel@tonic-gate pci_pbm_rem_intr(pci_t *pci_p)
34690Sstevel@tonic-gate {
34700Sstevel@tonic-gate 	ib_t		*ib_p = pci_p->pci_ib_p;
34710Sstevel@tonic-gate 	uint32_t	mondo;
34720Sstevel@tonic-gate 
34730Sstevel@tonic-gate 	mondo = IB_INO_TO_MONDO(pci_p->pci_ib_p, pci_p->pci_inos[CBNINTR_CDMA]);
34740Sstevel@tonic-gate 	mondo = CB_MONDO_TO_XMONDO(pci_p->pci_cb_p, mondo);
34750Sstevel@tonic-gate 
34760Sstevel@tonic-gate 	ib_intr_disable(ib_p, pci_p->pci_inos[CBNINTR_CDMA], IB_INTR_NOWAIT);
34770Sstevel@tonic-gate 	rem_ivintr(mondo, NULL);
34780Sstevel@tonic-gate }
34790Sstevel@tonic-gate 
34800Sstevel@tonic-gate void
34810Sstevel@tonic-gate pci_pbm_suspend(pci_t *pci_p)
34820Sstevel@tonic-gate {
34830Sstevel@tonic-gate 	pbm_t		*pbm_p = pci_p->pci_pbm_p;
34840Sstevel@tonic-gate 	ib_ino_t	ino = pci_p->pci_inos[CBNINTR_CDMA];
34850Sstevel@tonic-gate 
34860Sstevel@tonic-gate 	/* Save CDMA interrupt state */
34870Sstevel@tonic-gate 	pbm_p->pbm_cdma_imr_save = *ib_intr_map_reg_addr(pci_p->pci_ib_p, ino);
34880Sstevel@tonic-gate }
34890Sstevel@tonic-gate 
34900Sstevel@tonic-gate void
34910Sstevel@tonic-gate pci_pbm_resume(pci_t *pci_p)
34920Sstevel@tonic-gate {
34930Sstevel@tonic-gate 	pbm_t		*pbm_p = pci_p->pci_pbm_p;
34940Sstevel@tonic-gate 	ib_ino_t	ino = pci_p->pci_inos[CBNINTR_CDMA];
34950Sstevel@tonic-gate 
34960Sstevel@tonic-gate 	/* Restore CDMA interrupt state */
34970Sstevel@tonic-gate 	*ib_intr_map_reg_addr(pci_p->pci_ib_p, ino) = pbm_p->pbm_cdma_imr_save;
34980Sstevel@tonic-gate }
34990Sstevel@tonic-gate 
35000Sstevel@tonic-gate /*
35010Sstevel@tonic-gate  * pci_bus_quiesce
35020Sstevel@tonic-gate  *
35030Sstevel@tonic-gate  * This function is called as the corresponding control ops routine
35040Sstevel@tonic-gate  * to a DDI_CTLOPS_QUIESCE command.  Its mission is to halt all DMA
35050Sstevel@tonic-gate  * activity on the bus by disabling arbitration/parking.
35060Sstevel@tonic-gate  */
35070Sstevel@tonic-gate int
35080Sstevel@tonic-gate pci_bus_quiesce(pci_t *pci_p, dev_info_t *dip, void *result)
35090Sstevel@tonic-gate {
35100Sstevel@tonic-gate 	volatile uint64_t *ctrl_reg_p;
35110Sstevel@tonic-gate 	volatile uint64_t ctrl_reg;
35120Sstevel@tonic-gate 	pbm_t *pbm_p;
35130Sstevel@tonic-gate 
35140Sstevel@tonic-gate 	pbm_p = pci_p->pci_pbm_p;
35150Sstevel@tonic-gate 	ctrl_reg_p = pbm_p->pbm_ctrl_reg;
35160Sstevel@tonic-gate 
35170Sstevel@tonic-gate 	if (pbm_p->pbm_quiesce_count++ == 0) {
35180Sstevel@tonic-gate 
35190Sstevel@tonic-gate 		DEBUG0(DBG_PWR, dip, "quiescing bus\n");
35200Sstevel@tonic-gate 
35210Sstevel@tonic-gate 		ctrl_reg = *ctrl_reg_p;
35220Sstevel@tonic-gate 		pbm_p->pbm_saved_ctrl_reg = ctrl_reg;
35230Sstevel@tonic-gate 		ctrl_reg &= ~(SCHIZO_PCI_CTRL_ARB_EN_MASK |
35240Sstevel@tonic-gate 		    SCHIZO_PCI_CTRL_ARB_PARK);
35250Sstevel@tonic-gate 		*ctrl_reg_p = ctrl_reg;
35260Sstevel@tonic-gate #ifdef	DEBUG
35270Sstevel@tonic-gate 		ctrl_reg = *ctrl_reg_p;
35280Sstevel@tonic-gate 		if ((ctrl_reg & (SCHIZO_PCI_CTRL_ARB_EN_MASK |
35290Sstevel@tonic-gate 		    SCHIZO_PCI_CTRL_ARB_PARK)) != 0)
3530946Smathue 			panic("ctrl_reg didn't quiesce: 0x%lx\n", ctrl_reg);
35310Sstevel@tonic-gate #endif
35320Sstevel@tonic-gate 		if (pbm_p->pbm_anychild_cfgpa)
35330Sstevel@tonic-gate 			(void) ldphysio(pbm_p->pbm_anychild_cfgpa);
35340Sstevel@tonic-gate 	}
35350Sstevel@tonic-gate 
35360Sstevel@tonic-gate 	return (DDI_SUCCESS);
35370Sstevel@tonic-gate }
35380Sstevel@tonic-gate 
35390Sstevel@tonic-gate /*
35400Sstevel@tonic-gate  * pci_bus_unquiesce
35410Sstevel@tonic-gate  *
35420Sstevel@tonic-gate  * This function is called as the corresponding control ops routine
35430Sstevel@tonic-gate  * to a DDI_CTLOPS_UNQUIESCE command.  Its mission is to resume paused
35440Sstevel@tonic-gate  * DMA activity on the bus by re-enabling arbitration (and maybe parking).
35450Sstevel@tonic-gate  */
35460Sstevel@tonic-gate int
35470Sstevel@tonic-gate pci_bus_unquiesce(pci_t *pci_p, dev_info_t *dip, void *result)
35480Sstevel@tonic-gate {
35490Sstevel@tonic-gate 	volatile uint64_t *ctrl_reg_p;
35500Sstevel@tonic-gate 	pbm_t *pbm_p;
35510Sstevel@tonic-gate #ifdef	DEBUG
35520Sstevel@tonic-gate 	volatile uint64_t ctrl_reg;
35530Sstevel@tonic-gate #endif
35540Sstevel@tonic-gate 
35550Sstevel@tonic-gate 	pbm_p = pci_p->pci_pbm_p;
35560Sstevel@tonic-gate 	ctrl_reg_p = pbm_p->pbm_ctrl_reg;
35570Sstevel@tonic-gate 
35580Sstevel@tonic-gate 	ASSERT(pbm_p->pbm_quiesce_count > 0);
35590Sstevel@tonic-gate 	if (--pbm_p->pbm_quiesce_count == 0) {
35600Sstevel@tonic-gate 		*ctrl_reg_p = pbm_p->pbm_saved_ctrl_reg;
35610Sstevel@tonic-gate #ifdef	DEBUG
35620Sstevel@tonic-gate 		ctrl_reg = *ctrl_reg_p;
35630Sstevel@tonic-gate 		if ((ctrl_reg & (SCHIZO_PCI_CTRL_ARB_EN_MASK |
35640Sstevel@tonic-gate 		    SCHIZO_PCI_CTRL_ARB_PARK)) == 0)
3565946Smathue 			panic("ctrl_reg didn't unquiesce: 0x%lx\n", ctrl_reg);
35660Sstevel@tonic-gate #endif
35670Sstevel@tonic-gate 	}
35680Sstevel@tonic-gate 
35690Sstevel@tonic-gate 	return (DDI_SUCCESS);
35700Sstevel@tonic-gate }
35710Sstevel@tonic-gate 
35722251Selowe int
35732251Selowe pci_reloc_getkey(void)
35742251Selowe {
35752251Selowe 	return (0x200);
35762251Selowe }
35772251Selowe 
35780Sstevel@tonic-gate static void
35790Sstevel@tonic-gate tm_vmem_free(ddi_dma_impl_t *mp, iommu_t *iommu_p, dvma_addr_t dvma_pg,
35800Sstevel@tonic-gate 	int npages)
35810Sstevel@tonic-gate {
35820Sstevel@tonic-gate 	uint32_t dur_max, dur_base;
35830Sstevel@tonic-gate 	dvma_unbind_req_t *req_p, *req_max_p;
35840Sstevel@tonic-gate 	dvma_unbind_req_t *req_base_p = iommu_p->iommu_mtlb_req_p;
35850Sstevel@tonic-gate 	uint32_t tlb_vpn[IOMMU_TLB_ENTRIES];
35860Sstevel@tonic-gate 	caddr_t reg_base;
35870Sstevel@tonic-gate 	volatile uint64_t *tag_p;
35880Sstevel@tonic-gate 	int i, preserv_count = 0;
35890Sstevel@tonic-gate 
35900Sstevel@tonic-gate 	mutex_enter(&iommu_p->iommu_mtlb_lock);
35910Sstevel@tonic-gate 
35920Sstevel@tonic-gate 	iommu_p->iommu_mtlb_npgs += npages;
35930Sstevel@tonic-gate 	req_max_p = req_base_p + iommu_p->iommu_mtlb_nreq++;
35940Sstevel@tonic-gate 	req_max_p->dur_npg = npages;
35950Sstevel@tonic-gate 	req_max_p->dur_base = dvma_pg;
35960Sstevel@tonic-gate 	req_max_p->dur_flags = mp->dmai_flags & DMAI_FLAGS_VMEMCACHE;
35970Sstevel@tonic-gate 
35980Sstevel@tonic-gate 
35990Sstevel@tonic-gate 	if (iommu_p->iommu_mtlb_npgs <= iommu_p->iommu_mtlb_maxpgs)
36000Sstevel@tonic-gate 		goto done;
36010Sstevel@tonic-gate 
36020Sstevel@tonic-gate 	/* read TLB */
36030Sstevel@tonic-gate 	reg_base = iommu_p->iommu_pci_p->pci_address[0];
36040Sstevel@tonic-gate 	tag_p = (volatile uint64_t *)
36050Sstevel@tonic-gate 	    (reg_base + COMMON_IOMMU_TLB_TAG_DIAG_ACC_OFFSET);
36060Sstevel@tonic-gate 
36070Sstevel@tonic-gate 	for (i = 0; i < IOMMU_TLB_ENTRIES; i++)
36080Sstevel@tonic-gate 		tlb_vpn[i] = tag_p[i] & SCHIZO_VPN_MASK;
36090Sstevel@tonic-gate 
36100Sstevel@tonic-gate 	/* for each request search the TLB for a matching address */
36110Sstevel@tonic-gate 	for (req_p = req_base_p; req_p <= req_max_p; req_p++) {
36120Sstevel@tonic-gate 		dur_base = req_p->dur_base;
36130Sstevel@tonic-gate 		dur_max = req_p->dur_base + req_p->dur_npg;
36140Sstevel@tonic-gate 
36150Sstevel@tonic-gate 		for (i = 0; i < IOMMU_TLB_ENTRIES; i++) {
36160Sstevel@tonic-gate 			uint_t vpn = tlb_vpn[i];
36170Sstevel@tonic-gate 			if (vpn >= dur_base && vpn < dur_max)
36180Sstevel@tonic-gate 				break;
36190Sstevel@tonic-gate 		}
36200Sstevel@tonic-gate 		if (i >= IOMMU_TLB_ENTRIES) {
36210Sstevel@tonic-gate 			pci_vmem_do_free(iommu_p,
36220Sstevel@tonic-gate 			    (void *)IOMMU_PTOB(req_p->dur_base),
36230Sstevel@tonic-gate 			    req_p->dur_npg, req_p->dur_flags);
36240Sstevel@tonic-gate 			iommu_p->iommu_mtlb_npgs -= req_p->dur_npg;
36250Sstevel@tonic-gate 			continue;
36260Sstevel@tonic-gate 		}
36270Sstevel@tonic-gate 		/* if an empty slot exists */
36280Sstevel@tonic-gate 		if ((req_p - req_base_p) != preserv_count)
36290Sstevel@tonic-gate 			*(req_base_p + preserv_count) = *req_p;
36300Sstevel@tonic-gate 		preserv_count++;
36310Sstevel@tonic-gate 	}
36320Sstevel@tonic-gate 
36330Sstevel@tonic-gate 	iommu_p->iommu_mtlb_nreq = preserv_count;
36340Sstevel@tonic-gate done:
36350Sstevel@tonic-gate 	mutex_exit(&iommu_p->iommu_mtlb_lock);
36360Sstevel@tonic-gate }
36370Sstevel@tonic-gate 
36380Sstevel@tonic-gate void
36390Sstevel@tonic-gate pci_vmem_free(iommu_t *iommu_p, ddi_dma_impl_t *mp, void *dvma_addr,
36400Sstevel@tonic-gate     size_t npages)
36410Sstevel@tonic-gate {
36420Sstevel@tonic-gate 	if (tm_mtlb_gc)
36430Sstevel@tonic-gate 		tm_vmem_free(mp, iommu_p,
36440Sstevel@tonic-gate 		    (dvma_addr_t)IOMMU_BTOP((dvma_addr_t)dvma_addr), npages);
36450Sstevel@tonic-gate 	else
36460Sstevel@tonic-gate 		pci_vmem_do_free(iommu_p, dvma_addr, npages,
36470Sstevel@tonic-gate 		    (mp->dmai_flags & DMAI_FLAGS_VMEMCACHE));
36480Sstevel@tonic-gate }
36492050Ssuha 
36502050Ssuha /*
36512050Ssuha  * pci_iommu_bypass_end_configure
36522050Ssuha  *
36532050Ssuha  * Support for 42-bit bus width to SAFARI and JBUS in DVMA and
36542050Ssuha  * iommu bypass transfers:
36552050Ssuha  */
36562050Ssuha 
36572050Ssuha dma_bypass_addr_t
36582050Ssuha pci_iommu_bypass_end_configure(void)
36592050Ssuha {
36602050Ssuha 
36612050Ssuha 	return ((dma_bypass_addr_t)SAFARI_JBUS_IOMMU_BYPASS_END);
36622050Ssuha }
3663