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