xref: /onnv-gate/usr/src/uts/sun4u/io/pci/pcisch.c (revision 4039:8a5733b96af9)
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 /*
22*4039Skd93003  * Copyright 2007 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>
412973Sgovinda #include <sys/systm.h>
420Sstevel@tonic-gate #include <sys/ivintr.h>
430Sstevel@tonic-gate #include <sys/machsystm.h>	/* lddphys() */
440Sstevel@tonic-gate #include <sys/machsystm.h>	/* lddphys, intr_dist_add */
450Sstevel@tonic-gate #include <sys/iommutsb.h>
460Sstevel@tonic-gate #include <sys/promif.h>		/* prom_printf */
470Sstevel@tonic-gate #include <sys/map.h>
480Sstevel@tonic-gate #include <sys/ddi.h>
490Sstevel@tonic-gate #include <sys/sunddi.h>
500Sstevel@tonic-gate #include <sys/sunndi.h>
510Sstevel@tonic-gate #include <sys/spl.h>
520Sstevel@tonic-gate #include <sys/fm/util.h>
530Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
540Sstevel@tonic-gate #include <sys/fm/protocol.h>
550Sstevel@tonic-gate #include <sys/fm/io/sun4upci.h>
560Sstevel@tonic-gate #include <sys/fm/io/ddi.h>
570Sstevel@tonic-gate #include <sys/fm/io/pci.h>
580Sstevel@tonic-gate #include <sys/pci/pci_obj.h>
590Sstevel@tonic-gate #include <sys/pci/pcisch.h>
600Sstevel@tonic-gate #include <sys/pci/pcisch_asm.h>
610Sstevel@tonic-gate #include <sys/x_call.h>		/* XCALL_PIL */
620Sstevel@tonic-gate 
630Sstevel@tonic-gate /*LINTLIBRARY*/
640Sstevel@tonic-gate 
650Sstevel@tonic-gate extern uint8_t ldstub(uint8_t *);
660Sstevel@tonic-gate 
670Sstevel@tonic-gate #define	IOMMU_CTX_BITMAP_SIZE	(1 << (12 - 3))
680Sstevel@tonic-gate static void iommu_ctx_free(iommu_t *);
690Sstevel@tonic-gate static int iommu_tlb_scrub(iommu_t *, int);
700Sstevel@tonic-gate static uint32_t pci_identity_init(pci_t *);
710Sstevel@tonic-gate 
720Sstevel@tonic-gate static void pci_cb_clear_error(cb_t *, cb_errstate_t *);
730Sstevel@tonic-gate static void pci_clear_error(pci_t *, pbm_errstate_t *);
740Sstevel@tonic-gate static uint32_t pci_identity_init(pci_t *pci_p);
750Sstevel@tonic-gate static int pci_intr_setup(pci_t *pci_p);
760Sstevel@tonic-gate static void iommu_ereport_post(dev_info_t *, uint64_t, pbm_errstate_t *);
770Sstevel@tonic-gate static void cb_ereport_post(dev_info_t *, uint64_t, cb_errstate_t *);
780Sstevel@tonic-gate static void pcix_ereport_post(dev_info_t *, uint64_t, pbm_errstate_t *);
790Sstevel@tonic-gate static void pci_format_ecc_addr(dev_info_t *dip, uint64_t *afar,
800Sstevel@tonic-gate 		ecc_region_t region);
810Sstevel@tonic-gate static void pci_pbm_errstate_get(pci_t *pci_p, pbm_errstate_t *pbm_err_p);
820Sstevel@tonic-gate static void tm_vmem_free(ddi_dma_impl_t *mp, iommu_t *iommu_p,
830Sstevel@tonic-gate 		dvma_addr_t dvma_pg, int npages);
840Sstevel@tonic-gate 
850Sstevel@tonic-gate static int pcix_ma_behind_bridge(pbm_errstate_t *pbm_err_p);
860Sstevel@tonic-gate 
870Sstevel@tonic-gate static pci_ksinfo_t	*pci_name_kstat;
880Sstevel@tonic-gate static pci_ksinfo_t	*saf_name_kstat;
890Sstevel@tonic-gate 
900Sstevel@tonic-gate extern void pcix_set_cmd_reg(dev_info_t *child, uint16_t value);
910Sstevel@tonic-gate 
920Sstevel@tonic-gate /* called by pci_attach() DDI_ATTACH to initialize pci objects */
930Sstevel@tonic-gate int
940Sstevel@tonic-gate pci_obj_setup(pci_t *pci_p)
950Sstevel@tonic-gate {
960Sstevel@tonic-gate 	pci_common_t *cmn_p;
970Sstevel@tonic-gate 	uint32_t chip_id = pci_identity_init(pci_p);
980Sstevel@tonic-gate 	uint32_t cmn_id = PCI_CMN_ID(ID_CHIP_TYPE(chip_id), pci_p->pci_id);
990Sstevel@tonic-gate 	int ret;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	/* Perform allocations first to avoid delicate unwinding. */
1020Sstevel@tonic-gate 	if (pci_alloc_tsb(pci_p) != DDI_SUCCESS)
1030Sstevel@tonic-gate 		return (DDI_FAILURE);
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	mutex_enter(&pci_global_mutex);
1060Sstevel@tonic-gate 	cmn_p = get_pci_common_soft_state(cmn_id);
1070Sstevel@tonic-gate 	if (cmn_p == NULL) {
1080Sstevel@tonic-gate 		if (alloc_pci_common_soft_state(cmn_id) != DDI_SUCCESS) {
1090Sstevel@tonic-gate 			mutex_exit(&pci_global_mutex);
1100Sstevel@tonic-gate 			pci_free_tsb(pci_p);
1110Sstevel@tonic-gate 			return (DDI_FAILURE);
1120Sstevel@tonic-gate 		}
1130Sstevel@tonic-gate 		cmn_p = get_pci_common_soft_state(cmn_id);
1140Sstevel@tonic-gate 		cmn_p->pci_common_id = cmn_id;
1150Sstevel@tonic-gate 		cmn_p->pci_common_tsb_cookie = IOMMU_TSB_COOKIE_NONE;
1160Sstevel@tonic-gate 	}
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	ASSERT((pci_p->pci_side == 0) || (pci_p->pci_side == 1));
1190Sstevel@tonic-gate 	if (cmn_p->pci_p[pci_p->pci_side]) {
1200Sstevel@tonic-gate 		/* second side attach */
1210Sstevel@tonic-gate 		pci_p->pci_side = PCI_OTHER_SIDE(pci_p->pci_side);
1220Sstevel@tonic-gate 		ASSERT(cmn_p->pci_p[pci_p->pci_side] == NULL);
1230Sstevel@tonic-gate 	}
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	cmn_p->pci_p[pci_p->pci_side] = pci_p;
1260Sstevel@tonic-gate 	pci_p->pci_common_p = cmn_p;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	if (cmn_p->pci_common_refcnt == 0)
1290Sstevel@tonic-gate 		cmn_p->pci_chip_id = chip_id;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	ib_create(pci_p);
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	/*
1340Sstevel@tonic-gate 	 * The initialization of cb internal interrupts depends on ib
1350Sstevel@tonic-gate 	 */
1360Sstevel@tonic-gate 	if (cmn_p->pci_common_refcnt == 0) {
1370Sstevel@tonic-gate 		cb_create(pci_p);
1380Sstevel@tonic-gate 		cmn_p->pci_common_cb_p = pci_p->pci_cb_p;
1390Sstevel@tonic-gate 	} else
1400Sstevel@tonic-gate 		pci_p->pci_cb_p = cmn_p->pci_common_cb_p;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	iommu_create(pci_p);
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	if (cmn_p->pci_common_refcnt == 0) {
1450Sstevel@tonic-gate 		ecc_create(pci_p);
1460Sstevel@tonic-gate 		cmn_p->pci_common_ecc_p = pci_p->pci_ecc_p;
1470Sstevel@tonic-gate 	} else
1480Sstevel@tonic-gate 		pci_p->pci_ecc_p = cmn_p->pci_common_ecc_p;
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	pbm_create(pci_p);
1510Sstevel@tonic-gate 	sc_create(pci_p);
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	pci_fm_create(pci_p);
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	if ((ret = pci_intr_setup(pci_p)) != DDI_SUCCESS)
1560Sstevel@tonic-gate 		goto done;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	pci_kstat_create(pci_p);
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	cmn_p->pci_common_attachcnt++;
1610Sstevel@tonic-gate 	cmn_p->pci_common_refcnt++;
1620Sstevel@tonic-gate done:
1630Sstevel@tonic-gate 	mutex_exit(&pci_global_mutex);
1640Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
1650Sstevel@tonic-gate 		cmn_err(CE_WARN, "pci_obj_setup failed %x", ret);
1660Sstevel@tonic-gate 	return (ret);
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate /* called by pci_detach() DDI_DETACH to destroy pci objects */
1700Sstevel@tonic-gate void
1710Sstevel@tonic-gate pci_obj_destroy(pci_t *pci_p)
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate 	pci_common_t *cmn_p;
1740Sstevel@tonic-gate 	mutex_enter(&pci_global_mutex);
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	cmn_p = pci_p->pci_common_p;
1770Sstevel@tonic-gate 	cmn_p->pci_common_refcnt--;
1780Sstevel@tonic-gate 	cmn_p->pci_common_attachcnt--;
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	pci_kstat_destroy(pci_p);
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	/* schizo non-shared objects */
1830Sstevel@tonic-gate 	pci_fm_destroy(pci_p);
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	sc_destroy(pci_p);
1860Sstevel@tonic-gate 	pbm_destroy(pci_p);
1870Sstevel@tonic-gate 	iommu_destroy(pci_p);
1880Sstevel@tonic-gate 	ib_destroy(pci_p);
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	if (cmn_p->pci_common_refcnt != 0) {
1910Sstevel@tonic-gate 		pci_intr_teardown(pci_p);
1920Sstevel@tonic-gate 		cmn_p->pci_p[pci_p->pci_side] = NULL;
1930Sstevel@tonic-gate 		mutex_exit(&pci_global_mutex);
1940Sstevel@tonic-gate 		return;
1950Sstevel@tonic-gate 	}
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	/* schizo shared objects - uses cmn_p, must be destroyed before cmn */
1980Sstevel@tonic-gate 	ecc_destroy(pci_p);
1990Sstevel@tonic-gate 	cb_destroy(pci_p);
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	free_pci_common_soft_state(cmn_p->pci_common_id);
2020Sstevel@tonic-gate 	pci_intr_teardown(pci_p);
2030Sstevel@tonic-gate 	mutex_exit(&pci_global_mutex);
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate /* called by pci_attach() DDI_RESUME to (re)initialize pci objects */
2070Sstevel@tonic-gate void
2080Sstevel@tonic-gate pci_obj_resume(pci_t *pci_p)
2090Sstevel@tonic-gate {
2100Sstevel@tonic-gate 	pci_common_t *cmn_p = pci_p->pci_common_p;
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	mutex_enter(&pci_global_mutex);
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	ib_configure(pci_p->pci_ib_p);
2150Sstevel@tonic-gate 	iommu_configure(pci_p->pci_iommu_p);
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	if (cmn_p->pci_common_attachcnt == 0)
2180Sstevel@tonic-gate 		ecc_configure(pci_p);
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	ib_resume(pci_p->pci_ib_p);
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	pbm_configure(pci_p->pci_pbm_p);
2230Sstevel@tonic-gate 	sc_configure(pci_p->pci_sc_p);
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	if (cmn_p->pci_common_attachcnt == 0)
2260Sstevel@tonic-gate 		cb_resume(pci_p->pci_cb_p);
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	pbm_resume(pci_p->pci_pbm_p);
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	cmn_p->pci_common_attachcnt++;
2310Sstevel@tonic-gate 	mutex_exit(&pci_global_mutex);
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate /* called by pci_detach() DDI_SUSPEND to suspend pci objects */
2350Sstevel@tonic-gate void
2360Sstevel@tonic-gate pci_obj_suspend(pci_t *pci_p)
2370Sstevel@tonic-gate {
2380Sstevel@tonic-gate 	mutex_enter(&pci_global_mutex);
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	pbm_suspend(pci_p->pci_pbm_p);
2410Sstevel@tonic-gate 	ib_suspend(pci_p->pci_ib_p);
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	if (!--pci_p->pci_common_p->pci_common_attachcnt)
2440Sstevel@tonic-gate 		cb_suspend(pci_p->pci_cb_p);
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	mutex_exit(&pci_global_mutex);
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate /*
2500Sstevel@tonic-gate  * add an additional 0x35 or 0x36 ino interrupt on platforms don't have them
2510Sstevel@tonic-gate  * This routine has multiple places that assumes interrupt takes one cell
2520Sstevel@tonic-gate  * each and cell size is same as integer size.
2530Sstevel@tonic-gate  */
2540Sstevel@tonic-gate static int
2550Sstevel@tonic-gate pci_intr_setup(pci_t *pci_p)
2560Sstevel@tonic-gate {
2570Sstevel@tonic-gate 	dev_info_t *dip = pci_p->pci_dip;
2580Sstevel@tonic-gate 	pbm_t *pbm_p = pci_p->pci_pbm_p;
2590Sstevel@tonic-gate 	cb_t *cb_p = pci_p->pci_cb_p;
2603558Sdduvall 	uint32_t *intr_buf, *new_intr_buf;
2613558Sdduvall 	int intr_len, intr_cnt, ret;
2620Sstevel@tonic-gate 
263506Scth 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
2640Sstevel@tonic-gate 		"interrupts", (caddr_t)&intr_buf, &intr_len) != DDI_SUCCESS)
2650Sstevel@tonic-gate 		cmn_err(CE_PANIC, "%s%d: no interrupts property\n",
2660Sstevel@tonic-gate 			ddi_driver_name(dip), ddi_get_instance(dip));
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	intr_cnt = BYTES_TO_1275_CELLS(intr_len);
2690Sstevel@tonic-gate 	if (intr_cnt < CBNINTR_CDMA)	/* CBNINTR_CDMA is 0 based */
2700Sstevel@tonic-gate 		cmn_err(CE_PANIC, "%s%d: <%d interrupts", ddi_driver_name(dip),
2710Sstevel@tonic-gate 			ddi_get_instance(dip), CBNINTR_CDMA);
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	if (intr_cnt == CBNINTR_CDMA)
2740Sstevel@tonic-gate 		intr_cnt++;
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	new_intr_buf = kmem_alloc(CELLS_1275_TO_BYTES(intr_cnt), KM_SLEEP);
2770Sstevel@tonic-gate 	bcopy(intr_buf, new_intr_buf, intr_len);
2780Sstevel@tonic-gate 	kmem_free(intr_buf, intr_len);
2790Sstevel@tonic-gate 
2803558Sdduvall 	new_intr_buf[CBNINTR_CDMA] = PBM_CDMA_INO_BASE + pci_p->pci_side;
2810Sstevel@tonic-gate 	pci_p->pci_inos = new_intr_buf;
2820Sstevel@tonic-gate 	pci_p->pci_inos_len = CELLS_1275_TO_BYTES(intr_cnt);
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	if (ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "interrupts",
2850Sstevel@tonic-gate 		(int *)new_intr_buf, intr_cnt))
2860Sstevel@tonic-gate 		cmn_err(CE_PANIC, "%s%d: cannot update interrupts property\n",
2870Sstevel@tonic-gate 			ddi_driver_name(dip), ddi_get_instance(dip));
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	if (pci_p->pci_common_p->pci_common_refcnt == 0) {
2900Sstevel@tonic-gate 		cb_p->cb_no_of_inos = intr_cnt;
2910Sstevel@tonic-gate 		if (ret = cb_register_intr(pci_p))
2920Sstevel@tonic-gate 			goto teardown;
2930Sstevel@tonic-gate 		if (ret = ecc_register_intr(pci_p))
2940Sstevel@tonic-gate 			goto teardown;
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 		intr_dist_add(cb_intr_dist, cb_p);
2970Sstevel@tonic-gate 		cb_enable_intr(pci_p);
2980Sstevel@tonic-gate 		ecc_enable_intr(pci_p);
2990Sstevel@tonic-gate 	}
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	if (CHIP_TYPE(pci_p) != PCI_CHIP_SCHIZO)
3020Sstevel@tonic-gate 		pbm_p->pbm_sync_ino = pci_p->pci_inos[CBNINTR_PBM];
3030Sstevel@tonic-gate 	if (ret = pbm_register_intr(pbm_p)) {
3040Sstevel@tonic-gate 		if (pci_p->pci_common_p->pci_common_refcnt == 0)
3050Sstevel@tonic-gate 			intr_dist_rem(cb_intr_dist, cb_p);
3060Sstevel@tonic-gate 		goto teardown;
3070Sstevel@tonic-gate 	}
3080Sstevel@tonic-gate 	intr_dist_add(pbm_intr_dist, pbm_p);
3090Sstevel@tonic-gate 	ib_intr_enable(pci_p, pci_p->pci_inos[CBNINTR_PBM]);
3100Sstevel@tonic-gate 	ib_intr_enable(pci_p, pci_p->pci_inos[CBNINTR_CDMA]);
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	intr_dist_add_weighted(ib_intr_dist_all, pci_p->pci_ib_p);
3130Sstevel@tonic-gate 	return (DDI_SUCCESS);
3140Sstevel@tonic-gate teardown:
3150Sstevel@tonic-gate 	pci_intr_teardown(pci_p);
3160Sstevel@tonic-gate 	return (ret);
3170Sstevel@tonic-gate }
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate uint64_t
3200Sstevel@tonic-gate pci_sc_configure(pci_t *pci_p)
3210Sstevel@tonic-gate {
3220Sstevel@tonic-gate 	int instance;
3230Sstevel@tonic-gate 	dev_info_t *dip = pci_p->pci_dip;
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
3260Sstevel@tonic-gate 	if ((pci_xmits_sc_max_prf & (1 << instance)) &&
3270Sstevel@tonic-gate 	    (CHIP_TYPE(pci_p) == PCI_CHIP_XMITS))
3280Sstevel@tonic-gate 		return (XMITS_SC_MAX_PRF);
3290Sstevel@tonic-gate 	else
3300Sstevel@tonic-gate 		return (0);
3310Sstevel@tonic-gate }
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate static void
3340Sstevel@tonic-gate pci_schizo_cdma_sync(pbm_t *pbm_p)
3350Sstevel@tonic-gate {
3360Sstevel@tonic-gate 	pci_t *pci_p = pbm_p->pbm_pci_p;
3370Sstevel@tonic-gate 	hrtime_t start_time;
3383558Sdduvall 	volatile uint64_t *clr_p = ib_clear_intr_reg_addr(pci_p->pci_ib_p,
3393558Sdduvall 		pci_p->pci_inos[CBNINTR_CDMA]);
3400Sstevel@tonic-gate 	uint32_t fail_cnt = pci_cdma_intr_count;
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	mutex_enter(&pbm_p->pbm_sync_mutex);
3433558Sdduvall #ifdef PBM_CDMA_DEBUG
3443558Sdduvall 	pbm_p->pbm_cdma_req_cnt++;
3453558Sdduvall #endif /* PBM_CDMA_DEBUG */
3460Sstevel@tonic-gate 	pbm_p->pbm_cdma_flag = PBM_CDMA_PEND;
3470Sstevel@tonic-gate 	IB_INO_INTR_TRIG(clr_p);
3480Sstevel@tonic-gate wait:
3490Sstevel@tonic-gate 	start_time = gethrtime();
3503558Sdduvall 	while (pbm_p->pbm_cdma_flag != PBM_CDMA_DONE) {
3510Sstevel@tonic-gate 		if (gethrtime() - start_time <= pci_cdma_intr_timeout)
3520Sstevel@tonic-gate 			continue;
3530Sstevel@tonic-gate 		if (--fail_cnt > 0)
3540Sstevel@tonic-gate 			goto wait;
3550Sstevel@tonic-gate 		if (pbm_p->pbm_cdma_flag == PBM_CDMA_DONE)
3560Sstevel@tonic-gate 			break;
3573558Sdduvall 		cmn_err(CE_PANIC, "%s (%s): consistent dma sync timeout",
3583558Sdduvall 		    pbm_p->pbm_nameinst_str, pbm_p->pbm_nameaddr_str);
3590Sstevel@tonic-gate 	}
3603558Sdduvall #ifdef PBM_CDMA_DEBUG
3613558Sdduvall 	if (pbm_p->pbm_cdma_flag != PBM_CDMA_DONE)
3623558Sdduvall 		pbm_p->pbm_cdma_to_cnt++;
3633558Sdduvall 	else {
3643558Sdduvall 		start_time = gethrtime() - start_time;
3653558Sdduvall 		pbm_p->pbm_cdma_success_cnt++;
3663558Sdduvall 		pbm_p->pbm_cdma_latency_sum += start_time;
3673558Sdduvall 		if (start_time > pbm_p->pbm_cdma_latency_max)
3683558Sdduvall 			pbm_p->pbm_cdma_latency_max = start_time;
3693558Sdduvall 	}
3703558Sdduvall #endif /* PBM_CDMA_DEBUG */
3710Sstevel@tonic-gate 	mutex_exit(&pbm_p->pbm_sync_mutex);
3720Sstevel@tonic-gate }
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate #if !defined(lint)
3750Sstevel@tonic-gate #include <sys/cpuvar.h>
3760Sstevel@tonic-gate #endif
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate #define	SYNC_HW_BUSY(pa, mask)	(lddphysio(pa) & (mask))
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate /*
3810Sstevel@tonic-gate  * Consistent DMA Sync/Flush
3820Sstevel@tonic-gate  *
3830Sstevel@tonic-gate  * XMITS and Tomatillo use multi-threaded sync/flush register.
3840Sstevel@tonic-gate  * Called from interrupt wrapper: the associated ino is used to index
3850Sstevel@tonic-gate  *	the distinctive register bit.
3860Sstevel@tonic-gate  * Called from pci_dma_sync(): the bit belongs to PBM is shared
3870Sstevel@tonic-gate  *	for all calls from pci_dma_sync(). Xmits requires serialization
3880Sstevel@tonic-gate  *	while Tomatillo does not.
3890Sstevel@tonic-gate  */
3900Sstevel@tonic-gate void
3910Sstevel@tonic-gate pci_pbm_dma_sync(pbm_t *pbm_p, ib_ino_t ino)
3920Sstevel@tonic-gate {
3930Sstevel@tonic-gate 	pci_t *pci_p = pbm_p->pbm_pci_p;
3940Sstevel@tonic-gate 	hrtime_t start_time;
3950Sstevel@tonic-gate 	uint64_t ino_mask, sync_reg_pa;
3960Sstevel@tonic-gate 	volatile uint64_t flag_val;
3970Sstevel@tonic-gate 	uint32_t locked, chip_type = CHIP_TYPE(pci_p);
3980Sstevel@tonic-gate 	int	i;
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	if (chip_type == PCI_CHIP_SCHIZO) {
4010Sstevel@tonic-gate 		pci_schizo_cdma_sync(pbm_p);
4020Sstevel@tonic-gate 		return;
4030Sstevel@tonic-gate 	}
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	sync_reg_pa = pbm_p->pbm_sync_reg_pa;
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	locked = 0;
4080Sstevel@tonic-gate 	if (((chip_type == PCI_CHIP_XMITS) && (ino == pbm_p->pbm_sync_ino)) ||
4090Sstevel@tonic-gate 	    pci_sync_lock) {
4100Sstevel@tonic-gate 		locked = 1;
4110Sstevel@tonic-gate 		mutex_enter(&pbm_p->pbm_sync_mutex);
4120Sstevel@tonic-gate 	}
4130Sstevel@tonic-gate 	ino_mask = 1ull << ino;
4140Sstevel@tonic-gate 	stdphysio(sync_reg_pa, ino_mask);
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	for (i = 0; i < 5; i++) {
4170Sstevel@tonic-gate 		if ((flag_val = SYNC_HW_BUSY(sync_reg_pa, ino_mask)) == 0)
4180Sstevel@tonic-gate 			goto done;
4190Sstevel@tonic-gate 	}
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	start_time = gethrtime();
4220Sstevel@tonic-gate 	for (; (flag_val = SYNC_HW_BUSY(sync_reg_pa, ino_mask)) != 0; i++) {
4230Sstevel@tonic-gate 		if (gethrtime() - start_time > pci_sync_buf_timeout)
4240Sstevel@tonic-gate 			break;
4250Sstevel@tonic-gate 	}
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	if (flag_val && SYNC_HW_BUSY(sync_reg_pa, ino_mask) && !panicstr)
428946Smathue 		cmn_err(CE_PANIC, "%s: pbm dma sync %lx,%lx timeout!",
4290Sstevel@tonic-gate 			pbm_p->pbm_nameaddr_str, sync_reg_pa, flag_val);
4300Sstevel@tonic-gate done:
4310Sstevel@tonic-gate 	/* optional: stdphysio(sync_reg_pa - 8, ino_mask); */
4320Sstevel@tonic-gate 	if (locked)
4330Sstevel@tonic-gate 		mutex_exit(&pbm_p->pbm_sync_mutex);
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	if (tomatillo_store_store_wrka) {
4360Sstevel@tonic-gate #if !defined(lint)
4370Sstevel@tonic-gate 		kpreempt_disable();
4380Sstevel@tonic-gate #endif
4390Sstevel@tonic-gate 		tomatillo_store_store_order();
4400Sstevel@tonic-gate #if !defined(lint)
4410Sstevel@tonic-gate 		kpreempt_enable();
4420Sstevel@tonic-gate #endif
4430Sstevel@tonic-gate 	}
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate }
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate /*ARGSUSED*/
4480Sstevel@tonic-gate void
4490Sstevel@tonic-gate pci_fix_ranges(pci_ranges_t *rng_p, int rng_entries)
4500Sstevel@tonic-gate {
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate /*
4540Sstevel@tonic-gate  * map_pci_registers
4550Sstevel@tonic-gate  *
4560Sstevel@tonic-gate  * This function is called from the attach routine to map the registers
4570Sstevel@tonic-gate  * accessed by this driver.
4580Sstevel@tonic-gate  *
4590Sstevel@tonic-gate  * used by: pci_attach()
4600Sstevel@tonic-gate  *
4610Sstevel@tonic-gate  * return value: DDI_FAILURE on failure
4620Sstevel@tonic-gate  */
4630Sstevel@tonic-gate int
4640Sstevel@tonic-gate map_pci_registers(pci_t *pci_p, dev_info_t *dip)
4650Sstevel@tonic-gate {
4660Sstevel@tonic-gate 	ddi_device_acc_attr_t attr;
4670Sstevel@tonic-gate 	int len;
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
4700Sstevel@tonic-gate 	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	attr.devacc_attr_endian_flags = DDI_NEVERSWAP_ACC;
4731865Sdilpreet 
4740Sstevel@tonic-gate 	/*
4750Sstevel@tonic-gate 	 * Register set 0 is PCI CSR Base
4760Sstevel@tonic-gate 	 */
4770Sstevel@tonic-gate 	if (ddi_regs_map_setup(dip, 0, &pci_p->pci_address[0], 0, 0,
4780Sstevel@tonic-gate 	    &attr, &pci_p->pci_ac[0]) != DDI_SUCCESS) {
4790Sstevel@tonic-gate 		len = 0;
4800Sstevel@tonic-gate 		goto fail;
4810Sstevel@tonic-gate 	}
4820Sstevel@tonic-gate 	/*
4830Sstevel@tonic-gate 	 * Register set 1 is Schizo CSR Base
4840Sstevel@tonic-gate 	 */
4850Sstevel@tonic-gate 	if (ddi_regs_map_setup(dip, 1, &pci_p->pci_address[1], 0, 0,
4860Sstevel@tonic-gate 	    &attr, &pci_p->pci_ac[1]) != DDI_SUCCESS) {
4870Sstevel@tonic-gate 		len = 1;
4880Sstevel@tonic-gate 		goto fail;
4890Sstevel@tonic-gate 	}
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	/*
4920Sstevel@tonic-gate 	 * The third register set contains the bridge's configuration
4930Sstevel@tonic-gate 	 * header.  This header is at the very beginning of the bridge's
4940Sstevel@tonic-gate 	 * configuration space.  This space has litte-endian byte order.
4950Sstevel@tonic-gate 	 */
4960Sstevel@tonic-gate 	attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
4970Sstevel@tonic-gate 	if (ddi_regs_map_setup(dip, 2, &pci_p->pci_address[2], 0,
4980Sstevel@tonic-gate 	    PCI_CONF_HDR_SIZE, &attr, &pci_p->pci_ac[2]) != DDI_SUCCESS) {
4990Sstevel@tonic-gate 		len = 2;
5000Sstevel@tonic-gate 		goto fail;
5010Sstevel@tonic-gate 	}
5020Sstevel@tonic-gate 
503506Scth 	if (ddi_getproplen(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
5040Sstevel@tonic-gate 	    "reg", &len) || (len / sizeof (pci_nexus_regspec_t) < 4))
5050Sstevel@tonic-gate 		goto done;
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	/*
5080Sstevel@tonic-gate 	 * The optional fourth register bank points to the
5090Sstevel@tonic-gate 	 * interrupt concentrator registers.
5100Sstevel@tonic-gate 	 */
5110Sstevel@tonic-gate 	attr.devacc_attr_endian_flags = DDI_NEVERSWAP_ACC;
5120Sstevel@tonic-gate 	if (ddi_regs_map_setup(dip, 3, &pci_p->pci_address[3], 0,
5130Sstevel@tonic-gate 	    0, &attr, &pci_p->pci_ac[3]) != DDI_SUCCESS) {
5140Sstevel@tonic-gate 		len = 3;
5150Sstevel@tonic-gate 		goto fail;
5160Sstevel@tonic-gate 	}
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate done:
5190Sstevel@tonic-gate 	DEBUG4(DBG_ATTACH, dip, "address (%p,%p,%p,%p)\n",
5200Sstevel@tonic-gate 	    pci_p->pci_address[0], pci_p->pci_address[1],
5210Sstevel@tonic-gate 	    pci_p->pci_address[2], pci_p->pci_address[3]);
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	return (DDI_SUCCESS);
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate fail:
5270Sstevel@tonic-gate 	cmn_err(CE_WARN, "%s%d: unable to map reg entry %d\n",
5280Sstevel@tonic-gate 		ddi_driver_name(dip), ddi_get_instance(dip), len);
5290Sstevel@tonic-gate 	for (; len--; ddi_regs_map_free(&pci_p->pci_ac[len]));
5300Sstevel@tonic-gate 	return (DDI_FAILURE);
5310Sstevel@tonic-gate }
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate /*
5340Sstevel@tonic-gate  * unmap_pci_registers:
5350Sstevel@tonic-gate  *
5360Sstevel@tonic-gate  * This routine unmap the registers mapped by map_pci_registers.
5370Sstevel@tonic-gate  *
5380Sstevel@tonic-gate  * used by: pci_detach()
5390Sstevel@tonic-gate  *
5400Sstevel@tonic-gate  * return value: none
5410Sstevel@tonic-gate  */
5420Sstevel@tonic-gate void
5430Sstevel@tonic-gate unmap_pci_registers(pci_t *pci_p)
5440Sstevel@tonic-gate {
5450Sstevel@tonic-gate 	int i;
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 	for (i = 0; i < 4; i++) {
5480Sstevel@tonic-gate 		if (pci_p->pci_ac[i])
5490Sstevel@tonic-gate 			ddi_regs_map_free(&pci_p->pci_ac[i]);
5500Sstevel@tonic-gate 	}
5510Sstevel@tonic-gate }
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate uint64_t
5540Sstevel@tonic-gate ib_get_map_reg(ib_mondo_t mondo, uint32_t cpu_id)
5550Sstevel@tonic-gate {
5560Sstevel@tonic-gate 	uint32_t agent_id;
5570Sstevel@tonic-gate 	uint32_t node_id;
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 	/* ensure that cpu_id is only 10 bits. */
5600Sstevel@tonic-gate 	ASSERT((cpu_id & ~0x3ff) == 0);
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	agent_id = cpu_id & 0x1f;
5630Sstevel@tonic-gate 	node_id = (cpu_id >> 5) & 0x1f;
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	return ((mondo) | (agent_id << COMMON_INTR_MAP_REG_TID_SHIFT) |
5660Sstevel@tonic-gate 	    (node_id << SCHIZO_INTR_MAP_REG_NID_SHIFT) |
5670Sstevel@tonic-gate 	    COMMON_INTR_MAP_REG_VALID);
5680Sstevel@tonic-gate }
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate uint32_t
5710Sstevel@tonic-gate ib_map_reg_get_cpu(volatile uint64_t reg)
5720Sstevel@tonic-gate {
5730Sstevel@tonic-gate 	return (((reg & COMMON_INTR_MAP_REG_TID) >>
5740Sstevel@tonic-gate 		COMMON_INTR_MAP_REG_TID_SHIFT) |
5750Sstevel@tonic-gate 			((reg & SCHIZO_INTR_MAP_REG_NID) >>
5760Sstevel@tonic-gate 			(SCHIZO_INTR_MAP_REG_NID_SHIFT-5)));
5770Sstevel@tonic-gate }
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate uint64_t *
5800Sstevel@tonic-gate ib_intr_map_reg_addr(ib_t *ib_p, ib_ino_t ino)
5810Sstevel@tonic-gate {
5820Sstevel@tonic-gate 	/*
5830Sstevel@tonic-gate 	 * Schizo maps all interrupts in one contiguous area.
5840Sstevel@tonic-gate 	 * (PCI_CSRBase + 0x00.1000 + INO * 8).
5850Sstevel@tonic-gate 	 */
5860Sstevel@tonic-gate 	return ((uint64_t *)(ib_p->ib_intr_map_regs) + (ino & 0x3f));
5870Sstevel@tonic-gate }
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate uint64_t *
5900Sstevel@tonic-gate ib_clear_intr_reg_addr(ib_t *ib_p, ib_ino_t ino)	/* XXX - needs work */
5910Sstevel@tonic-gate {
5920Sstevel@tonic-gate 	/*
5930Sstevel@tonic-gate 	 * Schizo maps clear intr. registers in contiguous area.
5940Sstevel@tonic-gate 	 * (PCI_CSRBase + 0x00.1400 + INO * 8).
5950Sstevel@tonic-gate 	 */
5960Sstevel@tonic-gate 	return ((uint64_t *)(ib_p->ib_slot_clear_intr_regs) + (ino & 0x3f));
5970Sstevel@tonic-gate }
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate /*
6000Sstevel@tonic-gate  * schizo does not have mapping register per slot, so no sharing
6010Sstevel@tonic-gate  * is done.
6020Sstevel@tonic-gate  */
6030Sstevel@tonic-gate /*ARGSUSED*/
6040Sstevel@tonic-gate void
6050Sstevel@tonic-gate ib_ino_map_reg_share(ib_t *ib_p, ib_ino_t ino, ib_ino_info_t *ino_p)
6060Sstevel@tonic-gate {
6070Sstevel@tonic-gate }
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate /*
6100Sstevel@tonic-gate  * return true if there are interrupts using this mapping register
6110Sstevel@tonic-gate  */
6120Sstevel@tonic-gate /*ARGSUSED*/
6130Sstevel@tonic-gate int
6140Sstevel@tonic-gate ib_ino_map_reg_unshare(ib_t *ib_p, ib_ino_t ino, ib_ino_info_t *ino_p)
6150Sstevel@tonic-gate {
6162973Sgovinda 	return (ino_p->ino_ipil_size);
6170Sstevel@tonic-gate }
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate void
6200Sstevel@tonic-gate pci_pbm_intr_dist(pbm_t *pbm_p)
6210Sstevel@tonic-gate {
6220Sstevel@tonic-gate 	pci_t *pci_p = pbm_p->pbm_pci_p;
6230Sstevel@tonic-gate 	ib_t *ib_p = pci_p->pci_ib_p;
6240Sstevel@tonic-gate 	ib_ino_t ino = IB_MONDO_TO_INO(pci_p->pci_inos[CBNINTR_CDMA]);
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 	mutex_enter(&pbm_p->pbm_sync_mutex);
6270Sstevel@tonic-gate 	ib_intr_dist_nintr(ib_p, ino, ib_intr_map_reg_addr(ib_p, ino));
6280Sstevel@tonic-gate 	mutex_exit(&pbm_p->pbm_sync_mutex);
6290Sstevel@tonic-gate }
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate uint32_t
6320Sstevel@tonic-gate pci_xlate_intr(dev_info_t *dip, dev_info_t *rdip, ib_t *ib_p, uint32_t intr)
6330Sstevel@tonic-gate {
6340Sstevel@tonic-gate 	return (IB_INO_TO_MONDO(ib_p, intr));
6350Sstevel@tonic-gate }
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate /*
6390Sstevel@tonic-gate  * Return the cpuid to to be used for an ino.  We have no special cpu
6400Sstevel@tonic-gate  * assignment constraints for this nexus, so just call intr_dist_cpuid().
6410Sstevel@tonic-gate  */
6420Sstevel@tonic-gate /* ARGSUSED */
6430Sstevel@tonic-gate uint32_t
6440Sstevel@tonic-gate pci_intr_dist_cpuid(ib_t *ib_p, ib_ino_info_t *ino_p)
6450Sstevel@tonic-gate {
6460Sstevel@tonic-gate 	return (intr_dist_cpuid());
6470Sstevel@tonic-gate }
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate void
6500Sstevel@tonic-gate pci_cb_teardown(pci_t *pci_p)
6510Sstevel@tonic-gate {
6520Sstevel@tonic-gate 	cb_t 	*cb_p = pci_p->pci_cb_p;
6530Sstevel@tonic-gate 	uint32_t mondo;
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	if (!pci_buserr_interrupt)
6560Sstevel@tonic-gate 		return;
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	mondo = ((pci_p->pci_cb_p->cb_ign  << PCI_INO_BITS) |
6590Sstevel@tonic-gate 	    pci_p->pci_inos[CBNINTR_BUS_ERROR]);
6600Sstevel@tonic-gate 	mondo = CB_MONDO_TO_XMONDO(pci_p->pci_cb_p, mondo);
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 	cb_disable_nintr(cb_p, CBNINTR_BUS_ERROR, IB_INTR_WAIT);
6632973Sgovinda 	VERIFY(rem_ivintr(mondo, pci_pil[CBNINTR_BUS_ERROR]) == 0);
6640Sstevel@tonic-gate }
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate int
6670Sstevel@tonic-gate cb_register_intr(pci_t *pci_p)
6680Sstevel@tonic-gate {
6690Sstevel@tonic-gate 	uint32_t mondo;
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	if (!pci_buserr_interrupt)
6720Sstevel@tonic-gate 		return (DDI_SUCCESS);
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 	mondo = ((pci_p->pci_cb_p->cb_ign << PCI_INO_BITS) |
6750Sstevel@tonic-gate 	    pci_p->pci_inos[CBNINTR_BUS_ERROR]);
6760Sstevel@tonic-gate 	mondo = CB_MONDO_TO_XMONDO(pci_p->pci_cb_p, mondo);
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	VERIFY(add_ivintr(mondo, pci_pil[CBNINTR_BUS_ERROR],
6792973Sgovinda 	    (intrfunc)cb_buserr_intr, (caddr_t)pci_p->pci_cb_p,
6802973Sgovinda 	    NULL, 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 			tomatillo_store_store_wrka = 1;
11170Sstevel@tonic-gate 			tomatillo_disallow_bypass = 1;
11180Sstevel@tonic-gate 			if (pci_spurintr_msgs == PCI_SPURINTR_MSG_DEFAULT)
11190Sstevel@tonic-gate 				pci_spurintr_msgs = 0;
11200Sstevel@tonic-gate 		}
11210Sstevel@tonic-gate 	}
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate 	if (chip_id == TOMATILLO_VER_20 || chip_id == TOMATILLO_VER_21)
11240Sstevel@tonic-gate 		cmn_err(CE_WARN, "Unsupported Tomatillo rev (%x)", chip_id);
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 	if (chip_id < SCHIZO_VER_23)
11270Sstevel@tonic-gate 		pci_ctx_no_active_flush = 1;
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 	cb_p->cb_node_id = PCI_ID_TO_NODEID(pci_p->pci_id);
11300Sstevel@tonic-gate 	cb_p->cb_ign	 = PCI_ID_TO_IGN(pci_p->pci_id);
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate 	/*
11330Sstevel@tonic-gate 	 * schizo control status reg bank is on the 2nd "reg" property entry
11340Sstevel@tonic-gate 	 * interrupt mapping/clear/state regs are on the 1st "reg" entry.
11350Sstevel@tonic-gate 	 *
11360Sstevel@tonic-gate 	 * ALL internal interrupts except pbm interrupts are shared by both
11370Sstevel@tonic-gate 	 * sides, 1st-side-attached is used as *the* owner.
11380Sstevel@tonic-gate 	 */
11390Sstevel@tonic-gate 	pa = (uint64_t)hat_getpfnum(kas.a_hat, pci_p->pci_address[1]);
11400Sstevel@tonic-gate 	cb_p->cb_base_pa = pa << MMU_PAGESHIFT;
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 	pa = pci_p->pci_address[3] ?
11430Sstevel@tonic-gate 		(uint64_t)hat_getpfnum(kas.a_hat, pci_p->pci_address[3]) : 0;
11440Sstevel@tonic-gate 	cb_p->cb_icbase_pa = (pa == PFN_INVALID) ? 0 : pa << MMU_PAGESHIFT;
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate 	pa = (uint64_t)hat_getpfnum(kas.a_hat, pci_p->pci_address[0])
11470Sstevel@tonic-gate 		<< MMU_PAGESHIFT;
11480Sstevel@tonic-gate 	cb_p->cb_map_pa = pa + SCHIZO_IB_INTR_MAP_REG_OFFSET;
11490Sstevel@tonic-gate 	cb_p->cb_clr_pa = pa + SCHIZO_IB_CLEAR_INTR_REG_OFFSET;
11500Sstevel@tonic-gate 	cb_p->cb_obsta_pa = pa + COMMON_IB_OBIO_INTR_STATE_DIAG_REG;
11510Sstevel@tonic-gate }
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate void
11540Sstevel@tonic-gate pci_ecc_setup(ecc_t *ecc_p)
11550Sstevel@tonic-gate {
11560Sstevel@tonic-gate 	ecc_p->ecc_ue.ecc_errpndg_mask = SCHIZO_ECC_UE_AFSR_ERRPNDG;
11570Sstevel@tonic-gate 	ecc_p->ecc_ue.ecc_offset_mask = SCHIZO_ECC_UE_AFSR_QW_OFFSET;
11580Sstevel@tonic-gate 	ecc_p->ecc_ue.ecc_offset_shift = SCHIZO_ECC_UE_AFSR_QW_OFFSET_SHIFT;
11590Sstevel@tonic-gate 	ecc_p->ecc_ue.ecc_size_log2 = 4;
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 	ecc_p->ecc_ce.ecc_errpndg_mask = SCHIZO_ECC_CE_AFSR_ERRPNDG;
11620Sstevel@tonic-gate 	ecc_p->ecc_ce.ecc_offset_mask = SCHIZO_ECC_CE_AFSR_QW_OFFSET;
11630Sstevel@tonic-gate 	ecc_p->ecc_ce.ecc_offset_shift = SCHIZO_ECC_CE_AFSR_QW_OFFSET_SHIFT;
11640Sstevel@tonic-gate 	ecc_p->ecc_ce.ecc_size_log2 = 4;
11650Sstevel@tonic-gate }
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate ushort_t
11680Sstevel@tonic-gate pci_ecc_get_synd(uint64_t afsr)
11690Sstevel@tonic-gate {
11700Sstevel@tonic-gate 	return ((ushort_t)((afsr & SCHIZO_ECC_CE_AFSR_SYND) >>
11710Sstevel@tonic-gate 	    SCHIZO_ECC_CE_AFSR_SYND_SHIFT));
11720Sstevel@tonic-gate }
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate /*
11750Sstevel@tonic-gate  * overwrite dvma end address (only on virtual-dma systems)
11760Sstevel@tonic-gate  * initialize tsb size
11770Sstevel@tonic-gate  * reset context bits
11780Sstevel@tonic-gate  * return: IOMMU CSR bank base address (VA)
11790Sstevel@tonic-gate  */
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate uintptr_t
11820Sstevel@tonic-gate pci_iommu_setup(iommu_t *iommu_p)
11830Sstevel@tonic-gate {
11840Sstevel@tonic-gate 	pci_dvma_range_prop_t *dvma_prop;
11850Sstevel@tonic-gate 	int dvma_prop_len;
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 	uintptr_t a;
11880Sstevel@tonic-gate 	pci_t *pci_p = iommu_p->iommu_pci_p;
11890Sstevel@tonic-gate 	dev_info_t *dip = pci_p->pci_dip;
11900Sstevel@tonic-gate 	uint_t tsb_size = iommu_tsb_cookie_to_size(pci_p->pci_tsb_cookie);
1191566Ssuha 	uint_t tsb_size_prop;
11920Sstevel@tonic-gate 
11930Sstevel@tonic-gate 	/*
11940Sstevel@tonic-gate 	 * Initializations for Tomatillo's micro TLB bug. errata #82
11950Sstevel@tonic-gate 	 */
11960Sstevel@tonic-gate 	if (tm_mtlb_gc) {
11970Sstevel@tonic-gate 		iommu_p->iommu_mtlb_nreq = 0;
11980Sstevel@tonic-gate 		iommu_p->iommu_mtlb_npgs = 0;
11990Sstevel@tonic-gate 		iommu_p->iommu_mtlb_maxpgs = tm_mtlb_maxpgs;
12000Sstevel@tonic-gate 		iommu_p->iommu_mtlb_req_p = (dvma_unbind_req_t *)
12010Sstevel@tonic-gate 		    kmem_zalloc(sizeof (dvma_unbind_req_t) *
12020Sstevel@tonic-gate 		    (tm_mtlb_maxpgs + 1), KM_SLEEP);
12030Sstevel@tonic-gate 		mutex_init(&iommu_p->iommu_mtlb_lock, NULL, MUTEX_DRIVER, NULL);
12040Sstevel@tonic-gate 	}
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
12070Sstevel@tonic-gate 		"virtual-dma", (caddr_t)&dvma_prop, &dvma_prop_len) !=
12080Sstevel@tonic-gate 		DDI_PROP_SUCCESS)
12090Sstevel@tonic-gate 		goto tsb_done;
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 	if (dvma_prop_len != sizeof (pci_dvma_range_prop_t)) {
12120Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: invalid virtual-dma property",
12130Sstevel@tonic-gate 			ddi_driver_name(dip), ddi_get_instance(dip));
12140Sstevel@tonic-gate 		goto tsb_end;
12150Sstevel@tonic-gate 	}
12160Sstevel@tonic-gate 	iommu_p->iommu_dvma_end = dvma_prop->dvma_base +
12170Sstevel@tonic-gate 		(dvma_prop->dvma_len - 1);
1218566Ssuha 	tsb_size_prop = IOMMU_BTOP(dvma_prop->dvma_len) * sizeof (uint64_t);
1219566Ssuha 	tsb_size = MIN(tsb_size_prop, tsb_size);
12200Sstevel@tonic-gate tsb_end:
12210Sstevel@tonic-gate 	kmem_free(dvma_prop, dvma_prop_len);
12220Sstevel@tonic-gate tsb_done:
12230Sstevel@tonic-gate 	iommu_p->iommu_tsb_size = iommu_tsb_size_encode(tsb_size);
12240Sstevel@tonic-gate 	iommu_p->iommu_ctx_bitmap =
12250Sstevel@tonic-gate 		kmem_zalloc(IOMMU_CTX_BITMAP_SIZE, KM_SLEEP);
12260Sstevel@tonic-gate 	*iommu_p->iommu_ctx_bitmap = 1ull;	/* reserve context 0 */
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate 	/*
12290Sstevel@tonic-gate 	 * Determine the virtual address of the register block
12300Sstevel@tonic-gate 	 * containing the iommu control registers and determine
12310Sstevel@tonic-gate 	 * the virtual address of schizo specific iommu registers.
12320Sstevel@tonic-gate 	 */
12330Sstevel@tonic-gate 	a = (uintptr_t)pci_p->pci_address[0];
12340Sstevel@tonic-gate 	iommu_p->iommu_flush_ctx_reg =
12350Sstevel@tonic-gate 		(uint64_t *)(a + SCHIZO_IOMMU_FLUSH_CTX_REG_OFFSET);
12360Sstevel@tonic-gate 	if (CHIP_TYPE(pci_p) == PCI_CHIP_TOMATILLO)
12370Sstevel@tonic-gate 		iommu_p->iommu_tfar_reg =
12380Sstevel@tonic-gate 			(uint64_t *)(a + TOMATILLO_IOMMU_ERR_TFAR_OFFSET);
12390Sstevel@tonic-gate 	return (a);	/* PCICSRBase */
12400Sstevel@tonic-gate }
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate void
12430Sstevel@tonic-gate pci_iommu_teardown(iommu_t *iommu_p)
12440Sstevel@tonic-gate {
12450Sstevel@tonic-gate 	if (pci_use_contexts)
12460Sstevel@tonic-gate 		iommu_ctx_free(iommu_p);
12470Sstevel@tonic-gate 	if (iommu_p->iommu_mtlb_req_p) {
12480Sstevel@tonic-gate 		kmem_free(iommu_p->iommu_mtlb_req_p,
12490Sstevel@tonic-gate 		    sizeof (dvma_unbind_req_t) * (tm_mtlb_maxpgs + 1));
12500Sstevel@tonic-gate 		mutex_destroy(&iommu_p->iommu_mtlb_lock);
12510Sstevel@tonic-gate 		iommu_p->iommu_mtlb_req_p = NULL;
12520Sstevel@tonic-gate 		iommu_p->iommu_mtlb_nreq = 0;
12530Sstevel@tonic-gate 		iommu_p->iommu_mtlb_npgs = iommu_p->iommu_mtlb_maxpgs = 0;
12540Sstevel@tonic-gate 	}
12550Sstevel@tonic-gate }
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate uintptr_t
12580Sstevel@tonic-gate get_pbm_reg_base(pci_t *pci_p)
12590Sstevel@tonic-gate {
12600Sstevel@tonic-gate 	return ((uintptr_t)
12610Sstevel@tonic-gate 		(pci_p->pci_address[0] + SCHIZO_PCI_CTRL_REG_OFFSET));
12620Sstevel@tonic-gate }
12630Sstevel@tonic-gate 
12640Sstevel@tonic-gate /* ARGSUSED */
12650Sstevel@tonic-gate static boolean_t
12660Sstevel@tonic-gate pci_pbm_panic_callb(void *arg, int code)
12670Sstevel@tonic-gate {
12680Sstevel@tonic-gate 	pbm_t *pbm_p = (pbm_t *)arg;
12690Sstevel@tonic-gate 	volatile uint64_t *ctrl_reg_p;
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate 	if (pbm_p->pbm_quiesce_count > 0) {
12720Sstevel@tonic-gate 		ctrl_reg_p = pbm_p->pbm_ctrl_reg;
12730Sstevel@tonic-gate 		*ctrl_reg_p = pbm_p->pbm_saved_ctrl_reg;
12740Sstevel@tonic-gate 	}
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 	return (B_TRUE);
12770Sstevel@tonic-gate }
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate static boolean_t
12800Sstevel@tonic-gate pci_pbm_debug_callb(void *arg, int code)
12810Sstevel@tonic-gate {
12820Sstevel@tonic-gate 	pbm_t *pbm_p = (pbm_t *)arg;
12830Sstevel@tonic-gate 	volatile uint64_t *ctrl_reg_p;
12840Sstevel@tonic-gate 	uint64_t ctrl_reg;
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate 	if (pbm_p->pbm_quiesce_count > 0) {
12870Sstevel@tonic-gate 		ctrl_reg_p = pbm_p->pbm_ctrl_reg;
12880Sstevel@tonic-gate 		if (code == 0) {
12890Sstevel@tonic-gate 			*ctrl_reg_p = pbm_p->pbm_saved_ctrl_reg;
12900Sstevel@tonic-gate 		} else {
12910Sstevel@tonic-gate 			ctrl_reg = pbm_p->pbm_saved_ctrl_reg;
12920Sstevel@tonic-gate 			ctrl_reg &= ~(SCHIZO_PCI_CTRL_ARB_EN_MASK |
12930Sstevel@tonic-gate 			    SCHIZO_PCI_CTRL_ARB_PARK);
12940Sstevel@tonic-gate 			*ctrl_reg_p = ctrl_reg;
12950Sstevel@tonic-gate 		}
12960Sstevel@tonic-gate 	}
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate 	return (B_TRUE);
12990Sstevel@tonic-gate }
13000Sstevel@tonic-gate 
13010Sstevel@tonic-gate void
13020Sstevel@tonic-gate pci_pbm_setup(pbm_t *pbm_p)
13030Sstevel@tonic-gate {
13040Sstevel@tonic-gate 	pci_t *pci_p = pbm_p->pbm_pci_p;
13050Sstevel@tonic-gate 	caddr_t a = pci_p->pci_address[0]; /* PBM block base VA */
13060Sstevel@tonic-gate 	uint64_t pa = va_to_pa(a);
13070Sstevel@tonic-gate 	extern int segkmem_reloc;
13080Sstevel@tonic-gate 
13090Sstevel@tonic-gate 	mutex_init(&pbm_p->pbm_sync_mutex, NULL, MUTEX_DRIVER,
13100Sstevel@tonic-gate 	    (void *)ipltospl(XCALL_PIL));
13110Sstevel@tonic-gate 
13120Sstevel@tonic-gate 	pbm_p->pbm_config_header = (config_header_t *)pci_p->pci_address[2];
13130Sstevel@tonic-gate 	pbm_p->pbm_ctrl_reg = (uint64_t *)(a + SCHIZO_PCI_CTRL_REG_OFFSET);
13140Sstevel@tonic-gate 	pbm_p->pbm_diag_reg = (uint64_t *)(a + SCHIZO_PCI_DIAG_REG_OFFSET);
13150Sstevel@tonic-gate 	pbm_p->pbm_async_flt_status_reg =
13160Sstevel@tonic-gate 		(uint64_t *)(a + SCHIZO_PCI_ASYNC_FLT_STATUS_REG_OFFSET);
13170Sstevel@tonic-gate 	pbm_p->pbm_async_flt_addr_reg =
13180Sstevel@tonic-gate 		(uint64_t *)(a + SCHIZO_PCI_ASYNC_FLT_ADDR_REG_OFFSET);
13190Sstevel@tonic-gate 	pbm_p->pbm_estar_reg = (uint64_t *)(a + SCHIZO_PCI_ESTAR_REG_OFFSET);
13200Sstevel@tonic-gate 	pbm_p->pbm_pcix_err_stat_reg = (uint64_t *)(a +
13210Sstevel@tonic-gate 	    XMITS_PCI_X_ERROR_STATUS_REG_OFFSET);
13220Sstevel@tonic-gate 	pbm_p->pbm_pci_ped_ctrl = (uint64_t *)(a +
13230Sstevel@tonic-gate 	    XMITS_PARITY_DETECT_REG_OFFSET);
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 	/*
13260Sstevel@tonic-gate 	 * Create a property to indicate that this node supports DVMA
13270Sstevel@tonic-gate 	 * page relocation.
13280Sstevel@tonic-gate 	 */
13290Sstevel@tonic-gate 	if (CHIP_TYPE(pci_p) != PCI_CHIP_TOMATILLO && segkmem_reloc != 0) {
13300Sstevel@tonic-gate 		pci_dvma_remap_enabled = 1;
13310Sstevel@tonic-gate 		(void) ndi_prop_create_boolean(DDI_DEV_T_NONE,
13320Sstevel@tonic-gate 		    pci_p->pci_dip, "dvma-remap-supported");
13330Sstevel@tonic-gate 	}
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate 	/*
13360Sstevel@tonic-gate 	 * Register a panic callback so we can unquiesce this bus
13370Sstevel@tonic-gate 	 * if it has been placed in the quiesced state.
13380Sstevel@tonic-gate 	 */
13390Sstevel@tonic-gate 	pbm_p->pbm_panic_cb_id = callb_add(pci_pbm_panic_callb,
13400Sstevel@tonic-gate 	    (void *)pbm_p, CB_CL_PANIC, "pci_panic");
13410Sstevel@tonic-gate 	pbm_p->pbm_debug_cb_id = callb_add(pci_pbm_panic_callb,
13420Sstevel@tonic-gate 	    (void *)pbm_p, CB_CL_ENTER_DEBUGGER, "pci_debug_enter");
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate 	if (CHIP_TYPE(pci_p) != PCI_CHIP_SCHIZO)
13450Sstevel@tonic-gate 		goto non_schizo;
13460Sstevel@tonic-gate 
13470Sstevel@tonic-gate 	if (PCI_CHIP_ID(pci_p) >= SCHIZO_VER_23) {
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate 		pbm_p->pbm_sync_reg_pa = pa + SCHIZO_PBM_DMA_SYNC_REG_OFFSET;
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate 		/*
13520Sstevel@tonic-gate 		 * This is a software workaround to fix schizo hardware bug.
13530Sstevel@tonic-gate 		 * Create a boolean property and its existence means consistent
13540Sstevel@tonic-gate 		 * dma sync should not be done while in prom. The usb polled
13550Sstevel@tonic-gate 		 * code (OHCI,EHCI) will check for this property and will not
13560Sstevel@tonic-gate 		 * do dma sync if this property exist.
13570Sstevel@tonic-gate 		 */
13580Sstevel@tonic-gate 		(void) ndi_prop_create_boolean(DDI_DEV_T_NONE,
13590Sstevel@tonic-gate 		    pci_p->pci_dip, "no-prom-cdma-sync");
13600Sstevel@tonic-gate 	}
13610Sstevel@tonic-gate 	return;
13620Sstevel@tonic-gate non_schizo:
13630Sstevel@tonic-gate 	if (CHIP_TYPE(pci_p) == PCI_CHIP_TOMATILLO) {
13640Sstevel@tonic-gate 		pci_dvma_sync_before_unmap = 1;
13650Sstevel@tonic-gate 		pa = pci_p->pci_cb_p->cb_icbase_pa;
13660Sstevel@tonic-gate 	}
13671295Sdanice 	if (CHIP_TYPE(pci_p) == PCI_CHIP_XMITS)
13681295Sdanice 		pbm_p->pbm_upper_retry_counter_reg =
13691295Sdanice 		    (uint64_t *)(a + XMITS_UPPER_RETRY_COUNTER_REG_OFFSET);
13701295Sdanice 
13710Sstevel@tonic-gate 	pbm_p->pbm_sync_reg_pa = pa + PBM_DMA_SYNC_PEND_REG_OFFSET;
13720Sstevel@tonic-gate }
13730Sstevel@tonic-gate 
13740Sstevel@tonic-gate void
13750Sstevel@tonic-gate pci_pbm_teardown(pbm_t *pbm_p)
13760Sstevel@tonic-gate {
13770Sstevel@tonic-gate 	(void) callb_delete(pbm_p->pbm_panic_cb_id);
13780Sstevel@tonic-gate 	(void) callb_delete(pbm_p->pbm_debug_cb_id);
13790Sstevel@tonic-gate }
13800Sstevel@tonic-gate 
13810Sstevel@tonic-gate uintptr_t
13820Sstevel@tonic-gate pci_ib_setup(ib_t *ib_p)
13830Sstevel@tonic-gate {
13840Sstevel@tonic-gate 	/*
13850Sstevel@tonic-gate 	 * Determine virtual addresses of bridge specific registers,
13860Sstevel@tonic-gate 	 */
13870Sstevel@tonic-gate 	pci_t *pci_p = ib_p->ib_pci_p;
13880Sstevel@tonic-gate 	uintptr_t a = (uintptr_t)pci_p->pci_address[0];
13890Sstevel@tonic-gate 
13900Sstevel@tonic-gate 	ib_p->ib_ign = PCI_ID_TO_IGN(pci_p->pci_id);
13910Sstevel@tonic-gate 	ib_p->ib_max_ino = SCHIZO_MAX_INO;
13920Sstevel@tonic-gate 	ib_p->ib_slot_intr_map_regs = a + SCHIZO_IB_SLOT_INTR_MAP_REG_OFFSET;
13930Sstevel@tonic-gate 	ib_p->ib_intr_map_regs = a + SCHIZO_IB_INTR_MAP_REG_OFFSET;
13940Sstevel@tonic-gate 	ib_p->ib_slot_clear_intr_regs =
13950Sstevel@tonic-gate 		a + SCHIZO_IB_CLEAR_INTR_REG_OFFSET;
13960Sstevel@tonic-gate 	return (a);
13970Sstevel@tonic-gate }
13980Sstevel@tonic-gate 
13990Sstevel@tonic-gate void
14000Sstevel@tonic-gate pci_sc_setup(sc_t *sc_p)
14010Sstevel@tonic-gate {
14020Sstevel@tonic-gate 	pci_t *pci_p = sc_p->sc_pci_p;
14030Sstevel@tonic-gate 	uintptr_t a;
14040Sstevel@tonic-gate 
14050Sstevel@tonic-gate 	/*
14060Sstevel@tonic-gate 	 * Determine the virtual addresses of the stream cache
14070Sstevel@tonic-gate 	 * control/status and flush registers.
14080Sstevel@tonic-gate 	 */
14090Sstevel@tonic-gate 	a = (uintptr_t)pci_p->pci_address[0];	/* PCICSRBase */
14100Sstevel@tonic-gate 	sc_p->sc_ctrl_reg = (uint64_t *)(a + SCHIZO_SC_CTRL_REG_OFFSET);
14110Sstevel@tonic-gate 	sc_p->sc_invl_reg = (uint64_t *)(a + SCHIZO_SC_INVL_REG_OFFSET);
14120Sstevel@tonic-gate 	sc_p->sc_sync_reg = (uint64_t *)(a + SCHIZO_SC_SYNC_REG_OFFSET);
14130Sstevel@tonic-gate 	sc_p->sc_ctx_invl_reg = (uint64_t *)(a + SCHIZO_SC_CTX_INVL_REG_OFFSET);
14140Sstevel@tonic-gate 	sc_p->sc_ctx_match_reg =
14150Sstevel@tonic-gate 		(uint64_t *)(a + SCHIZO_SC_CTX_MATCH_REG_OFFSET);
14160Sstevel@tonic-gate 
14170Sstevel@tonic-gate 	/*
14180Sstevel@tonic-gate 	 * Determine the virtual addresses of the streaming cache
14190Sstevel@tonic-gate 	 * diagnostic access registers.
14200Sstevel@tonic-gate 	 */
14210Sstevel@tonic-gate 	sc_p->sc_data_diag_acc = (uint64_t *)(a + SCHIZO_SC_DATA_DIAG_OFFSET);
14220Sstevel@tonic-gate 	sc_p->sc_tag_diag_acc = (uint64_t *)(a + SCHIZO_SC_TAG_DIAG_OFFSET);
14230Sstevel@tonic-gate 	sc_p->sc_ltag_diag_acc = (uint64_t *)(a + SCHIZO_SC_LTAG_DIAG_OFFSET);
14240Sstevel@tonic-gate }
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate /*ARGSUSED*/
14270Sstevel@tonic-gate int
14280Sstevel@tonic-gate pci_get_numproxy(dev_info_t *dip)
14290Sstevel@tonic-gate {
14300Sstevel@tonic-gate 	/*
14310Sstevel@tonic-gate 	 * Schizo does not support interrupt proxies.
14320Sstevel@tonic-gate 	 */
14330Sstevel@tonic-gate 	return (0);
14340Sstevel@tonic-gate }
14350Sstevel@tonic-gate 
14360Sstevel@tonic-gate /*
14370Sstevel@tonic-gate  * pcisch error handling 101:
14380Sstevel@tonic-gate  *
14390Sstevel@tonic-gate  * The various functions below are responsible for error handling. Given
14400Sstevel@tonic-gate  * a particular error, they must gather the appropriate state, report all
14410Sstevel@tonic-gate  * errors with correct payload, and attempt recovery where ever possible.
14420Sstevel@tonic-gate  *
14430Sstevel@tonic-gate  * Recovery in the context of this driver is being able notify a leaf device
14440Sstevel@tonic-gate  * of the failed transaction. This leaf device may either be the master or
14450Sstevel@tonic-gate  * target for this transaction and may have already received an error
14460Sstevel@tonic-gate  * notification via a PCI interrupt. Notification is done via DMA and access
14470Sstevel@tonic-gate  * handles. If we capture an address for the transaction then we can map it
14480Sstevel@tonic-gate  * to a handle(if the leaf device is fma-compliant) and fault the handle as
14490Sstevel@tonic-gate  * well as call the device driver registered callback.
14500Sstevel@tonic-gate  *
14510Sstevel@tonic-gate  * The hardware can either interrupt or trap upon detection of an error, in
14520Sstevel@tonic-gate  * some rare cases it also causes a fatal reset.
14530Sstevel@tonic-gate  *
14540Sstevel@tonic-gate  * cb_buserr_intr() is responsible for handling control block
14550Sstevel@tonic-gate  * errors(errors which stem from the host bus side of the bridge). Since
14560Sstevel@tonic-gate  * we support multiple chips and host bus standards, cb_buserr_intr will
14570Sstevel@tonic-gate  * call a bus specific error handler to report and handle the detected
14580Sstevel@tonic-gate  * error. Since this error can either affect or orginate from either of the
14590Sstevel@tonic-gate  * two PCI busses which are connected to the bridge, we need to call
14600Sstevel@tonic-gate  * pci_pbm_err_handler() for each bus as well to report their errors. We
14610Sstevel@tonic-gate  * also need to gather possible errors which have been detected by their
14620Sstevel@tonic-gate  * compliant children(via ndi_fm_handler_dispatch()).
14630Sstevel@tonic-gate  *
14640Sstevel@tonic-gate  * pbm_error_intr() and ecc_intr() are responsible for PCI Block Module
14650Sstevel@tonic-gate  * errors(generic PCI + bridge specific) and ECC errors, respectively. They
14660Sstevel@tonic-gate  * are common between pcisch and pcipsy and therefore exist in pci_pbm.c and
14670Sstevel@tonic-gate  * pci_ecc.c. To support error handling certain chip specific handlers
14680Sstevel@tonic-gate  * must exist and they are defined below.
14690Sstevel@tonic-gate  *
14700Sstevel@tonic-gate  * cpu_deferred_error() and cpu_async_error(), handle the traps that may
14710Sstevel@tonic-gate  * have originated from IO space. They call into the registered IO callbacks
14720Sstevel@tonic-gate  * to report and handle errors that may have caused the trap.
14730Sstevel@tonic-gate  *
14740Sstevel@tonic-gate  * pci_pbm_err_handler() is called by pbm_error_intr() or pci_err_callback()
14750Sstevel@tonic-gate  * (generic fma callback for pcipsy/pcisch, pci_fm.c). pci_err_callback() is
14760Sstevel@tonic-gate  * called when the CPU has trapped because of a possible IO error(TO/BERR/UE).
14770Sstevel@tonic-gate  * It will call pci_pbm_err_handler() to report and handle all PCI/PBM/IOMMU
14780Sstevel@tonic-gate  * related errors which are detected by the chip.
14790Sstevel@tonic-gate  *
14800Sstevel@tonic-gate  * pci_pbm_err_handler() calls a generic interface pbm_afsr_report()(pci_pbm.c)
14810Sstevel@tonic-gate  * to report the pbm specific errors and attempt to map the failed address
14820Sstevel@tonic-gate  * (if captured) to a device instance. pbm_afsr_report() calls a chip specific
14830Sstevel@tonic-gate  * interface to interpret the afsr bits pci_pbm_classify()(pcisch.c/pcipsy.c).
14840Sstevel@tonic-gate  * pci_pbm_err_handler() also calls iommu_err_handler() to handle IOMMU related
14850Sstevel@tonic-gate  * errors.
14860Sstevel@tonic-gate  *
14870Sstevel@tonic-gate  * iommu_err_handler() can recover from most errors, as long as the requesting
14880Sstevel@tonic-gate  * device is notified and the iommu can be flushed. If an IOMMU error occurs
14890Sstevel@tonic-gate  * due to a UE then it will be passed on to the ecc_err_handler() for
14900Sstevel@tonic-gate  * subsequent handling.
14910Sstevel@tonic-gate  *
14920Sstevel@tonic-gate  * ecc_err_handler()(pci_ecc.c) also calls a chip specific interface to
14930Sstevel@tonic-gate  * interpret the afsr, pci_ecc_classify(). ecc_err_handler() also calls
14940Sstevel@tonic-gate  * pci_pbm_err_handler() to report any pbm errors detected.
14950Sstevel@tonic-gate  *
14960Sstevel@tonic-gate  * To make sure that the trap code and the interrupt code are not going
14970Sstevel@tonic-gate  * to step on each others toes we have a per chip pci_fm_mutex. This also
14980Sstevel@tonic-gate  * makes it necessary for us to be caution while we are at a high PIL, so
14990Sstevel@tonic-gate  * that we do not cause a subsequent trap that causes us to hang.
15000Sstevel@tonic-gate  *
15010Sstevel@tonic-gate  * The attempt to commonize code was meant to keep in line with the current
15020Sstevel@tonic-gate  * pci driver implementation and it was not meant to confuse. If you are
15030Sstevel@tonic-gate  * confused then don't worry, I was too.
15040Sstevel@tonic-gate  *
15050Sstevel@tonic-gate  */
15060Sstevel@tonic-gate static void
15070Sstevel@tonic-gate pci_cb_errstate_get(cb_t *cb_p, cb_errstate_t *cb_err_p)
15080Sstevel@tonic-gate {
15090Sstevel@tonic-gate 	uint64_t pa = cb_p->cb_base_pa;
15100Sstevel@tonic-gate 	int	i;
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate 	bzero(cb_err_p, sizeof (cb_errstate_t));
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cb_p->cb_pci_cmn_p->pci_fm_mutex));
15150Sstevel@tonic-gate 
15160Sstevel@tonic-gate 	cb_err_p->cb_bridge_type = PCI_BRIDGE_TYPE(cb_p->cb_pci_cmn_p);
15170Sstevel@tonic-gate 
15180Sstevel@tonic-gate 	cb_err_p->cb_csr = lddphysio(pa + SCHIZO_CB_CSR_OFFSET);
15190Sstevel@tonic-gate 	cb_err_p->cb_err = lddphysio(pa + SCHIZO_CB_ERRCTRL_OFFSET);
15200Sstevel@tonic-gate 	cb_err_p->cb_intr = lddphysio(pa + SCHIZO_CB_INTCTRL_OFFSET);
15210Sstevel@tonic-gate 	cb_err_p->cb_elog = lddphysio(pa + SCHIZO_CB_ERRLOG_OFFSET);
15220Sstevel@tonic-gate 	cb_err_p->cb_ecc = lddphysio(pa + SCHIZO_CB_ECCCTRL_OFFSET);
15230Sstevel@tonic-gate 	cb_err_p->cb_ue_afsr = lddphysio(pa + SCHIZO_CB_UEAFSR_OFFSET);
15240Sstevel@tonic-gate 	cb_err_p->cb_ue_afar = lddphysio(pa + SCHIZO_CB_UEAFAR_OFFSET);
15250Sstevel@tonic-gate 	cb_err_p->cb_ce_afsr = lddphysio(pa + SCHIZO_CB_CEAFSR_OFFSET);
15260Sstevel@tonic-gate 	cb_err_p->cb_ce_afar = lddphysio(pa + SCHIZO_CB_CEAFAR_OFFSET);
15270Sstevel@tonic-gate 
15280Sstevel@tonic-gate 	if ((CB_CHIP_TYPE((cb_t *)cb_p)) == PCI_CHIP_XMITS) {
15290Sstevel@tonic-gate 		cb_err_p->cb_first_elog = lddphysio(pa +
15300Sstevel@tonic-gate 				XMITS_CB_FIRST_ERROR_LOG);
15310Sstevel@tonic-gate 		cb_err_p->cb_first_eaddr = lddphysio(pa +
15320Sstevel@tonic-gate 				XMITS_CB_FIRST_ERROR_ADDR);
15330Sstevel@tonic-gate 		cb_err_p->cb_leaf_status = lddphysio(pa +
15340Sstevel@tonic-gate 				XMITS_CB_FIRST_ERROR_ADDR);
15350Sstevel@tonic-gate 	}
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate 	/* Gather PBM state information for both sides of this chip */
15380Sstevel@tonic-gate 	for (i = 0; i < 2; i++) {
15390Sstevel@tonic-gate 		if (cb_p->cb_pci_cmn_p->pci_p[i] == NULL)
15400Sstevel@tonic-gate 			continue;
15410Sstevel@tonic-gate 		pci_pbm_errstate_get(((cb_t *)cb_p)->cb_pci_cmn_p->
15420Sstevel@tonic-gate 					    pci_p[i], &cb_err_p->cb_pbm[i]);
15430Sstevel@tonic-gate 	}
15440Sstevel@tonic-gate }
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate static void
15470Sstevel@tonic-gate pci_cb_clear_error(cb_t *cb_p, cb_errstate_t *cb_err_p)
15480Sstevel@tonic-gate {
15490Sstevel@tonic-gate 	uint64_t pa = ((cb_t *)cb_p)->cb_base_pa;
15500Sstevel@tonic-gate 
15510Sstevel@tonic-gate 	stdphysio(pa + SCHIZO_CB_ERRLOG_OFFSET, cb_err_p->cb_elog);
15520Sstevel@tonic-gate }
15530Sstevel@tonic-gate 
15540Sstevel@tonic-gate static cb_fm_err_t safari_err_tbl[] = {
15550Sstevel@tonic-gate 	SAFARI_BAD_CMD,		SCHIZO_CB_ELOG_BAD_CMD,		CB_FATAL,
15560Sstevel@tonic-gate 	SAFARI_SSM_DIS,		SCHIZO_CB_ELOG_SSM_DIS,		CB_FATAL,
15570Sstevel@tonic-gate 	SAFARI_BAD_CMD_PCIA, 	SCHIZO_CB_ELOG_BAD_CMD_PCIA,	CB_FATAL,
15580Sstevel@tonic-gate 	SAFARI_BAD_CMD_PCIB, 	SCHIZO_CB_ELOG_BAD_CMD_PCIB,	CB_FATAL,
15590Sstevel@tonic-gate 	SAFARI_PAR_ERR_INT_PCIB, XMITS_CB_ELOG_PAR_ERR_INT_PCIB, CB_FATAL,
15600Sstevel@tonic-gate 	SAFARI_PAR_ERR_INT_PCIA, XMITS_CB_ELOG_PAR_ERR_INT_PCIA, CB_FATAL,
15610Sstevel@tonic-gate 	SAFARI_PAR_ERR_INT_SAF,	XMITS_CB_ELOG_PAR_ERR_INT_SAF,	CB_FATAL,
15620Sstevel@tonic-gate 	SAFARI_PLL_ERR_PCIB,	XMITS_CB_ELOG_PLL_ERR_PCIB,	CB_FATAL,
15630Sstevel@tonic-gate 	SAFARI_PLL_ERR_PCIA,	XMITS_CB_ELOG_PLL_ERR_PCIA,	CB_FATAL,
15640Sstevel@tonic-gate 	SAFARI_PLL_ERR_SAF,	XMITS_CB_ELOG_PLL_ERR_SAF,	CB_FATAL,
15650Sstevel@tonic-gate 	SAFARI_SAF_CIQ_TO,	SCHIZO_CB_ELOG_SAF_CIQ_TO,	CB_FATAL,
15660Sstevel@tonic-gate 	SAFARI_SAF_LPQ_TO,	SCHIZO_CB_ELOG_SAF_LPQ_TO,	CB_FATAL,
15670Sstevel@tonic-gate 	SAFARI_SAF_SFPQ_TO,	SCHIZO_CB_ELOG_SAF_SFPQ_TO,	CB_FATAL,
15680Sstevel@tonic-gate 	SAFARI_APERR,		SCHIZO_CB_ELOG_ADDR_PAR_ERR,	CB_FATAL,
15690Sstevel@tonic-gate 	SAFARI_UNMAP_ERR,	SCHIZO_CB_ELOG_UNMAP_ERR,	CB_FATAL,
15700Sstevel@tonic-gate 	SAFARI_BUS_ERR,		SCHIZO_CB_ELOG_BUS_ERR,		CB_FATAL,
15710Sstevel@tonic-gate 	SAFARI_TO_ERR,		SCHIZO_CB_ELOG_TO_ERR,		CB_FATAL,
15720Sstevel@tonic-gate 	SAFARI_DSTAT_ERR,	SCHIZO_CB_ELOG_DSTAT_ERR,	CB_FATAL,
15730Sstevel@tonic-gate 	SAFARI_SAF_UFPQ_TO,	SCHIZO_CB_ELOG_SAF_UFPQ_TO,	CB_FATAL,
15740Sstevel@tonic-gate 	SAFARI_CPU0_PAR_SINGLE,	SCHIZO_CB_ELOG_CPU0_PAR_SINGLE,	CB_FATAL,
15750Sstevel@tonic-gate 	SAFARI_CPU0_PAR_BIDI,	SCHIZO_CB_ELOG_CPU0_PAR_BIDI,	CB_FATAL,
15760Sstevel@tonic-gate 	SAFARI_CPU1_PAR_SINGLE,	SCHIZO_CB_ELOG_CPU1_PAR_SINGLE,	CB_FATAL,
15770Sstevel@tonic-gate 	SAFARI_CPU1_PAR_BIDI,	SCHIZO_CB_ELOG_CPU1_PAR_BIDI,	CB_FATAL,
15780Sstevel@tonic-gate 	NULL,			NULL,				NULL,
15790Sstevel@tonic-gate };
15800Sstevel@tonic-gate 
15810Sstevel@tonic-gate /*
15820Sstevel@tonic-gate  * Function used to handle and log Safari bus errors.
15830Sstevel@tonic-gate  */
15840Sstevel@tonic-gate static int
15850Sstevel@tonic-gate safari_err_handler(dev_info_t *dip, uint64_t fme_ena,
15860Sstevel@tonic-gate 		cb_errstate_t *cb_err_p)
15870Sstevel@tonic-gate {
15880Sstevel@tonic-gate 	int	i;
15890Sstevel@tonic-gate 	int	fatal = 0;
15900Sstevel@tonic-gate 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
15910Sstevel@tonic-gate 	pci_common_t *cmn_p = pci_p->pci_common_p;
15920Sstevel@tonic-gate 
15930Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cmn_p->pci_fm_mutex));
15940Sstevel@tonic-gate 
15950Sstevel@tonic-gate 	for (i = 0; safari_err_tbl[i].cb_err_class != NULL; i++) {
15960Sstevel@tonic-gate 		if (cb_err_p->cb_elog & safari_err_tbl[i].cb_reg_bit) {
15970Sstevel@tonic-gate 			cb_err_p->cb_err_class = safari_err_tbl[i].cb_err_class;
15980Sstevel@tonic-gate 			cb_ereport_post(dip, fme_ena, cb_err_p);
15990Sstevel@tonic-gate 			fatal += safari_err_tbl[i].cb_fatal;
16000Sstevel@tonic-gate 		}
16010Sstevel@tonic-gate 	}
16020Sstevel@tonic-gate 
16030Sstevel@tonic-gate 	if (fatal)
16040Sstevel@tonic-gate 		return (DDI_FM_FATAL);
16050Sstevel@tonic-gate 	return (DDI_FM_OK);
16060Sstevel@tonic-gate 
16070Sstevel@tonic-gate }
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate /*
16100Sstevel@tonic-gate  * Check pbm va log register for captured errant address, and fail handle
16110Sstevel@tonic-gate  * if in per device cache.
16120Sstevel@tonic-gate  * Called from jbus_err_handler.
16130Sstevel@tonic-gate  */
16140Sstevel@tonic-gate static int
16150Sstevel@tonic-gate jbus_check_va_log(cb_t *cb_p, uint64_t fme_ena,
16160Sstevel@tonic-gate     cb_errstate_t *cb_err_p)
16170Sstevel@tonic-gate {
16180Sstevel@tonic-gate 	int i;
16190Sstevel@tonic-gate 	int ret = DDI_FM_FATAL;
16200Sstevel@tonic-gate 	pci_common_t *cmn_p = cb_p->cb_pci_cmn_p;
16210Sstevel@tonic-gate 
16220Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cmn_p->pci_fm_mutex));
16230Sstevel@tonic-gate 	/*
16240Sstevel@tonic-gate 	 * Check VA log register for address associated with error,
16250Sstevel@tonic-gate 	 * if no address is registered then return failure
16260Sstevel@tonic-gate 	 */
16270Sstevel@tonic-gate 	for (i = 0; i < 2; i++) {
16280Sstevel@tonic-gate 
16290Sstevel@tonic-gate 		if (cb_p->cb_pci_cmn_p->pci_p[i] == NULL)
16300Sstevel@tonic-gate 			continue;
16310Sstevel@tonic-gate 		/*
16320Sstevel@tonic-gate 		 * Look up and fault handle associated with
16330Sstevel@tonic-gate 		 * logged DMA address
16340Sstevel@tonic-gate 		 */
16350Sstevel@tonic-gate 		if (cb_err_p->cb_pbm[i].pbm_va_log) {
16361865Sdilpreet 			void *addr = (void *)&cb_err_p->cb_pbm[i].pbm_va_log;
16371865Sdilpreet 			ret = ndi_fmc_error(cb_p->cb_pci_cmn_p->pci_p[i]->
16381865Sdilpreet 					pci_dip, NULL, DMA_HANDLE, fme_ena,
16391865Sdilpreet 					(void *)addr);
16400Sstevel@tonic-gate 			if (ret == DDI_FM_NONFATAL)
16410Sstevel@tonic-gate 				break;
16420Sstevel@tonic-gate 		}
16430Sstevel@tonic-gate 	}
16440Sstevel@tonic-gate 	return (ret);
16450Sstevel@tonic-gate }
16460Sstevel@tonic-gate 
16470Sstevel@tonic-gate static cb_fm_err_t jbus_err_tbl[] = {
16480Sstevel@tonic-gate 	JBUS_APERR,		SCHIZO_CB_ELOG_ADDR_PAR_ERR,	CB_FATAL,
16490Sstevel@tonic-gate 	JBUS_PWR_DATA_PERR,	TOMATILLO_CB_ELOG_WR_DATA_PAR_ERR, CB_FATAL,
16500Sstevel@tonic-gate 	JBUS_DRD_DATA_PERR,	TOMATILLO_CB_ELOG_RD_DATA_PAR_ERR, CB_NONFATAL,
16510Sstevel@tonic-gate 	JBUS_CTL_PERR,		TOMATILLO_CB_ELOG_CTL_PAR_ERR,	CB_FATAL,
16520Sstevel@tonic-gate 	JBUS_ILL_BYTE_EN,	TOMATILLO_CB_ELOG_ILL_BYTE_EN,	CB_FATAL,
16530Sstevel@tonic-gate 	JBUS_ILL_COH_IN,	TOMATILLO_CB_ELOG_ILL_COH_IN,	CB_FATAL,
16540Sstevel@tonic-gate 	JBUS_SNOOP_ERR_RD,	TOMATILLO_CB_ELOG_SNOOP_ERR_RD,	CB_FATAL,
16550Sstevel@tonic-gate 	JBUS_SNOOP_ERR_RDS,	TOMATILLO_CB_ELOG_SNOOP_ERR_RDS, CB_FATAL,
16560Sstevel@tonic-gate 	JBUS_SNOOP_ERR_RDSA,	TOMATILLO_CB_ELOG_SNOOP_ERR_RDSA, CB_FATAL,
16570Sstevel@tonic-gate 	JBUS_SNOOP_ERR_OWN,	TOMATILLO_CB_ELOG_SNOOP_ERR_OWN, CB_FATAL,
16580Sstevel@tonic-gate 	JBUS_SNOOP_ERR_RDO,	TOMATILLO_CB_ELOG_SNOOP_ERR_RDO, CB_FATAL,
16590Sstevel@tonic-gate 	JBUS_SNOOP_ERR_PCI,	TOMATILLO_CB_ELOG_SNOOP_ERR_PCI, CB_FATAL,
16600Sstevel@tonic-gate 	JBUS_SNOOP_ERR_GR,	TOMATILLO_CB_ELOG_SNOOP_ERR_GR,	CB_FATAL,
16610Sstevel@tonic-gate 	JBUS_SNOOP_ERR,		TOMATILLO_CB_ELOG_SNOOP_ERR,	CB_FATAL,
16620Sstevel@tonic-gate 	JBUS_BAD_CMD,		SCHIZO_CB_ELOG_BAD_CMD,		CB_FATAL,
16630Sstevel@tonic-gate 	JBUS_UNMAP_ERR,		SCHIZO_CB_ELOG_UNMAP_ERR,	CB_NONFATAL,
16640Sstevel@tonic-gate 	JBUS_TO_EXP_ERR,	TOMATILLO_CB_ELOG_TO_EXP_ERR,	CB_NONFATAL,
16650Sstevel@tonic-gate 	JBUS_TO_ERR,		SCHIZO_CB_ELOG_TO_ERR,		CB_NONFATAL,
16660Sstevel@tonic-gate 	JBUS_BUS_ERR,		SCHIZO_CB_ELOG_BUS_ERR,		CB_NONFATAL,
16670Sstevel@tonic-gate 	NULL,			NULL,				NULL,
16680Sstevel@tonic-gate };
16690Sstevel@tonic-gate 
16700Sstevel@tonic-gate /*
16710Sstevel@tonic-gate  * Function used to handle and log Jbus errors.
16720Sstevel@tonic-gate  */
16730Sstevel@tonic-gate static int
16740Sstevel@tonic-gate jbus_err_handler(dev_info_t *dip, uint64_t fme_ena,
16750Sstevel@tonic-gate     cb_errstate_t *cb_err_p)
16760Sstevel@tonic-gate {
16770Sstevel@tonic-gate 	int	fatal = 0;
16780Sstevel@tonic-gate 	int	nonfatal = 0;
16790Sstevel@tonic-gate 	int	i;
16800Sstevel@tonic-gate 	pci_t	*pci_p = get_pci_soft_state(ddi_get_instance(dip));
16810Sstevel@tonic-gate 	cb_t	*cb_p = pci_p->pci_cb_p;
16820Sstevel@tonic-gate 
16830Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pci_p->pci_common_p->pci_fm_mutex));
16840Sstevel@tonic-gate 
16850Sstevel@tonic-gate 	for (i = 0; jbus_err_tbl[i].cb_err_class != NULL; i++) {
16860Sstevel@tonic-gate 		if (!(cb_err_p->cb_elog & jbus_err_tbl[i].cb_reg_bit))
16870Sstevel@tonic-gate 			continue;
16880Sstevel@tonic-gate 		cb_err_p->cb_err_class = jbus_err_tbl[i].cb_err_class;
16890Sstevel@tonic-gate 		if (jbus_err_tbl[i].cb_fatal) {
16900Sstevel@tonic-gate 			fatal += jbus_err_tbl[i].cb_fatal;
16910Sstevel@tonic-gate 			continue;
16920Sstevel@tonic-gate 		}
16930Sstevel@tonic-gate 		if (jbus_check_va_log(cb_p, fme_ena, cb_err_p)
16940Sstevel@tonic-gate 				!= DDI_FM_NONFATAL) {
16950Sstevel@tonic-gate 			fatal++;
16960Sstevel@tonic-gate 		}
16970Sstevel@tonic-gate 		cb_ereport_post(dip, fme_ena, cb_err_p);
16980Sstevel@tonic-gate 	}
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate 	return (fatal ? DDI_FM_FATAL : (nonfatal ? DDI_FM_NONFATAL :
17010Sstevel@tonic-gate 				DDI_FM_OK));
17020Sstevel@tonic-gate }
17030Sstevel@tonic-gate 
17040Sstevel@tonic-gate /*
17050Sstevel@tonic-gate  * Control Block error interrupt handler.
17060Sstevel@tonic-gate  */
17070Sstevel@tonic-gate uint_t
17080Sstevel@tonic-gate cb_buserr_intr(caddr_t a)
17090Sstevel@tonic-gate {
17100Sstevel@tonic-gate 	cb_t *cb_p = (cb_t *)a;
17110Sstevel@tonic-gate 	pci_common_t *cmn_p = cb_p->cb_pci_cmn_p;
17120Sstevel@tonic-gate 	pci_t *pci_p = cmn_p->pci_p[0];
17130Sstevel@tonic-gate 	cb_errstate_t cb_err;
17140Sstevel@tonic-gate 	ddi_fm_error_t derr;
17150Sstevel@tonic-gate 	int ret = DDI_FM_FATAL;
17160Sstevel@tonic-gate 	int i;
17170Sstevel@tonic-gate 
17180Sstevel@tonic-gate 	if (pci_p == NULL)
17190Sstevel@tonic-gate 		pci_p = cmn_p->pci_p[1];
17200Sstevel@tonic-gate 
17210Sstevel@tonic-gate 	bzero(&derr, sizeof (ddi_fm_error_t));
17220Sstevel@tonic-gate 	derr.fme_version = DDI_FME_VERSION;
17230Sstevel@tonic-gate 	derr.fme_ena = fm_ena_generate(0, FM_ENA_FMT1);
17240Sstevel@tonic-gate 
17250Sstevel@tonic-gate 	mutex_enter(&cmn_p->pci_fm_mutex);
17260Sstevel@tonic-gate 
17270Sstevel@tonic-gate 	pci_cb_errstate_get(cb_p, &cb_err);
17280Sstevel@tonic-gate 
17290Sstevel@tonic-gate 	if (CB_CHIP_TYPE(cb_p) == PCI_CHIP_TOMATILLO)
17300Sstevel@tonic-gate 		ret = jbus_err_handler(pci_p->pci_dip, derr.fme_ena, &cb_err);
17310Sstevel@tonic-gate 	else if ((CB_CHIP_TYPE(cb_p) == PCI_CHIP_SCHIZO) ||
17320Sstevel@tonic-gate 			(CB_CHIP_TYPE(cb_p) == PCI_CHIP_XMITS))
17330Sstevel@tonic-gate 		ret = safari_err_handler(pci_p->pci_dip, derr.fme_ena,
17340Sstevel@tonic-gate 		    &cb_err);
17350Sstevel@tonic-gate 
17360Sstevel@tonic-gate 	/*
17370Sstevel@tonic-gate 	 * Check for related errors in PBM and IOMMU. The IOMMU could cause
17380Sstevel@tonic-gate 	 * a timeout on the jbus due to an IOMMU miss, so we need to check and
17390Sstevel@tonic-gate 	 * log the IOMMU error registers.
17400Sstevel@tonic-gate 	 */
17410Sstevel@tonic-gate 	for (i = 0; i < 2; i++) {
17420Sstevel@tonic-gate 		if (cmn_p->pci_p[i] == NULL)
17430Sstevel@tonic-gate 			continue;
17440Sstevel@tonic-gate 		if (pci_pbm_err_handler(cmn_p->pci_p[i]->pci_dip, &derr,
17450Sstevel@tonic-gate 		    (void *)cmn_p->pci_p[i], PCI_CB_CALL) == DDI_FM_FATAL)
17460Sstevel@tonic-gate 			ret = DDI_FM_FATAL;
17470Sstevel@tonic-gate 	}
17480Sstevel@tonic-gate 
17490Sstevel@tonic-gate 	/* Cleanup and reset error bits */
17500Sstevel@tonic-gate 	(void) pci_cb_clear_error(cb_p, &cb_err);
17510Sstevel@tonic-gate 	mutex_exit(&cmn_p->pci_fm_mutex);
17520Sstevel@tonic-gate 
17530Sstevel@tonic-gate 	if (ret == DDI_FM_FATAL) {
17540Sstevel@tonic-gate 		fm_panic("Fatal System Bus Error has occurred\n");
17550Sstevel@tonic-gate 	}
17560Sstevel@tonic-gate 
17570Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
17580Sstevel@tonic-gate }
17590Sstevel@tonic-gate 
17600Sstevel@tonic-gate static ecc_fm_err_t ecc_err_tbl[] = {
1761*4039Skd93003 	PCI_ECC_PIO_UE, COMMON_ECC_AFSR_E_PIO, CBNINTR_UE,
17620Sstevel@tonic-gate 	PBM_PRIMARY, SCHIZO_ECC_AFAR_PIOW_UPA64S, SCH_REG_UPA,
17630Sstevel@tonic-gate 	ACC_HANDLE,
17640Sstevel@tonic-gate 
1765*4039Skd93003 	PCI_ECC_PIO_UE, COMMON_ECC_AFSR_E_PIO, CBNINTR_UE,
17660Sstevel@tonic-gate 	PBM_PRIMARY, SCHIZO_ECC_AFAR_PIOW_PCIA_REG, SCH_REG_PCIA_REG,
17670Sstevel@tonic-gate 	ACC_HANDLE,
17680Sstevel@tonic-gate 
1769*4039Skd93003 	PCI_ECC_PIO_UE, COMMON_ECC_AFSR_E_PIO, CBNINTR_UE,
17700Sstevel@tonic-gate 	PBM_PRIMARY, SCHIZO_ECC_AFAR_PIOW_PCIA_MEM, SCH_REG_PCIA_MEM,
17710Sstevel@tonic-gate 	ACC_HANDLE,
17720Sstevel@tonic-gate 
1773*4039Skd93003 	PCI_ECC_PIO_UE, COMMON_ECC_AFSR_E_PIO, CBNINTR_UE,
17740Sstevel@tonic-gate 	PBM_PRIMARY, SCHIZO_ECC_AFAR_PIOW_PCIA_CFGIO, SCH_REG_PCIA_CFGIO,
17750Sstevel@tonic-gate 	ACC_HANDLE,
17760Sstevel@tonic-gate 
1777*4039Skd93003 	PCI_ECC_PIO_UE, COMMON_ECC_AFSR_E_PIO, CBNINTR_UE,
17780Sstevel@tonic-gate 	PBM_PRIMARY, SCHIZO_ECC_AFAR_PIOW_PCIB_REG, SCH_REG_PCIB_REG,
17790Sstevel@tonic-gate 	ACC_HANDLE,
17800Sstevel@tonic-gate 
1781*4039Skd93003 	PCI_ECC_PIO_UE, COMMON_ECC_AFSR_E_PIO, CBNINTR_UE,
17820Sstevel@tonic-gate 	PBM_PRIMARY, SCHIZO_ECC_AFAR_PIOW_PCIB_MEM, SCH_REG_PCIB_MEM,
17830Sstevel@tonic-gate 	ACC_HANDLE,
17840Sstevel@tonic-gate 
1785*4039Skd93003 	PCI_ECC_PIO_UE, COMMON_ECC_AFSR_E_PIO, CBNINTR_UE,
17860Sstevel@tonic-gate 	PBM_PRIMARY, SCHIZO_ECC_AFAR_PIOW_PCIB_CFGIO, SCH_REG_PCIB_CFGIO,
17870Sstevel@tonic-gate 	ACC_HANDLE,
17880Sstevel@tonic-gate 
1789*4039Skd93003 	PCI_ECC_PIO_UE, COMMON_ECC_AFSR_E_PIO, CBNINTR_UE,
17900Sstevel@tonic-gate 	PBM_PRIMARY, SCHIZO_ECC_AFAR_PIOW_SAFARI_REGS, SCH_REG_SAFARI_REGS,
17910Sstevel@tonic-gate 	ACC_HANDLE,
17920Sstevel@tonic-gate 
1793*4039Skd93003 	PCI_ECC_SEC_PIO_UE, COMMON_ECC_AFSR_E_PIO,  CBNINTR_UE,
17940Sstevel@tonic-gate 	PBM_SECONDARY, NULL, NULL, ACC_HANDLE,
17950Sstevel@tonic-gate 
1796*4039Skd93003 	PCI_ECC_PIO_CE, COMMON_ECC_AFSR_E_PIO,  CBNINTR_CE,
17970Sstevel@tonic-gate 	PBM_PRIMARY, NULL, NULL, ACC_HANDLE,
17980Sstevel@tonic-gate 
1799*4039Skd93003 	PCI_ECC_SEC_PIO_CE, COMMON_ECC_AFSR_E_PIO,  CBNINTR_CE,
18000Sstevel@tonic-gate 	PBM_SECONDARY, NULL, NULL, ACC_HANDLE,
18010Sstevel@tonic-gate 
1802*4039Skd93003 	PCI_ECC_DRD_UE, COMMON_ECC_AFSR_E_DRD, CBNINTR_UE,
18030Sstevel@tonic-gate 	PBM_PRIMARY, NULL, NULL, DMA_HANDLE,
18040Sstevel@tonic-gate 
1805*4039Skd93003 	PCI_ECC_SEC_DRD_UE, COMMON_ECC_AFSR_E_DRD, CBNINTR_UE,
18060Sstevel@tonic-gate 	PBM_SECONDARY, NULL, NULL, DMA_HANDLE,
18070Sstevel@tonic-gate 
1808*4039Skd93003 	PCI_ECC_DRD_CE, COMMON_ECC_AFSR_E_DRD, CBNINTR_CE,
18090Sstevel@tonic-gate 	PBM_PRIMARY, NULL, NULL, DMA_HANDLE,
18100Sstevel@tonic-gate 
1811*4039Skd93003 	PCI_ECC_SEC_DRD_CE, COMMON_ECC_AFSR_E_DRD, CBNINTR_CE,
18120Sstevel@tonic-gate 	PBM_SECONDARY, NULL, NULL, DMA_HANDLE,
18130Sstevel@tonic-gate 
1814*4039Skd93003 	PCI_ECC_DWR_UE, COMMON_ECC_AFSR_E_DWR, CBNINTR_UE,
18150Sstevel@tonic-gate 	PBM_PRIMARY, NULL, NULL, DMA_HANDLE,
18160Sstevel@tonic-gate 
1817*4039Skd93003 	PCI_ECC_SEC_DWR_UE, COMMON_ECC_AFSR_E_DWR, CBNINTR_UE,
18180Sstevel@tonic-gate 	PBM_SECONDARY, NULL, NULL, DMA_HANDLE,
18190Sstevel@tonic-gate 
1820*4039Skd93003 	PCI_ECC_DWR_CE, COMMON_ECC_AFSR_E_DWR, CBNINTR_CE,
18210Sstevel@tonic-gate 	PBM_PRIMARY, NULL, NULL, DMA_HANDLE,
18220Sstevel@tonic-gate 
1823*4039Skd93003 	PCI_ECC_SEC_DWR_CE, COMMON_ECC_AFSR_E_DWR, CBNINTR_CE,
18240Sstevel@tonic-gate 	PBM_SECONDARY, NULL, NULL, DMA_HANDLE,
18250Sstevel@tonic-gate 
18260Sstevel@tonic-gate 	NULL, NULL, NULL, NULL, NULL, NULL,
18270Sstevel@tonic-gate };
18280Sstevel@tonic-gate 
18290Sstevel@tonic-gate /*
18300Sstevel@tonic-gate  * pci_ecc_classify, called by ecc_handler to classify ecc errors
18310Sstevel@tonic-gate  * and determine if we should panic or not.
18320Sstevel@tonic-gate  */
18330Sstevel@tonic-gate void
18340Sstevel@tonic-gate pci_ecc_classify(uint64_t err, ecc_errstate_t *ecc_err_p)
18350Sstevel@tonic-gate {
18360Sstevel@tonic-gate 	struct async_flt *ecc_p = &ecc_err_p->ecc_aflt;
18370Sstevel@tonic-gate 	uint64_t region, afar = ecc_p->flt_addr;
18380Sstevel@tonic-gate 	int i, j, ret = 0;
18390Sstevel@tonic-gate 	int flag, fatal = 0;
18400Sstevel@tonic-gate 	pci_common_t *cmn_p = ecc_err_p->ecc_ii_p.ecc_p->ecc_pci_cmn_p;
18410Sstevel@tonic-gate 	pci_t *pci_p = cmn_p->pci_p[0];
18420Sstevel@tonic-gate 
18430Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cmn_p->pci_fm_mutex));
18440Sstevel@tonic-gate 
18450Sstevel@tonic-gate 	ecc_err_p->ecc_bridge_type = PCI_BRIDGE_TYPE(cmn_p);
18460Sstevel@tonic-gate 
18470Sstevel@tonic-gate 	if (pci_p == NULL)
18480Sstevel@tonic-gate 		pci_p = cmn_p->pci_p[1];
18490Sstevel@tonic-gate 
18500Sstevel@tonic-gate 	ecc_err_p->ecc_ctrl = lddphysio(ecc_err_p->ecc_ii_p.ecc_p->ecc_csr_pa);
18510Sstevel@tonic-gate 	ecc_err_p->ecc_err_addr = afar;
18520Sstevel@tonic-gate 	region = afar & SCHIZO_ECC_AFAR_PIOW_MASK;
18530Sstevel@tonic-gate 
18540Sstevel@tonic-gate 	for (i = 0; ecc_err_tbl[i].ecc_err_class != NULL; i++) {
18550Sstevel@tonic-gate 		if (!(err & ecc_err_tbl[i].ecc_reg_bit) ||
18560Sstevel@tonic-gate 			(ecc_err_p->ecc_ii_p.ecc_type !=
18570Sstevel@tonic-gate 			    ecc_err_tbl[i].ecc_type) ||
18580Sstevel@tonic-gate 			(ecc_err_p->ecc_pri != ecc_err_tbl[i].ecc_pri))
18590Sstevel@tonic-gate 			continue;
18600Sstevel@tonic-gate 
18610Sstevel@tonic-gate 		ecc_p->flt_erpt_class = ecc_err_tbl[i].ecc_err_class;
18620Sstevel@tonic-gate 		flag = ecc_err_tbl[i].ecc_flag;
18630Sstevel@tonic-gate 
18640Sstevel@tonic-gate 		if (!ecc_err_tbl[i].ecc_pri ||
18650Sstevel@tonic-gate 				(ecc_err_tbl[i].ecc_type == CBNINTR_CE)) {
18660Sstevel@tonic-gate 			fatal += (ecc_err_tbl[i].ecc_type == CBNINTR_UE) ?
18670Sstevel@tonic-gate 				1 : 0;
18680Sstevel@tonic-gate 			break;
18690Sstevel@tonic-gate 		}
18700Sstevel@tonic-gate 
18710Sstevel@tonic-gate 		if (flag == ACC_HANDLE &&
18720Sstevel@tonic-gate 			(region & ecc_err_tbl[i].ecc_region_bits)) {
18730Sstevel@tonic-gate 			ecc_err_p->ecc_region = ecc_err_tbl[i].ecc_region;
18740Sstevel@tonic-gate 			pci_format_ecc_addr(pci_p->pci_dip,
18750Sstevel@tonic-gate 					&ecc_err_p->ecc_err_addr,
18760Sstevel@tonic-gate 					ecc_err_p->ecc_region);
18770Sstevel@tonic-gate 		}
18780Sstevel@tonic-gate 
18790Sstevel@tonic-gate 		/*
18800Sstevel@tonic-gate 		 * Lookup and fault errant handle
18810Sstevel@tonic-gate 		 */
18820Sstevel@tonic-gate 		for (j = 0; j < 2; ++j) {
18830Sstevel@tonic-gate 			ret = DDI_FM_UNKNOWN;
18840Sstevel@tonic-gate 			if (cmn_p->pci_p[j] == NULL)
18850Sstevel@tonic-gate 				continue;
18861865Sdilpreet 			ret = ndi_fmc_error(cmn_p->pci_p[j]->pci_dip, NULL,
18870Sstevel@tonic-gate 					flag, ecc_err_p->ecc_ena,
18880Sstevel@tonic-gate 					(void *)&ecc_err_p->ecc_err_addr);
18890Sstevel@tonic-gate 			if (ret == DDI_FM_NONFATAL) {
18900Sstevel@tonic-gate 				fatal = 0;
18910Sstevel@tonic-gate 				break;
18920Sstevel@tonic-gate 			} else
18930Sstevel@tonic-gate 				fatal++;
18940Sstevel@tonic-gate 		}
18950Sstevel@tonic-gate 		break;
18960Sstevel@tonic-gate 	}
18970Sstevel@tonic-gate 
18980Sstevel@tonic-gate 	if (fatal)
18990Sstevel@tonic-gate 		ecc_p->flt_panic = 1;
19000Sstevel@tonic-gate 	else if (flag != ACC_HANDLE)
19010Sstevel@tonic-gate 		ecc_err_p->ecc_pg_ret = 1;
19020Sstevel@tonic-gate }
19030Sstevel@tonic-gate 
19040Sstevel@tonic-gate /*
19050Sstevel@tonic-gate  * Tables to define PCI-X Split Completion errors
19060Sstevel@tonic-gate  */
19070Sstevel@tonic-gate 
19080Sstevel@tonic-gate pcix_err_msg_rec_t pcix_completer_errs[] = {
19090Sstevel@tonic-gate 	{PCIX_CPLT_OUT_OF_RANGE,	"pcix", "oor"	},
19100Sstevel@tonic-gate };
19110Sstevel@tonic-gate 
19120Sstevel@tonic-gate pcix_err_tbl_t pcix_split_errs_tbl[] = {
19130Sstevel@tonic-gate 	{PCIX_CLASS_CPLT,
19140Sstevel@tonic-gate 		sizeof (pcix_completer_errs)/sizeof (pcix_err_msg_rec_t),
19150Sstevel@tonic-gate 		pcix_completer_errs		},
19160Sstevel@tonic-gate };
19170Sstevel@tonic-gate 
19180Sstevel@tonic-gate /*
19190Sstevel@tonic-gate  * Tables for the PCI-X error status messages
19200Sstevel@tonic-gate  */
19210Sstevel@tonic-gate pcix_err_msg_rec_t pcix_stat_errs[] = {
19220Sstevel@tonic-gate 	{XMITS_PCIX_STAT_SC_DSCRD,	"pcix", "discard"  	},
19230Sstevel@tonic-gate 	{XMITS_PCIX_STAT_SC_TTO,	"xmits.pbmx", "tato" 	},
19240Sstevel@tonic-gate 	{XMITS_PCIX_STAT_SMMU,		"xmits.pbmx", "stmmu"	},
19250Sstevel@tonic-gate 	{XMITS_PCIX_STAT_SDSTAT,	"xmits.pbmx", "stdst"	},
19260Sstevel@tonic-gate 	{XMITS_PCIX_STAT_CMMU,		"xmits.pbmx", "cnmmu"	},
19270Sstevel@tonic-gate 	{XMITS_PCIX_STAT_CDSTAT,	"xmits.pbmx", "cndst"	}
19280Sstevel@tonic-gate };
19290Sstevel@tonic-gate 
19300Sstevel@tonic-gate pcix_err_tbl_t pcix_stat_errs_tbl =
19310Sstevel@tonic-gate 	{PCIX_NO_CLASS,
19320Sstevel@tonic-gate 		sizeof (pcix_stat_errs)/sizeof (pcix_err_msg_rec_t),
19330Sstevel@tonic-gate 		pcix_stat_errs		};
19340Sstevel@tonic-gate 
19350Sstevel@tonic-gate 
19360Sstevel@tonic-gate /*
19370Sstevel@tonic-gate  * walk thru a table of error messages, printing as appropriate
19380Sstevel@tonic-gate  *
19390Sstevel@tonic-gate  * t - the table of messages to parse
19400Sstevel@tonic-gate  * err - the error to match against
19410Sstevel@tonic-gate  * multi - flag, sometimes multiple error bits may be set/desired
19420Sstevel@tonic-gate  */
19430Sstevel@tonic-gate static int
19440Sstevel@tonic-gate pcix_lookup_err_msgs(dev_info_t *dip, uint64_t ena, pcix_err_tbl_t t,
19450Sstevel@tonic-gate 		pbm_errstate_t *pbm_err_p)
19460Sstevel@tonic-gate {
19470Sstevel@tonic-gate 	uint32_t err_bits  = pbm_err_p->pbm_err & XMITS_PCIX_MSG_INDEX_MASK;
19480Sstevel@tonic-gate 	int nerr = 0;
19490Sstevel@tonic-gate 	int j;
19500Sstevel@tonic-gate 	char buf[FM_MAX_CLASS];
19510Sstevel@tonic-gate 
19520Sstevel@tonic-gate 	for (j = 0; j < t.err_rec_num; j++)  {
19530Sstevel@tonic-gate 		uint32_t msg_key = t.err_msg_tbl[j].msg_key;
19540Sstevel@tonic-gate 		if (pbm_err_p->pbm_multi ? !(err_bits & msg_key) : err_bits
19550Sstevel@tonic-gate 				!= msg_key)
19560Sstevel@tonic-gate 			continue;
19570Sstevel@tonic-gate 
19580Sstevel@tonic-gate 		(void) snprintf(buf, FM_MAX_CLASS, "%s.%s%s",
19590Sstevel@tonic-gate 		    t.err_msg_tbl[j].msg_class,
19600Sstevel@tonic-gate 		    pbm_err_p->pbm_pri ? "" : PCIX_SECONDARY,
19610Sstevel@tonic-gate 		    t.err_msg_tbl[j].msg_str);
19620Sstevel@tonic-gate 
19630Sstevel@tonic-gate 		pbm_err_p->pbm_err_class = buf;
19640Sstevel@tonic-gate 		pcix_ereport_post(dip, ena, pbm_err_p);
19650Sstevel@tonic-gate 		nerr++;
19660Sstevel@tonic-gate 	}
19670Sstevel@tonic-gate 	return (nerr ? DDI_FM_FATAL : DDI_FM_OK);
19680Sstevel@tonic-gate }
19690Sstevel@tonic-gate 
19700Sstevel@tonic-gate /*
19710Sstevel@tonic-gate  * Decodes primary(bit 27-24) or secondary(bit 15-12) PCI-X split
19720Sstevel@tonic-gate  * completion error message class and index in PBM AFSR.
19730Sstevel@tonic-gate  */
19740Sstevel@tonic-gate static void
19750Sstevel@tonic-gate pcix_log_split_err(dev_info_t *dip, uint64_t ena, pbm_errstate_t *pbm_err_p)
19760Sstevel@tonic-gate {
19770Sstevel@tonic-gate 	uint32_t class  = pbm_err_p->pbm_err & XMITS_PCIX_MSG_CLASS_MASK;
19780Sstevel@tonic-gate 	uint32_t num_classes = sizeof (pcix_split_errs_tbl) /
19790Sstevel@tonic-gate 	    sizeof (struct pcix_err_tbl);
19800Sstevel@tonic-gate 	int i;
19810Sstevel@tonic-gate 
19820Sstevel@tonic-gate 	for (i = 0; i < num_classes; i++) {
19830Sstevel@tonic-gate 		if (class == pcix_split_errs_tbl[i].err_class) {
19840Sstevel@tonic-gate 			pbm_err_p->pbm_multi = PCIX_SINGLE_ERR;
19850Sstevel@tonic-gate 			(void) pcix_lookup_err_msgs(dip, ena,
19860Sstevel@tonic-gate 			    pcix_split_errs_tbl[i], pbm_err_p);
19870Sstevel@tonic-gate 			break;
19880Sstevel@tonic-gate 		}
19890Sstevel@tonic-gate 	}
19900Sstevel@tonic-gate }
19910Sstevel@tonic-gate 
19920Sstevel@tonic-gate /*
19930Sstevel@tonic-gate  * Report PBM PCI-X Error Status Register if in PCI-X mode
19940Sstevel@tonic-gate  *
19950Sstevel@tonic-gate  * Once a PCI-X fault tree is constructed, the code below may need to
19960Sstevel@tonic-gate  * change.
19970Sstevel@tonic-gate  */
19980Sstevel@tonic-gate static int
19990Sstevel@tonic-gate pcix_log_pbm(pci_t *pci_p, uint64_t ena, pbm_errstate_t *pbm_err_p)
20000Sstevel@tonic-gate {
20010Sstevel@tonic-gate 	int fatal = 0;
20020Sstevel@tonic-gate 	int nonfatal = 0;
20030Sstevel@tonic-gate 	uint32_t e;
20040Sstevel@tonic-gate 
20050Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pci_p->pci_common_p->pci_fm_mutex));
20060Sstevel@tonic-gate 
20070Sstevel@tonic-gate 	DEBUG3(DBG_ERR_INTR, pci_p->pci_dip, "pcix_log_pbm: chip_type=%d "
20080Sstevel@tonic-gate 	    "ctr_stat=%lx afsr = 0x%lx", CHIP_TYPE(pci_p),
20090Sstevel@tonic-gate 	    pbm_err_p->pbm_ctl_stat, pbm_err_p->pbm_afsr);
20100Sstevel@tonic-gate 
20110Sstevel@tonic-gate 	if (!(CHIP_TYPE(pci_p) == PCI_CHIP_XMITS) ||
20120Sstevel@tonic-gate 	    !(pbm_err_p->pbm_ctl_stat & XMITS_PCI_CTRL_X_MODE))
20130Sstevel@tonic-gate 		return (DDI_FM_OK);
20140Sstevel@tonic-gate 
20150Sstevel@tonic-gate 	if (pbm_err_p->pbm_afsr & XMITS_PCI_X_AFSR_P_SC_ERR) {
20160Sstevel@tonic-gate 		pbm_err_p->pbm_err = PBM_AFSR_TO_PRISPLIT(pbm_err_p->pbm_afsr);
20170Sstevel@tonic-gate 		pbm_err_p->pbm_pri = PBM_PRIMARY;
20180Sstevel@tonic-gate 		pcix_log_split_err(pci_p->pci_dip, ena, pbm_err_p);
20190Sstevel@tonic-gate 		nonfatal++;
20200Sstevel@tonic-gate 	}
20210Sstevel@tonic-gate 	if (pbm_err_p->pbm_afsr & XMITS_PCI_X_AFSR_S_SC_ERR) {
20220Sstevel@tonic-gate 		pbm_err_p->pbm_err = PBM_AFSR_TO_PRISPLIT(pbm_err_p->pbm_afsr);
20230Sstevel@tonic-gate 		pbm_err_p->pbm_pri = PBM_PRIMARY;
20240Sstevel@tonic-gate 		pcix_log_split_err(pci_p->pci_dip, ena, pbm_err_p);
20250Sstevel@tonic-gate 		nonfatal++;
20260Sstevel@tonic-gate 	}
20270Sstevel@tonic-gate 
20280Sstevel@tonic-gate 	e = PBM_PCIX_TO_PRIERR(pbm_err_p->pbm_pcix_stat);
20290Sstevel@tonic-gate 	if (e) {
20300Sstevel@tonic-gate 		pbm_err_p->pbm_pri = PBM_PRIMARY;
20310Sstevel@tonic-gate 		pbm_err_p->pbm_err = e;
20320Sstevel@tonic-gate 		pbm_err_p->pbm_multi = PCIX_MULTI_ERR;
20330Sstevel@tonic-gate 		if (pcix_lookup_err_msgs(pci_p->pci_dip, ena,
20340Sstevel@tonic-gate 		    pcix_stat_errs_tbl, pbm_err_p) == DDI_FM_FATAL)
20350Sstevel@tonic-gate 			fatal++;
20360Sstevel@tonic-gate 		else
20370Sstevel@tonic-gate 			nonfatal++;
20380Sstevel@tonic-gate 	}
20390Sstevel@tonic-gate 
20400Sstevel@tonic-gate 	e = PBM_PCIX_TO_SECERR(pbm_err_p->pbm_pcix_stat);
20410Sstevel@tonic-gate 	if (e) {
20420Sstevel@tonic-gate 		pbm_err_p->pbm_pri = PBM_SECONDARY;
20430Sstevel@tonic-gate 		pbm_err_p->pbm_err = e;
20440Sstevel@tonic-gate 		pbm_err_p->pbm_multi = PCIX_MULTI_ERR;
20450Sstevel@tonic-gate 		if (pcix_lookup_err_msgs(pci_p->pci_dip, ena,
20460Sstevel@tonic-gate 		    pcix_stat_errs_tbl, pbm_err_p) == DDI_FM_FATAL)
20470Sstevel@tonic-gate 			fatal++;
20480Sstevel@tonic-gate 		else
20490Sstevel@tonic-gate 			nonfatal++;
20500Sstevel@tonic-gate 	}
20510Sstevel@tonic-gate 
20520Sstevel@tonic-gate 	if (!fatal && !nonfatal)
20530Sstevel@tonic-gate 		return (DDI_FM_OK);
20540Sstevel@tonic-gate 	else if (fatal)
20550Sstevel@tonic-gate 		return (DDI_FM_FATAL);
20560Sstevel@tonic-gate 	return (DDI_FM_NONFATAL);
20570Sstevel@tonic-gate }
20580Sstevel@tonic-gate 
20590Sstevel@tonic-gate static pbm_fm_err_t pbm_err_tbl[] = {
20600Sstevel@tonic-gate 	PCI_MA,			SCHIZO_PCI_AFSR_E_MA,	PBM_PRIMARY,
20610Sstevel@tonic-gate 	FM_LOG_PCI,	PCI_TARG_MA,
20620Sstevel@tonic-gate 
20630Sstevel@tonic-gate 	PCI_SEC_MA,		SCHIZO_PCI_AFSR_E_MA,	PBM_SECONDARY,
20640Sstevel@tonic-gate 	FM_LOG_PBM,	NULL,
20650Sstevel@tonic-gate 
20660Sstevel@tonic-gate 	PCI_REC_TA,		SCHIZO_PCI_AFSR_E_TA,	PBM_PRIMARY,
20670Sstevel@tonic-gate 	FM_LOG_PCI,	PCI_TARG_REC_TA,
20680Sstevel@tonic-gate 
20690Sstevel@tonic-gate 	PCI_SEC_REC_TA,		SCHIZO_PCI_AFSR_E_TA,	PBM_SECONDARY,
20700Sstevel@tonic-gate 	FM_LOG_PBM,	NULL,
20710Sstevel@tonic-gate 
20720Sstevel@tonic-gate 	PCI_PBM_RETRY,		SCHIZO_PCI_AFSR_E_RTRY,	PBM_PRIMARY,
20730Sstevel@tonic-gate 	FM_LOG_PBM,	PCI_PBM_TARG_RETRY,
20740Sstevel@tonic-gate 
20750Sstevel@tonic-gate 	PCI_SEC_PBM_RETRY,	SCHIZO_PCI_AFSR_E_RTRY,	PBM_SECONDARY,
20760Sstevel@tonic-gate 	FM_LOG_PBM,	NULL,
20770Sstevel@tonic-gate 
20780Sstevel@tonic-gate 	PCI_MDPE,		SCHIZO_PCI_AFSR_E_PERR,	PBM_PRIMARY,
20790Sstevel@tonic-gate 	FM_LOG_PCI,	PCI_TARG_MDPE,
20800Sstevel@tonic-gate 
20810Sstevel@tonic-gate 	PCI_SEC_MDPE,		SCHIZO_PCI_AFSR_E_PERR,	PBM_SECONDARY,
20820Sstevel@tonic-gate 	FM_LOG_PBM,	NULL,
20830Sstevel@tonic-gate 
20840Sstevel@tonic-gate 	PCI_PBM_TTO,		SCHIZO_PCI_AFSR_E_TTO,	PBM_PRIMARY,
20850Sstevel@tonic-gate 	FM_LOG_PBM,	PCI_PBM_TARG_TTO,
20860Sstevel@tonic-gate 
20870Sstevel@tonic-gate 	PCI_SEC_PBM_TTO,	SCHIZO_PCI_AFSR_E_TTO,	PBM_SECONDARY,
20880Sstevel@tonic-gate 	FM_LOG_PBM,	NULL,
20890Sstevel@tonic-gate 
20900Sstevel@tonic-gate 	PCI_SCH_BUS_UNUSABLE_ERR, SCHIZO_PCI_AFSR_E_UNUSABLE, PBM_PRIMARY,
20910Sstevel@tonic-gate 	FM_LOG_PBM,	NULL,
20920Sstevel@tonic-gate 
20930Sstevel@tonic-gate 	PCI_SEC_SCH_BUS_UNUSABLE_ERR, SCHIZO_PCI_AFSR_E_UNUSABLE, PBM_SECONDARY,
20940Sstevel@tonic-gate 	FM_LOG_PBM,	NULL,
20950Sstevel@tonic-gate 
20960Sstevel@tonic-gate 	NULL,			NULL,			NULL,
20970Sstevel@tonic-gate 	NULL,		NULL,
20980Sstevel@tonic-gate };
20990Sstevel@tonic-gate 
21000Sstevel@tonic-gate 
21010Sstevel@tonic-gate /*
21020Sstevel@tonic-gate  * pci_pbm_classify, called by pbm_afsr_report to classify piow afsr.
21030Sstevel@tonic-gate  */
21040Sstevel@tonic-gate int
21050Sstevel@tonic-gate pci_pbm_classify(pbm_errstate_t *pbm_err_p)
21060Sstevel@tonic-gate {
21070Sstevel@tonic-gate 	uint32_t err;
21080Sstevel@tonic-gate 	int nerr = 0;
21090Sstevel@tonic-gate 	int i;
21100Sstevel@tonic-gate 
21110Sstevel@tonic-gate 	err = pbm_err_p->pbm_pri ? PBM_AFSR_TO_PRIERR(pbm_err_p->pbm_afsr):
21120Sstevel@tonic-gate 		PBM_AFSR_TO_SECERR(pbm_err_p->pbm_afsr);
21130Sstevel@tonic-gate 
21140Sstevel@tonic-gate 	for (i = 0; pbm_err_tbl[i].pbm_err_class != NULL; i++) {
21150Sstevel@tonic-gate 		if ((err & pbm_err_tbl[i].pbm_reg_bit) &&
21160Sstevel@tonic-gate 		    (pbm_err_p->pbm_pri == pbm_err_tbl[i].pbm_pri)) {
21170Sstevel@tonic-gate 			if (pbm_err_tbl[i].pbm_flag == FM_LOG_PCI)
21180Sstevel@tonic-gate 				pbm_err_p->pbm_pci.pci_err_class =
21190Sstevel@tonic-gate 					pbm_err_tbl[i].pbm_err_class;
21200Sstevel@tonic-gate 			else
21210Sstevel@tonic-gate 				pbm_err_p->pbm_err_class =
21220Sstevel@tonic-gate 				    pbm_err_tbl[i].pbm_err_class;
21230Sstevel@tonic-gate 
21240Sstevel@tonic-gate 			pbm_err_p->pbm_terr_class =
21250Sstevel@tonic-gate 			    pbm_err_tbl[i].pbm_terr_class;
21260Sstevel@tonic-gate 			pbm_err_p->pbm_log = pbm_err_tbl[i].pbm_flag;
21270Sstevel@tonic-gate 			nerr++;
21280Sstevel@tonic-gate 			break;
21290Sstevel@tonic-gate 		}
21300Sstevel@tonic-gate 	}
21310Sstevel@tonic-gate 
21320Sstevel@tonic-gate 	return (nerr);
21330Sstevel@tonic-gate }
21340Sstevel@tonic-gate 
21350Sstevel@tonic-gate /*
21360Sstevel@tonic-gate  * Function used to handle and log IOMMU errors. Called by pci_pbm_err_handler,
21370Sstevel@tonic-gate  * with pci_fm_mutex held.
21380Sstevel@tonic-gate  */
21390Sstevel@tonic-gate static int
21400Sstevel@tonic-gate iommu_err_handler(dev_info_t *dip, uint64_t ena, pbm_errstate_t *pbm_err_p)
21410Sstevel@tonic-gate {
21420Sstevel@tonic-gate 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
21430Sstevel@tonic-gate 	iommu_t *iommu_p = pci_p->pci_iommu_p;
21440Sstevel@tonic-gate 	ecc_t *ecc_p = pci_p->pci_ecc_p;
21450Sstevel@tonic-gate 	uint64_t stat;
21460Sstevel@tonic-gate 	ushort_t ta_signalled;
21470Sstevel@tonic-gate 	int err = 0;
21480Sstevel@tonic-gate 	int fatal = 0;
21490Sstevel@tonic-gate 	int nonfatal = 0;
21500Sstevel@tonic-gate 	int ret;
21510Sstevel@tonic-gate 
21520Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ecc_p->ecc_pci_cmn_p->pci_fm_mutex));
21530Sstevel@tonic-gate 	if (!((stat = *iommu_p->iommu_ctrl_reg) & TOMATILLO_IOMMU_ERR)) {
21540Sstevel@tonic-gate 		pbm_err_p->pbm_err_class = PCI_SCH_MMU_ERR;
21550Sstevel@tonic-gate 		iommu_ereport_post(dip, ena, pbm_err_p);
21560Sstevel@tonic-gate 		return (DDI_FM_NONFATAL);
21570Sstevel@tonic-gate 	}
21580Sstevel@tonic-gate 
21590Sstevel@tonic-gate 	/*
21600Sstevel@tonic-gate 	 * Need to make sure a Target Abort was signalled to the device if
21610Sstevel@tonic-gate 	 * we have any hope of recovering. Tomatillo does not send a TA for
21620Sstevel@tonic-gate 	 * DMA Writes that result in a Translation Error, thus fooling the
21630Sstevel@tonic-gate 	 * device into believing everything is as it expects. Ignorance
21640Sstevel@tonic-gate 	 * is bliss, but knowledge is power.
21650Sstevel@tonic-gate 	 */
21660Sstevel@tonic-gate 	ta_signalled = pbm_err_p->pbm_pci.pci_cfg_stat &
21670Sstevel@tonic-gate 		PCI_STAT_S_TARG_AB;
21680Sstevel@tonic-gate 
21690Sstevel@tonic-gate 	if (stat & TOMATILLO_IOMMU_ERR_ILLTSBTBW) {
21700Sstevel@tonic-gate 		pbm_err_p->pbm_err_class = PCI_TOM_MMU_BAD_TSBTBW;
21710Sstevel@tonic-gate 		err = 1;
21720Sstevel@tonic-gate 		iommu_ereport_post(dip, ena, pbm_err_p);
21730Sstevel@tonic-gate 		if (!ta_signalled)
21740Sstevel@tonic-gate 			fatal++;
21750Sstevel@tonic-gate 		else
21760Sstevel@tonic-gate 			nonfatal++;
21770Sstevel@tonic-gate 	}
21780Sstevel@tonic-gate 
21790Sstevel@tonic-gate 	if (stat & TOMATILLO_IOMMU_ERR_BAD_VA) {
21800Sstevel@tonic-gate 		pbm_err_p->pbm_err_class = PCI_TOM_MMU_BAD_VA;
21810Sstevel@tonic-gate 		err = 1;
21820Sstevel@tonic-gate 		iommu_ereport_post(dip, ena, pbm_err_p);
21830Sstevel@tonic-gate 		if (!ta_signalled)
21840Sstevel@tonic-gate 			fatal++;
21850Sstevel@tonic-gate 		else
21860Sstevel@tonic-gate 			nonfatal++;
21870Sstevel@tonic-gate 	}
21880Sstevel@tonic-gate 
21890Sstevel@tonic-gate 	if (!err) {
21900Sstevel@tonic-gate 		stat = ((stat & TOMATILLO_IOMMU_ERRSTS) >>
21910Sstevel@tonic-gate 		    TOMATILLO_IOMMU_ERRSTS_SHIFT);
21920Sstevel@tonic-gate 		switch (stat) {
21930Sstevel@tonic-gate 		case TOMATILLO_IOMMU_PROTECTION_ERR:
21940Sstevel@tonic-gate 			pbm_err_p->pbm_err_class = PCI_TOM_MMU_PROT_ERR;
21950Sstevel@tonic-gate 			iommu_ereport_post(dip, ena, pbm_err_p);
21960Sstevel@tonic-gate 			fatal++;
21970Sstevel@tonic-gate 			break;
21980Sstevel@tonic-gate 		case TOMATILLO_IOMMU_INVALID_ERR:
21990Sstevel@tonic-gate 			pbm_err_p->pbm_err_class = PCI_TOM_MMU_INVAL_ERR;
22000Sstevel@tonic-gate 			/*
22010Sstevel@tonic-gate 			 * Fault the address in iommu_tfar
22020Sstevel@tonic-gate 			 * register to inform target driver of error
22030Sstevel@tonic-gate 			 */
22041865Sdilpreet 			ret = ndi_fmc_error(pci_p->pci_dip, NULL, DMA_HANDLE,
22050Sstevel@tonic-gate 				ena, (void *)&pbm_err_p->pbm_iommu.iommu_tfar);
22060Sstevel@tonic-gate 
22071865Sdilpreet 			if (ret != DDI_FM_NONFATAL)
22080Sstevel@tonic-gate 				if (ta_signalled)
22090Sstevel@tonic-gate 					nonfatal++;
22100Sstevel@tonic-gate 				else
22110Sstevel@tonic-gate 					fatal++;
22120Sstevel@tonic-gate 			else
22131865Sdilpreet 				nonfatal++;
22141865Sdilpreet 
22150Sstevel@tonic-gate 			iommu_ereport_post(dip, ena, pbm_err_p);
22160Sstevel@tonic-gate 			break;
22170Sstevel@tonic-gate 		case TOMATILLO_IOMMU_TIMEOUT_ERR:
22180Sstevel@tonic-gate 			pbm_err_p->pbm_err_class = PCI_TOM_MMU_TO_ERR;
22190Sstevel@tonic-gate 			fatal++;
22200Sstevel@tonic-gate 			iommu_ereport_post(dip, ena, pbm_err_p);
22210Sstevel@tonic-gate 			break;
22220Sstevel@tonic-gate 		case TOMATILLO_IOMMU_ECC_ERR:
22230Sstevel@tonic-gate 			pbm_err_p->pbm_err_class = PCI_TOM_MMU_UE;
22240Sstevel@tonic-gate 			iommu_ereport_post(dip, ena, pbm_err_p);
22250Sstevel@tonic-gate 			break;
22260Sstevel@tonic-gate 		}
22270Sstevel@tonic-gate 	}
22280Sstevel@tonic-gate 
22290Sstevel@tonic-gate 	if (fatal)
22300Sstevel@tonic-gate 		return (DDI_FM_FATAL);
22310Sstevel@tonic-gate 	else if (nonfatal)
22320Sstevel@tonic-gate 		return (DDI_FM_NONFATAL);
22330Sstevel@tonic-gate 
22340Sstevel@tonic-gate 	return (DDI_FM_OK);
22350Sstevel@tonic-gate }
22360Sstevel@tonic-gate 
22370Sstevel@tonic-gate int
22380Sstevel@tonic-gate pci_check_error(pci_t *pci_p)
22390Sstevel@tonic-gate {
22400Sstevel@tonic-gate 	pbm_t *pbm_p = pci_p->pci_pbm_p;
22410Sstevel@tonic-gate 	uint16_t pci_cfg_stat;
22420Sstevel@tonic-gate 	uint64_t pbm_ctl_stat, pbm_afsr, pbm_pcix_stat;
22430Sstevel@tonic-gate 	caddr_t a = pci_p->pci_address[0];
22440Sstevel@tonic-gate 	uint64_t *pbm_pcix_stat_reg;
22450Sstevel@tonic-gate 
22460Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pci_p->pci_common_p->pci_fm_mutex));
22470Sstevel@tonic-gate 
22480Sstevel@tonic-gate 	pci_cfg_stat = pbm_p->pbm_config_header->ch_status_reg;
22490Sstevel@tonic-gate 	pbm_ctl_stat = *pbm_p->pbm_ctrl_reg;
22500Sstevel@tonic-gate 	pbm_afsr = *pbm_p->pbm_async_flt_status_reg;
22510Sstevel@tonic-gate 
22520Sstevel@tonic-gate 	if ((pci_cfg_stat & (PCI_STAT_S_PERROR | PCI_STAT_S_TARG_AB |
22530Sstevel@tonic-gate 				PCI_STAT_R_TARG_AB | PCI_STAT_R_MAST_AB |
22540Sstevel@tonic-gate 				PCI_STAT_S_SYSERR | PCI_STAT_PERROR)) ||
22550Sstevel@tonic-gate 			(pbm_ctl_stat & (SCHIZO_PCI_CTRL_BUS_UNUSABLE |
22560Sstevel@tonic-gate 				TOMATILLO_PCI_CTRL_PCI_DTO_ERR |
22570Sstevel@tonic-gate 				SCHIZO_PCI_CTRL_PCI_TTO_ERR |
22580Sstevel@tonic-gate 				SCHIZO_PCI_CTRL_PCI_RTRY_ERR |
22590Sstevel@tonic-gate 				SCHIZO_PCI_CTRL_PCI_MMU_ERR |
22600Sstevel@tonic-gate 				COMMON_PCI_CTRL_SBH_ERR |
22610Sstevel@tonic-gate 				COMMON_PCI_CTRL_SERR)) ||
22620Sstevel@tonic-gate 			(PBM_AFSR_TO_PRIERR(pbm_afsr)))
22630Sstevel@tonic-gate 		return (1);
22640Sstevel@tonic-gate 
22650Sstevel@tonic-gate 	if ((CHIP_TYPE(pci_p) == PCI_CHIP_XMITS) &&
22660Sstevel@tonic-gate 			(pbm_ctl_stat & XMITS_PCI_CTRL_X_MODE)) {
22670Sstevel@tonic-gate 
22680Sstevel@tonic-gate 		pbm_pcix_stat_reg = (uint64_t *)(a +
22690Sstevel@tonic-gate 		    XMITS_PCI_X_ERROR_STATUS_REG_OFFSET);
22700Sstevel@tonic-gate 
22710Sstevel@tonic-gate 		pbm_pcix_stat = *pbm_pcix_stat_reg;
22720Sstevel@tonic-gate 
22730Sstevel@tonic-gate 		if (PBM_PCIX_TO_PRIERR(pbm_pcix_stat))
22740Sstevel@tonic-gate 			return (1);
22750Sstevel@tonic-gate 
22760Sstevel@tonic-gate 		if (pbm_pcix_stat & XMITS_PCIX_STAT_PERR_RECOV_INT)
22770Sstevel@tonic-gate 			return (1);
22780Sstevel@tonic-gate 	}
22790Sstevel@tonic-gate 
22800Sstevel@tonic-gate 	return (0);
22810Sstevel@tonic-gate 
22820Sstevel@tonic-gate }
22830Sstevel@tonic-gate 
22840Sstevel@tonic-gate static pbm_fm_err_t pci_pbm_err_tbl[] = {
22850Sstevel@tonic-gate 	PCI_PBM_RETRY,			SCHIZO_PCI_CTRL_PCI_RTRY_ERR,
22860Sstevel@tonic-gate 	NULL,	PBM_NONFATAL,	PCI_PBM_TARG_RETRY,
22870Sstevel@tonic-gate 
22880Sstevel@tonic-gate 	PCI_PBM_TTO,			SCHIZO_PCI_CTRL_PCI_TTO_ERR,
22890Sstevel@tonic-gate 	NULL,	PBM_NONFATAL,	PCI_PBM_TARG_TTO,
22900Sstevel@tonic-gate 
22910Sstevel@tonic-gate 	PCI_SCH_BUS_UNUSABLE_ERR,	SCHIZO_PCI_CTRL_BUS_UNUSABLE,
22920Sstevel@tonic-gate 	NULL,	PBM_NONFATAL,	NULL,
22930Sstevel@tonic-gate 
22940Sstevel@tonic-gate 	NULL,				NULL,
22950Sstevel@tonic-gate 	NULL,	NULL,		NULL
22960Sstevel@tonic-gate };
22970Sstevel@tonic-gate 
22980Sstevel@tonic-gate /*
22990Sstevel@tonic-gate  * Function used to log all PCI/PBM/IOMMU errors found in the system.
23000Sstevel@tonic-gate  * It is called by the pbm_error_intr as well as the pci_err_callback(trap
23010Sstevel@tonic-gate  * callback). To protect access we hold the pci_fm_mutex when calling
23020Sstevel@tonic-gate  * this function.
23030Sstevel@tonic-gate  */
23040Sstevel@tonic-gate int
23050Sstevel@tonic-gate pci_pbm_err_handler(dev_info_t *dip, ddi_fm_error_t *derr,
23060Sstevel@tonic-gate 		const void *impl_data, int caller)
23070Sstevel@tonic-gate {
23080Sstevel@tonic-gate 	int fatal = 0;
23090Sstevel@tonic-gate 	int nonfatal = 0;
23100Sstevel@tonic-gate 	int unknown = 0;
23110Sstevel@tonic-gate 	uint32_t prierr, secerr;
23120Sstevel@tonic-gate 	pbm_errstate_t pbm_err;
23130Sstevel@tonic-gate 	char buf[FM_MAX_CLASS];
23140Sstevel@tonic-gate 	pci_t *pci_p = (pci_t *)impl_data;
23150Sstevel@tonic-gate 	pbm_t *pbm_p = pci_p->pci_pbm_p;
23160Sstevel@tonic-gate 	int i, ret = 0;
23170Sstevel@tonic-gate 
23180Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pci_p->pci_common_p->pci_fm_mutex));
23190Sstevel@tonic-gate 	pci_pbm_errstate_get(pci_p, &pbm_err);
23200Sstevel@tonic-gate 
23210Sstevel@tonic-gate 	derr->fme_ena = derr->fme_ena ? derr->fme_ena :
23220Sstevel@tonic-gate 	    fm_ena_generate(0, FM_ENA_FMT1);
23230Sstevel@tonic-gate 
23240Sstevel@tonic-gate 	prierr = PBM_AFSR_TO_PRIERR(pbm_err.pbm_afsr);
23250Sstevel@tonic-gate 	secerr = PBM_AFSR_TO_SECERR(pbm_err.pbm_afsr);
23260Sstevel@tonic-gate 
23270Sstevel@tonic-gate 	if (derr->fme_flag == DDI_FM_ERR_EXPECTED) {
23280Sstevel@tonic-gate 		if (caller == PCI_TRAP_CALL) {
23290Sstevel@tonic-gate 			/*
23300Sstevel@tonic-gate 			 * For ddi_caut_get treat all events as nonfatal.
23310Sstevel@tonic-gate 			 * The trampoline will set err_ena = 0, err_status =
23320Sstevel@tonic-gate 			 * NONFATAL. We only really call this function so that
23330Sstevel@tonic-gate 			 * pci_clear_error() and ndi_fm_handler_dispatch() will
23340Sstevel@tonic-gate 			 * get called.
23350Sstevel@tonic-gate 			 */
23360Sstevel@tonic-gate 			derr->fme_status = DDI_FM_NONFATAL;
23370Sstevel@tonic-gate 			nonfatal++;
23380Sstevel@tonic-gate 			goto done;
23390Sstevel@tonic-gate 		} else {
23400Sstevel@tonic-gate 			/*
23410Sstevel@tonic-gate 			 * For ddi_caut_put treat all events as nonfatal. Here
23420Sstevel@tonic-gate 			 * we have the handle and can call ndi_fm_acc_err_set().
23430Sstevel@tonic-gate 			 */
23440Sstevel@tonic-gate 			derr->fme_status = DDI_FM_NONFATAL;
23450Sstevel@tonic-gate 			ndi_fm_acc_err_set(pbm_p->pbm_excl_handle, derr);
23460Sstevel@tonic-gate 			nonfatal++;
23470Sstevel@tonic-gate 			goto done;
23480Sstevel@tonic-gate 		}
23490Sstevel@tonic-gate 	} else if (derr->fme_flag == DDI_FM_ERR_PEEK) {
23500Sstevel@tonic-gate 		/*
23510Sstevel@tonic-gate 		 * For ddi_peek treat all events as nonfatal. We only
23520Sstevel@tonic-gate 		 * really call this function so that pci_clear_error()
23530Sstevel@tonic-gate 		 * and ndi_fm_handler_dispatch() will get called.
23540Sstevel@tonic-gate 		 */
23550Sstevel@tonic-gate 		nonfatal++;
23560Sstevel@tonic-gate 		goto done;
23570Sstevel@tonic-gate 	} else if (derr->fme_flag == DDI_FM_ERR_POKE) {
23580Sstevel@tonic-gate 		/*
23590Sstevel@tonic-gate 		 * For ddi_poke we can treat as nonfatal if the
23600Sstevel@tonic-gate 		 * following conditions are met :
23610Sstevel@tonic-gate 		 * 1. Make sure only primary error is MA/TA
23620Sstevel@tonic-gate 		 * 2. Make sure no secondary error bits set
23630Sstevel@tonic-gate 		 * 3. check pci config header stat reg to see MA/TA is
23640Sstevel@tonic-gate 		 *    logged. We cannot verify only MA/TA is recorded
23650Sstevel@tonic-gate 		 *    since it gets much more complicated when a
23660Sstevel@tonic-gate 		 *    PCI-to-PCI bridge is present.
23670Sstevel@tonic-gate 		 */
23680Sstevel@tonic-gate 		if ((prierr == SCHIZO_PCI_AFSR_E_MA) && !secerr &&
23690Sstevel@tonic-gate 		    (pbm_err.pbm_pci.pci_cfg_stat & PCI_STAT_R_MAST_AB)) {
23700Sstevel@tonic-gate 			nonfatal++;
23710Sstevel@tonic-gate 			goto done;
23720Sstevel@tonic-gate 		} else if ((*pbm_p->pbm_ctrl_reg & XMITS_PCI_CTRL_X_MODE) &&
23730Sstevel@tonic-gate 		    pcix_ma_behind_bridge(&pbm_err)) {
23740Sstevel@tonic-gate 			/*
23750Sstevel@tonic-gate 			 * MAs behind a PCI-X bridge get sent back to
23760Sstevel@tonic-gate 			 * the host as a Split Completion Error Message.
23770Sstevel@tonic-gate 			 * We handle this the same as the above check.
23780Sstevel@tonic-gate 			 */
23790Sstevel@tonic-gate 			nonfatal++;
23800Sstevel@tonic-gate 			goto done;
23810Sstevel@tonic-gate 		}
23820Sstevel@tonic-gate 		if ((prierr == SCHIZO_PCI_AFSR_E_TA) && !secerr &&
23830Sstevel@tonic-gate 		    (pbm_err.pbm_pci.pci_cfg_stat & PCI_STAT_R_TARG_AB)) {
23840Sstevel@tonic-gate 			nonfatal++;
23850Sstevel@tonic-gate 			goto done;
23860Sstevel@tonic-gate 		}
23870Sstevel@tonic-gate 	}
23880Sstevel@tonic-gate 
23890Sstevel@tonic-gate 	DEBUG2(DBG_ERR_INTR, dip, "pci_pbm_err_handler: prierr=0x%x "
23900Sstevel@tonic-gate 	    "secerr=0x%x", prierr, secerr);
23910Sstevel@tonic-gate 
23920Sstevel@tonic-gate 	if (prierr || secerr) {
23930Sstevel@tonic-gate 		ret = pbm_afsr_report(dip, derr->fme_ena, &pbm_err);
23940Sstevel@tonic-gate 		if (ret == DDI_FM_FATAL)
23950Sstevel@tonic-gate 			fatal++;
23960Sstevel@tonic-gate 		else
23970Sstevel@tonic-gate 			nonfatal++;
23980Sstevel@tonic-gate 	}
23990Sstevel@tonic-gate 	if ((ret = pcix_log_pbm(pci_p, derr->fme_ena, &pbm_err))
24000Sstevel@tonic-gate 			== DDI_FM_FATAL)
24010Sstevel@tonic-gate 		fatal++;
24020Sstevel@tonic-gate 	else if (ret == DDI_FM_NONFATAL)
24030Sstevel@tonic-gate 		nonfatal++;
24040Sstevel@tonic-gate 
24050Sstevel@tonic-gate 	if ((ret = pci_cfg_report(dip, derr, &pbm_err.pbm_pci, caller, prierr))
24060Sstevel@tonic-gate 			== DDI_FM_FATAL)
24070Sstevel@tonic-gate 		fatal++;
24080Sstevel@tonic-gate 	else if (ret == DDI_FM_NONFATAL)
24090Sstevel@tonic-gate 		nonfatal++;
24100Sstevel@tonic-gate 
24110Sstevel@tonic-gate 	for (i = 0; pci_pbm_err_tbl[i].pbm_err_class != NULL; i++) {
24120Sstevel@tonic-gate 		if ((pbm_err.pbm_ctl_stat & pci_pbm_err_tbl[i].pbm_reg_bit) &&
24130Sstevel@tonic-gate 		    !prierr) {
24140Sstevel@tonic-gate 			pbm_err.pbm_err_class =
24150Sstevel@tonic-gate 				pci_pbm_err_tbl[i].pbm_err_class;
24160Sstevel@tonic-gate 			pbm_ereport_post(dip, derr->fme_ena, &pbm_err);
24170Sstevel@tonic-gate 			if (pci_pbm_err_tbl[i].pbm_flag)
24180Sstevel@tonic-gate 				fatal++;
24190Sstevel@tonic-gate 			else
24200Sstevel@tonic-gate 				nonfatal++;
24210Sstevel@tonic-gate 			if (caller == PCI_TRAP_CALL &&
24221865Sdilpreet 			    pci_pbm_err_tbl[i].pbm_terr_class)
24231865Sdilpreet 				pci_target_enqueue(derr->fme_ena,
24241865Sdilpreet 				    pci_pbm_err_tbl[i].pbm_terr_class,
24251865Sdilpreet 				    pbm_err.pbm_bridge_type,
24261865Sdilpreet 				    (uint64_t)derr->fme_bus_specific);
24270Sstevel@tonic-gate 		}
24280Sstevel@tonic-gate 	}
24290Sstevel@tonic-gate 
24300Sstevel@tonic-gate 	if ((pbm_err.pbm_ctl_stat & COMMON_PCI_CTRL_SBH_ERR) &&
24310Sstevel@tonic-gate 	    (CHIP_TYPE(pci_p) != PCI_CHIP_TOMATILLO)) {
24320Sstevel@tonic-gate 		pbm_err.pbm_err_class = PCI_SCH_SBH;
24330Sstevel@tonic-gate 		pbm_ereport_post(dip, derr->fme_ena, &pbm_err);
24340Sstevel@tonic-gate 		if (pci_panic_on_sbh_errors)
24350Sstevel@tonic-gate 			fatal++;
24360Sstevel@tonic-gate 		else
24370Sstevel@tonic-gate 			nonfatal++;
24380Sstevel@tonic-gate 	}
24390Sstevel@tonic-gate 
24400Sstevel@tonic-gate 	/*
24410Sstevel@tonic-gate 	 * PBM Received System Error - During any transaction, or
24420Sstevel@tonic-gate 	 * at any point on the bus, some device may detect a critical
24430Sstevel@tonic-gate 	 * error and signal a system error to the system.
24440Sstevel@tonic-gate 	 */
24450Sstevel@tonic-gate 	if (pbm_err.pbm_ctl_stat & COMMON_PCI_CTRL_SERR) {
24460Sstevel@tonic-gate 		/*
24470Sstevel@tonic-gate 		 * may be expected (master abort from pci-pci bridge during
24480Sstevel@tonic-gate 		 * poke will generate SERR)
24490Sstevel@tonic-gate 		 */
24500Sstevel@tonic-gate 		if (derr->fme_flag != DDI_FM_ERR_POKE) {
24510Sstevel@tonic-gate 			DEBUG1(DBG_ERR_INTR, dip, "pci_pbm_err_handler: "
24520Sstevel@tonic-gate 			    "ereport_post: %s", buf);
24530Sstevel@tonic-gate 			(void) snprintf(buf, FM_MAX_CLASS, "%s.%s",
24540Sstevel@tonic-gate 				PCI_ERROR_SUBCLASS, PCI_REC_SERR);
24550Sstevel@tonic-gate 			ddi_fm_ereport_post(dip, buf, derr->fme_ena,
24560Sstevel@tonic-gate 			    DDI_NOSLEEP, FM_VERSION, DATA_TYPE_UINT8, 0,
24570Sstevel@tonic-gate 			    PCI_CONFIG_STATUS, DATA_TYPE_UINT16,
24580Sstevel@tonic-gate 			    pbm_err.pbm_pci.pci_cfg_stat, PCI_CONFIG_COMMAND,
24590Sstevel@tonic-gate 			    DATA_TYPE_UINT16, pbm_err.pbm_pci.pci_cfg_comm,
24600Sstevel@tonic-gate 			    PCI_PA, DATA_TYPE_UINT64, (uint64_t)0, NULL);
24610Sstevel@tonic-gate 		}
24621865Sdilpreet 		unknown++;
24630Sstevel@tonic-gate 	}
24640Sstevel@tonic-gate 
24650Sstevel@tonic-gate 	/*
24660Sstevel@tonic-gate 	 * PCI Retry Timeout - Device fails to retry deferred
24670Sstevel@tonic-gate 	 * transaction within timeout. Only Tomatillo
24680Sstevel@tonic-gate 	 */
24690Sstevel@tonic-gate 	if (pbm_err.pbm_ctl_stat & TOMATILLO_PCI_CTRL_PCI_DTO_ERR) {
24700Sstevel@tonic-gate 		if (pci_dto_fault_warn == CE_PANIC)
24710Sstevel@tonic-gate 			fatal++;
24720Sstevel@tonic-gate 		else
24730Sstevel@tonic-gate 			nonfatal++;
24740Sstevel@tonic-gate 
24750Sstevel@tonic-gate 		(void) snprintf(buf, FM_MAX_CLASS, "%s.%s",
24760Sstevel@tonic-gate 			PCI_ERROR_SUBCLASS, PCI_DTO);
24770Sstevel@tonic-gate 		ddi_fm_ereport_post(dip, buf, derr->fme_ena, DDI_NOSLEEP,
24780Sstevel@tonic-gate 		    FM_VERSION, DATA_TYPE_UINT8, 0,
24790Sstevel@tonic-gate 		    PCI_CONFIG_STATUS, DATA_TYPE_UINT16,
24800Sstevel@tonic-gate 		    pbm_err.pbm_pci.pci_cfg_stat,
24810Sstevel@tonic-gate 		    PCI_CONFIG_COMMAND, DATA_TYPE_UINT16,
24820Sstevel@tonic-gate 		    pbm_err.pbm_pci.pci_cfg_comm,
24830Sstevel@tonic-gate 		    PCI_PA, DATA_TYPE_UINT64, (uint64_t)0, NULL);
24840Sstevel@tonic-gate 	}
24850Sstevel@tonic-gate 
24860Sstevel@tonic-gate 	/*
24870Sstevel@tonic-gate 	 * PBM Detected Data Parity Error - DPE detected during a DMA Write
24880Sstevel@tonic-gate 	 * or PIO Read. Later case is taken care of by cpu_deferred_error
24890Sstevel@tonic-gate 	 * and sent here to be logged.
24900Sstevel@tonic-gate 	 */
24910Sstevel@tonic-gate 	if ((pbm_err.pbm_pci.pci_cfg_stat & PCI_STAT_PERROR) &&
24920Sstevel@tonic-gate 			!(pbm_err.pbm_pci.pci_cfg_stat & PCI_STAT_S_SYSERR)) {
24930Sstevel@tonic-gate 		/*
24940Sstevel@tonic-gate 		 * If we have an address then fault
24950Sstevel@tonic-gate 		 * it, if not probe for errant device
24960Sstevel@tonic-gate 		 */
24970Sstevel@tonic-gate 		ret = DDI_FM_FATAL;
24980Sstevel@tonic-gate 		if (caller != PCI_TRAP_CALL) {
24991865Sdilpreet 			if (pbm_err.pbm_va_log) {
25001865Sdilpreet 				ret = ndi_fmc_error(dip, NULL, DMA_HANDLE,
25011865Sdilpreet 				    derr->fme_ena, (void *)&pbm_err.pbm_va_log);
25021865Sdilpreet 			}
25030Sstevel@tonic-gate 			if (ret == DDI_FM_NONFATAL)
25040Sstevel@tonic-gate 				nonfatal++;
25050Sstevel@tonic-gate 			else
25060Sstevel@tonic-gate 				fatal++;
25070Sstevel@tonic-gate 		} else
25080Sstevel@tonic-gate 			nonfatal++;
25090Sstevel@tonic-gate 
25100Sstevel@tonic-gate 	}
25110Sstevel@tonic-gate 
25120Sstevel@tonic-gate 	/* PBM Detected IOMMU Error */
25130Sstevel@tonic-gate 	if (pbm_err.pbm_ctl_stat & SCHIZO_PCI_CTRL_PCI_MMU_ERR) {
25140Sstevel@tonic-gate 		if (iommu_err_handler(dip, derr->fme_ena, &pbm_err)
25150Sstevel@tonic-gate 				== DDI_FM_FATAL)
25160Sstevel@tonic-gate 			fatal++;
25170Sstevel@tonic-gate 		else
25180Sstevel@tonic-gate 			nonfatal++;
25190Sstevel@tonic-gate 	}
25200Sstevel@tonic-gate 
25210Sstevel@tonic-gate done:
25220Sstevel@tonic-gate 	ret = ndi_fm_handler_dispatch(dip, NULL, derr);
25230Sstevel@tonic-gate 	if (ret == DDI_FM_FATAL) {
25240Sstevel@tonic-gate 		fatal++;
25250Sstevel@tonic-gate 	} else if (ret == DDI_FM_NONFATAL) {
25260Sstevel@tonic-gate 		nonfatal++;
25270Sstevel@tonic-gate 	} else if (ret == DDI_FM_UNKNOWN) {
25280Sstevel@tonic-gate 		unknown++;
25290Sstevel@tonic-gate 	}
25300Sstevel@tonic-gate 
25310Sstevel@tonic-gate 	/*
25320Sstevel@tonic-gate 	 * RSERR not claimed as nonfatal by a child is considered fatal
25330Sstevel@tonic-gate 	 */
25341865Sdilpreet 	if (unknown && !fatal && !nonfatal)
25350Sstevel@tonic-gate 		fatal++;
25360Sstevel@tonic-gate 
25370Sstevel@tonic-gate 	/* Cleanup and reset error bits */
25380Sstevel@tonic-gate 	pci_clear_error(pci_p, &pbm_err);
25390Sstevel@tonic-gate 
25400Sstevel@tonic-gate 	return (fatal ? DDI_FM_FATAL : (nonfatal ? DDI_FM_NONFATAL :
25410Sstevel@tonic-gate 	    (unknown ? DDI_FM_UNKNOWN : DDI_FM_OK)));
25420Sstevel@tonic-gate }
25430Sstevel@tonic-gate 
25440Sstevel@tonic-gate /*
25450Sstevel@tonic-gate  * Function returns TRUE if a Primary error is Split Completion Error
25460Sstevel@tonic-gate  * that indicates a Master Abort occured behind a PCI-X bridge.
25470Sstevel@tonic-gate  * This function should only be called for busses running in PCI-X mode.
25480Sstevel@tonic-gate  */
25490Sstevel@tonic-gate static int
25500Sstevel@tonic-gate pcix_ma_behind_bridge(pbm_errstate_t *pbm_err_p)
25510Sstevel@tonic-gate {
25520Sstevel@tonic-gate 	uint64_t msg;
25530Sstevel@tonic-gate 
25540Sstevel@tonic-gate 	if (pbm_err_p->pbm_afsr & XMITS_PCI_X_AFSR_S_SC_ERR)
25550Sstevel@tonic-gate 		return (0);
25560Sstevel@tonic-gate 
25570Sstevel@tonic-gate 	if (pbm_err_p->pbm_afsr & XMITS_PCI_X_AFSR_P_SC_ERR) {
25580Sstevel@tonic-gate 		msg = (pbm_err_p->pbm_afsr >> XMITS_PCI_X_P_MSG_SHIFT) &
25590Sstevel@tonic-gate 		    XMITS_PCIX_MSG_MASK;
25600Sstevel@tonic-gate 		if (msg & PCIX_CLASS_BRIDGE)
25610Sstevel@tonic-gate 			if (msg & PCIX_BRIDGE_MASTER_ABORT) {
25620Sstevel@tonic-gate 				return (1);
25630Sstevel@tonic-gate 			}
25640Sstevel@tonic-gate 	}
25650Sstevel@tonic-gate 
25660Sstevel@tonic-gate 	return (0);
25670Sstevel@tonic-gate }
25680Sstevel@tonic-gate 
25690Sstevel@tonic-gate /*
25700Sstevel@tonic-gate  * Function used to gather PBM/PCI/IOMMU error state for the
25710Sstevel@tonic-gate  * pci_pbm_err_handler and the cb_buserr_intr. This function must be
25720Sstevel@tonic-gate  * called while pci_fm_mutex is held.
25730Sstevel@tonic-gate  */
25740Sstevel@tonic-gate static void
25750Sstevel@tonic-gate pci_pbm_errstate_get(pci_t *pci_p, pbm_errstate_t *pbm_err_p)
25760Sstevel@tonic-gate {
25770Sstevel@tonic-gate 	pbm_t *pbm_p = pci_p->pci_pbm_p;
25780Sstevel@tonic-gate 	iommu_t *iommu_p = pci_p->pci_iommu_p;
25790Sstevel@tonic-gate 	caddr_t a = pci_p->pci_address[0];
25800Sstevel@tonic-gate 	uint64_t *pbm_pcix_stat_reg;
25810Sstevel@tonic-gate 
25820Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pci_p->pci_common_p->pci_fm_mutex));
25830Sstevel@tonic-gate 	bzero(pbm_err_p, sizeof (pbm_errstate_t));
25840Sstevel@tonic-gate 
25850Sstevel@tonic-gate 	/*
25860Sstevel@tonic-gate 	 * Capture all pbm error state for later logging
25870Sstevel@tonic-gate 	 */
25880Sstevel@tonic-gate 	pbm_err_p->pbm_bridge_type = PCI_BRIDGE_TYPE(pci_p->pci_common_p);
25890Sstevel@tonic-gate 
25900Sstevel@tonic-gate 	pbm_err_p->pbm_pci.pci_cfg_stat =
25910Sstevel@tonic-gate 		pbm_p->pbm_config_header->ch_status_reg;
25920Sstevel@tonic-gate 	pbm_err_p->pbm_ctl_stat = *pbm_p->pbm_ctrl_reg;
25930Sstevel@tonic-gate 	pbm_err_p->pbm_afsr = *pbm_p->pbm_async_flt_status_reg;
25940Sstevel@tonic-gate 	pbm_err_p->pbm_afar = *pbm_p->pbm_async_flt_addr_reg;
25950Sstevel@tonic-gate 	pbm_err_p->pbm_iommu.iommu_stat = *iommu_p->iommu_ctrl_reg;
25960Sstevel@tonic-gate 	pbm_err_p->pbm_pci.pci_cfg_comm =
25970Sstevel@tonic-gate 		pbm_p->pbm_config_header->ch_command_reg;
25980Sstevel@tonic-gate 	pbm_err_p->pbm_pci.pci_pa = *pbm_p->pbm_async_flt_addr_reg;
25990Sstevel@tonic-gate 
26000Sstevel@tonic-gate 	/*
26010Sstevel@tonic-gate 	 * Record errant slot for Xmits and Schizo
26020Sstevel@tonic-gate 	 * Not stored in Tomatillo
26030Sstevel@tonic-gate 	 */
26040Sstevel@tonic-gate 	if (CHIP_TYPE(pci_p) == PCI_CHIP_XMITS ||
26050Sstevel@tonic-gate 			CHIP_TYPE(pci_p) == PCI_CHIP_SCHIZO) {
26060Sstevel@tonic-gate 		pbm_err_p->pbm_err_sl = (pbm_err_p->pbm_ctl_stat &
26070Sstevel@tonic-gate 				SCHIZO_PCI_CTRL_ERR_SLOT) >>
26080Sstevel@tonic-gate 			SCHIZO_PCI_CTRL_ERR_SLOT_SHIFT;
26090Sstevel@tonic-gate 
26100Sstevel@tonic-gate 		/*
26110Sstevel@tonic-gate 		 * The bit 51 on XMITS rev1.0 is same as
26120Sstevel@tonic-gate 		 * SCHIZO_PCI_CTRL_ERR_SLOT_LOCK on schizo2.3. But
26130Sstevel@tonic-gate 		 * this bit needs to be cleared to be able to latch
26140Sstevel@tonic-gate 		 * the slot info on next fault.
26150Sstevel@tonic-gate 		 * But in XMITS Rev2.0, this bit indicates a DMA Write
26160Sstevel@tonic-gate 		 * Parity error.
26170Sstevel@tonic-gate 		 */
26180Sstevel@tonic-gate 		if (pbm_err_p->pbm_ctl_stat & XMITS_PCI_CTRL_DMA_WR_PERR) {
26190Sstevel@tonic-gate 			if ((PCI_CHIP_ID(pci_p) == XMITS_VER_10) ||
26200Sstevel@tonic-gate 				(PCI_CHIP_ID(pci_p) <= SCHIZO_VER_23)) {
26210Sstevel@tonic-gate 				/*
26220Sstevel@tonic-gate 				 * top 32 bits are W1C and we just want to
26230Sstevel@tonic-gate 				 * clear SLOT_LOCK. Leave bottom 32 bits
26240Sstevel@tonic-gate 				 * unchanged
26250Sstevel@tonic-gate 				 */
26260Sstevel@tonic-gate 				*pbm_p->pbm_ctrl_reg =
26270Sstevel@tonic-gate 					pbm_err_p->pbm_ctl_stat &
26280Sstevel@tonic-gate 					(SCHIZO_PCI_CTRL_ERR_SLOT_LOCK |
26290Sstevel@tonic-gate 					0xffffffff);
26300Sstevel@tonic-gate 				pbm_err_p->pbm_ctl_stat =
26310Sstevel@tonic-gate 					*pbm_p->pbm_ctrl_reg;
26320Sstevel@tonic-gate 			}
26330Sstevel@tonic-gate 		}
26340Sstevel@tonic-gate 	}
26350Sstevel@tonic-gate 
26360Sstevel@tonic-gate 	/*
26370Sstevel@tonic-gate 	 * Tomatillo specific registers
26380Sstevel@tonic-gate 	 */
26390Sstevel@tonic-gate 	if (CHIP_TYPE(pci_p) == PCI_CHIP_TOMATILLO) {
2640946Smathue 		pbm_err_p->pbm_va_log = (uint64_t)va_to_pa(
2641946Smathue 		    (void *)(uintptr_t)*(a + TOMATILLO_TGT_ERR_VALOG_OFFSET));
26420Sstevel@tonic-gate 		pbm_err_p->pbm_iommu.iommu_tfar = *iommu_p->iommu_tfar_reg;
26430Sstevel@tonic-gate 	}
26440Sstevel@tonic-gate 
26450Sstevel@tonic-gate 	/*
26460Sstevel@tonic-gate 	 * Xmits PCI-X register
26470Sstevel@tonic-gate 	 */
26480Sstevel@tonic-gate 	if ((CHIP_TYPE(pci_p) == PCI_CHIP_XMITS) &&
26490Sstevel@tonic-gate 			(pbm_err_p->pbm_ctl_stat & XMITS_PCI_CTRL_X_MODE)) {
26500Sstevel@tonic-gate 
26510Sstevel@tonic-gate 		pbm_pcix_stat_reg = (uint64_t *)(a +
26520Sstevel@tonic-gate 		    XMITS_PCI_X_ERROR_STATUS_REG_OFFSET);
26530Sstevel@tonic-gate 
26540Sstevel@tonic-gate 		pbm_err_p->pbm_pcix_stat = *pbm_pcix_stat_reg;
26550Sstevel@tonic-gate 		pbm_err_p->pbm_pcix_pfar = pbm_err_p->pbm_pcix_stat &
26560Sstevel@tonic-gate 				XMITS_PCI_X_STATUS_PFAR_MASK;
26570Sstevel@tonic-gate 	}
26580Sstevel@tonic-gate }
26590Sstevel@tonic-gate 
26600Sstevel@tonic-gate /*
26610Sstevel@tonic-gate  * Function used to clear PBM/PCI/IOMMU error state after error handling
26620Sstevel@tonic-gate  * is complete. Only clearing error bits which have been logged. Called by
26630Sstevel@tonic-gate  * pci_pbm_err_handler and pci_bus_exit.
26640Sstevel@tonic-gate  */
26650Sstevel@tonic-gate static void
26660Sstevel@tonic-gate pci_clear_error(pci_t *pci_p, pbm_errstate_t *pbm_err_p)
26670Sstevel@tonic-gate {
26680Sstevel@tonic-gate 	pbm_t *pbm_p = pci_p->pci_pbm_p;
26690Sstevel@tonic-gate 	iommu_t *iommu_p = pci_p->pci_iommu_p;
26700Sstevel@tonic-gate 
26710Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pbm_p->pbm_pci_p->pci_common_p->pci_fm_mutex));
26720Sstevel@tonic-gate 
26730Sstevel@tonic-gate 	if (*pbm_p->pbm_ctrl_reg & SCHIZO_PCI_CTRL_PCI_MMU_ERR) {
26740Sstevel@tonic-gate 		iommu_tlb_scrub(pci_p->pci_iommu_p, 1);
26750Sstevel@tonic-gate 	}
26760Sstevel@tonic-gate 	pbm_p->pbm_config_header->ch_status_reg =
26770Sstevel@tonic-gate 		pbm_err_p->pbm_pci.pci_cfg_stat;
26780Sstevel@tonic-gate 	*pbm_p->pbm_ctrl_reg = pbm_err_p->pbm_ctl_stat;
26790Sstevel@tonic-gate 	*pbm_p->pbm_async_flt_status_reg = pbm_err_p->pbm_afsr;
26800Sstevel@tonic-gate 	*iommu_p->iommu_ctrl_reg = pbm_err_p->pbm_iommu.iommu_stat;
26810Sstevel@tonic-gate }
26820Sstevel@tonic-gate 
26830Sstevel@tonic-gate void
26840Sstevel@tonic-gate pbm_clear_error(pbm_t *pbm_p)
26850Sstevel@tonic-gate {
26860Sstevel@tonic-gate 	uint64_t pbm_afsr, pbm_ctl_stat;
26870Sstevel@tonic-gate 
26880Sstevel@tonic-gate 	/*
26890Sstevel@tonic-gate 	 * for poke() support - called from POKE_FLUSH. Spin waiting
26900Sstevel@tonic-gate 	 * for MA, TA or SERR to be cleared by a pbm_error_intr().
26910Sstevel@tonic-gate 	 * We have to wait for SERR too in case the device is beyond
26920Sstevel@tonic-gate 	 * a pci-pci bridge.
26930Sstevel@tonic-gate 	 */
26940Sstevel@tonic-gate 	pbm_ctl_stat = *pbm_p->pbm_ctrl_reg;
26950Sstevel@tonic-gate 	pbm_afsr = *pbm_p->pbm_async_flt_status_reg;
26960Sstevel@tonic-gate 	while (((pbm_afsr >> SCHIZO_PCI_AFSR_PE_SHIFT) &
26970Sstevel@tonic-gate 	    (SCHIZO_PCI_AFSR_E_MA | SCHIZO_PCI_AFSR_E_TA)) ||
26980Sstevel@tonic-gate 	    (pbm_ctl_stat & COMMON_PCI_CTRL_SERR)) {
26990Sstevel@tonic-gate 		pbm_ctl_stat = *pbm_p->pbm_ctrl_reg;
27000Sstevel@tonic-gate 		pbm_afsr = *pbm_p->pbm_async_flt_status_reg;
27010Sstevel@tonic-gate 	}
27020Sstevel@tonic-gate }
27030Sstevel@tonic-gate 
27040Sstevel@tonic-gate /*
27050Sstevel@tonic-gate  * Function used to convert the 32 bit captured PCI error address
27060Sstevel@tonic-gate  * to the full Safari or Jbus address. This is so we can look this address
27070Sstevel@tonic-gate  * up in our handle caches.
27080Sstevel@tonic-gate  */
27090Sstevel@tonic-gate void
27100Sstevel@tonic-gate pci_format_addr(dev_info_t *dip, uint64_t *afar, uint64_t afsr)
27110Sstevel@tonic-gate {
27120Sstevel@tonic-gate 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
27130Sstevel@tonic-gate 	pci_ranges_t *io_range, *mem_range;
27140Sstevel@tonic-gate 	uint64_t err_pa = 0;
27150Sstevel@tonic-gate 
27160Sstevel@tonic-gate 	if (afsr & SCHIZO_PCI_AFSR_CONF_SPACE) {
27170Sstevel@tonic-gate 		err_pa |= pci_p->pci_ranges->parent_high;
27180Sstevel@tonic-gate 		err_pa = err_pa << 32;
27190Sstevel@tonic-gate 		err_pa |= pci_p->pci_ranges->parent_low;
27200Sstevel@tonic-gate 	} else if (afsr & SCHIZO_PCI_AFSR_IO_SPACE) {
27210Sstevel@tonic-gate 		io_range = pci_p->pci_ranges + 1;
27220Sstevel@tonic-gate 		err_pa |= io_range->parent_high;
27230Sstevel@tonic-gate 		err_pa = err_pa << 32;
27240Sstevel@tonic-gate 		err_pa |= io_range->parent_low;
27250Sstevel@tonic-gate 	} else if (afsr & SCHIZO_PCI_AFSR_MEM_SPACE) {
27260Sstevel@tonic-gate 		mem_range = pci_p->pci_ranges + 2;
27270Sstevel@tonic-gate 		err_pa |= mem_range->parent_high;
27280Sstevel@tonic-gate 		err_pa = err_pa << 32;
27290Sstevel@tonic-gate 		err_pa |= mem_range->parent_low;
27300Sstevel@tonic-gate 	}
27310Sstevel@tonic-gate 	*afar |= err_pa;
27320Sstevel@tonic-gate }
27330Sstevel@tonic-gate 
27340Sstevel@tonic-gate static ecc_format_t ecc_format_tbl[] = {
27350Sstevel@tonic-gate 	SCH_REG_UPA,		NULL,				NULL,
27360Sstevel@tonic-gate 	SCH_REG_PCIA_REG,	SCHIZO_PCI_AFSR_CONF_SPACE,	PCI_SIDEA,
27370Sstevel@tonic-gate 	SCH_REG_PCIA_MEM,	SCHIZO_PCI_AFSR_MEM_SPACE,	PCI_SIDEA,
27380Sstevel@tonic-gate 	SCH_REG_PCIA_CFGIO,	SCHIZO_PCI_AFSR_IO_SPACE,	PCI_SIDEA,
27390Sstevel@tonic-gate 	SCH_REG_PCIB_REG,	SCHIZO_PCI_AFSR_CONF_SPACE,	PCI_SIDEB,
27400Sstevel@tonic-gate 	SCH_REG_PCIB_MEM,	SCHIZO_PCI_AFSR_MEM_SPACE,	PCI_SIDEB,
27410Sstevel@tonic-gate 	SCH_REG_PCIB_CFGIO,	SCHIZO_PCI_AFSR_IO_SPACE,	PCI_SIDEB,
27420Sstevel@tonic-gate 	SCH_REG_SAFARI_REGS,	NULL,				NULL,
27430Sstevel@tonic-gate 	NULL,			NULL,				NULL,
27440Sstevel@tonic-gate };
27450Sstevel@tonic-gate 
27460Sstevel@tonic-gate /*
27470Sstevel@tonic-gate  * Function used to convert the 32 bit PIO address captured for a
27480Sstevel@tonic-gate  * Safari Bus UE(during PIO Rd/Wr) to a full Safari Bus Address.
27490Sstevel@tonic-gate  */
27500Sstevel@tonic-gate static void
27510Sstevel@tonic-gate pci_format_ecc_addr(dev_info_t *dip, uint64_t *afar, ecc_region_t region)
27520Sstevel@tonic-gate {
27530Sstevel@tonic-gate 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
27540Sstevel@tonic-gate 	pci_common_t *cmn_p = pci_p->pci_common_p;
27550Sstevel@tonic-gate 	cb_t *cb_p = pci_p->pci_cb_p;
27560Sstevel@tonic-gate 	int i, pci_side = 0;
27570Sstevel@tonic-gate 	int swap = 0;
27580Sstevel@tonic-gate 	uint64_t pa = cb_p->cb_base_pa;
27590Sstevel@tonic-gate 	uint64_t flag, schizo_base, pci_csr_base;
27600Sstevel@tonic-gate 
27610Sstevel@tonic-gate 	if (pci_p == NULL)
27620Sstevel@tonic-gate 		return;
27630Sstevel@tonic-gate 
27640Sstevel@tonic-gate 	pci_csr_base = va_to_pa(pci_p->pci_address[0]);
27650Sstevel@tonic-gate 
27660Sstevel@tonic-gate 	/*
27670Sstevel@tonic-gate 	 * Using the csr_base address to determine which side
27680Sstevel@tonic-gate 	 * we are on.
27690Sstevel@tonic-gate 	 */
27700Sstevel@tonic-gate 	if (pci_csr_base & PCI_SIDE_ADDR_MASK)
27710Sstevel@tonic-gate 		pci_side = 1;
27720Sstevel@tonic-gate 	else
27730Sstevel@tonic-gate 		pci_side = 0;
27740Sstevel@tonic-gate 
27750Sstevel@tonic-gate 	schizo_base = pa - PBM_CTRL_OFFSET;
27760Sstevel@tonic-gate 
27770Sstevel@tonic-gate 	for (i = 0; ecc_format_tbl[i].ecc_region != NULL; i++) {
27780Sstevel@tonic-gate 		if (region == ecc_format_tbl[i].ecc_region) {
27790Sstevel@tonic-gate 			flag = ecc_format_tbl[i].ecc_space;
27800Sstevel@tonic-gate 			if (ecc_format_tbl[i].ecc_side != pci_side)
27810Sstevel@tonic-gate 				swap = 1;
27820Sstevel@tonic-gate 			if (region == SCH_REG_SAFARI_REGS)
27830Sstevel@tonic-gate 				*afar |= schizo_base;
27840Sstevel@tonic-gate 			break;
27850Sstevel@tonic-gate 		}
27860Sstevel@tonic-gate 	}
27870Sstevel@tonic-gate 
27880Sstevel@tonic-gate 	if (swap) {
27890Sstevel@tonic-gate 		pci_p = cmn_p->pci_p[PCI_OTHER_SIDE(pci_p->pci_side)];
27900Sstevel@tonic-gate 
27910Sstevel@tonic-gate 		if (pci_p == NULL)
27920Sstevel@tonic-gate 			return;
27930Sstevel@tonic-gate 	}
27940Sstevel@tonic-gate 	pci_format_addr(pci_p->pci_dip, afar, flag);
27950Sstevel@tonic-gate }
27960Sstevel@tonic-gate 
27970Sstevel@tonic-gate /*
27980Sstevel@tonic-gate  * Function used to post control block specific ereports.
27990Sstevel@tonic-gate  */
28000Sstevel@tonic-gate static void
28010Sstevel@tonic-gate cb_ereport_post(dev_info_t *dip, uint64_t ena, cb_errstate_t *cb_err)
28020Sstevel@tonic-gate {
28030Sstevel@tonic-gate 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
28040Sstevel@tonic-gate 	char buf[FM_MAX_CLASS], dev_path[MAXPATHLEN], *ptr;
28050Sstevel@tonic-gate 	struct i_ddi_fmhdl *fmhdl = DEVI(dip)->devi_fmhdl;
28060Sstevel@tonic-gate 	nvlist_t *ereport, *detector;
28070Sstevel@tonic-gate 	errorq_elem_t *eqep;
28080Sstevel@tonic-gate 	nv_alloc_t *nva;
28090Sstevel@tonic-gate 
28100Sstevel@tonic-gate 	DEBUG1(DBG_ATTACH, dip, "cb_ereport_post: elog 0x%lx",
28110Sstevel@tonic-gate 	    cb_err->cb_elog);
28120Sstevel@tonic-gate 
28130Sstevel@tonic-gate 	/*
28140Sstevel@tonic-gate 	 * We do not use ddi_fm_ereport_post because we need to set a
28150Sstevel@tonic-gate 	 * special detector here. Since we do not have a device path for
28160Sstevel@tonic-gate 	 * the bridge chip we use what we think it should be to aid in
28170Sstevel@tonic-gate 	 * diagnosis.
28180Sstevel@tonic-gate 	 */
28190Sstevel@tonic-gate 	(void) snprintf(buf, FM_MAX_CLASS, "%s.%s.%s", DDI_IO_CLASS,
28200Sstevel@tonic-gate 	    cb_err->cb_bridge_type, cb_err->cb_err_class);
28210Sstevel@tonic-gate 
28220Sstevel@tonic-gate 	ena = ena ? ena : fm_ena_generate(0, FM_ENA_FMT1);
28230Sstevel@tonic-gate 
28240Sstevel@tonic-gate 	eqep = errorq_reserve(fmhdl->fh_errorq);
28250Sstevel@tonic-gate 	if (eqep == NULL)
28260Sstevel@tonic-gate 		return;
28270Sstevel@tonic-gate 
28280Sstevel@tonic-gate 	ereport = errorq_elem_nvl(fmhdl->fh_errorq, eqep);
28290Sstevel@tonic-gate 	nva = errorq_elem_nva(fmhdl->fh_errorq, eqep);
28300Sstevel@tonic-gate 	detector = fm_nvlist_create(nva);
28310Sstevel@tonic-gate 
28320Sstevel@tonic-gate 	ASSERT(ereport);
28330Sstevel@tonic-gate 	ASSERT(nva);
28340Sstevel@tonic-gate 	ASSERT(detector);
28350Sstevel@tonic-gate 
28360Sstevel@tonic-gate 	ddi_pathname(dip, dev_path);
28370Sstevel@tonic-gate 	ptr = strrchr(dev_path, (int)',');
28380Sstevel@tonic-gate 
28390Sstevel@tonic-gate 	if (ptr)
28400Sstevel@tonic-gate 		*ptr = '\0';
28410Sstevel@tonic-gate 
28420Sstevel@tonic-gate 	fm_fmri_dev_set(detector, FM_DEV_SCHEME_VERSION, NULL, dev_path, NULL);
28430Sstevel@tonic-gate 
28440Sstevel@tonic-gate 	DEBUG1(DBG_ERR_INTR, dip, "cb_ereport_post: ereport_set: %s", buf);
28450Sstevel@tonic-gate 
28460Sstevel@tonic-gate 	if (CHIP_TYPE(pci_p) == PCI_CHIP_SCHIZO ||
28470Sstevel@tonic-gate 	    CHIP_TYPE(pci_p) == PCI_CHIP_XMITS) {
28480Sstevel@tonic-gate 		fm_ereport_set(ereport, FM_EREPORT_VERSION, buf, ena, detector,
28490Sstevel@tonic-gate 		    SAFARI_CSR, DATA_TYPE_UINT64, cb_err->cb_csr,
28500Sstevel@tonic-gate 		    SAFARI_ERR, DATA_TYPE_UINT64, cb_err->cb_err,
28510Sstevel@tonic-gate 		    SAFARI_INTR, DATA_TYPE_UINT64, cb_err->cb_intr,
28520Sstevel@tonic-gate 		    SAFARI_ELOG, DATA_TYPE_UINT64, cb_err->cb_elog,
28530Sstevel@tonic-gate 		    SAFARI_PCR, DATA_TYPE_UINT64, cb_err->cb_pcr,
28540Sstevel@tonic-gate 		    NULL);
28550Sstevel@tonic-gate 	} else if (CHIP_TYPE(pci_p) == PCI_CHIP_TOMATILLO) {
28560Sstevel@tonic-gate 		fm_ereport_set(ereport, FM_EREPORT_VERSION, buf, ena, detector,
28570Sstevel@tonic-gate 		    JBUS_CSR, DATA_TYPE_UINT64, cb_err->cb_csr,
28580Sstevel@tonic-gate 		    JBUS_ERR, DATA_TYPE_UINT64, cb_err->cb_err,
28590Sstevel@tonic-gate 		    JBUS_INTR, DATA_TYPE_UINT64, cb_err->cb_intr,
28600Sstevel@tonic-gate 		    JBUS_ELOG, DATA_TYPE_UINT64, cb_err->cb_elog,
28610Sstevel@tonic-gate 		    JBUS_PCR, DATA_TYPE_UINT64, cb_err->cb_pcr,
28620Sstevel@tonic-gate 		    NULL);
28630Sstevel@tonic-gate 	}
28640Sstevel@tonic-gate 	errorq_commit(fmhdl->fh_errorq, eqep, ERRORQ_ASYNC);
28650Sstevel@tonic-gate }
28660Sstevel@tonic-gate 
28670Sstevel@tonic-gate /*
28680Sstevel@tonic-gate  * Function used to post IOMMU specific ereports.
28690Sstevel@tonic-gate  */
28700Sstevel@tonic-gate static void
28710Sstevel@tonic-gate iommu_ereport_post(dev_info_t *dip, uint64_t ena, pbm_errstate_t *pbm_err)
28720Sstevel@tonic-gate {
28730Sstevel@tonic-gate 	char buf[FM_MAX_CLASS];
28740Sstevel@tonic-gate 
28750Sstevel@tonic-gate 	(void) snprintf(buf, FM_MAX_CLASS, "%s.%s",
28760Sstevel@tonic-gate 		    pbm_err->pbm_bridge_type, pbm_err->pbm_err_class);
28770Sstevel@tonic-gate 
28780Sstevel@tonic-gate 	ena = ena ? ena : fm_ena_generate(0, FM_ENA_FMT1);
28790Sstevel@tonic-gate 
28800Sstevel@tonic-gate 	DEBUG1(DBG_ERR_INTR, dip, "iommu_ereport_post: ereport_set: %s", buf);
28810Sstevel@tonic-gate 
28820Sstevel@tonic-gate 	ddi_fm_ereport_post(dip, buf, ena, DDI_NOSLEEP,
28830Sstevel@tonic-gate 	    FM_VERSION, DATA_TYPE_UINT8, 0,
28840Sstevel@tonic-gate 	    PCI_CONFIG_STATUS, DATA_TYPE_UINT16, pbm_err->pbm_pci.pci_cfg_stat,
28850Sstevel@tonic-gate 	    PCI_CONFIG_COMMAND, DATA_TYPE_UINT16, pbm_err->pbm_pci.pci_cfg_comm,
28860Sstevel@tonic-gate 	    PCI_PBM_CSR, DATA_TYPE_UINT64, pbm_err->pbm_ctl_stat,
28870Sstevel@tonic-gate 	    PCI_PBM_IOMMU_CTRL, DATA_TYPE_UINT64, pbm_err->pbm_iommu.iommu_stat,
28880Sstevel@tonic-gate 	    PCI_PBM_IOMMU_TFAR, DATA_TYPE_UINT64, pbm_err->pbm_iommu.iommu_tfar,
28890Sstevel@tonic-gate 	    PCI_PBM_SLOT, DATA_TYPE_UINT64, pbm_err->pbm_err_sl,
28900Sstevel@tonic-gate 	    PCI_PBM_VALOG, DATA_TYPE_UINT64, pbm_err->pbm_va_log,
28910Sstevel@tonic-gate 	    NULL);
28920Sstevel@tonic-gate }
28930Sstevel@tonic-gate 
28940Sstevel@tonic-gate /*
28950Sstevel@tonic-gate  * Function used to post PCI-X generic ereports.
28960Sstevel@tonic-gate  * This function needs to be fixed once the Fault Boundary Analysis
28970Sstevel@tonic-gate  * for PCI-X is conducted. The payload should be made more generic.
28980Sstevel@tonic-gate  */
28990Sstevel@tonic-gate static void
29000Sstevel@tonic-gate pcix_ereport_post(dev_info_t *dip, uint64_t ena, pbm_errstate_t *pbm_err)
29010Sstevel@tonic-gate {
29020Sstevel@tonic-gate 	char buf[FM_MAX_CLASS];
29030Sstevel@tonic-gate 
29041865Sdilpreet 	(void) snprintf(buf, FM_MAX_CLASS, "%s.%s",
29051865Sdilpreet 		    pbm_err->pbm_bridge_type, pbm_err->pbm_err_class);
29061865Sdilpreet 
29070Sstevel@tonic-gate 	ena = ena ? ena : fm_ena_generate(0, FM_ENA_FMT1);
29080Sstevel@tonic-gate 
29090Sstevel@tonic-gate 	DEBUG1(DBG_ERR_INTR, dip, "pcix_ereport_post: ereport_post: %s", buf);
29100Sstevel@tonic-gate 
29111865Sdilpreet 	ddi_fm_ereport_post(dip, buf, ena, DDI_NOSLEEP,
29120Sstevel@tonic-gate 	    FM_VERSION, DATA_TYPE_UINT8, 0,
29130Sstevel@tonic-gate 	    PCI_CONFIG_STATUS, DATA_TYPE_UINT16, pbm_err->pbm_pci.pci_cfg_stat,
29140Sstevel@tonic-gate 	    PCI_CONFIG_COMMAND, DATA_TYPE_UINT16, pbm_err->pbm_pci.pci_cfg_comm,
29150Sstevel@tonic-gate 	    PCI_PBM_CSR, DATA_TYPE_UINT64, pbm_err->pbm_ctl_stat,
29160Sstevel@tonic-gate 	    PCI_PBM_AFSR, DATA_TYPE_UINT64, pbm_err->pbm_afsr,
29170Sstevel@tonic-gate 	    PCI_PBM_AFAR, DATA_TYPE_UINT64, pbm_err->pbm_afar,
29180Sstevel@tonic-gate 	    PCI_PBM_SLOT, DATA_TYPE_UINT64, pbm_err->pbm_err_sl,
29190Sstevel@tonic-gate 	    PCIX_STAT, DATA_TYPE_UINT64, pbm_err->pbm_pcix_stat,
29200Sstevel@tonic-gate 	    PCIX_PFAR, DATA_TYPE_UINT32, pbm_err->pbm_pcix_pfar,
29210Sstevel@tonic-gate 	    NULL);
29220Sstevel@tonic-gate }
29230Sstevel@tonic-gate 
29240Sstevel@tonic-gate static void
29250Sstevel@tonic-gate iommu_ctx_free(iommu_t *iommu_p)
29260Sstevel@tonic-gate {
29270Sstevel@tonic-gate 	kmem_free(iommu_p->iommu_ctx_bitmap, IOMMU_CTX_BITMAP_SIZE);
29280Sstevel@tonic-gate }
29290Sstevel@tonic-gate 
29300Sstevel@tonic-gate /*
29310Sstevel@tonic-gate  * iommu_tlb_scrub():
29320Sstevel@tonic-gate  *	Exam TLB entries through TLB diagnostic registers and look for errors.
29330Sstevel@tonic-gate  *	scrub = 1 : cleanup all error bits in tlb, called in FAULT_RESET case
29340Sstevel@tonic-gate  *	scrub = 0 : log all error conditions to console, FAULT_LOG case
29350Sstevel@tonic-gate  *	In both cases, it returns number of errors found in tlb entries.
29360Sstevel@tonic-gate  */
29370Sstevel@tonic-gate static int
29380Sstevel@tonic-gate iommu_tlb_scrub(iommu_t *iommu_p, int scrub)
29390Sstevel@tonic-gate {
29400Sstevel@tonic-gate 	int i, nerr = 0;
29410Sstevel@tonic-gate 	dev_info_t *dip = iommu_p->iommu_pci_p->pci_dip;
29420Sstevel@tonic-gate 	char *neg = "not ";
29430Sstevel@tonic-gate 
29440Sstevel@tonic-gate 	uint64_t base = (uint64_t)iommu_p->iommu_ctrl_reg -
29450Sstevel@tonic-gate 		COMMON_IOMMU_CTRL_REG_OFFSET;
29460Sstevel@tonic-gate 
29470Sstevel@tonic-gate 	volatile uint64_t *tlb_tag = (volatile uint64_t *)
29480Sstevel@tonic-gate 		(base + COMMON_IOMMU_TLB_TAG_DIAG_ACC_OFFSET);
29490Sstevel@tonic-gate 	volatile uint64_t *tlb_data = (volatile uint64_t *)
29500Sstevel@tonic-gate 		(base + COMMON_IOMMU_TLB_DATA_DIAG_ACC_OFFSET);
29510Sstevel@tonic-gate 	for (i = 0; i < IOMMU_TLB_ENTRIES; i++) {
29520Sstevel@tonic-gate 		uint64_t tag = tlb_tag[i];
29530Sstevel@tonic-gate 		uint64_t data = tlb_data[i];
29540Sstevel@tonic-gate 		uint32_t errstat;
29550Sstevel@tonic-gate 		iopfn_t pfn;
29560Sstevel@tonic-gate 
29570Sstevel@tonic-gate 		if (!(tag & TLBTAG_ERR_BIT))
29580Sstevel@tonic-gate 			continue;
29590Sstevel@tonic-gate 
29600Sstevel@tonic-gate 		pfn = (iopfn_t)(data & TLBDATA_MEMPA_BITS);
29610Sstevel@tonic-gate 		errstat = (uint32_t)
29620Sstevel@tonic-gate 			((tag & TLBTAG_ERRSTAT_BITS) >> TLBTAG_ERRSTAT_SHIFT);
29630Sstevel@tonic-gate 		if (errstat == TLBTAG_ERRSTAT_INVALID) {
29640Sstevel@tonic-gate 			if (scrub)
29650Sstevel@tonic-gate 				tlb_tag[i] = tlb_data[i] = 0ull;
29660Sstevel@tonic-gate 		} else
29670Sstevel@tonic-gate 			nerr++;
29680Sstevel@tonic-gate 
29690Sstevel@tonic-gate 		if (scrub)
29700Sstevel@tonic-gate 			continue;
29710Sstevel@tonic-gate 
29720Sstevel@tonic-gate 		cmn_err(CE_CONT, "%s%d: Error %x on IOMMU TLB entry %x:\n"
2973946Smathue 		"\tContext=%lx %sWritable %sStreamable\n"
2974946Smathue 		"\tPCI Page Size=%sk Address in page %lx\n",
29750Sstevel@tonic-gate 			ddi_driver_name(dip), ddi_get_instance(dip), errstat, i,
29760Sstevel@tonic-gate 			(tag & TLBTAG_CONTEXT_BITS) >> TLBTAG_CONTEXT_SHIFT,
29770Sstevel@tonic-gate 			(tag & TLBTAG_WRITABLE_BIT) ? "" : neg,
29780Sstevel@tonic-gate 			(tag & TLBTAG_STREAM_BIT) ? "" : neg,
29790Sstevel@tonic-gate 			(tag & TLBTAG_PGSIZE_BIT) ? "64" : "8",
29800Sstevel@tonic-gate 			(tag & TLBTAG_PCIVPN_BITS) << 13);
2981946Smathue 		cmn_err(CE_CONT, "Memory: %sValid %sCacheable Page Frame=%lx\n",
29820Sstevel@tonic-gate 			(data & TLBDATA_VALID_BIT) ? "" : neg,
29830Sstevel@tonic-gate 			(data & TLBDATA_CACHE_BIT) ? "" : neg, pfn);
29840Sstevel@tonic-gate 	}
29850Sstevel@tonic-gate 	return (nerr);
29860Sstevel@tonic-gate }
29870Sstevel@tonic-gate 
29880Sstevel@tonic-gate /*
29890Sstevel@tonic-gate  * pci_iommu_disp: calculates the displacement needed in tomatillo's
29900Sstevel@tonic-gate  *	iommu control register and modifies the control value template
29910Sstevel@tonic-gate  *	from caller. It also clears any error status bit that are new
29920Sstevel@tonic-gate  *	in tomatillo.
29930Sstevel@tonic-gate  * return value: an 8-bit mask to enable corresponding 512 MB segments
29940Sstevel@tonic-gate  *	suitable for tomatillo's target address register.
29950Sstevel@tonic-gate  *	0x00: no programming is needed, use existing value from prom
29960Sstevel@tonic-gate  *	0x60: use segment 5 and 6 to form a 1GB dvma range
29970Sstevel@tonic-gate  */
29980Sstevel@tonic-gate static uint64_t
29990Sstevel@tonic-gate pci_iommu_disp(iommu_t *iommu_p, uint64_t *ctl_p)
30000Sstevel@tonic-gate {
30010Sstevel@tonic-gate 	uint64_t ctl_old;
30020Sstevel@tonic-gate 	if (CHIP_TYPE(iommu_p->iommu_pci_p) != PCI_CHIP_TOMATILLO)
30030Sstevel@tonic-gate 		return (0);
30040Sstevel@tonic-gate 
30050Sstevel@tonic-gate 	ctl_old = *iommu_p->iommu_ctrl_reg;
30060Sstevel@tonic-gate 	/* iommu ctrl reg error bits are W1C */
30070Sstevel@tonic-gate 	if (ctl_old >> TOMATIILO_IOMMU_ERR_REG_SHIFT) {
3008946Smathue 		cmn_err(CE_WARN, "Tomatillo iommu err: %lx", ctl_old);
30090Sstevel@tonic-gate 		*ctl_p |= (ctl_old >> TOMATIILO_IOMMU_ERR_REG_SHIFT)
30100Sstevel@tonic-gate 		    << TOMATIILO_IOMMU_ERR_REG_SHIFT;
30110Sstevel@tonic-gate 	}
30120Sstevel@tonic-gate 
30130Sstevel@tonic-gate 	if (iommu_p->iommu_tsb_size != TOMATILLO_IOMMU_TSB_MAX)
30140Sstevel@tonic-gate 		return (0);
30150Sstevel@tonic-gate 
30160Sstevel@tonic-gate 	/* Tomatillo 2.0 and later, and 1GB DVMA range */
30170Sstevel@tonic-gate 	*ctl_p |= 1 << TOMATILLO_IOMMU_SEG_DISP_SHIFT;
30180Sstevel@tonic-gate 	return (3 << (iommu_p->iommu_dvma_base >> (32 - 3)));
30190Sstevel@tonic-gate }
30200Sstevel@tonic-gate 
30210Sstevel@tonic-gate void
30220Sstevel@tonic-gate pci_iommu_config(iommu_t *iommu_p, uint64_t iommu_ctl, uint64_t cfgpa)
30230Sstevel@tonic-gate {
30240Sstevel@tonic-gate 	uintptr_t pbm_regbase = get_pbm_reg_base(iommu_p->iommu_pci_p);
30250Sstevel@tonic-gate 	volatile uint64_t *pbm_csr_p = (volatile uint64_t *)pbm_regbase;
30260Sstevel@tonic-gate 	volatile uint64_t *tgt_space_p = (volatile uint64_t *)(pbm_regbase |
30270Sstevel@tonic-gate 		(TOMATILLO_TGT_ADDR_SPACE_OFFSET - SCHIZO_PCI_CTRL_REG_OFFSET));
30280Sstevel@tonic-gate 	volatile uint64_t pbm_ctl = *pbm_csr_p;
30290Sstevel@tonic-gate 
30300Sstevel@tonic-gate 	volatile uint64_t *iommu_ctl_p = iommu_p->iommu_ctrl_reg;
30310Sstevel@tonic-gate 	volatile uint64_t tsb_bar_val = iommu_p->iommu_tsb_paddr;
30320Sstevel@tonic-gate 	volatile uint64_t *tsb_bar_p = iommu_p->iommu_tsb_base_addr_reg;
30330Sstevel@tonic-gate 	uint64_t mask = pci_iommu_disp(iommu_p, &iommu_ctl);
30340Sstevel@tonic-gate 
30350Sstevel@tonic-gate 	DEBUG2(DBG_ATTACH, iommu_p->iommu_pci_p->pci_dip,
30360Sstevel@tonic-gate 		"\npci_iommu_config: pbm_csr_p=%llx pbm_ctl=%llx",
30370Sstevel@tonic-gate 		pbm_csr_p, pbm_ctl);
30380Sstevel@tonic-gate 	DEBUG2(DBG_ATTACH|DBG_CONT, iommu_p->iommu_pci_p->pci_dip,
30390Sstevel@tonic-gate 		"\n\tiommu_ctl_p=%llx iommu_ctl=%llx",
30400Sstevel@tonic-gate 		iommu_ctl_p, iommu_ctl);
30410Sstevel@tonic-gate 	DEBUG4(DBG_ATTACH|DBG_CONT, iommu_p->iommu_pci_p->pci_dip,
30420Sstevel@tonic-gate 		"\n\tcfgpa=%llx tgt_space_p=%llx mask=%x tsb=%llx\n",
30430Sstevel@tonic-gate 		cfgpa, tgt_space_p, mask, tsb_bar_val);
30440Sstevel@tonic-gate 
30450Sstevel@tonic-gate 	if (!cfgpa)
30460Sstevel@tonic-gate 		goto reprog;
30470Sstevel@tonic-gate 
30480Sstevel@tonic-gate 	/* disable PBM arbiters - turn off bits 0-7 */
30490Sstevel@tonic-gate 	*pbm_csr_p = (pbm_ctl >> 8) << 8;
30500Sstevel@tonic-gate 
30510Sstevel@tonic-gate 	/*
30520Sstevel@tonic-gate 	 * For non-XMITS, flush any previous writes. This is only
30530Sstevel@tonic-gate 	 * necessary for host bridges that may have a USB keywboard
30540Sstevel@tonic-gate 	 * attached.  XMITS does not.
30550Sstevel@tonic-gate 	 */
30560Sstevel@tonic-gate 	if (!(CHIP_TYPE(iommu_p->iommu_pci_p) == PCI_CHIP_XMITS))
30570Sstevel@tonic-gate 		(void) ldphysio(cfgpa);
30580Sstevel@tonic-gate 
30590Sstevel@tonic-gate reprog:
30600Sstevel@tonic-gate 	if (mask)
30610Sstevel@tonic-gate 		*tgt_space_p = mask;
30620Sstevel@tonic-gate 
30630Sstevel@tonic-gate 	*tsb_bar_p = tsb_bar_val;
30640Sstevel@tonic-gate 	*iommu_ctl_p = iommu_ctl;
30650Sstevel@tonic-gate 
30660Sstevel@tonic-gate 	*pbm_csr_p = pbm_ctl;	/* re-enable bus arbitration */
30670Sstevel@tonic-gate 	pbm_ctl = *pbm_csr_p;	/* flush all prev writes */
30680Sstevel@tonic-gate }
30690Sstevel@tonic-gate 
30700Sstevel@tonic-gate 
30710Sstevel@tonic-gate int
30720Sstevel@tonic-gate pci_get_portid(dev_info_t *dip)
30730Sstevel@tonic-gate {
30740Sstevel@tonic-gate 	return (ddi_getprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
30750Sstevel@tonic-gate 	    "portid", -1));
30760Sstevel@tonic-gate }
30770Sstevel@tonic-gate 
30780Sstevel@tonic-gate /*
30790Sstevel@tonic-gate  * Schizo Safari Performance Events.
30800Sstevel@tonic-gate  */
30810Sstevel@tonic-gate pci_kev_mask_t
30820Sstevel@tonic-gate schizo_saf_events[] = {
30830Sstevel@tonic-gate 	{"saf_bus_cycles", 0x1},	{"saf_pause_asserted_cycles", 0x2},
30840Sstevel@tonic-gate 	{"saf_frn_coherent_cmds", 0x3},	{"saf_frn_coherent_hits", 0x4},
30850Sstevel@tonic-gate 	{"saf_my_coherent_cmds", 0x5},	{"saf_my_coherent_hits", 0x6},
30860Sstevel@tonic-gate 	{"saf_frn_io_cmds", 0x7}, 	{"saf_frn_io_hits", 0x8},
30870Sstevel@tonic-gate 	{"merge_buffer", 0x9}, 		{"interrupts", 0xa},
30880Sstevel@tonic-gate 	{"csr_pios", 0xc}, 		{"upa_pios", 0xd},
30890Sstevel@tonic-gate 	{"pcia_pios", 0xe}, 		{"pcib_pios", 0xf},
30900Sstevel@tonic-gate 	{"saf_pause_seen_cycles", 0x11}, 	{"dvma_reads", 0x12},
30910Sstevel@tonic-gate 	{"dvma_writes", 0x13},		{"saf_orq_full_cycles", 0x14},
30920Sstevel@tonic-gate 	{"saf_data_in_cycles", 0x15},	{"saf_data_out_cycles", 0x16},
30930Sstevel@tonic-gate 	{"clear_pic", 0x1f}
30940Sstevel@tonic-gate };
30950Sstevel@tonic-gate 
30960Sstevel@tonic-gate 
30970Sstevel@tonic-gate /*
30980Sstevel@tonic-gate  * Schizo PCI Performance Events.
30990Sstevel@tonic-gate  */
31000Sstevel@tonic-gate pci_kev_mask_t
31010Sstevel@tonic-gate schizo_pci_events[] = {
31020Sstevel@tonic-gate 	{"dvma_stream_rd", 0x0}, 	{"dvma_stream_wr", 0x1},
31030Sstevel@tonic-gate 	{"dvma_const_rd", 0x2},		{"dvma_const_wr", 0x3},
31040Sstevel@tonic-gate 	{"dvma_stream_buf_mis", 0x4},	{"dvma_cycles", 0x5},
31050Sstevel@tonic-gate 	{"dvma_wd_xfr", 0x6},		{"pio_cycles", 0x7},
31060Sstevel@tonic-gate 	{"dvma_tlb_misses", 0x10},	{"interrupts", 0x11},
31070Sstevel@tonic-gate 	{"saf_inter_nack", 0x12},	{"pio_reads", 0x13},
31080Sstevel@tonic-gate 	{"pio_writes", 0x14},		{"dvma_rd_buf_timeout", 0x15},
31090Sstevel@tonic-gate 	{"dvma_rd_rtry_stc", 0x16},	{"dvma_wr_rtry_stc", 0x17},
31100Sstevel@tonic-gate 	{"dvma_rd_rtry_nonstc", 0x18},	{"dvma_wr_rtry_nonstc", 0x19},
31110Sstevel@tonic-gate 	{"E*_slow_transitions", 0x1a},	{"E*_slow_cycles_per_64", 0x1b},
31120Sstevel@tonic-gate 	{"clear_pic", 0x1f}
31130Sstevel@tonic-gate };
31140Sstevel@tonic-gate 
31150Sstevel@tonic-gate 
31160Sstevel@tonic-gate /*
31170Sstevel@tonic-gate  * Create the picN kstats for the pci
31180Sstevel@tonic-gate  * and safari events.
31190Sstevel@tonic-gate  */
31200Sstevel@tonic-gate void
31210Sstevel@tonic-gate pci_kstat_init()
31220Sstevel@tonic-gate {
31230Sstevel@tonic-gate 	pci_name_kstat = (pci_ksinfo_t *)kmem_alloc(sizeof (pci_ksinfo_t),
31240Sstevel@tonic-gate 		KM_NOSLEEP);
31250Sstevel@tonic-gate 
31260Sstevel@tonic-gate 	if (pci_name_kstat == NULL) {
31270Sstevel@tonic-gate 		cmn_err(CE_WARN, "pcisch : no space for kstat\n");
31280Sstevel@tonic-gate 	} else {
31290Sstevel@tonic-gate 		pci_name_kstat->pic_no_evs =
31300Sstevel@tonic-gate 			sizeof (schizo_pci_events) / sizeof (pci_kev_mask_t);
31310Sstevel@tonic-gate 		pci_name_kstat->pic_shift[0] = SCHIZO_SHIFT_PIC0;
31320Sstevel@tonic-gate 		pci_name_kstat->pic_shift[1] = SCHIZO_SHIFT_PIC1;
31330Sstevel@tonic-gate 		pci_create_name_kstat("pcis",
31340Sstevel@tonic-gate 			pci_name_kstat, schizo_pci_events);
31350Sstevel@tonic-gate 	}
31360Sstevel@tonic-gate 
31370Sstevel@tonic-gate 	saf_name_kstat = (pci_ksinfo_t *)kmem_alloc(sizeof (pci_ksinfo_t),
31380Sstevel@tonic-gate 		KM_NOSLEEP);
31390Sstevel@tonic-gate 	if (saf_name_kstat == NULL) {
31400Sstevel@tonic-gate 		cmn_err(CE_WARN, "pcisch : no space for kstat\n");
31410Sstevel@tonic-gate 	} else {
31420Sstevel@tonic-gate 		saf_name_kstat->pic_no_evs =
31430Sstevel@tonic-gate 			sizeof (schizo_saf_events) / sizeof (pci_kev_mask_t);
31440Sstevel@tonic-gate 		saf_name_kstat->pic_shift[0] = SCHIZO_SHIFT_PIC0;
31450Sstevel@tonic-gate 		saf_name_kstat->pic_shift[1] = SCHIZO_SHIFT_PIC1;
31460Sstevel@tonic-gate 		pci_create_name_kstat("saf", saf_name_kstat, schizo_saf_events);
31470Sstevel@tonic-gate 	}
31480Sstevel@tonic-gate }
31490Sstevel@tonic-gate 
31500Sstevel@tonic-gate void
31510Sstevel@tonic-gate pci_kstat_fini()
31520Sstevel@tonic-gate {
31530Sstevel@tonic-gate 	if (pci_name_kstat != NULL) {
31540Sstevel@tonic-gate 		pci_delete_name_kstat(pci_name_kstat);
31550Sstevel@tonic-gate 		kmem_free(pci_name_kstat, sizeof (pci_ksinfo_t));
31560Sstevel@tonic-gate 		pci_name_kstat = NULL;
31570Sstevel@tonic-gate 	}
31580Sstevel@tonic-gate 
31590Sstevel@tonic-gate 	if (saf_name_kstat != NULL) {
31600Sstevel@tonic-gate 		pci_delete_name_kstat(saf_name_kstat);
31610Sstevel@tonic-gate 		kmem_free(saf_name_kstat, sizeof (pci_ksinfo_t));
31620Sstevel@tonic-gate 		saf_name_kstat = NULL;
31630Sstevel@tonic-gate 	}
31640Sstevel@tonic-gate }
31650Sstevel@tonic-gate 
31660Sstevel@tonic-gate /*
31670Sstevel@tonic-gate  * Create 'counters' kstat for pci events.
31680Sstevel@tonic-gate  */
31690Sstevel@tonic-gate void
31700Sstevel@tonic-gate pci_add_pci_kstat(pci_t *pci_p)
31710Sstevel@tonic-gate {
31720Sstevel@tonic-gate 	pci_cntr_addr_t *cntr_addr_p = &pci_p->pci_ks_addr;
31730Sstevel@tonic-gate 	uintptr_t regbase = (uintptr_t)pci_p->pci_address[0];
31740Sstevel@tonic-gate 
31750Sstevel@tonic-gate 	cntr_addr_p->pcr_addr = (uint64_t *)
31760Sstevel@tonic-gate 		(regbase + SCHIZO_PERF_PCI_PCR_OFFSET);
31770Sstevel@tonic-gate 	cntr_addr_p->pic_addr = (uint64_t *)
31780Sstevel@tonic-gate 		(regbase + SCHIZO_PERF_PCI_PIC_OFFSET);
31790Sstevel@tonic-gate 
31800Sstevel@tonic-gate 	pci_p->pci_ksp = pci_create_cntr_kstat(pci_p, "pcis",
31810Sstevel@tonic-gate 		NUM_OF_PICS, pci_cntr_kstat_update, cntr_addr_p);
31820Sstevel@tonic-gate 
31830Sstevel@tonic-gate 	if (pci_p->pci_ksp == NULL) {
31840Sstevel@tonic-gate 		cmn_err(CE_WARN, "pcisch : cannot create counter kstat");
31850Sstevel@tonic-gate 	}
31860Sstevel@tonic-gate }
31870Sstevel@tonic-gate 
31880Sstevel@tonic-gate void
31890Sstevel@tonic-gate pci_rem_pci_kstat(pci_t *pci_p)
31900Sstevel@tonic-gate {
31910Sstevel@tonic-gate 	if (pci_p->pci_ksp != NULL)
31920Sstevel@tonic-gate 		kstat_delete(pci_p->pci_ksp);
31930Sstevel@tonic-gate 	pci_p->pci_ksp = NULL;
31940Sstevel@tonic-gate }
31950Sstevel@tonic-gate 
31960Sstevel@tonic-gate void
31970Sstevel@tonic-gate pci_add_upstream_kstat(pci_t *pci_p)
31980Sstevel@tonic-gate {
31990Sstevel@tonic-gate 	pci_common_t	*cmn_p = pci_p->pci_common_p;
32000Sstevel@tonic-gate 	pci_cntr_pa_t	*cntr_pa_p = &cmn_p->pci_cmn_uks_pa;
32010Sstevel@tonic-gate 	uint64_t regbase = va_to_pa(pci_p->pci_address[1]);
32020Sstevel@tonic-gate 
32030Sstevel@tonic-gate 	cntr_pa_p->pcr_pa =
32040Sstevel@tonic-gate 		regbase + SCHIZO_PERF_SAF_PCR_OFFSET;
32050Sstevel@tonic-gate 	cntr_pa_p->pic_pa =
32060Sstevel@tonic-gate 		regbase + SCHIZO_PERF_SAF_PIC_OFFSET;
32070Sstevel@tonic-gate 
32080Sstevel@tonic-gate 	cmn_p->pci_common_uksp = pci_create_cntr_kstat(pci_p, "saf",
32090Sstevel@tonic-gate 		NUM_OF_PICS, pci_cntr_kstat_pa_update, cntr_pa_p);
32100Sstevel@tonic-gate }
32110Sstevel@tonic-gate 
32120Sstevel@tonic-gate /*
32130Sstevel@tonic-gate  * Extract the drivers binding name to identify which chip
32140Sstevel@tonic-gate  * we're binding to.  Whenever a new bus bridge is created, the driver alias
32150Sstevel@tonic-gate  * entry should be added here to identify the device if needed.  If a device
32160Sstevel@tonic-gate  * isn't added, the identity defaults to PCI_CHIP_UNIDENTIFIED.
32170Sstevel@tonic-gate  */
32180Sstevel@tonic-gate static uint32_t
32190Sstevel@tonic-gate pci_identity_init(pci_t *pci_p)
32200Sstevel@tonic-gate {
32210Sstevel@tonic-gate 	dev_info_t *dip = pci_p->pci_dip;
32220Sstevel@tonic-gate 	char *name = ddi_binding_name(dip);
32230Sstevel@tonic-gate 	uint32_t ver = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
32240Sstevel@tonic-gate 		"version#", 0);
32250Sstevel@tonic-gate 
32260Sstevel@tonic-gate 	if (strcmp(name, "pci108e,a801") == 0)
32270Sstevel@tonic-gate 		return (CHIP_ID(PCI_CHIP_TOMATILLO, ver, 0x00));
32280Sstevel@tonic-gate 
32290Sstevel@tonic-gate 	if (strcmp(name, "pci108e,8001") == 0)
32300Sstevel@tonic-gate 		return (CHIP_ID(PCI_CHIP_SCHIZO, ver, 0x00));
32310Sstevel@tonic-gate 
32320Sstevel@tonic-gate 	if (strcmp(name, "pci108e,8002") == 0) {
32330Sstevel@tonic-gate 		uint32_t mod_rev = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
32340Sstevel@tonic-gate 			DDI_PROP_DONTPASS, "module-revision#", 0);
32350Sstevel@tonic-gate 		return (CHIP_ID(PCI_CHIP_XMITS, ver, mod_rev));
32360Sstevel@tonic-gate 	}
32370Sstevel@tonic-gate 
32380Sstevel@tonic-gate 	cmn_err(CE_WARN, "%s%d: Unknown PCI Host bridge %s %x\n",
32390Sstevel@tonic-gate 		ddi_driver_name(dip), ddi_get_instance(dip), name, ver);
32400Sstevel@tonic-gate 
32410Sstevel@tonic-gate 	return (PCI_CHIP_UNIDENTIFIED);
32420Sstevel@tonic-gate }
32430Sstevel@tonic-gate 
32440Sstevel@tonic-gate /*
32450Sstevel@tonic-gate  * Setup a physical pointer to one leaf config space area. This
32460Sstevel@tonic-gate  * is used in several places in order to do a dummy read which
32470Sstevel@tonic-gate  * guarantees the nexus (and not a bus master) has gained control
32480Sstevel@tonic-gate  * of the bus.
32490Sstevel@tonic-gate  */
32500Sstevel@tonic-gate static void
32510Sstevel@tonic-gate pci_setup_cfgpa(pci_t *pci_p)
32520Sstevel@tonic-gate {
32530Sstevel@tonic-gate 	dev_info_t *dip = pci_p->pci_dip;
32540Sstevel@tonic-gate 	dev_info_t *cdip;
32550Sstevel@tonic-gate 	pbm_t *pbm_p = pci_p->pci_pbm_p;
32560Sstevel@tonic-gate 	uint64_t cfgpa = pci_get_cfg_pabase(pci_p);
32570Sstevel@tonic-gate 	uint32_t *reg_p;
32580Sstevel@tonic-gate 	int reg_len;
32590Sstevel@tonic-gate 
32600Sstevel@tonic-gate 	for (cdip = ddi_get_child(dip); cdip != NULL;
32610Sstevel@tonic-gate 	    cdip = ddi_get_next_sibling(cdip)) {
3262506Scth 		if (ddi_getlongprop(DDI_DEV_T_ANY, cdip, DDI_PROP_DONTPASS,
32630Sstevel@tonic-gate 		    "reg", (caddr_t)&reg_p, &reg_len) != DDI_PROP_SUCCESS)
32640Sstevel@tonic-gate 			continue;
32650Sstevel@tonic-gate 		cfgpa += (*reg_p) & (PCI_CONF_ADDR_MASK ^ PCI_REG_REG_M);
32660Sstevel@tonic-gate 		kmem_free(reg_p, reg_len);
32670Sstevel@tonic-gate 		break;
32680Sstevel@tonic-gate 	}
32690Sstevel@tonic-gate 	pbm_p->pbm_anychild_cfgpa = cfgpa;
32700Sstevel@tonic-gate }
32710Sstevel@tonic-gate 
32720Sstevel@tonic-gate void
32730Sstevel@tonic-gate pci_post_init_child(pci_t *pci_p, dev_info_t *child)
32740Sstevel@tonic-gate {
32750Sstevel@tonic-gate 	volatile uint64_t *ctrl_reg_p;
32760Sstevel@tonic-gate 	pbm_t *pbm_p = pci_p->pci_pbm_p;
32770Sstevel@tonic-gate 
32780Sstevel@tonic-gate 	pci_setup_cfgpa(pci_p);
32790Sstevel@tonic-gate 
32800Sstevel@tonic-gate 	/*
32810Sstevel@tonic-gate 	 * This is a hack for skyhawk/casinni combination to address
32820Sstevel@tonic-gate 	 * hardware problems between the request and grant signals which
32830Sstevel@tonic-gate 	 * causes a bus hang.  One workaround, which is applied here,
32840Sstevel@tonic-gate 	 * is to disable bus parking if the child contains the property
32850Sstevel@tonic-gate 	 * pci-req-removal.  Note that if the bus is quiesced we must mask
32860Sstevel@tonic-gate 	 * off the parking bit in the saved control registers, since the
32870Sstevel@tonic-gate 	 * quiesce operation temporarily turns off PCI bus parking.
32880Sstevel@tonic-gate 	 */
32890Sstevel@tonic-gate 	if (ddi_prop_exists(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
32900Sstevel@tonic-gate 		"pci-req-removal") == 1) {
32910Sstevel@tonic-gate 
32920Sstevel@tonic-gate 		if (pbm_p->pbm_quiesce_count > 0) {
32930Sstevel@tonic-gate 			pbm_p->pbm_saved_ctrl_reg &= ~SCHIZO_PCI_CTRL_ARB_PARK;
32940Sstevel@tonic-gate 		} else {
32950Sstevel@tonic-gate 			ctrl_reg_p = pbm_p->pbm_ctrl_reg;
32960Sstevel@tonic-gate 			*ctrl_reg_p &= ~SCHIZO_PCI_CTRL_ARB_PARK;
32970Sstevel@tonic-gate 		}
32980Sstevel@tonic-gate 	}
32990Sstevel@tonic-gate 
33000Sstevel@tonic-gate 	if (CHIP_TYPE(pci_p) == PCI_CHIP_XMITS) {
33010Sstevel@tonic-gate 		if (*pbm_p->pbm_ctrl_reg & XMITS_PCI_CTRL_X_MODE) {
33020Sstevel@tonic-gate 			int value;
33030Sstevel@tonic-gate 
33040Sstevel@tonic-gate 			/*
33050Sstevel@tonic-gate 			 * Due to a XMITS bug, we need to set the outstanding
33060Sstevel@tonic-gate 			 * split transactions to 1 for all PCI-X functions
33070Sstevel@tonic-gate 			 * behind the leaf.
33080Sstevel@tonic-gate 			 */
33090Sstevel@tonic-gate 			value = (xmits_max_transactions << 4) |
33100Sstevel@tonic-gate 			    (xmits_max_read_bytes << 2);
33110Sstevel@tonic-gate 
33120Sstevel@tonic-gate 			DEBUG1(DBG_INIT_CLD, child, "Turning on XMITS NCPQ "
33130Sstevel@tonic-gate 			    "Workaround: value = %x\n", value);
33140Sstevel@tonic-gate 
33150Sstevel@tonic-gate 			pcix_set_cmd_reg(child, value);
33160Sstevel@tonic-gate 
33170Sstevel@tonic-gate 			(void) ndi_prop_update_int(DDI_DEV_T_NONE,
33180Sstevel@tonic-gate 			    child, "pcix-update-cmd-reg", value);
33190Sstevel@tonic-gate 		}
33202358Sdanice 
33212358Sdanice 		if (PCI_CHIP_ID(pci_p) >= XMITS_VER_30) {
33222358Sdanice 			uint64_t *pbm_pcix_diag_reg =
33232358Sdanice 			    (uint64_t *)(pci_p->pci_address[0] +
33242358Sdanice 			    XMITS_PCI_X_DIAG_REG_OFFSET);
33252358Sdanice 			uint64_t bugcntl = (*pbm_pcix_diag_reg >>
33262358Sdanice 			    XMITS_PCI_X_DIAG_BUGCNTL_SHIFT) &
33272358Sdanice 			    XMITS_PCI_X_DIAG_BUGCNTL_MASK;
33282358Sdanice 			uint64_t tunable = (*pbm_p->pbm_ctrl_reg &
33292358Sdanice 			    XMITS_PCI_CTRL_X_MODE ?
33302358Sdanice 			    xmits_pcix_diag_bugcntl_pcix :
33312358Sdanice 			    xmits_pcix_diag_bugcntl_pci)
33322358Sdanice 			    & XMITS_PCI_X_DIAG_BUGCNTL_MASK;
33332358Sdanice 
33342358Sdanice 			DEBUG4(DBG_INIT_CLD, pci_p->pci_dip, "%s: XMITS "
33352358Sdanice 			    "pcix diag bugcntl=0x%lx, tunable=0x%lx, mode=%s\n",
33362358Sdanice 			    ddi_driver_name(child), bugcntl, tunable,
33372358Sdanice 			    ((*pbm_p->pbm_ctrl_reg & XMITS_PCI_CTRL_X_MODE)?
33382358Sdanice 			    "PCI-X":"PCI"));
33392358Sdanice 
33402358Sdanice 			DEBUG2(DBG_INIT_CLD, pci_p->pci_dip, "%s: XMITS "
33412358Sdanice 			    "pcix diag reg=0x%lx (CUR)\n",
33422358Sdanice 			    ddi_driver_name(child), *pbm_pcix_diag_reg);
33432358Sdanice 
33442358Sdanice 			/*
33452358Sdanice 			 * Due to a XMITS 3.x hw bug, we need to
33462358Sdanice 			 * read PBM's xmits pci ctrl status register to
33472358Sdanice 			 * determine mode (PCI or PCI-X) and then update
33482358Sdanice 			 * PBM's pcix diag register with new BUG_FIX_CNTL
33492358Sdanice 			 * bits (47:32) _if_ different from tunable's mode
33502358Sdanice 			 * based value. This update is performed only once
33512358Sdanice 			 * during the PBM's first child init.
33522358Sdanice 			 *
33532358Sdanice 			 * Per instructions from xmits hw engineering,
33542358Sdanice 			 * non-BUG_FIX_CNTL bits should not be preserved
33552358Sdanice 			 * when updating the pcix diag register. Such bits
33562358Sdanice 			 * should be written as 0s.
33572358Sdanice 			 */
33582358Sdanice 
33592358Sdanice 			if (bugcntl != tunable) {
33602358Sdanice 				*pbm_pcix_diag_reg = tunable <<
33612358Sdanice 				    XMITS_PCI_X_DIAG_BUGCNTL_SHIFT;
33622358Sdanice 
33632358Sdanice 				DEBUG2(DBG_INIT_CLD, pci_p->pci_dip, "%s: XMITS"
33642358Sdanice 				    " pcix diag reg=0x%lx (NEW)\n",
33652358Sdanice 				    ddi_driver_name(child), *pbm_pcix_diag_reg);
33662358Sdanice 			}
33672358Sdanice 		}
33680Sstevel@tonic-gate 	}
33690Sstevel@tonic-gate }
33700Sstevel@tonic-gate 
33710Sstevel@tonic-gate void
33720Sstevel@tonic-gate pci_post_uninit_child(pci_t *pci_p)
33730Sstevel@tonic-gate {
33740Sstevel@tonic-gate 	pci_setup_cfgpa(pci_p);
33750Sstevel@tonic-gate }
33760Sstevel@tonic-gate 
33770Sstevel@tonic-gate static int
33780Sstevel@tonic-gate pci_tom_nbintr_op(pci_t *pci_p, uint32_t inum, intrfunc f, caddr_t arg,
33790Sstevel@tonic-gate     int flag)
33800Sstevel@tonic-gate {
33810Sstevel@tonic-gate 	uint32_t ino = pci_p->pci_inos[inum];
33820Sstevel@tonic-gate 	uint32_t mondo = IB_INO_TO_NBMONDO(pci_p->pci_ib_p, ino);
33830Sstevel@tonic-gate 	int ret = DDI_SUCCESS;
33840Sstevel@tonic-gate 
33850Sstevel@tonic-gate 	mondo = CB_MONDO_TO_XMONDO(pci_p->pci_cb_p, mondo); /* no op on tom */
33860Sstevel@tonic-gate 
33870Sstevel@tonic-gate 	switch (flag) {
33880Sstevel@tonic-gate 	case PCI_OBJ_INTR_ADD:
33892973Sgovinda 		VERIFY(add_ivintr(mondo, pci_pil[inum], f,
33902973Sgovinda 		    arg, NULL, NULL) == 0);
33910Sstevel@tonic-gate 		break;
33920Sstevel@tonic-gate 	case PCI_OBJ_INTR_REMOVE:
33932973Sgovinda 		VERIFY(rem_ivintr(mondo, pci_pil[inum]) == 0);
33940Sstevel@tonic-gate 		break;
33950Sstevel@tonic-gate 	default:
33960Sstevel@tonic-gate 		ret = DDI_FAILURE;
33970Sstevel@tonic-gate 		break;
33980Sstevel@tonic-gate 	}
33990Sstevel@tonic-gate 
34000Sstevel@tonic-gate 	return (ret);
34010Sstevel@tonic-gate }
34020Sstevel@tonic-gate 
34030Sstevel@tonic-gate int
34040Sstevel@tonic-gate pci_ecc_add_intr(pci_t *pci_p, int inum, ecc_intr_info_t *eii_p)
34050Sstevel@tonic-gate {
34060Sstevel@tonic-gate 	uint32_t mondo;
34070Sstevel@tonic-gate 	int	r;
34080Sstevel@tonic-gate 
34090Sstevel@tonic-gate 	mondo = ((pci_p->pci_cb_p->cb_ign << PCI_INO_BITS) |
34100Sstevel@tonic-gate 	    pci_p->pci_inos[inum]);
34110Sstevel@tonic-gate 	mondo = CB_MONDO_TO_XMONDO(pci_p->pci_cb_p, mondo);
34120Sstevel@tonic-gate 
34132973Sgovinda 	VERIFY(add_ivintr(mondo, pci_pil[inum], (intrfunc)ecc_intr,
34142973Sgovinda 	    (caddr_t)eii_p, NULL, NULL) == 0);
34150Sstevel@tonic-gate 
34160Sstevel@tonic-gate 	if (CHIP_TYPE(pci_p) != PCI_CHIP_TOMATILLO)
34170Sstevel@tonic-gate 		return (PCI_ATTACH_RETCODE(PCI_ECC_OBJ, PCI_OBJ_INTR_ADD,
34180Sstevel@tonic-gate 		    DDI_SUCCESS));
34190Sstevel@tonic-gate 
34202973Sgovinda 	r = pci_tom_nbintr_op(pci_p, inum, (intrfunc)ecc_intr,
34210Sstevel@tonic-gate 	    (caddr_t)eii_p, PCI_OBJ_INTR_ADD);
34220Sstevel@tonic-gate 	return (PCI_ATTACH_RETCODE(PCI_ECC_OBJ, PCI_OBJ_INTR_ADD, r));
34230Sstevel@tonic-gate }
34240Sstevel@tonic-gate 
34250Sstevel@tonic-gate void
34260Sstevel@tonic-gate pci_ecc_rem_intr(pci_t *pci_p, int inum, ecc_intr_info_t *eii_p)
34270Sstevel@tonic-gate {
34280Sstevel@tonic-gate 	uint32_t mondo;
34290Sstevel@tonic-gate 
34300Sstevel@tonic-gate 	mondo = ((pci_p->pci_cb_p->cb_ign << PCI_INO_BITS) |
34310Sstevel@tonic-gate 	    pci_p->pci_inos[inum]);
34320Sstevel@tonic-gate 	mondo = CB_MONDO_TO_XMONDO(pci_p->pci_cb_p, mondo);
34330Sstevel@tonic-gate 
34342973Sgovinda 	VERIFY(rem_ivintr(mondo, pci_pil[inum]) == 0);
34350Sstevel@tonic-gate 
34360Sstevel@tonic-gate 	if (CHIP_TYPE(pci_p) == PCI_CHIP_TOMATILLO)
34372973Sgovinda 		pci_tom_nbintr_op(pci_p, inum, (intrfunc)ecc_intr,
34380Sstevel@tonic-gate 			(caddr_t)eii_p, PCI_OBJ_INTR_REMOVE);
34390Sstevel@tonic-gate }
34400Sstevel@tonic-gate 
34410Sstevel@tonic-gate static uint_t
34420Sstevel@tonic-gate pci_pbm_cdma_intr(caddr_t a)
34430Sstevel@tonic-gate {
34440Sstevel@tonic-gate 	pbm_t *pbm_p = (pbm_t *)a;
34450Sstevel@tonic-gate 	pbm_p->pbm_cdma_flag = PBM_CDMA_DONE;
34463558Sdduvall #ifdef PBM_CDMA_DEBUG
34473558Sdduvall 	pbm_p->pbm_cdma_intr_cnt++;
34483558Sdduvall #endif /* PBM_CDMA_DEBUG */
34490Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
34500Sstevel@tonic-gate }
34510Sstevel@tonic-gate 
34520Sstevel@tonic-gate int
34530Sstevel@tonic-gate pci_pbm_add_intr(pci_t *pci_p)
34540Sstevel@tonic-gate {
34550Sstevel@tonic-gate 	uint32_t mondo;
34560Sstevel@tonic-gate 
34570Sstevel@tonic-gate 	mondo = IB_INO_TO_MONDO(pci_p->pci_ib_p, pci_p->pci_inos[CBNINTR_CDMA]);
34580Sstevel@tonic-gate 	mondo = CB_MONDO_TO_XMONDO(pci_p->pci_cb_p, mondo);
34590Sstevel@tonic-gate 
34600Sstevel@tonic-gate 	VERIFY(add_ivintr(mondo, pci_pil[CBNINTR_CDMA],
34612973Sgovinda 	    (intrfunc)pci_pbm_cdma_intr, (caddr_t)pci_p->pci_pbm_p,
34622973Sgovinda 	    NULL, 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);
34772973Sgovinda 	VERIFY(rem_ivintr(mondo, pci_pil[CBNINTR_CDMA]) == 0);
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