xref: /onnv-gate/usr/src/uts/sun4u/io/px/px_lib4u.c (revision 2053:c81a99e1e596)
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
51540Skini  * Common Development and Distribution License (the "License").
61540Skini  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
221531Skini  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/kmem.h>
300Sstevel@tonic-gate #include <sys/conf.h>
310Sstevel@tonic-gate #include <sys/ddi.h>
320Sstevel@tonic-gate #include <sys/sunddi.h>
3327Sjchu #include <sys/fm/protocol.h>
3427Sjchu #include <sys/fm/util.h>
350Sstevel@tonic-gate #include <sys/modctl.h>
360Sstevel@tonic-gate #include <sys/disp.h>
370Sstevel@tonic-gate #include <sys/stat.h>
380Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
390Sstevel@tonic-gate #include <sys/vmem.h>
400Sstevel@tonic-gate #include <sys/iommutsb.h>
410Sstevel@tonic-gate #include <sys/cpuvar.h>
4227Sjchu #include <sys/ivintr.h>
43383Set142600 #include <sys/byteorder.h>
441531Skini #include <sys/hotplug/pci/pciehpc.h>
450Sstevel@tonic-gate #include <px_obj.h>
460Sstevel@tonic-gate #include <pcie_pwr.h>
471772Sjl139090 #include "px_tools_var.h"
480Sstevel@tonic-gate #include <px_regs.h>
490Sstevel@tonic-gate #include <px_csr.h>
5027Sjchu #include <sys/machsystm.h>
510Sstevel@tonic-gate #include "px_lib4u.h"
5227Sjchu #include "px_err.h"
531772Sjl139090 #include "oberon_regs.h"
540Sstevel@tonic-gate 
550Sstevel@tonic-gate #pragma weak jbus_stst_order
560Sstevel@tonic-gate 
570Sstevel@tonic-gate extern void jbus_stst_order();
580Sstevel@tonic-gate 
590Sstevel@tonic-gate ulong_t px_mmu_dvma_end = 0xfffffffful;
600Sstevel@tonic-gate uint_t px_ranges_phi_mask = 0xfffffffful;
611772Sjl139090 uint64_t *px_oberon_ubc_scratch_regs;
620Sstevel@tonic-gate 
630Sstevel@tonic-gate static int px_goto_l23ready(px_t *px_p);
64118Sjchu static int px_goto_l0(px_t *px_p);
65118Sjchu static int px_pre_pwron_check(px_t *px_p);
660Sstevel@tonic-gate static uint32_t px_identity_chip(px_t *px_p);
67435Sjchu static boolean_t px_cpr_callb(void *arg, int code);
681648Sjchu static uint_t px_cb_intr(caddr_t arg);
6927Sjchu 
7027Sjchu /*
7127Sjchu  * px_lib_map_registers
7227Sjchu  *
7327Sjchu  * This function is called from the attach routine to map the registers
7427Sjchu  * accessed by this driver.
7527Sjchu  *
7627Sjchu  * used by: px_attach()
7727Sjchu  *
7827Sjchu  * return value: DDI_FAILURE on failure
7927Sjchu  */
8027Sjchu int
8127Sjchu px_lib_map_regs(pxu_t *pxu_p, dev_info_t *dip)
8227Sjchu {
8327Sjchu 	ddi_device_acc_attr_t	attr;
8427Sjchu 	px_reg_bank_t		reg_bank = PX_REG_CSR;
8527Sjchu 
8627Sjchu 	DBG(DBG_ATTACH, dip, "px_lib_map_regs: pxu_p:0x%p, dip 0x%p\n",
8727Sjchu 		pxu_p, dip);
8827Sjchu 
8927Sjchu 	attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
9027Sjchu 	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
9127Sjchu 	attr.devacc_attr_endian_flags = DDI_NEVERSWAP_ACC;
9227Sjchu 
9327Sjchu 	/*
9427Sjchu 	 * PCI CSR Base
9527Sjchu 	 */
9627Sjchu 	if (ddi_regs_map_setup(dip, reg_bank, &pxu_p->px_address[reg_bank],
9727Sjchu 	    0, 0, &attr, &pxu_p->px_ac[reg_bank]) != DDI_SUCCESS) {
9827Sjchu 		goto fail;
9927Sjchu 	}
10027Sjchu 
10127Sjchu 	reg_bank++;
10227Sjchu 
10327Sjchu 	/*
10427Sjchu 	 * XBUS CSR Base
10527Sjchu 	 */
10627Sjchu 	if (ddi_regs_map_setup(dip, reg_bank, &pxu_p->px_address[reg_bank],
10727Sjchu 	    0, 0, &attr, &pxu_p->px_ac[reg_bank]) != DDI_SUCCESS) {
10827Sjchu 		goto fail;
10927Sjchu 	}
11027Sjchu 
11127Sjchu 	pxu_p->px_address[reg_bank] -= FIRE_CONTROL_STATUS;
11227Sjchu 
11327Sjchu done:
11427Sjchu 	for (; reg_bank >= PX_REG_CSR; reg_bank--) {
11527Sjchu 		DBG(DBG_ATTACH, dip, "reg_bank 0x%x address 0x%p\n",
11627Sjchu 		    reg_bank, pxu_p->px_address[reg_bank]);
11727Sjchu 	}
11827Sjchu 
11927Sjchu 	return (DDI_SUCCESS);
12027Sjchu 
12127Sjchu fail:
12227Sjchu 	cmn_err(CE_WARN, "%s%d: unable to map reg entry %d\n",
12327Sjchu 	    ddi_driver_name(dip), ddi_get_instance(dip), reg_bank);
12427Sjchu 
12527Sjchu 	for (reg_bank--; reg_bank >= PX_REG_CSR; reg_bank--) {
12627Sjchu 		pxu_p->px_address[reg_bank] = NULL;
12727Sjchu 		ddi_regs_map_free(&pxu_p->px_ac[reg_bank]);
12827Sjchu 	}
12927Sjchu 
13027Sjchu 	return (DDI_FAILURE);
13127Sjchu }
13227Sjchu 
13327Sjchu /*
13427Sjchu  * px_lib_unmap_regs:
13527Sjchu  *
13627Sjchu  * This routine unmaps the registers mapped by map_px_registers.
13727Sjchu  *
13827Sjchu  * used by: px_detach(), and error conditions in px_attach()
13927Sjchu  *
14027Sjchu  * return value: none
14127Sjchu  */
14227Sjchu void
14327Sjchu px_lib_unmap_regs(pxu_t *pxu_p)
14427Sjchu {
14527Sjchu 	int i;
14627Sjchu 
14727Sjchu 	for (i = 0; i < PX_REG_MAX; i++) {
14827Sjchu 		if (pxu_p->px_ac[i])
14927Sjchu 			ddi_regs_map_free(&pxu_p->px_ac[i]);
15027Sjchu 	}
15127Sjchu }
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate int
1540Sstevel@tonic-gate px_lib_dev_init(dev_info_t *dip, devhandle_t *dev_hdl)
1550Sstevel@tonic-gate {
1560Sstevel@tonic-gate 	px_t		*px_p = DIP_TO_STATE(dip);
15727Sjchu 	caddr_t		xbc_csr_base, csr_base;
1580Sstevel@tonic-gate 	px_dvma_range_prop_t	px_dvma_range;
1590Sstevel@tonic-gate 	uint32_t	chip_id;
1600Sstevel@tonic-gate 	pxu_t		*pxu_p;
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	DBG(DBG_ATTACH, dip, "px_lib_dev_init: dip 0x%p\n", dip);
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	if ((chip_id = px_identity_chip(px_p)) == PX_CHIP_UNIDENTIFIED)
1650Sstevel@tonic-gate 		return (DDI_FAILURE);
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	switch (chip_id) {
1680Sstevel@tonic-gate 	case FIRE_VER_10:
169225Sess 		cmn_err(CE_WARN, "FIRE Hardware Version 1.0 is not supported");
170225Sess 		return (DDI_FAILURE);
1710Sstevel@tonic-gate 	case FIRE_VER_20:
1720Sstevel@tonic-gate 		DBG(DBG_ATTACH, dip, "FIRE Hardware Version 2.0\n");
1730Sstevel@tonic-gate 		break;
1741772Sjl139090 	case OBERON_VER_10:
1751772Sjl139090 		DBG(DBG_ATTACH, dip, "Oberon Hardware Version 1.0\n");
1761772Sjl139090 		break;
1770Sstevel@tonic-gate 	default:
1781772Sjl139090 		cmn_err(CE_WARN, "%s%d: PX Hardware Version Unknown\n",
1790Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
1800Sstevel@tonic-gate 		return (DDI_FAILURE);
1810Sstevel@tonic-gate 	}
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	/*
1840Sstevel@tonic-gate 	 * Allocate platform specific structure and link it to
1850Sstevel@tonic-gate 	 * the px state structure.
1860Sstevel@tonic-gate 	 */
1870Sstevel@tonic-gate 	pxu_p = kmem_zalloc(sizeof (pxu_t), KM_SLEEP);
1880Sstevel@tonic-gate 	pxu_p->chip_id = chip_id;
1890Sstevel@tonic-gate 	pxu_p->portid  = ddi_getprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
1900Sstevel@tonic-gate 	    "portid", -1);
1910Sstevel@tonic-gate 
19227Sjchu 	/* Map in the registers */
19327Sjchu 	if (px_lib_map_regs(pxu_p, dip) == DDI_FAILURE) {
19427Sjchu 		kmem_free(pxu_p, sizeof (pxu_t));
19527Sjchu 
19627Sjchu 		return (DDI_FAILURE);
19727Sjchu 	}
19827Sjchu 
19927Sjchu 	xbc_csr_base = (caddr_t)pxu_p->px_address[PX_REG_XBC];
20027Sjchu 	csr_base = (caddr_t)pxu_p->px_address[PX_REG_CSR];
20127Sjchu 
2020Sstevel@tonic-gate 	pxu_p->tsb_cookie = iommu_tsb_alloc(pxu_p->portid);
2030Sstevel@tonic-gate 	pxu_p->tsb_size = iommu_tsb_cookie_to_size(pxu_p->tsb_cookie);
2040Sstevel@tonic-gate 	pxu_p->tsb_vaddr = iommu_tsb_cookie_to_va(pxu_p->tsb_cookie);
2050Sstevel@tonic-gate 
2061772Sjl139090 	pxu_p->tsb_paddr = va_to_pa(pxu_p->tsb_vaddr);
2071772Sjl139090 
2080Sstevel@tonic-gate 	/*
2090Sstevel@tonic-gate 	 * Create "virtual-dma" property to support child devices
2100Sstevel@tonic-gate 	 * needing to know DVMA range.
2110Sstevel@tonic-gate 	 */
2120Sstevel@tonic-gate 	px_dvma_range.dvma_base = (uint32_t)px_mmu_dvma_end + 1
2130Sstevel@tonic-gate 	    - ((pxu_p->tsb_size >> 3) << MMU_PAGE_SHIFT);
2140Sstevel@tonic-gate 	px_dvma_range.dvma_len = (uint32_t)
2150Sstevel@tonic-gate 	    px_mmu_dvma_end - px_dvma_range.dvma_base + 1;
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	(void) ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
2180Sstevel@tonic-gate 		"virtual-dma", (caddr_t)&px_dvma_range,
2190Sstevel@tonic-gate 		sizeof (px_dvma_range_prop_t));
2200Sstevel@tonic-gate 	/*
2210Sstevel@tonic-gate 	 * Initilize all fire hardware specific blocks.
2220Sstevel@tonic-gate 	 */
2230Sstevel@tonic-gate 	hvio_cb_init(xbc_csr_base, pxu_p);
2240Sstevel@tonic-gate 	hvio_ib_init(csr_base, pxu_p);
2250Sstevel@tonic-gate 	hvio_pec_init(csr_base, pxu_p);
2260Sstevel@tonic-gate 	hvio_mmu_init(csr_base, pxu_p);
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	px_p->px_plat_p = (void *)pxu_p;
2290Sstevel@tonic-gate 
23027Sjchu 	/*
23127Sjchu 	 * Initialize all the interrupt handlers
23227Sjchu 	 */
2331772Sjl139090 	switch (PX_CHIP_TYPE(pxu_p)) {
2341772Sjl139090 	case PX_CHIP_OBERON:
2351772Sjl139090 		px_err_reg_enable(px_p, PX_ERR_UBC);
2361772Sjl139090 		px_err_reg_enable(px_p, PX_ERR_MMU);
2371772Sjl139090 		px_err_reg_enable(px_p, PX_ERR_IMU);
2381772Sjl139090 		px_err_reg_enable(px_p, PX_ERR_TLU_UE);
2391772Sjl139090 		px_err_reg_enable(px_p, PX_ERR_TLU_CE);
2401772Sjl139090 		px_err_reg_enable(px_p, PX_ERR_TLU_OE);
2412044Sjj156685 
2422044Sjj156685 		/*
2432044Sjj156685 		 * Oberon hotplug uses SPARE3 field in ILU Error Log Enable
2442044Sjj156685 		 * register to indicate the status of leaf reset,
2452044Sjj156685 		 * we need to preserve the value of this bit, and keep it in
2462044Sjj156685 		 * px_ilu_log_mask to reflect the state of the bit
2472044Sjj156685 		 */
2482044Sjj156685 		if (CSR_BR(csr_base, ILU_ERROR_LOG_ENABLE, SPARE3))
2492044Sjj156685 			px_ilu_log_mask |= (1ull <<
2502044Sjj156685 			    ILU_ERROR_LOG_ENABLE_SPARE3);
2512044Sjj156685 		else
2522044Sjj156685 			px_ilu_log_mask &= ~(1ull <<
2532044Sjj156685 			    ILU_ERROR_LOG_ENABLE_SPARE3);
2541772Sjl139090 		px_err_reg_enable(px_p, PX_ERR_ILU);
2551772Sjl139090 
2561772Sjl139090 		px_fabric_die_rc_ue |= PCIE_AER_UCE_UC;
2571772Sjl139090 		break;
2581772Sjl139090 
2591772Sjl139090 	case PX_CHIP_FIRE:
2601772Sjl139090 		px_err_reg_enable(px_p, PX_ERR_JBC);
2611772Sjl139090 		px_err_reg_enable(px_p, PX_ERR_MMU);
2621772Sjl139090 		px_err_reg_enable(px_p, PX_ERR_IMU);
2631772Sjl139090 		px_err_reg_enable(px_p, PX_ERR_TLU_UE);
2641772Sjl139090 		px_err_reg_enable(px_p, PX_ERR_TLU_CE);
2651772Sjl139090 		px_err_reg_enable(px_p, PX_ERR_TLU_OE);
2661772Sjl139090 		px_err_reg_enable(px_p, PX_ERR_ILU);
2671772Sjl139090 		px_err_reg_enable(px_p, PX_ERR_LPU_LINK);
2681772Sjl139090 		px_err_reg_enable(px_p, PX_ERR_LPU_PHY);
2691772Sjl139090 		px_err_reg_enable(px_p, PX_ERR_LPU_RX);
2701772Sjl139090 		px_err_reg_enable(px_p, PX_ERR_LPU_TX);
2711772Sjl139090 		px_err_reg_enable(px_p, PX_ERR_LPU_LTSSM);
2721772Sjl139090 		px_err_reg_enable(px_p, PX_ERR_LPU_GIGABLZ);
2731772Sjl139090 		break;
2741772Sjl139090 	default:
2751772Sjl139090 		cmn_err(CE_WARN, "%s%d: PX primary bus Unknown\n",
2761772Sjl139090 		    ddi_driver_name(dip), ddi_get_instance(dip));
2771772Sjl139090 		return (DDI_FAILURE);
2781772Sjl139090 	}
27927Sjchu 
2800Sstevel@tonic-gate 	/* Initilize device handle */
2810Sstevel@tonic-gate 	*dev_hdl = (devhandle_t)csr_base;
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	DBG(DBG_ATTACH, dip, "px_lib_dev_init: dev_hdl 0x%llx\n", *dev_hdl);
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	return (DDI_SUCCESS);
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate int
2890Sstevel@tonic-gate px_lib_dev_fini(dev_info_t *dip)
2900Sstevel@tonic-gate {
2910Sstevel@tonic-gate 	px_t	*px_p = DIP_TO_STATE(dip);
2920Sstevel@tonic-gate 	pxu_t	*pxu_p = (pxu_t *)px_p->px_plat_p;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	DBG(DBG_DETACH, dip, "px_lib_dev_fini: dip 0x%p\n", dip);
2950Sstevel@tonic-gate 
29627Sjchu 	/*
29727Sjchu 	 * Deinitialize all the interrupt handlers
29827Sjchu 	 */
2991772Sjl139090 	switch (PX_CHIP_TYPE(pxu_p)) {
3001772Sjl139090 	case PX_CHIP_OBERON:
3011772Sjl139090 		px_err_reg_disable(px_p, PX_ERR_UBC);
3021772Sjl139090 		px_err_reg_disable(px_p, PX_ERR_MMU);
3031772Sjl139090 		px_err_reg_disable(px_p, PX_ERR_IMU);
3041772Sjl139090 		px_err_reg_disable(px_p, PX_ERR_TLU_UE);
3051772Sjl139090 		px_err_reg_disable(px_p, PX_ERR_TLU_CE);
3061772Sjl139090 		px_err_reg_disable(px_p, PX_ERR_TLU_OE);
3071772Sjl139090 		px_err_reg_disable(px_p, PX_ERR_ILU);
3081772Sjl139090 		break;
3091772Sjl139090 	case PX_CHIP_FIRE:
3101772Sjl139090 		px_err_reg_disable(px_p, PX_ERR_JBC);
3111772Sjl139090 		px_err_reg_disable(px_p, PX_ERR_MMU);
3121772Sjl139090 		px_err_reg_disable(px_p, PX_ERR_IMU);
3131772Sjl139090 		px_err_reg_disable(px_p, PX_ERR_TLU_UE);
3141772Sjl139090 		px_err_reg_disable(px_p, PX_ERR_TLU_CE);
3151772Sjl139090 		px_err_reg_disable(px_p, PX_ERR_TLU_OE);
3161772Sjl139090 		px_err_reg_disable(px_p, PX_ERR_ILU);
3171772Sjl139090 		px_err_reg_disable(px_p, PX_ERR_LPU_LINK);
3181772Sjl139090 		px_err_reg_disable(px_p, PX_ERR_LPU_PHY);
3191772Sjl139090 		px_err_reg_disable(px_p, PX_ERR_LPU_RX);
3201772Sjl139090 		px_err_reg_disable(px_p, PX_ERR_LPU_TX);
3211772Sjl139090 		px_err_reg_disable(px_p, PX_ERR_LPU_LTSSM);
3221772Sjl139090 		px_err_reg_disable(px_p, PX_ERR_LPU_GIGABLZ);
3231772Sjl139090 		break;
3241772Sjl139090 	default:
3251772Sjl139090 		cmn_err(CE_WARN, "%s%d: PX primary bus Unknown\n",
3261772Sjl139090 		    ddi_driver_name(dip), ddi_get_instance(dip));
3271772Sjl139090 		return (DDI_FAILURE);
3281772Sjl139090 	}
32927Sjchu 
3300Sstevel@tonic-gate 	iommu_tsb_free(pxu_p->tsb_cookie);
3310Sstevel@tonic-gate 
33227Sjchu 	px_lib_unmap_regs((pxu_t *)px_p->px_plat_p);
33327Sjchu 	kmem_free(px_p->px_plat_p, sizeof (pxu_t));
3340Sstevel@tonic-gate 	px_p->px_plat_p = NULL;
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	return (DDI_SUCCESS);
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate /*ARGSUSED*/
3400Sstevel@tonic-gate int
3410Sstevel@tonic-gate px_lib_intr_devino_to_sysino(dev_info_t *dip, devino_t devino,
3420Sstevel@tonic-gate     sysino_t *sysino)
3430Sstevel@tonic-gate {
3440Sstevel@tonic-gate 	px_t	*px_p = DIP_TO_STATE(dip);
3450Sstevel@tonic-gate 	pxu_t	*pxu_p = (pxu_t *)px_p->px_plat_p;
3460Sstevel@tonic-gate 	uint64_t	ret;
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_devino_to_sysino: dip 0x%p "
3490Sstevel@tonic-gate 	    "devino 0x%x\n", dip, devino);
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	if ((ret = hvio_intr_devino_to_sysino(DIP_TO_HANDLE(dip),
3520Sstevel@tonic-gate 	    pxu_p, devino, sysino)) != H_EOK) {
3530Sstevel@tonic-gate 		DBG(DBG_LIB_INT, dip,
3540Sstevel@tonic-gate 		    "hvio_intr_devino_to_sysino failed, ret 0x%lx\n", ret);
3550Sstevel@tonic-gate 		return (DDI_FAILURE);
3560Sstevel@tonic-gate 	}
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_devino_to_sysino: sysino 0x%llx\n",
3590Sstevel@tonic-gate 	    *sysino);
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	return (DDI_SUCCESS);
3620Sstevel@tonic-gate }
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate /*ARGSUSED*/
3650Sstevel@tonic-gate int
3660Sstevel@tonic-gate px_lib_intr_getvalid(dev_info_t *dip, sysino_t sysino,
3670Sstevel@tonic-gate     intr_valid_state_t *intr_valid_state)
3680Sstevel@tonic-gate {
3690Sstevel@tonic-gate 	uint64_t	ret;
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_getvalid: dip 0x%p sysino 0x%llx\n",
3720Sstevel@tonic-gate 	    dip, sysino);
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	if ((ret = hvio_intr_getvalid(DIP_TO_HANDLE(dip),
3750Sstevel@tonic-gate 	    sysino, intr_valid_state)) != H_EOK) {
3760Sstevel@tonic-gate 		DBG(DBG_LIB_INT, dip, "hvio_intr_getvalid failed, ret 0x%lx\n",
3770Sstevel@tonic-gate 		    ret);
3780Sstevel@tonic-gate 		return (DDI_FAILURE);
3790Sstevel@tonic-gate 	}
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_getvalid: intr_valid_state 0x%x\n",
3820Sstevel@tonic-gate 	    *intr_valid_state);
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	return (DDI_SUCCESS);
3850Sstevel@tonic-gate }
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate /*ARGSUSED*/
3880Sstevel@tonic-gate int
3890Sstevel@tonic-gate px_lib_intr_setvalid(dev_info_t *dip, sysino_t sysino,
3900Sstevel@tonic-gate     intr_valid_state_t intr_valid_state)
3910Sstevel@tonic-gate {
3920Sstevel@tonic-gate 	uint64_t	ret;
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_setvalid: dip 0x%p sysino 0x%llx "
3950Sstevel@tonic-gate 	    "intr_valid_state 0x%x\n", dip, sysino, intr_valid_state);
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	if ((ret = hvio_intr_setvalid(DIP_TO_HANDLE(dip),
3980Sstevel@tonic-gate 	    sysino, intr_valid_state)) != H_EOK) {
3990Sstevel@tonic-gate 		DBG(DBG_LIB_INT, dip, "hvio_intr_setvalid failed, ret 0x%lx\n",
4000Sstevel@tonic-gate 		    ret);
4010Sstevel@tonic-gate 		return (DDI_FAILURE);
4020Sstevel@tonic-gate 	}
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 	return (DDI_SUCCESS);
4050Sstevel@tonic-gate }
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate /*ARGSUSED*/
4080Sstevel@tonic-gate int
4090Sstevel@tonic-gate px_lib_intr_getstate(dev_info_t *dip, sysino_t sysino,
4100Sstevel@tonic-gate     intr_state_t *intr_state)
4110Sstevel@tonic-gate {
4120Sstevel@tonic-gate 	uint64_t	ret;
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_getstate: dip 0x%p sysino 0x%llx\n",
4150Sstevel@tonic-gate 	    dip, sysino);
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	if ((ret = hvio_intr_getstate(DIP_TO_HANDLE(dip),
4180Sstevel@tonic-gate 	    sysino, intr_state)) != H_EOK) {
4190Sstevel@tonic-gate 		DBG(DBG_LIB_INT, dip, "hvio_intr_getstate failed, ret 0x%lx\n",
4200Sstevel@tonic-gate 		    ret);
4210Sstevel@tonic-gate 		return (DDI_FAILURE);
4220Sstevel@tonic-gate 	}
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_getstate: intr_state 0x%x\n",
4250Sstevel@tonic-gate 	    *intr_state);
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	return (DDI_SUCCESS);
4280Sstevel@tonic-gate }
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate /*ARGSUSED*/
4310Sstevel@tonic-gate int
4320Sstevel@tonic-gate px_lib_intr_setstate(dev_info_t *dip, sysino_t sysino,
4330Sstevel@tonic-gate     intr_state_t intr_state)
4340Sstevel@tonic-gate {
4350Sstevel@tonic-gate 	uint64_t	ret;
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_setstate: dip 0x%p sysino 0x%llx "
4380Sstevel@tonic-gate 	    "intr_state 0x%x\n", dip, sysino, intr_state);
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 	if ((ret = hvio_intr_setstate(DIP_TO_HANDLE(dip),
4410Sstevel@tonic-gate 	    sysino, intr_state)) != H_EOK) {
4420Sstevel@tonic-gate 		DBG(DBG_LIB_INT, dip, "hvio_intr_setstate failed, ret 0x%lx\n",
4430Sstevel@tonic-gate 		    ret);
4440Sstevel@tonic-gate 		return (DDI_FAILURE);
4450Sstevel@tonic-gate 	}
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 	return (DDI_SUCCESS);
4480Sstevel@tonic-gate }
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate /*ARGSUSED*/
4510Sstevel@tonic-gate int
4520Sstevel@tonic-gate px_lib_intr_gettarget(dev_info_t *dip, sysino_t sysino, cpuid_t *cpuid)
4530Sstevel@tonic-gate {
4541772Sjl139090 	px_t		*px_p = DIP_TO_STATE(dip);
4551772Sjl139090 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
4560Sstevel@tonic-gate 	uint64_t	ret;
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_gettarget: dip 0x%p sysino 0x%llx\n",
4590Sstevel@tonic-gate 	    dip, sysino);
4600Sstevel@tonic-gate 
4611772Sjl139090 	if ((ret = hvio_intr_gettarget(DIP_TO_HANDLE(dip), pxu_p,
4620Sstevel@tonic-gate 	    sysino, cpuid)) != H_EOK) {
4630Sstevel@tonic-gate 		DBG(DBG_LIB_INT, dip, "hvio_intr_gettarget failed, ret 0x%lx\n",
4640Sstevel@tonic-gate 		    ret);
4650Sstevel@tonic-gate 		return (DDI_FAILURE);
4660Sstevel@tonic-gate 	}
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_gettarget: cpuid 0x%x\n", cpuid);
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	return (DDI_SUCCESS);
4710Sstevel@tonic-gate }
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate /*ARGSUSED*/
4740Sstevel@tonic-gate int
4750Sstevel@tonic-gate px_lib_intr_settarget(dev_info_t *dip, sysino_t sysino, cpuid_t cpuid)
4760Sstevel@tonic-gate {
4771772Sjl139090 	px_t		*px_p = DIP_TO_STATE(dip);
4781772Sjl139090 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
4790Sstevel@tonic-gate 	uint64_t	ret;
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_settarget: dip 0x%p sysino 0x%llx "
4820Sstevel@tonic-gate 	    "cpuid 0x%x\n", dip, sysino, cpuid);
4830Sstevel@tonic-gate 
4841772Sjl139090 	if ((ret = hvio_intr_settarget(DIP_TO_HANDLE(dip), pxu_p,
4850Sstevel@tonic-gate 	    sysino, cpuid)) != H_EOK) {
4860Sstevel@tonic-gate 		DBG(DBG_LIB_INT, dip, "hvio_intr_settarget failed, ret 0x%lx\n",
4870Sstevel@tonic-gate 		    ret);
4880Sstevel@tonic-gate 		return (DDI_FAILURE);
4890Sstevel@tonic-gate 	}
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	return (DDI_SUCCESS);
4920Sstevel@tonic-gate }
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate /*ARGSUSED*/
4950Sstevel@tonic-gate int
4960Sstevel@tonic-gate px_lib_intr_reset(dev_info_t *dip)
4970Sstevel@tonic-gate {
4980Sstevel@tonic-gate 	devino_t	ino;
4990Sstevel@tonic-gate 	sysino_t	sysino;
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_reset: dip 0x%p\n", dip);
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	/* Reset all Interrupts */
5040Sstevel@tonic-gate 	for (ino = 0; ino < INTERRUPT_MAPPING_ENTRIES; ino++) {
5050Sstevel@tonic-gate 		if (px_lib_intr_devino_to_sysino(dip, ino,
5060Sstevel@tonic-gate 		    &sysino) != DDI_SUCCESS)
5070Sstevel@tonic-gate 			return (BF_FATAL);
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 		if (px_lib_intr_setstate(dip, sysino,
5100Sstevel@tonic-gate 		    INTR_IDLE_STATE) != DDI_SUCCESS)
5110Sstevel@tonic-gate 			return (BF_FATAL);
5120Sstevel@tonic-gate 	}
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	return (BF_NONE);
5150Sstevel@tonic-gate }
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate /*ARGSUSED*/
5180Sstevel@tonic-gate int
5190Sstevel@tonic-gate px_lib_iommu_map(dev_info_t *dip, tsbid_t tsbid, pages_t pages,
5201617Sgovinda     io_attributes_t attr, void *addr, size_t pfn_index, int flags)
5210Sstevel@tonic-gate {
5220Sstevel@tonic-gate 	px_t		*px_p = DIP_TO_STATE(dip);
5230Sstevel@tonic-gate 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
5240Sstevel@tonic-gate 	uint64_t	ret;
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_map: dip 0x%p tsbid 0x%llx "
5271617Sgovinda 	    "pages 0x%x attr 0x%x addr 0x%p pfn_index 0x%llx flags 0x%x\n",
5281617Sgovinda 	    dip, tsbid, pages, attr, addr, pfn_index, flags);
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 	if ((ret = hvio_iommu_map(px_p->px_dev_hdl, pxu_p, tsbid, pages,
5311617Sgovinda 	    attr, addr, pfn_index, flags)) != H_EOK) {
5320Sstevel@tonic-gate 		DBG(DBG_LIB_DMA, dip,
5330Sstevel@tonic-gate 		    "px_lib_iommu_map failed, ret 0x%lx\n", ret);
5340Sstevel@tonic-gate 		return (DDI_FAILURE);
5350Sstevel@tonic-gate 	}
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	return (DDI_SUCCESS);
5380Sstevel@tonic-gate }
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate /*ARGSUSED*/
5410Sstevel@tonic-gate int
5420Sstevel@tonic-gate px_lib_iommu_demap(dev_info_t *dip, tsbid_t tsbid, pages_t pages)
5430Sstevel@tonic-gate {
5440Sstevel@tonic-gate 	px_t		*px_p = DIP_TO_STATE(dip);
5450Sstevel@tonic-gate 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
5460Sstevel@tonic-gate 	uint64_t	ret;
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_demap: dip 0x%p tsbid 0x%llx "
5490Sstevel@tonic-gate 	    "pages 0x%x\n", dip, tsbid, pages);
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	if ((ret = hvio_iommu_demap(px_p->px_dev_hdl, pxu_p, tsbid, pages))
5520Sstevel@tonic-gate 	    != H_EOK) {
5530Sstevel@tonic-gate 		DBG(DBG_LIB_DMA, dip,
5540Sstevel@tonic-gate 		    "px_lib_iommu_demap failed, ret 0x%lx\n", ret);
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 		return (DDI_FAILURE);
5570Sstevel@tonic-gate 	}
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 	return (DDI_SUCCESS);
5600Sstevel@tonic-gate }
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate /*ARGSUSED*/
5630Sstevel@tonic-gate int
5641617Sgovinda px_lib_iommu_getmap(dev_info_t *dip, tsbid_t tsbid, io_attributes_t *attr_p,
5651617Sgovinda     r_addr_t *r_addr_p)
5660Sstevel@tonic-gate {
5670Sstevel@tonic-gate 	px_t	*px_p = DIP_TO_STATE(dip);
5680Sstevel@tonic-gate 	pxu_t	*pxu_p = (pxu_t *)px_p->px_plat_p;
5690Sstevel@tonic-gate 	uint64_t	ret;
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_getmap: dip 0x%p tsbid 0x%llx\n",
5720Sstevel@tonic-gate 	    dip, tsbid);
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 	if ((ret = hvio_iommu_getmap(DIP_TO_HANDLE(dip), pxu_p, tsbid,
5751617Sgovinda 	    attr_p, r_addr_p)) != H_EOK) {
5760Sstevel@tonic-gate 		DBG(DBG_LIB_DMA, dip,
5770Sstevel@tonic-gate 		    "hvio_iommu_getmap failed, ret 0x%lx\n", ret);
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 		return ((ret == H_ENOMAP) ? DDI_DMA_NOMAPPING:DDI_FAILURE);
5800Sstevel@tonic-gate 	}
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_getmap: attr 0x%x r_addr 0x%llx\n",
5831617Sgovinda 	    *attr_p, *r_addr_p);
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 	return (DDI_SUCCESS);
5860Sstevel@tonic-gate }
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate /*
5900Sstevel@tonic-gate  * Checks dma attributes against system bypass ranges
5910Sstevel@tonic-gate  * The bypass range is determined by the hardware. Return them so the
5920Sstevel@tonic-gate  * common code can do generic checking against them.
5930Sstevel@tonic-gate  */
5940Sstevel@tonic-gate /*ARGSUSED*/
5950Sstevel@tonic-gate int
5961772Sjl139090 px_lib_dma_bypass_rngchk(dev_info_t *dip, ddi_dma_attr_t *attr_p,
5971772Sjl139090     uint64_t *lo_p, uint64_t *hi_p)
5980Sstevel@tonic-gate {
5991772Sjl139090 	px_t	*px_p = DIP_TO_STATE(dip);
6001772Sjl139090 	pxu_t	*pxu_p = (pxu_t *)px_p->px_plat_p;
6011772Sjl139090 
6021772Sjl139090 	*lo_p = hvio_get_bypass_base(pxu_p);
6031772Sjl139090 	*hi_p = hvio_get_bypass_end(pxu_p);
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	return (DDI_SUCCESS);
6060Sstevel@tonic-gate }
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate /*ARGSUSED*/
6100Sstevel@tonic-gate int
6111617Sgovinda px_lib_iommu_getbypass(dev_info_t *dip, r_addr_t ra, io_attributes_t attr,
6121617Sgovinda     io_addr_t *io_addr_p)
6130Sstevel@tonic-gate {
6140Sstevel@tonic-gate 	uint64_t	ret;
6151772Sjl139090 	px_t	*px_p = DIP_TO_STATE(dip);
6161772Sjl139090 	pxu_t	*pxu_p = (pxu_t *)px_p->px_plat_p;
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_getbypass: dip 0x%p ra 0x%llx "
6191617Sgovinda 	    "attr 0x%x\n", dip, ra, attr);
6200Sstevel@tonic-gate 
6211772Sjl139090 	if ((ret = hvio_iommu_getbypass(DIP_TO_HANDLE(dip), pxu_p, ra,
6221772Sjl139090 	    attr, io_addr_p)) != H_EOK) {
6230Sstevel@tonic-gate 		DBG(DBG_LIB_DMA, dip,
6240Sstevel@tonic-gate 		    "hvio_iommu_getbypass failed, ret 0x%lx\n", ret);
6250Sstevel@tonic-gate 		return (DDI_FAILURE);
6260Sstevel@tonic-gate 	}
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_getbypass: io_addr 0x%llx\n",
6290Sstevel@tonic-gate 	    *io_addr_p);
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 	return (DDI_SUCCESS);
6320Sstevel@tonic-gate }
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate /*
6350Sstevel@tonic-gate  * bus dma sync entry point.
6360Sstevel@tonic-gate  */
6370Sstevel@tonic-gate /*ARGSUSED*/
6380Sstevel@tonic-gate int
6390Sstevel@tonic-gate px_lib_dma_sync(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle,
6401617Sgovinda     off_t off, size_t len, uint_t cache_flags)
6410Sstevel@tonic-gate {
6420Sstevel@tonic-gate 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
6431772Sjl139090 	px_t	*px_p = DIP_TO_STATE(dip);
6441772Sjl139090 	pxu_t	*pxu_p = (pxu_t *)px_p->px_plat_p;
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 	DBG(DBG_LIB_DMA, dip, "px_lib_dma_sync: dip 0x%p rdip 0x%p "
6470Sstevel@tonic-gate 	    "handle 0x%llx off 0x%x len 0x%x flags 0x%x\n",
6480Sstevel@tonic-gate 	    dip, rdip, handle, off, len, cache_flags);
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 	/*
6511772Sjl139090 	 * No flush needed for Oberon
6521772Sjl139090 	 */
6531772Sjl139090 	if (PX_CHIP_TYPE(pxu_p) == PX_CHIP_OBERON)
6541772Sjl139090 		return (DDI_SUCCESS);
6551772Sjl139090 
6561772Sjl139090 	/*
6570Sstevel@tonic-gate 	 * jbus_stst_order is found only in certain cpu modules.
6580Sstevel@tonic-gate 	 * Just return success if not present.
6590Sstevel@tonic-gate 	 */
6600Sstevel@tonic-gate 	if (&jbus_stst_order == NULL)
6610Sstevel@tonic-gate 		return (DDI_SUCCESS);
6620Sstevel@tonic-gate 
663909Segillett 	if (!(mp->dmai_flags & PX_DMAI_FLAGS_INUSE)) {
66427Sjchu 		cmn_err(CE_WARN, "%s%d: Unbound dma handle %p.",
66527Sjchu 		    ddi_driver_name(rdip), ddi_get_instance(rdip), (void *)mp);
66627Sjchu 
6670Sstevel@tonic-gate 		return (DDI_FAILURE);
6680Sstevel@tonic-gate 	}
6690Sstevel@tonic-gate 
670909Segillett 	if (mp->dmai_flags & PX_DMAI_FLAGS_NOSYNC)
6710Sstevel@tonic-gate 		return (DDI_SUCCESS);
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	/*
6740Sstevel@tonic-gate 	 * No flush needed when sending data from memory to device.
6750Sstevel@tonic-gate 	 * Nothing to do to "sync" memory to what device would already see.
6760Sstevel@tonic-gate 	 */
6770Sstevel@tonic-gate 	if (!(mp->dmai_rflags & DDI_DMA_READ) ||
6780Sstevel@tonic-gate 	    ((cache_flags & PX_DMA_SYNC_DDI_FLAGS) == DDI_DMA_SYNC_FORDEV))
6790Sstevel@tonic-gate 		return (DDI_SUCCESS);
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	/*
6820Sstevel@tonic-gate 	 * Perform necessary cpu workaround to ensure jbus ordering.
6830Sstevel@tonic-gate 	 * CPU's internal "invalidate FIFOs" are flushed.
6840Sstevel@tonic-gate 	 */
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate #if !defined(lint)
6870Sstevel@tonic-gate 	kpreempt_disable();
6880Sstevel@tonic-gate #endif
6890Sstevel@tonic-gate 	jbus_stst_order();
6900Sstevel@tonic-gate #if !defined(lint)
6910Sstevel@tonic-gate 	kpreempt_enable();
6920Sstevel@tonic-gate #endif
6930Sstevel@tonic-gate 	return (DDI_SUCCESS);
6940Sstevel@tonic-gate }
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate /*
6970Sstevel@tonic-gate  * MSIQ Functions:
6980Sstevel@tonic-gate  */
6990Sstevel@tonic-gate /*ARGSUSED*/
7000Sstevel@tonic-gate int
7010Sstevel@tonic-gate px_lib_msiq_init(dev_info_t *dip)
7020Sstevel@tonic-gate {
7030Sstevel@tonic-gate 	px_t		*px_p = DIP_TO_STATE(dip);
7040Sstevel@tonic-gate 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
7050Sstevel@tonic-gate 	px_msiq_state_t	*msiq_state_p = &px_p->px_ib_p->ib_msiq_state;
7060Sstevel@tonic-gate 	caddr_t		msiq_addr;
7070Sstevel@tonic-gate 	px_dvma_addr_t	pg_index;
7080Sstevel@tonic-gate 	size_t		size;
7090Sstevel@tonic-gate 	int		ret;
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_init: dip 0x%p\n", dip);
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	/*
7140Sstevel@tonic-gate 	 * Map the EQ memory into the Fire MMU (has to be 512KB aligned)
7150Sstevel@tonic-gate 	 * and then initialize the base address register.
7160Sstevel@tonic-gate 	 *
7170Sstevel@tonic-gate 	 * Allocate entries from Fire IOMMU so that the resulting address
7180Sstevel@tonic-gate 	 * is properly aligned.  Calculate the index of the first allocated
7190Sstevel@tonic-gate 	 * entry.  Note: The size of the mapping is assumed to be a multiple
7200Sstevel@tonic-gate 	 * of the page size.
7210Sstevel@tonic-gate 	 */
7220Sstevel@tonic-gate 	msiq_addr = (caddr_t)(((uint64_t)msiq_state_p->msiq_buf_p +
7230Sstevel@tonic-gate 	    (MMU_PAGE_SIZE - 1)) >> MMU_PAGE_SHIFT << MMU_PAGE_SHIFT);
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 	size = msiq_state_p->msiq_cnt *
7260Sstevel@tonic-gate 	    msiq_state_p->msiq_rec_cnt * sizeof (msiq_rec_t);
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 	pxu_p->msiq_mapped_p = vmem_xalloc(px_p->px_mmu_p->mmu_dvma_map,
7290Sstevel@tonic-gate 	    size, (512 * 1024), 0, 0, NULL, NULL, VM_NOSLEEP | VM_BESTFIT);
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 	if (pxu_p->msiq_mapped_p == NULL)
7320Sstevel@tonic-gate 		return (DDI_FAILURE);
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 	pg_index = MMU_PAGE_INDEX(px_p->px_mmu_p,
7350Sstevel@tonic-gate 	    MMU_BTOP((ulong_t)pxu_p->msiq_mapped_p));
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 	if ((ret = px_lib_iommu_map(px_p->px_dip, PCI_TSBID(0, pg_index),
7380Sstevel@tonic-gate 	    MMU_BTOP(size), PCI_MAP_ATTR_WRITE, (void *)msiq_addr, 0,
7390Sstevel@tonic-gate 	    MMU_MAP_BUF)) != DDI_SUCCESS) {
7400Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip,
7410Sstevel@tonic-gate 		    "hvio_msiq_init failed, ret 0x%lx\n", ret);
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 		(void) px_lib_msiq_fini(dip);
7440Sstevel@tonic-gate 		return (DDI_FAILURE);
7450Sstevel@tonic-gate 	}
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 	(void) hvio_msiq_init(DIP_TO_HANDLE(dip), pxu_p);
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 	return (DDI_SUCCESS);
7500Sstevel@tonic-gate }
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate /*ARGSUSED*/
7530Sstevel@tonic-gate int
7540Sstevel@tonic-gate px_lib_msiq_fini(dev_info_t *dip)
7550Sstevel@tonic-gate {
7560Sstevel@tonic-gate 	px_t		*px_p = DIP_TO_STATE(dip);
7570Sstevel@tonic-gate 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
7580Sstevel@tonic-gate 	px_msiq_state_t	*msiq_state_p = &px_p->px_ib_p->ib_msiq_state;
7590Sstevel@tonic-gate 	px_dvma_addr_t	pg_index;
7600Sstevel@tonic-gate 	size_t		size;
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_fini: dip 0x%p\n", dip);
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	/*
7650Sstevel@tonic-gate 	 * Unmap and free the EQ memory that had been mapped
7660Sstevel@tonic-gate 	 * into the Fire IOMMU.
7670Sstevel@tonic-gate 	 */
7680Sstevel@tonic-gate 	size = msiq_state_p->msiq_cnt *
7690Sstevel@tonic-gate 	    msiq_state_p->msiq_rec_cnt * sizeof (msiq_rec_t);
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	pg_index = MMU_PAGE_INDEX(px_p->px_mmu_p,
7720Sstevel@tonic-gate 	    MMU_BTOP((ulong_t)pxu_p->msiq_mapped_p));
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 	(void) px_lib_iommu_demap(px_p->px_dip,
7750Sstevel@tonic-gate 	    PCI_TSBID(0, pg_index), MMU_BTOP(size));
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 	/* Free the entries from the Fire MMU */
7780Sstevel@tonic-gate 	vmem_xfree(px_p->px_mmu_p->mmu_dvma_map,
7790Sstevel@tonic-gate 	    (void *)pxu_p->msiq_mapped_p, size);
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	return (DDI_SUCCESS);
7820Sstevel@tonic-gate }
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate /*ARGSUSED*/
7850Sstevel@tonic-gate int
7860Sstevel@tonic-gate px_lib_msiq_info(dev_info_t *dip, msiqid_t msiq_id, r_addr_t *ra_p,
7870Sstevel@tonic-gate     uint_t *msiq_rec_cnt_p)
7880Sstevel@tonic-gate {
7890Sstevel@tonic-gate 	px_t		*px_p = DIP_TO_STATE(dip);
7900Sstevel@tonic-gate 	px_msiq_state_t	*msiq_state_p = &px_p->px_ib_p->ib_msiq_state;
7910Sstevel@tonic-gate 	uint64_t	*msiq_addr;
7920Sstevel@tonic-gate 	size_t		msiq_size;
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_msiq_info: dip 0x%p msiq_id 0x%x\n",
7950Sstevel@tonic-gate 	    dip, msiq_id);
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 	msiq_addr = (uint64_t *)(((uint64_t)msiq_state_p->msiq_buf_p +
7980Sstevel@tonic-gate 	    (MMU_PAGE_SIZE - 1)) >> MMU_PAGE_SHIFT << MMU_PAGE_SHIFT);
7990Sstevel@tonic-gate 	msiq_size = msiq_state_p->msiq_rec_cnt * sizeof (msiq_rec_t);
8000Sstevel@tonic-gate 	ra_p = (r_addr_t *)((caddr_t)msiq_addr + (msiq_id * msiq_size));
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 	*msiq_rec_cnt_p = msiq_state_p->msiq_rec_cnt;
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_msiq_info: ra_p 0x%p msiq_rec_cnt 0x%x\n",
8050Sstevel@tonic-gate 	    ra_p, *msiq_rec_cnt_p);
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 	return (DDI_SUCCESS);
8080Sstevel@tonic-gate }
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate /*ARGSUSED*/
8110Sstevel@tonic-gate int
8120Sstevel@tonic-gate px_lib_msiq_getvalid(dev_info_t *dip, msiqid_t msiq_id,
8130Sstevel@tonic-gate     pci_msiq_valid_state_t *msiq_valid_state)
8140Sstevel@tonic-gate {
8150Sstevel@tonic-gate 	uint64_t	ret;
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_getvalid: dip 0x%p msiq_id 0x%x\n",
8180Sstevel@tonic-gate 	    dip, msiq_id);
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 	if ((ret = hvio_msiq_getvalid(DIP_TO_HANDLE(dip),
8210Sstevel@tonic-gate 	    msiq_id, msiq_valid_state)) != H_EOK) {
8220Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip,
8230Sstevel@tonic-gate 		    "hvio_msiq_getvalid failed, ret 0x%lx\n", ret);
8240Sstevel@tonic-gate 		return (DDI_FAILURE);
8250Sstevel@tonic-gate 	}
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_getvalid: msiq_valid_state 0x%x\n",
8280Sstevel@tonic-gate 	    *msiq_valid_state);
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 	return (DDI_SUCCESS);
8310Sstevel@tonic-gate }
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate /*ARGSUSED*/
8340Sstevel@tonic-gate int
8350Sstevel@tonic-gate px_lib_msiq_setvalid(dev_info_t *dip, msiqid_t msiq_id,
8360Sstevel@tonic-gate     pci_msiq_valid_state_t msiq_valid_state)
8370Sstevel@tonic-gate {
8380Sstevel@tonic-gate 	uint64_t	ret;
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_setvalid: dip 0x%p msiq_id 0x%x "
8410Sstevel@tonic-gate 	    "msiq_valid_state 0x%x\n", dip, msiq_id, msiq_valid_state);
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	if ((ret = hvio_msiq_setvalid(DIP_TO_HANDLE(dip),
8440Sstevel@tonic-gate 	    msiq_id, msiq_valid_state)) != H_EOK) {
8450Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip,
8460Sstevel@tonic-gate 		    "hvio_msiq_setvalid failed, ret 0x%lx\n", ret);
8470Sstevel@tonic-gate 		return (DDI_FAILURE);
8480Sstevel@tonic-gate 	}
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 	return (DDI_SUCCESS);
8510Sstevel@tonic-gate }
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate /*ARGSUSED*/
8540Sstevel@tonic-gate int
8550Sstevel@tonic-gate px_lib_msiq_getstate(dev_info_t *dip, msiqid_t msiq_id,
8560Sstevel@tonic-gate     pci_msiq_state_t *msiq_state)
8570Sstevel@tonic-gate {
8580Sstevel@tonic-gate 	uint64_t	ret;
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_getstate: dip 0x%p msiq_id 0x%x\n",
8610Sstevel@tonic-gate 	    dip, msiq_id);
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 	if ((ret = hvio_msiq_getstate(DIP_TO_HANDLE(dip),
8640Sstevel@tonic-gate 	    msiq_id, msiq_state)) != H_EOK) {
8650Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip,
8660Sstevel@tonic-gate 		    "hvio_msiq_getstate failed, ret 0x%lx\n", ret);
8670Sstevel@tonic-gate 		return (DDI_FAILURE);
8680Sstevel@tonic-gate 	}
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_getstate: msiq_state 0x%x\n",
8710Sstevel@tonic-gate 	    *msiq_state);
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 	return (DDI_SUCCESS);
8740Sstevel@tonic-gate }
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate /*ARGSUSED*/
8770Sstevel@tonic-gate int
8780Sstevel@tonic-gate px_lib_msiq_setstate(dev_info_t *dip, msiqid_t msiq_id,
8790Sstevel@tonic-gate     pci_msiq_state_t msiq_state)
8800Sstevel@tonic-gate {
8810Sstevel@tonic-gate 	uint64_t	ret;
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_setstate: dip 0x%p msiq_id 0x%x "
8840Sstevel@tonic-gate 	    "msiq_state 0x%x\n", dip, msiq_id, msiq_state);
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 	if ((ret = hvio_msiq_setstate(DIP_TO_HANDLE(dip),
8870Sstevel@tonic-gate 	    msiq_id, msiq_state)) != H_EOK) {
8880Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip,
8890Sstevel@tonic-gate 		    "hvio_msiq_setstate failed, ret 0x%lx\n", ret);
8900Sstevel@tonic-gate 		return (DDI_FAILURE);
8910Sstevel@tonic-gate 	}
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate 	return (DDI_SUCCESS);
8940Sstevel@tonic-gate }
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate /*ARGSUSED*/
8970Sstevel@tonic-gate int
8980Sstevel@tonic-gate px_lib_msiq_gethead(dev_info_t *dip, msiqid_t msiq_id,
8990Sstevel@tonic-gate     msiqhead_t *msiq_head)
9000Sstevel@tonic-gate {
9010Sstevel@tonic-gate 	uint64_t	ret;
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_gethead: dip 0x%p msiq_id 0x%x\n",
9040Sstevel@tonic-gate 	    dip, msiq_id);
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 	if ((ret = hvio_msiq_gethead(DIP_TO_HANDLE(dip),
9070Sstevel@tonic-gate 	    msiq_id, msiq_head)) != H_EOK) {
9080Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip,
9090Sstevel@tonic-gate 		    "hvio_msiq_gethead failed, ret 0x%lx\n", ret);
9100Sstevel@tonic-gate 		return (DDI_FAILURE);
9110Sstevel@tonic-gate 	}
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_gethead: msiq_head 0x%x\n",
9140Sstevel@tonic-gate 	    *msiq_head);
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 	return (DDI_SUCCESS);
9170Sstevel@tonic-gate }
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate /*ARGSUSED*/
9200Sstevel@tonic-gate int
9210Sstevel@tonic-gate px_lib_msiq_sethead(dev_info_t *dip, msiqid_t msiq_id,
9220Sstevel@tonic-gate     msiqhead_t msiq_head)
9230Sstevel@tonic-gate {
9240Sstevel@tonic-gate 	uint64_t	ret;
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_sethead: dip 0x%p msiq_id 0x%x "
9270Sstevel@tonic-gate 	    "msiq_head 0x%x\n", dip, msiq_id, msiq_head);
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 	if ((ret = hvio_msiq_sethead(DIP_TO_HANDLE(dip),
9300Sstevel@tonic-gate 	    msiq_id, msiq_head)) != H_EOK) {
9310Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip,
9320Sstevel@tonic-gate 		    "hvio_msiq_sethead failed, ret 0x%lx\n", ret);
9330Sstevel@tonic-gate 		return (DDI_FAILURE);
9340Sstevel@tonic-gate 	}
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate 	return (DDI_SUCCESS);
9370Sstevel@tonic-gate }
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate /*ARGSUSED*/
9400Sstevel@tonic-gate int
9410Sstevel@tonic-gate px_lib_msiq_gettail(dev_info_t *dip, msiqid_t msiq_id,
9420Sstevel@tonic-gate     msiqtail_t *msiq_tail)
9430Sstevel@tonic-gate {
9440Sstevel@tonic-gate 	uint64_t	ret;
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_gettail: dip 0x%p msiq_id 0x%x\n",
9470Sstevel@tonic-gate 	    dip, msiq_id);
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate 	if ((ret = hvio_msiq_gettail(DIP_TO_HANDLE(dip),
9500Sstevel@tonic-gate 	    msiq_id, msiq_tail)) != H_EOK) {
9510Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip,
9520Sstevel@tonic-gate 		    "hvio_msiq_gettail failed, ret 0x%lx\n", ret);
9530Sstevel@tonic-gate 		return (DDI_FAILURE);
9540Sstevel@tonic-gate 	}
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_gettail: msiq_tail 0x%x\n",
9570Sstevel@tonic-gate 	    *msiq_tail);
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate 	return (DDI_SUCCESS);
9600Sstevel@tonic-gate }
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate /*ARGSUSED*/
9630Sstevel@tonic-gate void
9640Sstevel@tonic-gate px_lib_get_msiq_rec(dev_info_t *dip, px_msiq_t *msiq_p, msiq_rec_t *msiq_rec_p)
9650Sstevel@tonic-gate {
9660Sstevel@tonic-gate 	eq_rec_t	*eq_rec_p = (eq_rec_t *)msiq_p->msiq_curr;
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_get_msiq_rec: dip 0x%p eq_rec_p 0x%p\n",
9690Sstevel@tonic-gate 	    dip, eq_rec_p);
9700Sstevel@tonic-gate 
971287Smg140465 	if (!eq_rec_p->eq_rec_fmt_type) {
972287Smg140465 		/* Set msiq_rec_type to zero */
973287Smg140465 		msiq_rec_p->msiq_rec_type = 0;
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 		return;
9760Sstevel@tonic-gate 	}
9770Sstevel@tonic-gate 
9780Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_get_msiq_rec: EQ RECORD, "
9790Sstevel@tonic-gate 	    "eq_rec_rid 0x%llx eq_rec_fmt_type 0x%llx "
9800Sstevel@tonic-gate 	    "eq_rec_len 0x%llx eq_rec_addr0 0x%llx "
9810Sstevel@tonic-gate 	    "eq_rec_addr1 0x%llx eq_rec_data0 0x%llx "
9820Sstevel@tonic-gate 	    "eq_rec_data1 0x%llx\n", eq_rec_p->eq_rec_rid,
9830Sstevel@tonic-gate 	    eq_rec_p->eq_rec_fmt_type, eq_rec_p->eq_rec_len,
9840Sstevel@tonic-gate 	    eq_rec_p->eq_rec_addr0, eq_rec_p->eq_rec_addr1,
9850Sstevel@tonic-gate 	    eq_rec_p->eq_rec_data0, eq_rec_p->eq_rec_data1);
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 	/*
9880Sstevel@tonic-gate 	 * Only upper 4 bits of eq_rec_fmt_type is used
9890Sstevel@tonic-gate 	 * to identify the EQ record type.
9900Sstevel@tonic-gate 	 */
9910Sstevel@tonic-gate 	switch (eq_rec_p->eq_rec_fmt_type >> 3) {
9920Sstevel@tonic-gate 	case EQ_REC_MSI32:
9930Sstevel@tonic-gate 		msiq_rec_p->msiq_rec_type = MSI32_REC;
9940Sstevel@tonic-gate 
995225Sess 		msiq_rec_p->msiq_rec_data.msi.msi_data =
996225Sess 		    eq_rec_p->eq_rec_data0;
9970Sstevel@tonic-gate 		break;
9980Sstevel@tonic-gate 	case EQ_REC_MSI64:
9990Sstevel@tonic-gate 		msiq_rec_p->msiq_rec_type = MSI64_REC;
10000Sstevel@tonic-gate 
1001225Sess 		msiq_rec_p->msiq_rec_data.msi.msi_data =
1002225Sess 		    eq_rec_p->eq_rec_data0;
10030Sstevel@tonic-gate 		break;
10040Sstevel@tonic-gate 	case EQ_REC_MSG:
10050Sstevel@tonic-gate 		msiq_rec_p->msiq_rec_type = MSG_REC;
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 		msiq_rec_p->msiq_rec_data.msg.msg_route =
10080Sstevel@tonic-gate 		    eq_rec_p->eq_rec_fmt_type & 7;
10090Sstevel@tonic-gate 		msiq_rec_p->msiq_rec_data.msg.msg_targ = eq_rec_p->eq_rec_rid;
10100Sstevel@tonic-gate 		msiq_rec_p->msiq_rec_data.msg.msg_code = eq_rec_p->eq_rec_data0;
10110Sstevel@tonic-gate 		break;
10120Sstevel@tonic-gate 	default:
10130Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: px_lib_get_msiq_rec: "
1014671Skrishnae 		    "0x%x is an unknown EQ record type",
10150Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip),
1016671Skrishnae 		    (int)eq_rec_p->eq_rec_fmt_type);
10170Sstevel@tonic-gate 		break;
10180Sstevel@tonic-gate 	}
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 	msiq_rec_p->msiq_rec_rid = eq_rec_p->eq_rec_rid;
10210Sstevel@tonic-gate 	msiq_rec_p->msiq_rec_msi_addr = ((eq_rec_p->eq_rec_addr1 << 16) |
10220Sstevel@tonic-gate 	    (eq_rec_p->eq_rec_addr0 << 2));
10230Sstevel@tonic-gate 
1024287Smg140465 	/* Zero out eq_rec_fmt_type field */
1025287Smg140465 	eq_rec_p->eq_rec_fmt_type = 0;
10260Sstevel@tonic-gate }
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate /*
10290Sstevel@tonic-gate  * MSI Functions:
10300Sstevel@tonic-gate  */
10310Sstevel@tonic-gate /*ARGSUSED*/
10320Sstevel@tonic-gate int
10330Sstevel@tonic-gate px_lib_msi_init(dev_info_t *dip)
10340Sstevel@tonic-gate {
10350Sstevel@tonic-gate 	px_t		*px_p = DIP_TO_STATE(dip);
10360Sstevel@tonic-gate 	px_msi_state_t	*msi_state_p = &px_p->px_ib_p->ib_msi_state;
10370Sstevel@tonic-gate 	uint64_t	ret;
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_init: dip 0x%p\n", dip);
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate 	if ((ret = hvio_msi_init(DIP_TO_HANDLE(dip),
10420Sstevel@tonic-gate 	    msi_state_p->msi_addr32, msi_state_p->msi_addr64)) != H_EOK) {
10430Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip, "px_lib_msi_init failed, ret 0x%lx\n",
10440Sstevel@tonic-gate 		    ret);
10450Sstevel@tonic-gate 		return (DDI_FAILURE);
10460Sstevel@tonic-gate 	}
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 	return (DDI_SUCCESS);
10490Sstevel@tonic-gate }
10500Sstevel@tonic-gate 
10510Sstevel@tonic-gate /*ARGSUSED*/
10520Sstevel@tonic-gate int
10530Sstevel@tonic-gate px_lib_msi_getmsiq(dev_info_t *dip, msinum_t msi_num,
10540Sstevel@tonic-gate     msiqid_t *msiq_id)
10550Sstevel@tonic-gate {
10560Sstevel@tonic-gate 	uint64_t	ret;
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getmsiq: dip 0x%p msi_num 0x%x\n",
10590Sstevel@tonic-gate 	    dip, msi_num);
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate 	if ((ret = hvio_msi_getmsiq(DIP_TO_HANDLE(dip),
10620Sstevel@tonic-gate 	    msi_num, msiq_id)) != H_EOK) {
10630Sstevel@tonic-gate 		DBG(DBG_LIB_MSI, dip,
10640Sstevel@tonic-gate 		    "hvio_msi_getmsiq failed, ret 0x%lx\n", ret);
10650Sstevel@tonic-gate 		return (DDI_FAILURE);
10660Sstevel@tonic-gate 	}
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getmsiq: msiq_id 0x%x\n",
10690Sstevel@tonic-gate 	    *msiq_id);
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate 	return (DDI_SUCCESS);
10720Sstevel@tonic-gate }
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate /*ARGSUSED*/
10750Sstevel@tonic-gate int
10760Sstevel@tonic-gate px_lib_msi_setmsiq(dev_info_t *dip, msinum_t msi_num,
10770Sstevel@tonic-gate     msiqid_t msiq_id, msi_type_t msitype)
10780Sstevel@tonic-gate {
10790Sstevel@tonic-gate 	uint64_t	ret;
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_setmsiq: dip 0x%p msi_num 0x%x "
10820Sstevel@tonic-gate 	    "msq_id 0x%x\n", dip, msi_num, msiq_id);
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate 	if ((ret = hvio_msi_setmsiq(DIP_TO_HANDLE(dip),
10850Sstevel@tonic-gate 	    msi_num, msiq_id)) != H_EOK) {
10860Sstevel@tonic-gate 		DBG(DBG_LIB_MSI, dip,
10870Sstevel@tonic-gate 		    "hvio_msi_setmsiq failed, ret 0x%lx\n", ret);
10880Sstevel@tonic-gate 		return (DDI_FAILURE);
10890Sstevel@tonic-gate 	}
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 	return (DDI_SUCCESS);
10920Sstevel@tonic-gate }
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate /*ARGSUSED*/
10950Sstevel@tonic-gate int
10960Sstevel@tonic-gate px_lib_msi_getvalid(dev_info_t *dip, msinum_t msi_num,
10970Sstevel@tonic-gate     pci_msi_valid_state_t *msi_valid_state)
10980Sstevel@tonic-gate {
10990Sstevel@tonic-gate 	uint64_t	ret;
11000Sstevel@tonic-gate 
11010Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getvalid: dip 0x%p msi_num 0x%x\n",
11020Sstevel@tonic-gate 	    dip, msi_num);
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate 	if ((ret = hvio_msi_getvalid(DIP_TO_HANDLE(dip),
11050Sstevel@tonic-gate 	    msi_num, msi_valid_state)) != H_EOK) {
11060Sstevel@tonic-gate 		DBG(DBG_LIB_MSI, dip,
11070Sstevel@tonic-gate 		    "hvio_msi_getvalid failed, ret 0x%lx\n", ret);
11080Sstevel@tonic-gate 		return (DDI_FAILURE);
11090Sstevel@tonic-gate 	}
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getvalid: msiq_id 0x%x\n",
11120Sstevel@tonic-gate 	    *msi_valid_state);
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate 	return (DDI_SUCCESS);
11150Sstevel@tonic-gate }
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate /*ARGSUSED*/
11180Sstevel@tonic-gate int
11190Sstevel@tonic-gate px_lib_msi_setvalid(dev_info_t *dip, msinum_t msi_num,
11200Sstevel@tonic-gate     pci_msi_valid_state_t msi_valid_state)
11210Sstevel@tonic-gate {
11220Sstevel@tonic-gate 	uint64_t	ret;
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_setvalid: dip 0x%p msi_num 0x%x "
11250Sstevel@tonic-gate 	    "msi_valid_state 0x%x\n", dip, msi_num, msi_valid_state);
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 	if ((ret = hvio_msi_setvalid(DIP_TO_HANDLE(dip),
11280Sstevel@tonic-gate 	    msi_num, msi_valid_state)) != H_EOK) {
11290Sstevel@tonic-gate 		DBG(DBG_LIB_MSI, dip,
11300Sstevel@tonic-gate 		    "hvio_msi_setvalid failed, ret 0x%lx\n", ret);
11310Sstevel@tonic-gate 		return (DDI_FAILURE);
11320Sstevel@tonic-gate 	}
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 	return (DDI_SUCCESS);
11350Sstevel@tonic-gate }
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate /*ARGSUSED*/
11380Sstevel@tonic-gate int
11390Sstevel@tonic-gate px_lib_msi_getstate(dev_info_t *dip, msinum_t msi_num,
11400Sstevel@tonic-gate     pci_msi_state_t *msi_state)
11410Sstevel@tonic-gate {
11420Sstevel@tonic-gate 	uint64_t	ret;
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getstate: dip 0x%p msi_num 0x%x\n",
11450Sstevel@tonic-gate 	    dip, msi_num);
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate 	if ((ret = hvio_msi_getstate(DIP_TO_HANDLE(dip),
11480Sstevel@tonic-gate 	    msi_num, msi_state)) != H_EOK) {
11490Sstevel@tonic-gate 		DBG(DBG_LIB_MSI, dip,
11500Sstevel@tonic-gate 		    "hvio_msi_getstate failed, ret 0x%lx\n", ret);
11510Sstevel@tonic-gate 		return (DDI_FAILURE);
11520Sstevel@tonic-gate 	}
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getstate: msi_state 0x%x\n",
11550Sstevel@tonic-gate 	    *msi_state);
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate 	return (DDI_SUCCESS);
11580Sstevel@tonic-gate }
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate /*ARGSUSED*/
11610Sstevel@tonic-gate int
11620Sstevel@tonic-gate px_lib_msi_setstate(dev_info_t *dip, msinum_t msi_num,
11630Sstevel@tonic-gate     pci_msi_state_t msi_state)
11640Sstevel@tonic-gate {
11650Sstevel@tonic-gate 	uint64_t	ret;
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_setstate: dip 0x%p msi_num 0x%x "
11680Sstevel@tonic-gate 	    "msi_state 0x%x\n", dip, msi_num, msi_state);
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate 	if ((ret = hvio_msi_setstate(DIP_TO_HANDLE(dip),
11710Sstevel@tonic-gate 	    msi_num, msi_state)) != H_EOK) {
11720Sstevel@tonic-gate 		DBG(DBG_LIB_MSI, dip,
11730Sstevel@tonic-gate 		    "hvio_msi_setstate failed, ret 0x%lx\n", ret);
11740Sstevel@tonic-gate 		return (DDI_FAILURE);
11750Sstevel@tonic-gate 	}
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate 	return (DDI_SUCCESS);
11780Sstevel@tonic-gate }
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate /*
11810Sstevel@tonic-gate  * MSG Functions:
11820Sstevel@tonic-gate  */
11830Sstevel@tonic-gate /*ARGSUSED*/
11840Sstevel@tonic-gate int
11850Sstevel@tonic-gate px_lib_msg_getmsiq(dev_info_t *dip, pcie_msg_type_t msg_type,
11860Sstevel@tonic-gate     msiqid_t *msiq_id)
11870Sstevel@tonic-gate {
11880Sstevel@tonic-gate 	uint64_t	ret;
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 	DBG(DBG_LIB_MSG, dip, "px_lib_msg_getmsiq: dip 0x%p msg_type 0x%x\n",
11910Sstevel@tonic-gate 	    dip, msg_type);
11920Sstevel@tonic-gate 
11930Sstevel@tonic-gate 	if ((ret = hvio_msg_getmsiq(DIP_TO_HANDLE(dip),
11940Sstevel@tonic-gate 	    msg_type, msiq_id)) != H_EOK) {
11950Sstevel@tonic-gate 		DBG(DBG_LIB_MSG, dip,
11960Sstevel@tonic-gate 		    "hvio_msg_getmsiq failed, ret 0x%lx\n", ret);
11970Sstevel@tonic-gate 		return (DDI_FAILURE);
11980Sstevel@tonic-gate 	}
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msg_getmsiq: msiq_id 0x%x\n",
12010Sstevel@tonic-gate 	    *msiq_id);
12020Sstevel@tonic-gate 
12030Sstevel@tonic-gate 	return (DDI_SUCCESS);
12040Sstevel@tonic-gate }
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate /*ARGSUSED*/
12070Sstevel@tonic-gate int
12080Sstevel@tonic-gate px_lib_msg_setmsiq(dev_info_t *dip, pcie_msg_type_t msg_type,
12090Sstevel@tonic-gate     msiqid_t msiq_id)
12100Sstevel@tonic-gate {
12110Sstevel@tonic-gate 	uint64_t	ret;
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 	DBG(DBG_LIB_MSG, dip, "px_lib_msi_setstate: dip 0x%p msg_type 0x%x "
12140Sstevel@tonic-gate 	    "msiq_id 0x%x\n", dip, msg_type, msiq_id);
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 	if ((ret = hvio_msg_setmsiq(DIP_TO_HANDLE(dip),
12170Sstevel@tonic-gate 	    msg_type, msiq_id)) != H_EOK) {
12180Sstevel@tonic-gate 		DBG(DBG_LIB_MSG, dip,
12190Sstevel@tonic-gate 		    "hvio_msg_setmsiq failed, ret 0x%lx\n", ret);
12200Sstevel@tonic-gate 		return (DDI_FAILURE);
12210Sstevel@tonic-gate 	}
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 	return (DDI_SUCCESS);
12240Sstevel@tonic-gate }
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate /*ARGSUSED*/
12270Sstevel@tonic-gate int
12280Sstevel@tonic-gate px_lib_msg_getvalid(dev_info_t *dip, pcie_msg_type_t msg_type,
12290Sstevel@tonic-gate     pcie_msg_valid_state_t *msg_valid_state)
12300Sstevel@tonic-gate {
12310Sstevel@tonic-gate 	uint64_t	ret;
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 	DBG(DBG_LIB_MSG, dip, "px_lib_msg_getvalid: dip 0x%p msg_type 0x%x\n",
12340Sstevel@tonic-gate 	    dip, msg_type);
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 	if ((ret = hvio_msg_getvalid(DIP_TO_HANDLE(dip), msg_type,
12370Sstevel@tonic-gate 	    msg_valid_state)) != H_EOK) {
12380Sstevel@tonic-gate 		DBG(DBG_LIB_MSG, dip,
12390Sstevel@tonic-gate 		    "hvio_msg_getvalid failed, ret 0x%lx\n", ret);
12400Sstevel@tonic-gate 		return (DDI_FAILURE);
12410Sstevel@tonic-gate 	}
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msg_getvalid: msg_valid_state 0x%x\n",
12440Sstevel@tonic-gate 	    *msg_valid_state);
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 	return (DDI_SUCCESS);
12470Sstevel@tonic-gate }
12480Sstevel@tonic-gate 
12490Sstevel@tonic-gate /*ARGSUSED*/
12500Sstevel@tonic-gate int
12510Sstevel@tonic-gate px_lib_msg_setvalid(dev_info_t *dip, pcie_msg_type_t msg_type,
12520Sstevel@tonic-gate     pcie_msg_valid_state_t msg_valid_state)
12530Sstevel@tonic-gate {
12540Sstevel@tonic-gate 	uint64_t	ret;
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate 	DBG(DBG_LIB_MSG, dip, "px_lib_msg_setvalid: dip 0x%p msg_type 0x%x "
12570Sstevel@tonic-gate 	    "msg_valid_state 0x%x\n", dip, msg_type, msg_valid_state);
12580Sstevel@tonic-gate 
12590Sstevel@tonic-gate 	if ((ret = hvio_msg_setvalid(DIP_TO_HANDLE(dip), msg_type,
12600Sstevel@tonic-gate 	    msg_valid_state)) != H_EOK) {
12610Sstevel@tonic-gate 		DBG(DBG_LIB_MSG, dip,
12620Sstevel@tonic-gate 		    "hvio_msg_setvalid failed, ret 0x%lx\n", ret);
12630Sstevel@tonic-gate 		return (DDI_FAILURE);
12640Sstevel@tonic-gate 	}
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate 	return (DDI_SUCCESS);
12670Sstevel@tonic-gate }
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate /*
12700Sstevel@tonic-gate  * Suspend/Resume Functions:
12710Sstevel@tonic-gate  * Currently unsupported by hypervisor
12720Sstevel@tonic-gate  */
12730Sstevel@tonic-gate int
12740Sstevel@tonic-gate px_lib_suspend(dev_info_t *dip)
12750Sstevel@tonic-gate {
12760Sstevel@tonic-gate 	px_t		*px_p = DIP_TO_STATE(dip);
12770Sstevel@tonic-gate 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
12781648Sjchu 	px_cb_t		*cb_p = PX2CB(px_p);
12790Sstevel@tonic-gate 	devhandle_t	dev_hdl, xbus_dev_hdl;
12801648Sjchu 	uint64_t	ret = H_EOK;
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 	DBG(DBG_DETACH, dip, "px_lib_suspend: dip 0x%p\n", dip);
12830Sstevel@tonic-gate 
128427Sjchu 	dev_hdl = (devhandle_t)pxu_p->px_address[PX_REG_CSR];
128527Sjchu 	xbus_dev_hdl = (devhandle_t)pxu_p->px_address[PX_REG_XBC];
12860Sstevel@tonic-gate 
12871648Sjchu 	if ((ret = hvio_suspend(dev_hdl, pxu_p)) != H_EOK)
12881648Sjchu 		goto fail;
12891648Sjchu 
12901648Sjchu 	if (--cb_p->attachcnt == 0) {
12911648Sjchu 		ret = hvio_cb_suspend(xbus_dev_hdl, pxu_p);
12921648Sjchu 		if (ret != H_EOK)
12931648Sjchu 			cb_p->attachcnt++;
12940Sstevel@tonic-gate 	}
12950Sstevel@tonic-gate 
12961648Sjchu fail:
12970Sstevel@tonic-gate 	return ((ret != H_EOK) ? DDI_FAILURE: DDI_SUCCESS);
12980Sstevel@tonic-gate }
12990Sstevel@tonic-gate 
13000Sstevel@tonic-gate void
13010Sstevel@tonic-gate px_lib_resume(dev_info_t *dip)
13020Sstevel@tonic-gate {
13030Sstevel@tonic-gate 	px_t		*px_p = DIP_TO_STATE(dip);
13040Sstevel@tonic-gate 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
13051648Sjchu 	px_cb_t		*cb_p = PX2CB(px_p);
13060Sstevel@tonic-gate 	devhandle_t	dev_hdl, xbus_dev_hdl;
13070Sstevel@tonic-gate 	devino_t	pec_ino = px_p->px_inos[PX_INTR_PEC];
13080Sstevel@tonic-gate 	devino_t	xbc_ino = px_p->px_inos[PX_INTR_XBC];
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate 	DBG(DBG_ATTACH, dip, "px_lib_resume: dip 0x%p\n", dip);
13110Sstevel@tonic-gate 
131227Sjchu 	dev_hdl = (devhandle_t)pxu_p->px_address[PX_REG_CSR];
131327Sjchu 	xbus_dev_hdl = (devhandle_t)pxu_p->px_address[PX_REG_XBC];
13140Sstevel@tonic-gate 
13151648Sjchu 	if (++cb_p->attachcnt == 1)
13160Sstevel@tonic-gate 		hvio_cb_resume(dev_hdl, xbus_dev_hdl, xbc_ino, pxu_p);
13170Sstevel@tonic-gate 
13181648Sjchu 	hvio_resume(dev_hdl, pec_ino, pxu_p);
13190Sstevel@tonic-gate }
13200Sstevel@tonic-gate 
13211772Sjl139090 /*
13221772Sjl139090  * Generate a unique Oberon UBC ID based on the Logicial System Board and
13231772Sjl139090  * the IO Channel from the portid property field.
13241772Sjl139090  */
13251772Sjl139090 static uint64_t
13261772Sjl139090 oberon_get_ubc_id(dev_info_t *dip)
13271772Sjl139090 {
13281772Sjl139090 	px_t	*px_p = DIP_TO_STATE(dip);
13291772Sjl139090 	pxu_t	*pxu_p = (pxu_t *)px_p->px_plat_p;
13301772Sjl139090 	uint64_t	ubc_id;
13311772Sjl139090 
13321772Sjl139090 	/*
13331772Sjl139090 	 * Generate a unique 6 bit UBC ID using the 2 IO_Channel#[1:0] bits and
13341772Sjl139090 	 * the 4 LSB_ID[3:0] bits from the Oberon's portid property.
13351772Sjl139090 	 */
13361772Sjl139090 	ubc_id = (((pxu_p->portid >> OBERON_PORT_ID_IOC) &
13371772Sjl139090 	    OBERON_PORT_ID_IOC_MASK) | (((pxu_p->portid >>
13381772Sjl139090 	    OBERON_PORT_ID_LSB) & OBERON_PORT_ID_LSB_MASK)
13391772Sjl139090 	    << OBERON_UBC_ID_LSB));
13401772Sjl139090 
13411772Sjl139090 	return (ubc_id);
13421772Sjl139090 }
13431772Sjl139090 
13441772Sjl139090 /*
13451772Sjl139090  * Oberon does not have a UBC scratch register, so alloc an array of scratch
13461772Sjl139090  * registers when needed and use a unique UBC ID as an index. This code
13471772Sjl139090  * can be simplified if we use a pre-allocated array. They are currently
13481772Sjl139090  * being dynamically allocated because it's only needed by the Oberon.
13491772Sjl139090  */
13501772Sjl139090 static void
13511772Sjl139090 oberon_set_cb(dev_info_t *dip, uint64_t val)
13521772Sjl139090 {
13531772Sjl139090 	uint64_t	ubc_id;
13541772Sjl139090 
13551772Sjl139090 	if (px_oberon_ubc_scratch_regs == NULL)
13561772Sjl139090 		px_oberon_ubc_scratch_regs =
13571772Sjl139090 		    (uint64_t *)kmem_zalloc(sizeof (uint64_t)*
13581772Sjl139090 		    OBERON_UBC_ID_MAX, KM_SLEEP);
13591772Sjl139090 
13601772Sjl139090 	ubc_id = oberon_get_ubc_id(dip);
13611772Sjl139090 
13621772Sjl139090 	px_oberon_ubc_scratch_regs[ubc_id] = val;
13631772Sjl139090 
13641772Sjl139090 	/*
13651772Sjl139090 	 * Check if any scratch registers are still in use. If all scratch
13661772Sjl139090 	 * registers are currently set to zero, then deallocate the scratch
13671772Sjl139090 	 * register array.
13681772Sjl139090 	 */
13691772Sjl139090 	for (ubc_id = 0; ubc_id < OBERON_UBC_ID_MAX; ubc_id++) {
13701772Sjl139090 		if (px_oberon_ubc_scratch_regs[ubc_id] != NULL)
13711772Sjl139090 			return;
13721772Sjl139090 	}
13731772Sjl139090 
13741772Sjl139090 	/*
13751772Sjl139090 	 * All scratch registers are set to zero so deallocate the scratch
13761772Sjl139090 	 * register array and set the pointer to NULL.
13771772Sjl139090 	 */
13781772Sjl139090 	kmem_free(px_oberon_ubc_scratch_regs,
13791772Sjl139090 	    (sizeof (uint64_t)*OBERON_UBC_ID_MAX));
13801772Sjl139090 
13811772Sjl139090 	px_oberon_ubc_scratch_regs = NULL;
13821772Sjl139090 }
13831772Sjl139090 
13841772Sjl139090 /*
13851772Sjl139090  * Oberon does not have a UBC scratch register, so use an allocated array of
13861772Sjl139090  * scratch registers and use the unique UBC ID as an index into that array.
13871772Sjl139090  */
13881772Sjl139090 static uint64_t
13891772Sjl139090 oberon_get_cb(dev_info_t *dip)
13901772Sjl139090 {
13911772Sjl139090 	uint64_t	ubc_id;
13921772Sjl139090 
13931772Sjl139090 	if (px_oberon_ubc_scratch_regs == NULL)
13941772Sjl139090 		return (0);
13951772Sjl139090 
13961772Sjl139090 	ubc_id = oberon_get_ubc_id(dip);
13971772Sjl139090 
13981772Sjl139090 	return (px_oberon_ubc_scratch_regs[ubc_id]);
13991772Sjl139090 }
14001772Sjl139090 
14011772Sjl139090 /*
14021772Sjl139090  * Misc Functions:
14031772Sjl139090  * Currently unsupported by hypervisor
14041772Sjl139090  */
14051772Sjl139090 static uint64_t
14061772Sjl139090 px_get_cb(dev_info_t *dip)
14071772Sjl139090 {
14081772Sjl139090 	px_t	*px_p = DIP_TO_STATE(dip);
14091772Sjl139090 	pxu_t	*pxu_p = (pxu_t *)px_p->px_plat_p;
14101772Sjl139090 
14111772Sjl139090 	/*
14121772Sjl139090 	 * Oberon does not currently have Scratchpad registers.
14131772Sjl139090 	 */
14141772Sjl139090 	if (PX_CHIP_TYPE(pxu_p) == PX_CHIP_OBERON)
14151772Sjl139090 		return (oberon_get_cb(dip));
14161772Sjl139090 
14171772Sjl139090 	return (CSR_XR((caddr_t)pxu_p->px_address[PX_REG_XBC], JBUS_SCRATCH_1));
14181772Sjl139090 }
14191772Sjl139090 
14201772Sjl139090 static void
14211772Sjl139090 px_set_cb(dev_info_t *dip, uint64_t val)
14221772Sjl139090 {
14231772Sjl139090 	px_t	*px_p = DIP_TO_STATE(dip);
14241772Sjl139090 	pxu_t	*pxu_p = (pxu_t *)px_p->px_plat_p;
14251772Sjl139090 
14261772Sjl139090 	/*
14271772Sjl139090 	 * Oberon does not currently have Scratchpad registers.
14281772Sjl139090 	 */
14291772Sjl139090 	if (PX_CHIP_TYPE(pxu_p) == PX_CHIP_OBERON) {
14301772Sjl139090 		oberon_set_cb(dip, val);
14311772Sjl139090 		return;
14321772Sjl139090 	}
14331772Sjl139090 
14341772Sjl139090 	CSR_XS((caddr_t)pxu_p->px_address[PX_REG_XBC], JBUS_SCRATCH_1, val);
14351772Sjl139090 }
14361772Sjl139090 
14370Sstevel@tonic-gate /*ARGSUSED*/
14380Sstevel@tonic-gate int
14390Sstevel@tonic-gate px_lib_map_vconfig(dev_info_t *dip,
14400Sstevel@tonic-gate 	ddi_map_req_t *mp, pci_config_offset_t off,
14410Sstevel@tonic-gate 		pci_regspec_t *rp, caddr_t *addrp)
14420Sstevel@tonic-gate {
14430Sstevel@tonic-gate 	/*
14440Sstevel@tonic-gate 	 * No special config space access services in this layer.
14450Sstevel@tonic-gate 	 */
14460Sstevel@tonic-gate 	return (DDI_FAILURE);
14470Sstevel@tonic-gate }
14480Sstevel@tonic-gate 
1449624Sschwartz void
1450677Sjchu px_lib_map_attr_check(ddi_map_req_t *mp)
1451677Sjchu {
1452677Sjchu 	ddi_acc_hdl_t *hp = mp->map_handlep;
1453677Sjchu 
1454677Sjchu 	/* fire does not accept byte masks from PIO store merge */
1455677Sjchu 	if (hp->ah_acc.devacc_attr_dataorder == DDI_STORECACHING_OK_ACC)
1456677Sjchu 		hp->ah_acc.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
1457677Sjchu }
1458677Sjchu 
1459677Sjchu void
1460624Sschwartz px_lib_clr_errs(px_t *px_p)
146127Sjchu {
1462624Sschwartz 	px_pec_t	*pec_p = px_p->px_pec_p;
146327Sjchu 	dev_info_t	*rpdip = px_p->px_dip;
146427Sjchu 	int		err = PX_OK, ret;
146527Sjchu 	int		acctype = pec_p->pec_safeacc_type;
146627Sjchu 	ddi_fm_error_t	derr;
146727Sjchu 
146827Sjchu 	/* Create the derr */
146927Sjchu 	bzero(&derr, sizeof (ddi_fm_error_t));
147027Sjchu 	derr.fme_version = DDI_FME_VERSION;
147127Sjchu 	derr.fme_ena = fm_ena_generate(0, FM_ENA_FMT1);
147227Sjchu 	derr.fme_flag = acctype;
147327Sjchu 
147427Sjchu 	if (acctype == DDI_FM_ERR_EXPECTED) {
147527Sjchu 		derr.fme_status = DDI_FM_NONFATAL;
147627Sjchu 		ndi_fm_acc_err_set(pec_p->pec_acc_hdl, &derr);
147727Sjchu 	}
147827Sjchu 
14791648Sjchu 	mutex_enter(&px_p->px_fm_mutex);
148027Sjchu 
148127Sjchu 	/* send ereport/handle/clear fire registers */
148227Sjchu 	err = px_err_handle(px_p, &derr, PX_LIB_CALL, B_TRUE);
148327Sjchu 
148427Sjchu 	/* Check all child devices for errors */
148527Sjchu 	ret = ndi_fm_handler_dispatch(rpdip, NULL, &derr);
148627Sjchu 
14871648Sjchu 	mutex_exit(&px_p->px_fm_mutex);
148827Sjchu 
148927Sjchu 	/*
149027Sjchu 	 * PX_FATAL_HW indicates a condition recovered from Fatal-Reset,
149127Sjchu 	 * therefore it does not cause panic.
149227Sjchu 	 */
149327Sjchu 	if ((err & (PX_FATAL_GOS | PX_FATAL_SW)) || (ret == DDI_FM_FATAL))
1494677Sjchu 		PX_FM_PANIC("Fatal System Port Error has occurred\n");
149527Sjchu }
149627Sjchu 
14970Sstevel@tonic-gate #ifdef  DEBUG
14980Sstevel@tonic-gate int	px_peekfault_cnt = 0;
14990Sstevel@tonic-gate int	px_pokefault_cnt = 0;
15000Sstevel@tonic-gate #endif  /* DEBUG */
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate /*ARGSUSED*/
15030Sstevel@tonic-gate static int
15040Sstevel@tonic-gate px_lib_do_poke(dev_info_t *dip, dev_info_t *rdip,
15050Sstevel@tonic-gate     peekpoke_ctlops_t *in_args)
15060Sstevel@tonic-gate {
15070Sstevel@tonic-gate 	px_t *px_p = DIP_TO_STATE(dip);
15080Sstevel@tonic-gate 	px_pec_t *pec_p = px_p->px_pec_p;
15090Sstevel@tonic-gate 	int err = DDI_SUCCESS;
15100Sstevel@tonic-gate 	on_trap_data_t otd;
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate 	mutex_enter(&pec_p->pec_pokefault_mutex);
15130Sstevel@tonic-gate 	pec_p->pec_ontrap_data = &otd;
151427Sjchu 	pec_p->pec_safeacc_type = DDI_FM_ERR_POKE;
15150Sstevel@tonic-gate 
15160Sstevel@tonic-gate 	/* Set up protected environment. */
15170Sstevel@tonic-gate 	if (!on_trap(&otd, OT_DATA_ACCESS)) {
15180Sstevel@tonic-gate 		uintptr_t tramp = otd.ot_trampoline;
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 		otd.ot_trampoline = (uintptr_t)&poke_fault;
15210Sstevel@tonic-gate 		err = do_poke(in_args->size, (void *)in_args->dev_addr,
15220Sstevel@tonic-gate 		    (void *)in_args->host_addr);
15230Sstevel@tonic-gate 		otd.ot_trampoline = tramp;
15240Sstevel@tonic-gate 	} else
15250Sstevel@tonic-gate 		err = DDI_FAILURE;
15260Sstevel@tonic-gate 
1527624Sschwartz 	px_lib_clr_errs(px_p);
152827Sjchu 
15290Sstevel@tonic-gate 	if (otd.ot_trap & OT_DATA_ACCESS)
15300Sstevel@tonic-gate 		err = DDI_FAILURE;
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate 	/* Take down protected environment. */
15330Sstevel@tonic-gate 	no_trap();
15340Sstevel@tonic-gate 
15350Sstevel@tonic-gate 	pec_p->pec_ontrap_data = NULL;
153627Sjchu 	pec_p->pec_safeacc_type = DDI_FM_ERR_UNEXPECTED;
15370Sstevel@tonic-gate 	mutex_exit(&pec_p->pec_pokefault_mutex);
15380Sstevel@tonic-gate 
15390Sstevel@tonic-gate #ifdef  DEBUG
15400Sstevel@tonic-gate 	if (err == DDI_FAILURE)
15410Sstevel@tonic-gate 		px_pokefault_cnt++;
15420Sstevel@tonic-gate #endif
15430Sstevel@tonic-gate 	return (err);
15440Sstevel@tonic-gate }
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate /*ARGSUSED*/
15470Sstevel@tonic-gate static int
15480Sstevel@tonic-gate px_lib_do_caut_put(dev_info_t *dip, dev_info_t *rdip,
15490Sstevel@tonic-gate     peekpoke_ctlops_t *cautacc_ctlops_arg)
15500Sstevel@tonic-gate {
15510Sstevel@tonic-gate 	size_t size = cautacc_ctlops_arg->size;
15520Sstevel@tonic-gate 	uintptr_t dev_addr = cautacc_ctlops_arg->dev_addr;
15530Sstevel@tonic-gate 	uintptr_t host_addr = cautacc_ctlops_arg->host_addr;
15540Sstevel@tonic-gate 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)cautacc_ctlops_arg->handle;
15550Sstevel@tonic-gate 	size_t repcount = cautacc_ctlops_arg->repcount;
15560Sstevel@tonic-gate 	uint_t flags = cautacc_ctlops_arg->flags;
15570Sstevel@tonic-gate 
15580Sstevel@tonic-gate 	px_t *px_p = DIP_TO_STATE(dip);
15590Sstevel@tonic-gate 	px_pec_t *pec_p = px_p->px_pec_p;
15600Sstevel@tonic-gate 	int err = DDI_SUCCESS;
15610Sstevel@tonic-gate 
156227Sjchu 	/*
156327Sjchu 	 * Note that i_ndi_busop_access_enter ends up grabbing the pokefault
156427Sjchu 	 * mutex.
156527Sjchu 	 */
15660Sstevel@tonic-gate 	i_ndi_busop_access_enter(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp);
15670Sstevel@tonic-gate 
156827Sjchu 	pec_p->pec_ontrap_data = (on_trap_data_t *)hp->ahi_err->err_ontrap;
156927Sjchu 	pec_p->pec_safeacc_type = DDI_FM_ERR_EXPECTED;
157027Sjchu 	hp->ahi_err->err_expected = DDI_FM_ERR_EXPECTED;
15710Sstevel@tonic-gate 
15720Sstevel@tonic-gate 	if (!i_ddi_ontrap((ddi_acc_handle_t)hp)) {
15730Sstevel@tonic-gate 		for (; repcount; repcount--) {
15740Sstevel@tonic-gate 			switch (size) {
15750Sstevel@tonic-gate 
15760Sstevel@tonic-gate 			case sizeof (uint8_t):
15770Sstevel@tonic-gate 				i_ddi_put8(hp, (uint8_t *)dev_addr,
15780Sstevel@tonic-gate 				    *(uint8_t *)host_addr);
15790Sstevel@tonic-gate 				break;
15800Sstevel@tonic-gate 
15810Sstevel@tonic-gate 			case sizeof (uint16_t):
15820Sstevel@tonic-gate 				i_ddi_put16(hp, (uint16_t *)dev_addr,
15830Sstevel@tonic-gate 				    *(uint16_t *)host_addr);
15840Sstevel@tonic-gate 				break;
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate 			case sizeof (uint32_t):
15870Sstevel@tonic-gate 				i_ddi_put32(hp, (uint32_t *)dev_addr,
15880Sstevel@tonic-gate 				    *(uint32_t *)host_addr);
15890Sstevel@tonic-gate 				break;
15900Sstevel@tonic-gate 
15910Sstevel@tonic-gate 			case sizeof (uint64_t):
15920Sstevel@tonic-gate 				i_ddi_put64(hp, (uint64_t *)dev_addr,
15930Sstevel@tonic-gate 				    *(uint64_t *)host_addr);
15940Sstevel@tonic-gate 				break;
15950Sstevel@tonic-gate 			}
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate 			host_addr += size;
15980Sstevel@tonic-gate 
15990Sstevel@tonic-gate 			if (flags == DDI_DEV_AUTOINCR)
16000Sstevel@tonic-gate 				dev_addr += size;
16010Sstevel@tonic-gate 
1602624Sschwartz 			px_lib_clr_errs(px_p);
160327Sjchu 
16040Sstevel@tonic-gate 			if (pec_p->pec_ontrap_data->ot_trap & OT_DATA_ACCESS) {
16050Sstevel@tonic-gate 				err = DDI_FAILURE;
16060Sstevel@tonic-gate #ifdef  DEBUG
16070Sstevel@tonic-gate 				px_pokefault_cnt++;
16080Sstevel@tonic-gate #endif
16090Sstevel@tonic-gate 				break;
16100Sstevel@tonic-gate 			}
16110Sstevel@tonic-gate 		}
16120Sstevel@tonic-gate 	}
16130Sstevel@tonic-gate 
16140Sstevel@tonic-gate 	i_ddi_notrap((ddi_acc_handle_t)hp);
16150Sstevel@tonic-gate 	pec_p->pec_ontrap_data = NULL;
161627Sjchu 	pec_p->pec_safeacc_type = DDI_FM_ERR_UNEXPECTED;
16170Sstevel@tonic-gate 	i_ndi_busop_access_exit(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp);
16180Sstevel@tonic-gate 	hp->ahi_err->err_expected = DDI_FM_ERR_UNEXPECTED;
16190Sstevel@tonic-gate 
16200Sstevel@tonic-gate 	return (err);
16210Sstevel@tonic-gate }
16220Sstevel@tonic-gate 
16230Sstevel@tonic-gate 
16240Sstevel@tonic-gate int
16250Sstevel@tonic-gate px_lib_ctlops_poke(dev_info_t *dip, dev_info_t *rdip,
16260Sstevel@tonic-gate     peekpoke_ctlops_t *in_args)
16270Sstevel@tonic-gate {
16280Sstevel@tonic-gate 	return (in_args->handle ? px_lib_do_caut_put(dip, rdip, in_args) :
16290Sstevel@tonic-gate 	    px_lib_do_poke(dip, rdip, in_args));
16300Sstevel@tonic-gate }
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate 
16330Sstevel@tonic-gate /*ARGSUSED*/
16340Sstevel@tonic-gate static int
16350Sstevel@tonic-gate px_lib_do_peek(dev_info_t *dip, peekpoke_ctlops_t *in_args)
16360Sstevel@tonic-gate {
163727Sjchu 	px_t *px_p = DIP_TO_STATE(dip);
163827Sjchu 	px_pec_t *pec_p = px_p->px_pec_p;
16390Sstevel@tonic-gate 	int err = DDI_SUCCESS;
16400Sstevel@tonic-gate 	on_trap_data_t otd;
16410Sstevel@tonic-gate 
164227Sjchu 	mutex_enter(&pec_p->pec_pokefault_mutex);
164327Sjchu 	pec_p->pec_safeacc_type = DDI_FM_ERR_PEEK;
164427Sjchu 
16450Sstevel@tonic-gate 	if (!on_trap(&otd, OT_DATA_ACCESS)) {
16460Sstevel@tonic-gate 		uintptr_t tramp = otd.ot_trampoline;
16470Sstevel@tonic-gate 
16480Sstevel@tonic-gate 		otd.ot_trampoline = (uintptr_t)&peek_fault;
16490Sstevel@tonic-gate 		err = do_peek(in_args->size, (void *)in_args->dev_addr,
16500Sstevel@tonic-gate 		    (void *)in_args->host_addr);
16510Sstevel@tonic-gate 		otd.ot_trampoline = tramp;
16520Sstevel@tonic-gate 	} else
16530Sstevel@tonic-gate 		err = DDI_FAILURE;
16540Sstevel@tonic-gate 
16550Sstevel@tonic-gate 	no_trap();
165627Sjchu 	pec_p->pec_safeacc_type = DDI_FM_ERR_UNEXPECTED;
165727Sjchu 	mutex_exit(&pec_p->pec_pokefault_mutex);
16580Sstevel@tonic-gate 
16590Sstevel@tonic-gate #ifdef  DEBUG
16600Sstevel@tonic-gate 	if (err == DDI_FAILURE)
16610Sstevel@tonic-gate 		px_peekfault_cnt++;
16620Sstevel@tonic-gate #endif
16630Sstevel@tonic-gate 	return (err);
16640Sstevel@tonic-gate }
16650Sstevel@tonic-gate 
16660Sstevel@tonic-gate 
16670Sstevel@tonic-gate static int
16680Sstevel@tonic-gate px_lib_do_caut_get(dev_info_t *dip, peekpoke_ctlops_t *cautacc_ctlops_arg)
16690Sstevel@tonic-gate {
16700Sstevel@tonic-gate 	size_t size = cautacc_ctlops_arg->size;
16710Sstevel@tonic-gate 	uintptr_t dev_addr = cautacc_ctlops_arg->dev_addr;
16720Sstevel@tonic-gate 	uintptr_t host_addr = cautacc_ctlops_arg->host_addr;
16730Sstevel@tonic-gate 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)cautacc_ctlops_arg->handle;
16740Sstevel@tonic-gate 	size_t repcount = cautacc_ctlops_arg->repcount;
16750Sstevel@tonic-gate 	uint_t flags = cautacc_ctlops_arg->flags;
16760Sstevel@tonic-gate 
16770Sstevel@tonic-gate 	px_t *px_p = DIP_TO_STATE(dip);
16780Sstevel@tonic-gate 	px_pec_t *pec_p = px_p->px_pec_p;
16790Sstevel@tonic-gate 	int err = DDI_SUCCESS;
16800Sstevel@tonic-gate 
168127Sjchu 	/*
168227Sjchu 	 * Note that i_ndi_busop_access_enter ends up grabbing the pokefault
168327Sjchu 	 * mutex.
168427Sjchu 	 */
168527Sjchu 	i_ndi_busop_access_enter(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp);
168627Sjchu 
168727Sjchu 	pec_p->pec_ontrap_data = (on_trap_data_t *)hp->ahi_err->err_ontrap;
168827Sjchu 	pec_p->pec_safeacc_type = DDI_FM_ERR_EXPECTED;
16890Sstevel@tonic-gate 	hp->ahi_err->err_expected = DDI_FM_ERR_EXPECTED;
16900Sstevel@tonic-gate 
16910Sstevel@tonic-gate 	if (repcount == 1) {
16920Sstevel@tonic-gate 		if (!i_ddi_ontrap((ddi_acc_handle_t)hp)) {
16930Sstevel@tonic-gate 			i_ddi_caut_get(size, (void *)dev_addr,
16940Sstevel@tonic-gate 			    (void *)host_addr);
16950Sstevel@tonic-gate 		} else {
16960Sstevel@tonic-gate 			int i;
16970Sstevel@tonic-gate 			uint8_t *ff_addr = (uint8_t *)host_addr;
16980Sstevel@tonic-gate 			for (i = 0; i < size; i++)
16990Sstevel@tonic-gate 				*ff_addr++ = 0xff;
17000Sstevel@tonic-gate 
17010Sstevel@tonic-gate 			err = DDI_FAILURE;
17020Sstevel@tonic-gate #ifdef  DEBUG
17030Sstevel@tonic-gate 			px_peekfault_cnt++;
17040Sstevel@tonic-gate #endif
17050Sstevel@tonic-gate 		}
17060Sstevel@tonic-gate 	} else {
17070Sstevel@tonic-gate 		if (!i_ddi_ontrap((ddi_acc_handle_t)hp)) {
17080Sstevel@tonic-gate 			for (; repcount; repcount--) {
17090Sstevel@tonic-gate 				i_ddi_caut_get(size, (void *)dev_addr,
17100Sstevel@tonic-gate 				    (void *)host_addr);
17110Sstevel@tonic-gate 
17120Sstevel@tonic-gate 				host_addr += size;
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate 				if (flags == DDI_DEV_AUTOINCR)
17150Sstevel@tonic-gate 					dev_addr += size;
17160Sstevel@tonic-gate 			}
17170Sstevel@tonic-gate 		} else {
17180Sstevel@tonic-gate 			err = DDI_FAILURE;
17190Sstevel@tonic-gate #ifdef  DEBUG
17200Sstevel@tonic-gate 			px_peekfault_cnt++;
17210Sstevel@tonic-gate #endif
17220Sstevel@tonic-gate 		}
17230Sstevel@tonic-gate 	}
17240Sstevel@tonic-gate 
17250Sstevel@tonic-gate 	i_ddi_notrap((ddi_acc_handle_t)hp);
17260Sstevel@tonic-gate 	pec_p->pec_ontrap_data = NULL;
172727Sjchu 	pec_p->pec_safeacc_type = DDI_FM_ERR_UNEXPECTED;
17280Sstevel@tonic-gate 	i_ndi_busop_access_exit(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp);
17290Sstevel@tonic-gate 	hp->ahi_err->err_expected = DDI_FM_ERR_UNEXPECTED;
17300Sstevel@tonic-gate 
17310Sstevel@tonic-gate 	return (err);
17320Sstevel@tonic-gate }
17330Sstevel@tonic-gate 
17340Sstevel@tonic-gate /*ARGSUSED*/
17350Sstevel@tonic-gate int
17360Sstevel@tonic-gate px_lib_ctlops_peek(dev_info_t *dip, dev_info_t *rdip,
17370Sstevel@tonic-gate     peekpoke_ctlops_t *in_args, void *result)
17380Sstevel@tonic-gate {
17390Sstevel@tonic-gate 	result = (void *)in_args->host_addr;
17400Sstevel@tonic-gate 	return (in_args->handle ? px_lib_do_caut_get(dip, in_args) :
17410Sstevel@tonic-gate 	    px_lib_do_peek(dip, in_args));
17420Sstevel@tonic-gate }
1743118Sjchu 
17440Sstevel@tonic-gate /*
17450Sstevel@tonic-gate  * implements PPM interface
17460Sstevel@tonic-gate  */
17470Sstevel@tonic-gate int
17480Sstevel@tonic-gate px_lib_pmctl(int cmd, px_t *px_p)
17490Sstevel@tonic-gate {
17500Sstevel@tonic-gate 	ASSERT((cmd & ~PPMREQ_MASK) == PPMREQ);
17510Sstevel@tonic-gate 	switch (cmd) {
17520Sstevel@tonic-gate 	case PPMREQ_PRE_PWR_OFF:
17530Sstevel@tonic-gate 		/*
17540Sstevel@tonic-gate 		 * Currently there is no device power management for
17550Sstevel@tonic-gate 		 * the root complex (fire). When there is we need to make
17560Sstevel@tonic-gate 		 * sure that it is at full power before trying to send the
17570Sstevel@tonic-gate 		 * PME_Turn_Off message.
17580Sstevel@tonic-gate 		 */
17590Sstevel@tonic-gate 		DBG(DBG_PWR, px_p->px_dip,
17600Sstevel@tonic-gate 		    "ioctl: request to send PME_Turn_Off\n");
17610Sstevel@tonic-gate 		return (px_goto_l23ready(px_p));
17620Sstevel@tonic-gate 
17630Sstevel@tonic-gate 	case PPMREQ_PRE_PWR_ON:
1764118Sjchu 		DBG(DBG_PWR, px_p->px_dip, "ioctl: PRE_PWR_ON request\n");
1765118Sjchu 		return (px_pre_pwron_check(px_p));
1766118Sjchu 
17670Sstevel@tonic-gate 	case PPMREQ_POST_PWR_ON:
1768118Sjchu 		DBG(DBG_PWR, px_p->px_dip, "ioctl: POST_PWR_ON request\n");
1769118Sjchu 		return (px_goto_l0(px_p));
17700Sstevel@tonic-gate 
17710Sstevel@tonic-gate 	default:
17720Sstevel@tonic-gate 		return (DDI_FAILURE);
17730Sstevel@tonic-gate 	}
17740Sstevel@tonic-gate }
17750Sstevel@tonic-gate 
17760Sstevel@tonic-gate /*
17770Sstevel@tonic-gate  * sends PME_Turn_Off message to put the link in L2/L3 ready state.
17780Sstevel@tonic-gate  * called by px_ioctl.
17790Sstevel@tonic-gate  * returns DDI_SUCCESS or DDI_FAILURE
17800Sstevel@tonic-gate  * 1. Wait for link to be in L1 state (link status reg)
17810Sstevel@tonic-gate  * 2. write to PME_Turn_off reg to boradcast
17820Sstevel@tonic-gate  * 3. set timeout
17830Sstevel@tonic-gate  * 4. If timeout, return failure.
17840Sstevel@tonic-gate  * 5. If PM_TO_Ack, wait till link is in L2/L3 ready
17850Sstevel@tonic-gate  */
17860Sstevel@tonic-gate static int
17870Sstevel@tonic-gate px_goto_l23ready(px_t *px_p)
17880Sstevel@tonic-gate {
17890Sstevel@tonic-gate 	pcie_pwr_t	*pwr_p;
179027Sjchu 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
179127Sjchu 	caddr_t	csr_base = (caddr_t)pxu_p->px_address[PX_REG_CSR];
17920Sstevel@tonic-gate 	int		ret = DDI_SUCCESS;
17930Sstevel@tonic-gate 	clock_t		end, timeleft;
1794118Sjchu 	int		mutex_held = 1;
17950Sstevel@tonic-gate 
17960Sstevel@tonic-gate 	/* If no PM info, return failure */
17970Sstevel@tonic-gate 	if (!PCIE_PMINFO(px_p->px_dip) ||
17980Sstevel@tonic-gate 	    !(pwr_p = PCIE_NEXUS_PMINFO(px_p->px_dip)))
17990Sstevel@tonic-gate 		return (DDI_FAILURE);
18000Sstevel@tonic-gate 
18010Sstevel@tonic-gate 	mutex_enter(&pwr_p->pwr_lock);
1802118Sjchu 	mutex_enter(&px_p->px_l23ready_lock);
18030Sstevel@tonic-gate 	/* Clear the PME_To_ACK receieved flag */
1804118Sjchu 	px_p->px_pm_flags &= ~PX_PMETOACK_RECVD;
1805287Smg140465 	/*
1806287Smg140465 	 * When P25 is the downstream device, after receiving
1807287Smg140465 	 * PME_To_ACK, fire will go to Detect state, which causes
1808287Smg140465 	 * the link down event. Inform FMA that this is expected.
1809287Smg140465 	 * In case of all other cards complaint with the pci express
1810287Smg140465 	 * spec, this will happen when the power is re-applied. FMA
1811287Smg140465 	 * code will clear this flag after one instance of LDN. Since
1812287Smg140465 	 * there will not be a LDN event for the spec compliant cards,
1813287Smg140465 	 * we need to clear the flag after receiving PME_To_ACK.
1814287Smg140465 	 */
1815287Smg140465 	px_p->px_pm_flags |= PX_LDN_EXPECTED;
18160Sstevel@tonic-gate 	if (px_send_pme_turnoff(csr_base) != DDI_SUCCESS) {
18170Sstevel@tonic-gate 		ret = DDI_FAILURE;
18180Sstevel@tonic-gate 		goto l23ready_done;
18190Sstevel@tonic-gate 	}
1820118Sjchu 	px_p->px_pm_flags |= PX_PME_TURNOFF_PENDING;
18210Sstevel@tonic-gate 
18220Sstevel@tonic-gate 	end = ddi_get_lbolt() + drv_usectohz(px_pme_to_ack_timeout);
1823118Sjchu 	while (!(px_p->px_pm_flags & PX_PMETOACK_RECVD)) {
1824118Sjchu 		timeleft = cv_timedwait(&px_p->px_l23ready_cv,
1825118Sjchu 		    &px_p->px_l23ready_lock, end);
18260Sstevel@tonic-gate 		/*
18270Sstevel@tonic-gate 		 * if cv_timedwait returns -1, it is either
18280Sstevel@tonic-gate 		 * 1) timed out or
18290Sstevel@tonic-gate 		 * 2) there was a pre-mature wakeup but by the time
18300Sstevel@tonic-gate 		 * cv_timedwait is called again end < lbolt i.e.
18310Sstevel@tonic-gate 		 * end is in the past.
18320Sstevel@tonic-gate 		 * 3) By the time we make first cv_timedwait call,
18330Sstevel@tonic-gate 		 * end < lbolt is true.
18340Sstevel@tonic-gate 		 */
18350Sstevel@tonic-gate 		if (timeleft == -1)
18360Sstevel@tonic-gate 			break;
18370Sstevel@tonic-gate 	}
1838118Sjchu 	if (!(px_p->px_pm_flags & PX_PMETOACK_RECVD)) {
18390Sstevel@tonic-gate 		/*
18400Sstevel@tonic-gate 		 * Either timedout or interrupt didn't get a
18410Sstevel@tonic-gate 		 * chance to grab the mutex and set the flag.
18420Sstevel@tonic-gate 		 * release the mutex and delay for sometime.
18430Sstevel@tonic-gate 		 * This will 1) give a chance for interrupt to
18440Sstevel@tonic-gate 		 * set the flag 2) creates a delay between two
18450Sstevel@tonic-gate 		 * consequetive requests.
18460Sstevel@tonic-gate 		 */
1847118Sjchu 		mutex_exit(&px_p->px_l23ready_lock);
18481147Sjchu 		delay(drv_usectohz(50 * PX_MSEC_TO_USEC));
1849118Sjchu 		mutex_held = 0;
1850118Sjchu 		if (!(px_p->px_pm_flags & PX_PMETOACK_RECVD)) {
18510Sstevel@tonic-gate 			ret = DDI_FAILURE;
18520Sstevel@tonic-gate 			DBG(DBG_PWR, px_p->px_dip, " Timed out while waiting"
18530Sstevel@tonic-gate 			    " for PME_TO_ACK\n");
18540Sstevel@tonic-gate 		}
18550Sstevel@tonic-gate 	}
1856287Smg140465 	px_p->px_pm_flags &=
1857287Smg140465 	    ~(PX_PME_TURNOFF_PENDING | PX_PMETOACK_RECVD | PX_LDN_EXPECTED);
18580Sstevel@tonic-gate 
18590Sstevel@tonic-gate l23ready_done:
1860118Sjchu 	if (mutex_held)
1861118Sjchu 		mutex_exit(&px_p->px_l23ready_lock);
1862118Sjchu 	/*
1863118Sjchu 	 * Wait till link is in L1 idle, if sending PME_Turn_Off
1864118Sjchu 	 * was succesful.
1865118Sjchu 	 */
1866118Sjchu 	if (ret == DDI_SUCCESS) {
1867118Sjchu 		if (px_link_wait4l1idle(csr_base) != DDI_SUCCESS) {
1868118Sjchu 			DBG(DBG_PWR, px_p->px_dip, " Link is not at L1"
1869287Smg140465 			    " even though we received PME_To_ACK.\n");
1870287Smg140465 			/*
1871287Smg140465 			 * Workaround for hardware bug with P25.
1872287Smg140465 			 * Due to a hardware bug with P25, link state
1873287Smg140465 			 * will be Detect state rather than L1 after
1874287Smg140465 			 * link is transitioned to L23Ready state. Since
1875287Smg140465 			 * we don't know whether link is L23ready state
1876287Smg140465 			 * without Fire's state being L1_idle, we delay
1877287Smg140465 			 * here just to make sure that we wait till link
1878287Smg140465 			 * is transitioned to L23Ready state.
1879287Smg140465 			 */
18801147Sjchu 			delay(drv_usectohz(100 * PX_MSEC_TO_USEC));
1881287Smg140465 		}
1882287Smg140465 		pwr_p->pwr_link_lvl = PM_LEVEL_L3;
1883118Sjchu 
1884118Sjchu 	}
18850Sstevel@tonic-gate 	mutex_exit(&pwr_p->pwr_lock);
18860Sstevel@tonic-gate 	return (ret);
18870Sstevel@tonic-gate }
18880Sstevel@tonic-gate 
1889118Sjchu /*
1890118Sjchu  * Message interrupt handler intended to be shared for both
1891118Sjchu  * PME and PME_TO_ACK msg handling, currently only handles
1892118Sjchu  * PME_To_ACK message.
1893118Sjchu  */
1894118Sjchu uint_t
1895118Sjchu px_pmeq_intr(caddr_t arg)
1896118Sjchu {
1897118Sjchu 	px_t	*px_p = (px_t *)arg;
1898118Sjchu 
1899287Smg140465 	DBG(DBG_PWR, px_p->px_dip, " PME_To_ACK received \n");
1900118Sjchu 	mutex_enter(&px_p->px_l23ready_lock);
1901118Sjchu 	cv_broadcast(&px_p->px_l23ready_cv);
1902118Sjchu 	if (px_p->px_pm_flags & PX_PME_TURNOFF_PENDING) {
1903118Sjchu 		px_p->px_pm_flags |= PX_PMETOACK_RECVD;
1904118Sjchu 	} else {
1905118Sjchu 		/*
1906118Sjchu 		 * This maybe the second ack received. If so then,
1907118Sjchu 		 * we should be receiving it during wait4L1 stage.
1908118Sjchu 		 */
1909118Sjchu 		px_p->px_pmetoack_ignored++;
1910118Sjchu 	}
1911118Sjchu 	mutex_exit(&px_p->px_l23ready_lock);
1912118Sjchu 	return (DDI_INTR_CLAIMED);
1913118Sjchu }
1914118Sjchu 
1915118Sjchu static int
1916118Sjchu px_pre_pwron_check(px_t *px_p)
1917118Sjchu {
1918118Sjchu 	pcie_pwr_t	*pwr_p;
1919118Sjchu 
1920118Sjchu 	/* If no PM info, return failure */
1921118Sjchu 	if (!PCIE_PMINFO(px_p->px_dip) ||
1922118Sjchu 	    !(pwr_p = PCIE_NEXUS_PMINFO(px_p->px_dip)))
1923118Sjchu 		return (DDI_FAILURE);
1924118Sjchu 
1925287Smg140465 	/*
1926287Smg140465 	 * For the spec compliant downstream cards link down
1927287Smg140465 	 * is expected when the device is powered on.
1928287Smg140465 	 */
1929287Smg140465 	px_p->px_pm_flags |= PX_LDN_EXPECTED;
1930118Sjchu 	return (pwr_p->pwr_link_lvl == PM_LEVEL_L3 ? DDI_SUCCESS : DDI_FAILURE);
1931118Sjchu }
1932118Sjchu 
1933118Sjchu static int
1934118Sjchu px_goto_l0(px_t *px_p)
1935118Sjchu {
1936118Sjchu 	pcie_pwr_t	*pwr_p;
1937118Sjchu 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
1938118Sjchu 	caddr_t csr_base = (caddr_t)pxu_p->px_address[PX_REG_CSR];
1939118Sjchu 	int		ret = DDI_SUCCESS;
19401147Sjchu 	uint64_t	time_spent = 0;
1941118Sjchu 
1942118Sjchu 	/* If no PM info, return failure */
1943118Sjchu 	if (!PCIE_PMINFO(px_p->px_dip) ||
1944118Sjchu 	    !(pwr_p = PCIE_NEXUS_PMINFO(px_p->px_dip)))
1945118Sjchu 		return (DDI_FAILURE);
1946118Sjchu 
1947118Sjchu 	mutex_enter(&pwr_p->pwr_lock);
1948287Smg140465 	/*
19491147Sjchu 	 * The following link retrain activity will cause LDN and LUP event.
19501147Sjchu 	 * Receiving LDN prior to receiving LUP is expected, not an error in
19511147Sjchu 	 * this case.  Receiving LUP indicates link is fully up to support
19521147Sjchu 	 * powering up down stream device, and of course any further LDN and
19531147Sjchu 	 * LUP outside this context will be error.
1954287Smg140465 	 */
19551147Sjchu 	px_p->px_lup_pending = 1;
1956118Sjchu 	if (px_link_retrain(csr_base) != DDI_SUCCESS) {
1957118Sjchu 		ret = DDI_FAILURE;
1958118Sjchu 		goto l0_done;
1959118Sjchu 	}
1960118Sjchu 
19611147Sjchu 	/* LUP event takes the order of 15ms amount of time to occur */
19621147Sjchu 	for (; px_p->px_lup_pending && (time_spent < px_lup_poll_to);
19631147Sjchu 	    time_spent += px_lup_poll_interval)
19641147Sjchu 		drv_usecwait(px_lup_poll_interval);
19651147Sjchu 	if (px_p->px_lup_pending)
19661147Sjchu 		ret = DDI_FAILURE;
1967118Sjchu l0_done:
1968287Smg140465 	px_enable_detect_quiet(csr_base);
1969118Sjchu 	if (ret == DDI_SUCCESS)
1970287Smg140465 		pwr_p->pwr_link_lvl = PM_LEVEL_L0;
1971118Sjchu 	mutex_exit(&pwr_p->pwr_lock);
1972118Sjchu 	return (ret);
1973118Sjchu }
1974118Sjchu 
19750Sstevel@tonic-gate /*
19760Sstevel@tonic-gate  * Extract the drivers binding name to identify which chip we're binding to.
19770Sstevel@tonic-gate  * Whenever a new bus bridge is created, the driver alias entry should be
19780Sstevel@tonic-gate  * added here to identify the device if needed.  If a device isn't added,
19790Sstevel@tonic-gate  * the identity defaults to PX_CHIP_UNIDENTIFIED.
19800Sstevel@tonic-gate  */
19810Sstevel@tonic-gate static uint32_t
19820Sstevel@tonic-gate px_identity_chip(px_t *px_p)
19830Sstevel@tonic-gate {
19840Sstevel@tonic-gate 	dev_info_t	*dip = px_p->px_dip;
19850Sstevel@tonic-gate 	char		*name = ddi_binding_name(dip);
19860Sstevel@tonic-gate 	uint32_t	revision = 0;
19870Sstevel@tonic-gate 
19880Sstevel@tonic-gate 	revision = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
19890Sstevel@tonic-gate 	    "module-revision#", 0);
19900Sstevel@tonic-gate 
19910Sstevel@tonic-gate 	/* Check for Fire driver binding name */
1992226Set142600 	if ((strcmp(name, "pci108e,80f0") == 0) ||
1993226Set142600 	    (strcmp(name, "pciex108e,80f0") == 0)) {
19940Sstevel@tonic-gate 		DBG(DBG_ATTACH, dip, "px_identity_chip: %s%d: "
19950Sstevel@tonic-gate 		    "name %s module-revision %d\n", ddi_driver_name(dip),
19960Sstevel@tonic-gate 		    ddi_get_instance(dip), name, revision);
19970Sstevel@tonic-gate 
19980Sstevel@tonic-gate 		return (PX_CHIP_ID(PX_CHIP_FIRE, revision, 0x00));
19990Sstevel@tonic-gate 	}
20000Sstevel@tonic-gate 
20011772Sjl139090 	/* Check for Oberon driver binding name */
20021772Sjl139090 	if (strcmp(name, "pciex108e,80f8") == 0) {
20031772Sjl139090 		DBG(DBG_ATTACH, dip, "px_identity_chip: %s%d: "
20041772Sjl139090 		    "name %s module-revision %d\n", ddi_driver_name(dip),
20051772Sjl139090 		    ddi_get_instance(dip), name, revision);
20061772Sjl139090 
20071772Sjl139090 		return (PX_CHIP_ID(PX_CHIP_OBERON, revision, 0x00));
20081772Sjl139090 	}
20091772Sjl139090 
20100Sstevel@tonic-gate 	DBG(DBG_ATTACH, dip, "%s%d: Unknown PCI Express Host bridge %s %x\n",
20110Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), name, revision);
20120Sstevel@tonic-gate 
20130Sstevel@tonic-gate 	return (PX_CHIP_UNIDENTIFIED);
20140Sstevel@tonic-gate }
201527Sjchu 
201627Sjchu int
201727Sjchu px_err_add_intr(px_fault_t *px_fault_p)
201827Sjchu {
201927Sjchu 	dev_info_t	*dip = px_fault_p->px_fh_dip;
202027Sjchu 	px_t		*px_p = DIP_TO_STATE(dip);
202127Sjchu 
202227Sjchu 	VERIFY(add_ivintr(px_fault_p->px_fh_sysino, PX_ERR_PIL,
202327Sjchu 		px_fault_p->px_err_func, (caddr_t)px_fault_p, NULL) == 0);
202427Sjchu 
202527Sjchu 	px_ib_intr_enable(px_p, intr_dist_cpuid(), px_fault_p->px_intr_ino);
202627Sjchu 
202727Sjchu 	return (DDI_SUCCESS);
202827Sjchu }
202927Sjchu 
203027Sjchu void
203127Sjchu px_err_rem_intr(px_fault_t *px_fault_p)
203227Sjchu {
203327Sjchu 	dev_info_t	*dip = px_fault_p->px_fh_dip;
203427Sjchu 	px_t		*px_p = DIP_TO_STATE(dip);
203527Sjchu 
203627Sjchu 	px_ib_intr_disable(px_p->px_ib_p, px_fault_p->px_intr_ino,
203727Sjchu 		IB_INTR_WAIT);
2038965Sgovinda 
2039965Sgovinda 	rem_ivintr(px_fault_p->px_fh_sysino, NULL);
204027Sjchu }
204127Sjchu 
20421648Sjchu /*
20431648Sjchu  * px_cb_add_intr() - Called from attach(9E) to create CB if not yet
20441648Sjchu  * created, to add CB interrupt vector always, but enable only once.
20451648Sjchu  */
20461648Sjchu int
20471648Sjchu px_cb_add_intr(px_fault_t *fault_p)
20481648Sjchu {
20491648Sjchu 	px_t		*px_p = DIP_TO_STATE(fault_p->px_fh_dip);
20501648Sjchu 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
20511772Sjl139090 	px_cb_t		*cb_p = (px_cb_t *)px_get_cb(fault_p->px_fh_dip);
20521648Sjchu 	px_cb_list_t	*pxl, *pxl_new;
20531648Sjchu 	cpuid_t		cpuid;
20541648Sjchu 
20551648Sjchu 
20561648Sjchu 	if (cb_p == NULL) {
20571648Sjchu 		cb_p = kmem_zalloc(sizeof (px_cb_t), KM_SLEEP);
20581648Sjchu 		mutex_init(&cb_p->cb_mutex, NULL, MUTEX_DRIVER, NULL);
20591648Sjchu 		cb_p->px_cb_func = px_cb_intr;
20601648Sjchu 		pxu_p->px_cb_p = cb_p;
20611772Sjl139090 		px_set_cb(fault_p->px_fh_dip, (uint64_t)cb_p);
20621648Sjchu 	} else
20631648Sjchu 		pxu_p->px_cb_p = cb_p;
20641648Sjchu 
20651648Sjchu 	mutex_enter(&cb_p->cb_mutex);
20661648Sjchu 
20671648Sjchu 	VERIFY(add_ivintr(fault_p->px_fh_sysino, PX_ERR_PIL,
20681648Sjchu 	    cb_p->px_cb_func, (caddr_t)cb_p, NULL) == 0);
20691648Sjchu 
20701648Sjchu 	if (cb_p->pxl == NULL) {
20711648Sjchu 
20721648Sjchu 		cpuid = intr_dist_cpuid(),
20731648Sjchu 		px_ib_intr_enable(px_p, cpuid, fault_p->px_intr_ino);
20741648Sjchu 
20751648Sjchu 		pxl = kmem_zalloc(sizeof (px_cb_list_t), KM_SLEEP);
20761648Sjchu 		pxl->pxp = px_p;
20771648Sjchu 
20781648Sjchu 		cb_p->pxl = pxl;
20791648Sjchu 		cb_p->sysino = fault_p->px_fh_sysino;
20801648Sjchu 		cb_p->cpuid = cpuid;
20811648Sjchu 
20821648Sjchu 	} else {
20831648Sjchu 		/*
20841648Sjchu 		 * Find the last pxl or
20851648Sjchu 		 * stop short at encoutering a redundent, or
20861648Sjchu 		 * both.
20871648Sjchu 		 */
20881648Sjchu 		pxl = cb_p->pxl;
20891648Sjchu 		for (; !(pxl->pxp == px_p) && pxl->next; pxl = pxl->next);
20901648Sjchu 		if (pxl->pxp == px_p) {
20911648Sjchu 			cmn_err(CE_WARN, "px_cb_add_intr: reregister sysino "
20921650Sjchu 			    "%lx by px_p 0x%p\n", cb_p->sysino, (void *)px_p);
20931648Sjchu 			return (DDI_FAILURE);
20941648Sjchu 		}
20951648Sjchu 
20961648Sjchu 		/* add to linked list */
20971648Sjchu 		pxl_new = kmem_zalloc(sizeof (px_cb_list_t), KM_SLEEP);
20981648Sjchu 		pxl_new->pxp = px_p;
20991648Sjchu 		pxl->next = pxl_new;
21001648Sjchu 	}
21011648Sjchu 	cb_p->attachcnt++;
21021648Sjchu 
21031648Sjchu 	mutex_exit(&cb_p->cb_mutex);
21041648Sjchu 
21051648Sjchu 	return (DDI_SUCCESS);
21061648Sjchu }
21071648Sjchu 
21081648Sjchu /*
21091648Sjchu  * px_cb_rem_intr() - Called from detach(9E) to remove its CB
21101648Sjchu  * interrupt vector, to shift proxy to the next available px,
21111648Sjchu  * or disable CB interrupt when itself is the last.
21121648Sjchu  */
21131648Sjchu void
21141648Sjchu px_cb_rem_intr(px_fault_t *fault_p)
21151648Sjchu {
21161648Sjchu 	px_t		*px_p = DIP_TO_STATE(fault_p->px_fh_dip), *pxp;
21171648Sjchu 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
21181648Sjchu 	px_cb_t		*cb_p = PX2CB(px_p);
21191648Sjchu 	px_cb_list_t	*pxl, *prev;
21201648Sjchu 	px_fault_t	*f_p;
21211648Sjchu 
21221648Sjchu 	ASSERT(cb_p->pxl);
21231648Sjchu 
21241648Sjchu 	/* De-list the target px, move the next px up */
21251648Sjchu 
21261648Sjchu 	mutex_enter(&cb_p->cb_mutex);
21271648Sjchu 
21281648Sjchu 	pxl = cb_p->pxl;
21291648Sjchu 	if (pxl->pxp == px_p) {
21301648Sjchu 		cb_p->pxl = pxl->next;
21311648Sjchu 	} else {
21321648Sjchu 		prev = pxl;
21331648Sjchu 		pxl = pxl->next;
21341648Sjchu 		for (; pxl && (pxl->pxp != px_p); prev = pxl, pxl = pxl->next);
21351648Sjchu 		if (!pxl) {
21361648Sjchu 			cmn_err(CE_WARN, "px_cb_rem_intr: can't find px_p 0x%p "
21371650Sjchu 			    "in registered CB list.", (void *)px_p);
21381648Sjchu 			return;
21391648Sjchu 		}
21401648Sjchu 		prev->next = pxl->next;
21411648Sjchu 	}
21421648Sjchu 	kmem_free(pxl, sizeof (px_cb_list_t));
21431648Sjchu 
21441648Sjchu 	if (fault_p->px_fh_sysino == cb_p->sysino) {
21451648Sjchu 		px_ib_intr_disable(px_p->px_ib_p, fault_p->px_intr_ino,
21461648Sjchu 		    IB_INTR_WAIT);
21471648Sjchu 
21481648Sjchu 		if (cb_p->pxl) {
21491648Sjchu 			pxp = cb_p->pxl->pxp;
21501648Sjchu 			f_p = &pxp->px_cb_fault;
21511648Sjchu 			cb_p->sysino = f_p->px_fh_sysino;
21521648Sjchu 
21531648Sjchu 			PX_INTR_ENABLE(pxp->px_dip, cb_p->sysino, cb_p->cpuid);
21541650Sjchu 			(void) px_lib_intr_setstate(pxp->px_dip, cb_p->sysino,
21551648Sjchu 			    INTR_IDLE_STATE);
21561648Sjchu 		}
21571648Sjchu 	}
21581648Sjchu 
21591648Sjchu 	rem_ivintr(fault_p->px_fh_sysino, NULL);
21601648Sjchu 	pxu_p->px_cb_p = NULL;
21611648Sjchu 	cb_p->attachcnt--;
21621648Sjchu 	if (cb_p->pxl) {
21631648Sjchu 		mutex_exit(&cb_p->cb_mutex);
21641648Sjchu 		return;
21651648Sjchu 	}
21661648Sjchu 	mutex_exit(&cb_p->cb_mutex);
21671648Sjchu 
21681648Sjchu 	mutex_destroy(&cb_p->cb_mutex);
21691772Sjl139090 	px_set_cb(fault_p->px_fh_dip, 0ull);
21701648Sjchu 	kmem_free(cb_p, sizeof (px_cb_t));
21711648Sjchu }
21721648Sjchu 
21731648Sjchu /*
21741648Sjchu  * px_cb_intr() - sun4u only,  CB interrupt dispatcher
21751648Sjchu  */
21761648Sjchu uint_t
21771648Sjchu px_cb_intr(caddr_t arg)
21781648Sjchu {
21791648Sjchu 	px_cb_t		*cb_p = (px_cb_t *)arg;
21801648Sjchu 	px_cb_list_t	*pxl = cb_p->pxl;
21811648Sjchu 	px_t		*pxp = pxl ? pxl->pxp : NULL;
21821648Sjchu 	px_fault_t	*fault_p;
21831648Sjchu 
21841648Sjchu 	while (pxl && pxp && (pxp->px_state != PX_ATTACHED)) {
21851648Sjchu 		pxl = pxl->next;
21861648Sjchu 		pxp = (pxl) ? pxl->pxp : NULL;
21871648Sjchu 	}
21881648Sjchu 
21891648Sjchu 	if (pxp) {
21901648Sjchu 		fault_p = &pxp->px_cb_fault;
21911648Sjchu 		return (fault_p->px_err_func((caddr_t)fault_p));
21921648Sjchu 	} else
21931648Sjchu 		return (DDI_INTR_UNCLAIMED);
21941648Sjchu }
21951648Sjchu 
21961648Sjchu /*
21971648Sjchu  * px_cb_intr_redist() - sun4u only, CB interrupt redistribution
21981648Sjchu  */
21991648Sjchu void
22001648Sjchu px_cb_intr_redist(px_t	*px_p)
22011648Sjchu {
22021648Sjchu 	px_fault_t	*f_p = &px_p->px_cb_fault;
22031648Sjchu 	px_cb_t		*cb_p = PX2CB(px_p);
22041648Sjchu 	devino_t	ino = px_p->px_inos[PX_INTR_XBC];
22051648Sjchu 	cpuid_t		cpuid;
22061648Sjchu 
22071648Sjchu 	mutex_enter(&cb_p->cb_mutex);
22081648Sjchu 
22091648Sjchu 	if (cb_p->sysino != f_p->px_fh_sysino) {
22101648Sjchu 		mutex_exit(&cb_p->cb_mutex);
22111648Sjchu 		return;
22121648Sjchu 	}
22131648Sjchu 
22141648Sjchu 	cb_p->cpuid = cpuid = intr_dist_cpuid();
22151648Sjchu 	px_ib_intr_dist_en(px_p->px_dip, cpuid, ino, B_FALSE);
22161648Sjchu 
22171648Sjchu 	mutex_exit(&cb_p->cb_mutex);
22181648Sjchu }
22191648Sjchu 
222027Sjchu #ifdef FMA
222127Sjchu void
222227Sjchu px_fill_rc_status(px_fault_t *px_fault_p, pciex_rc_error_regs_t *rc_status)
222327Sjchu {
222427Sjchu 	/* populate the rc_status by reading the registers - TBD */
222527Sjchu }
222627Sjchu #endif /* FMA */
2227383Set142600 
2228383Set142600 /*
2229383Set142600  * Unprotected raw reads/writes of fabric device's config space.
2230383Set142600  * Only used for temporary PCI-E Fabric Error Handling.
2231383Set142600  */
2232383Set142600 uint32_t
22331648Sjchu px_fab_get(px_t *px_p, pcie_req_id_t bdf, uint16_t offset)
22341648Sjchu {
2235383Set142600 	px_ranges_t	*rp = px_p->px_ranges_p;
2236383Set142600 	uint64_t	range_prop, base_addr;
2237383Set142600 	int		bank = PCI_REG_ADDR_G(PCI_ADDR_CONFIG);
2238383Set142600 	uint32_t	val;
2239383Set142600 
2240383Set142600 	/* Get Fire's Physical Base Address */
22411772Sjl139090 	range_prop = px_get_range_prop(px_p, rp, bank);
2242383Set142600 
2243383Set142600 	/* Get config space first. */
2244383Set142600 	base_addr = range_prop + PX_BDF_TO_CFGADDR(bdf, offset);
2245383Set142600 
2246383Set142600 	val = ldphysio(base_addr);
2247383Set142600 
2248383Set142600 	return (LE_32(val));
2249383Set142600 }
2250383Set142600 
2251383Set142600 void
2252383Set142600 px_fab_set(px_t *px_p, pcie_req_id_t bdf, uint16_t offset,
2253383Set142600     uint32_t val) {
2254383Set142600 	px_ranges_t	*rp = px_p->px_ranges_p;
2255383Set142600 	uint64_t	range_prop, base_addr;
2256383Set142600 	int		bank = PCI_REG_ADDR_G(PCI_ADDR_CONFIG);
2257383Set142600 
2258383Set142600 	/* Get Fire's Physical Base Address */
22591772Sjl139090 	range_prop = px_get_range_prop(px_p, rp, bank);
2260383Set142600 
2261383Set142600 	/* Get config space first. */
2262383Set142600 	base_addr = range_prop + PX_BDF_TO_CFGADDR(bdf, offset);
2263383Set142600 
2264383Set142600 	stphysio(base_addr, LE_32(val));
2265383Set142600 }
2266435Sjchu 
2267435Sjchu /*
2268435Sjchu  * cpr callback
2269435Sjchu  *
2270435Sjchu  * disable fabric error msg interrupt prior to suspending
2271435Sjchu  * all device drivers; re-enable fabric error msg interrupt
2272435Sjchu  * after all devices are resumed.
2273435Sjchu  */
2274435Sjchu static boolean_t
2275435Sjchu px_cpr_callb(void *arg, int code)
2276435Sjchu {
2277435Sjchu 	px_t		*px_p = (px_t *)arg;
2278435Sjchu 	px_ib_t		*ib_p = px_p->px_ib_p;
2279435Sjchu 	px_pec_t	*pec_p = px_p->px_pec_p;
2280435Sjchu 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
2281435Sjchu 	caddr_t		csr_base;
2282435Sjchu 	devino_t	ce_ino, nf_ino, f_ino;
2283435Sjchu 	px_ib_ino_info_t	*ce_ino_p, *nf_ino_p, *f_ino_p;
2284435Sjchu 	uint64_t	imu_log_enable, imu_intr_enable;
2285435Sjchu 	uint64_t	imu_log_mask, imu_intr_mask;
2286435Sjchu 
2287435Sjchu 	ce_ino = px_msiqid_to_devino(px_p, pec_p->pec_corr_msg_msiq_id);
2288435Sjchu 	nf_ino = px_msiqid_to_devino(px_p, pec_p->pec_non_fatal_msg_msiq_id);
2289435Sjchu 	f_ino = px_msiqid_to_devino(px_p, pec_p->pec_fatal_msg_msiq_id);
2290435Sjchu 	csr_base = (caddr_t)pxu_p->px_address[PX_REG_CSR];
2291435Sjchu 
2292435Sjchu 	imu_log_enable = CSR_XR(csr_base, IMU_ERROR_LOG_ENABLE);
2293435Sjchu 	imu_intr_enable = CSR_XR(csr_base, IMU_INTERRUPT_ENABLE);
2294435Sjchu 
2295435Sjchu 	imu_log_mask = BITMASK(IMU_ERROR_LOG_ENABLE_FATAL_MES_NOT_EN_LOG_EN) |
2296435Sjchu 	    BITMASK(IMU_ERROR_LOG_ENABLE_NONFATAL_MES_NOT_EN_LOG_EN) |
2297435Sjchu 	    BITMASK(IMU_ERROR_LOG_ENABLE_COR_MES_NOT_EN_LOG_EN);
2298435Sjchu 
2299435Sjchu 	imu_intr_mask =
2300435Sjchu 	    BITMASK(IMU_INTERRUPT_ENABLE_FATAL_MES_NOT_EN_S_INT_EN) |
2301435Sjchu 	    BITMASK(IMU_INTERRUPT_ENABLE_NONFATAL_MES_NOT_EN_S_INT_EN) |
2302435Sjchu 	    BITMASK(IMU_INTERRUPT_ENABLE_COR_MES_NOT_EN_S_INT_EN) |
2303435Sjchu 	    BITMASK(IMU_INTERRUPT_ENABLE_FATAL_MES_NOT_EN_P_INT_EN) |
2304435Sjchu 	    BITMASK(IMU_INTERRUPT_ENABLE_NONFATAL_MES_NOT_EN_P_INT_EN) |
2305435Sjchu 	    BITMASK(IMU_INTERRUPT_ENABLE_COR_MES_NOT_EN_P_INT_EN);
2306435Sjchu 
2307435Sjchu 	switch (code) {
2308435Sjchu 	case CB_CODE_CPR_CHKPT:
2309435Sjchu 		/* disable imu rbne on corr/nonfatal/fatal errors */
2310435Sjchu 		CSR_XS(csr_base, IMU_ERROR_LOG_ENABLE,
2311435Sjchu 		    imu_log_enable & (~imu_log_mask));
2312435Sjchu 
2313435Sjchu 		CSR_XS(csr_base, IMU_INTERRUPT_ENABLE,
2314435Sjchu 		    imu_intr_enable & (~imu_intr_mask));
2315435Sjchu 
2316435Sjchu 		/* disable CORR intr mapping */
2317435Sjchu 		px_ib_intr_disable(ib_p, ce_ino, IB_INTR_NOWAIT);
2318435Sjchu 
2319435Sjchu 		/* disable NON FATAL intr mapping */
2320435Sjchu 		px_ib_intr_disable(ib_p, nf_ino, IB_INTR_NOWAIT);
2321435Sjchu 
2322435Sjchu 		/* disable FATAL intr mapping */
2323435Sjchu 		px_ib_intr_disable(ib_p, f_ino, IB_INTR_NOWAIT);
2324435Sjchu 
2325435Sjchu 		break;
2326435Sjchu 
2327435Sjchu 	case CB_CODE_CPR_RESUME:
2328435Sjchu 		mutex_enter(&ib_p->ib_ino_lst_mutex);
2329435Sjchu 
2330435Sjchu 		ce_ino_p = px_ib_locate_ino(ib_p, ce_ino);
2331435Sjchu 		nf_ino_p = px_ib_locate_ino(ib_p, nf_ino);
2332435Sjchu 		f_ino_p = px_ib_locate_ino(ib_p, f_ino);
2333435Sjchu 
2334435Sjchu 		/* enable CORR intr mapping */
2335435Sjchu 		if (ce_ino_p)
2336435Sjchu 			px_ib_intr_enable(px_p, ce_ino_p->ino_cpuid, ce_ino);
2337435Sjchu 		else
2338435Sjchu 			cmn_err(CE_WARN, "px_cpr_callb: RESUME unable to "
2339435Sjchu 			    "reenable PCIe Correctable msg intr.\n");
2340435Sjchu 
2341435Sjchu 		/* enable NON FATAL intr mapping */
2342435Sjchu 		if (nf_ino_p)
2343435Sjchu 			px_ib_intr_enable(px_p, nf_ino_p->ino_cpuid, nf_ino);
2344435Sjchu 		else
2345435Sjchu 			cmn_err(CE_WARN, "px_cpr_callb: RESUME unable to "
2346435Sjchu 			    "reenable PCIe Non Fatal msg intr.\n");
2347435Sjchu 
2348435Sjchu 		/* enable FATAL intr mapping */
2349435Sjchu 		if (f_ino_p)
2350435Sjchu 			px_ib_intr_enable(px_p, f_ino_p->ino_cpuid, f_ino);
2351435Sjchu 		else
2352435Sjchu 			cmn_err(CE_WARN, "px_cpr_callb: RESUME unable to "
2353435Sjchu 			    "reenable PCIe Fatal msg intr.\n");
2354435Sjchu 
2355435Sjchu 		mutex_exit(&ib_p->ib_ino_lst_mutex);
2356435Sjchu 
2357435Sjchu 		/* enable corr/nonfatal/fatal not enable error */
2358435Sjchu 		CSR_XS(csr_base, IMU_ERROR_LOG_ENABLE, (imu_log_enable |
2359435Sjchu 		    (imu_log_mask & px_imu_log_mask)));
2360435Sjchu 		CSR_XS(csr_base, IMU_INTERRUPT_ENABLE, (imu_intr_enable |
2361435Sjchu 		    (imu_intr_mask & px_imu_intr_mask)));
2362435Sjchu 
2363435Sjchu 		break;
2364435Sjchu 	}
2365435Sjchu 
2366435Sjchu 	return (B_TRUE);
2367435Sjchu }
2368435Sjchu 
2369*2053Sschwartz uint64_t
2370*2053Sschwartz px_get_rng_parent_hi_mask(px_t *px_p)
2371*2053Sschwartz {
2372*2053Sschwartz 	pxu_t *pxu_p = (pxu_t *)px_p->px_plat_p;
2373*2053Sschwartz 	uint64_t mask;
2374*2053Sschwartz 
2375*2053Sschwartz 	switch (PX_CHIP_TYPE(pxu_p)) {
2376*2053Sschwartz 	case PX_CHIP_OBERON:
2377*2053Sschwartz 		mask = OBERON_RANGE_PROP_MASK;
2378*2053Sschwartz 		break;
2379*2053Sschwartz 	case PX_CHIP_FIRE:
2380*2053Sschwartz 		mask = PX_RANGE_PROP_MASK;
2381*2053Sschwartz 		break;
2382*2053Sschwartz 	default:
2383*2053Sschwartz 		mask = PX_RANGE_PROP_MASK;
2384*2053Sschwartz 	}
2385*2053Sschwartz 
2386*2053Sschwartz 	return (mask);
2387*2053Sschwartz }
2388*2053Sschwartz 
2389435Sjchu /*
23901772Sjl139090  * fetch chip's range propery's value
23911772Sjl139090  */
23921772Sjl139090 uint64_t
23931772Sjl139090 px_get_range_prop(px_t *px_p, px_ranges_t *rp, int bank)
23941772Sjl139090 {
23951772Sjl139090 	uint64_t mask, range_prop;
23961772Sjl139090 
2397*2053Sschwartz 	mask = px_get_rng_parent_hi_mask(px_p);
23981772Sjl139090 	range_prop = (((uint64_t)(rp[bank].parent_high & mask)) << 32) |
23991772Sjl139090 		rp[bank].parent_low;
24001772Sjl139090 
24011772Sjl139090 	return (range_prop);
24021772Sjl139090 }
24031772Sjl139090 
24041772Sjl139090 /*
2405435Sjchu  * add cpr callback
2406435Sjchu  */
2407435Sjchu void
2408435Sjchu px_cpr_add_callb(px_t *px_p)
2409435Sjchu {
2410435Sjchu 	px_p->px_cprcb_id = callb_add(px_cpr_callb, (void *)px_p,
2411435Sjchu 	CB_CL_CPR_POST_USER, "px_cpr");
2412435Sjchu }
2413435Sjchu 
2414435Sjchu /*
2415435Sjchu  * remove cpr callback
2416435Sjchu  */
2417435Sjchu void
2418435Sjchu px_cpr_rem_callb(px_t *px_p)
2419435Sjchu {
2420435Sjchu 	(void) callb_delete(px_p->px_cprcb_id);
2421435Sjchu }
24221531Skini 
24231531Skini /*ARGSUSED*/
24241772Sjl139090 static uint_t
24251772Sjl139090 px_hp_intr(caddr_t arg1, caddr_t arg2)
24261772Sjl139090 {
24271772Sjl139090 	px_t *px_p = (px_t *)arg1;
24281772Sjl139090 	int rval;
24291772Sjl139090 
24301772Sjl139090 	rval = pciehpc_intr(px_p->px_dip);
24311772Sjl139090 
24321772Sjl139090 #ifdef  DEBUG
24331772Sjl139090 	if (rval == DDI_INTR_UNCLAIMED)
24341772Sjl139090 	    cmn_err(CE_WARN, "%s%d: UNCLAIMED intr\n",
24351772Sjl139090 		ddi_driver_name(px_p->px_dip),
24361772Sjl139090 		ddi_get_instance(px_p->px_dip));
24371772Sjl139090 #endif
24381772Sjl139090 
24391772Sjl139090 	return (rval);
24401772Sjl139090 }
24411772Sjl139090 
24421531Skini int
24431531Skini px_lib_hotplug_init(dev_info_t *dip, void *arg)
24441531Skini {
24451772Sjl139090 	px_t	*px_p = DIP_TO_STATE(dip);
24461772Sjl139090 	uint64_t ret;
24471772Sjl139090 
24481772Sjl139090 	if ((ret = hvio_hotplug_init(dip, arg)) == DDI_SUCCESS) {
24491772Sjl139090 		sysino_t sysino;
24501772Sjl139090 
24511772Sjl139090 		if (px_lib_intr_devino_to_sysino(px_p->px_dip,
24521772Sjl139090 		    px_p->px_inos[PX_INTR_HOTPLUG], &sysino) !=
24531772Sjl139090 		    DDI_SUCCESS) {
24541772Sjl139090 #ifdef	DEBUG
24551772Sjl139090 			cmn_err(CE_WARN, "%s%d: devino_to_sysino fails\n",
24561772Sjl139090 			    ddi_driver_name(px_p->px_dip),
24571772Sjl139090 			    ddi_get_instance(px_p->px_dip));
24581772Sjl139090 #endif
24591772Sjl139090 			return (DDI_FAILURE);
24601772Sjl139090 		}
24611772Sjl139090 
24621772Sjl139090 		VERIFY(add_ivintr(sysino, PX_PCIEHP_PIL,
24631772Sjl139090 		    (intrfunc)px_hp_intr, (caddr_t)px_p, NULL) == 0);
24641772Sjl139090 	}
24651772Sjl139090 
24661772Sjl139090 	return (ret);
24671531Skini }
24681531Skini 
24691531Skini void
24701531Skini px_lib_hotplug_uninit(dev_info_t *dip)
24711531Skini {
24721772Sjl139090 	if (hvio_hotplug_uninit(dip) == DDI_SUCCESS) {
24731772Sjl139090 		px_t	*px_p = DIP_TO_STATE(dip);
24741772Sjl139090 		sysino_t sysino;
24751772Sjl139090 
24761772Sjl139090 		if (px_lib_intr_devino_to_sysino(px_p->px_dip,
24771772Sjl139090 		    px_p->px_inos[PX_INTR_HOTPLUG], &sysino) !=
24781772Sjl139090 		    DDI_SUCCESS) {
24791772Sjl139090 #ifdef	DEBUG
24801772Sjl139090 			cmn_err(CE_WARN, "%s%d: devino_to_sysino fails\n",
24811772Sjl139090 			    ddi_driver_name(px_p->px_dip),
24821772Sjl139090 			    ddi_get_instance(px_p->px_dip));
24831772Sjl139090 #endif
24841772Sjl139090 			return;
24851772Sjl139090 		}
24861772Sjl139090 
24871772Sjl139090 		rem_ivintr(sysino, NULL);
24881772Sjl139090 	}
24891531Skini }
2490