xref: /onnv-gate/usr/src/uts/sun4u/io/px/px_lib4u.c (revision 2588:6ca5ef0f409c)
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;
622276Sschwartz uint64_t px_paddr_mask;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate static int px_goto_l23ready(px_t *px_p);
65118Sjchu static int px_goto_l0(px_t *px_p);
66118Sjchu static int px_pre_pwron_check(px_t *px_p);
672426Sschwartz static uint32_t px_identity_init(px_t *px_p);
68435Sjchu static boolean_t px_cpr_callb(void *arg, int code);
691648Sjchu static uint_t px_cb_intr(caddr_t arg);
7027Sjchu 
7127Sjchu /*
7227Sjchu  * px_lib_map_registers
7327Sjchu  *
7427Sjchu  * This function is called from the attach routine to map the registers
7527Sjchu  * accessed by this driver.
7627Sjchu  *
7727Sjchu  * used by: px_attach()
7827Sjchu  *
7927Sjchu  * return value: DDI_FAILURE on failure
8027Sjchu  */
8127Sjchu int
8227Sjchu px_lib_map_regs(pxu_t *pxu_p, dev_info_t *dip)
8327Sjchu {
8427Sjchu 	ddi_device_acc_attr_t	attr;
8527Sjchu 	px_reg_bank_t		reg_bank = PX_REG_CSR;
8627Sjchu 
8727Sjchu 	DBG(DBG_ATTACH, dip, "px_lib_map_regs: pxu_p:0x%p, dip 0x%p\n",
8827Sjchu 		pxu_p, dip);
8927Sjchu 
9027Sjchu 	attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
9127Sjchu 	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
9227Sjchu 	attr.devacc_attr_endian_flags = DDI_NEVERSWAP_ACC;
9327Sjchu 
9427Sjchu 	/*
9527Sjchu 	 * PCI CSR Base
9627Sjchu 	 */
9727Sjchu 	if (ddi_regs_map_setup(dip, reg_bank, &pxu_p->px_address[reg_bank],
9827Sjchu 	    0, 0, &attr, &pxu_p->px_ac[reg_bank]) != DDI_SUCCESS) {
9927Sjchu 		goto fail;
10027Sjchu 	}
10127Sjchu 
10227Sjchu 	reg_bank++;
10327Sjchu 
10427Sjchu 	/*
10527Sjchu 	 * XBUS CSR Base
10627Sjchu 	 */
10727Sjchu 	if (ddi_regs_map_setup(dip, reg_bank, &pxu_p->px_address[reg_bank],
10827Sjchu 	    0, 0, &attr, &pxu_p->px_ac[reg_bank]) != DDI_SUCCESS) {
10927Sjchu 		goto fail;
11027Sjchu 	}
11127Sjchu 
11227Sjchu 	pxu_p->px_address[reg_bank] -= FIRE_CONTROL_STATUS;
11327Sjchu 
11427Sjchu done:
11527Sjchu 	for (; reg_bank >= PX_REG_CSR; reg_bank--) {
11627Sjchu 		DBG(DBG_ATTACH, dip, "reg_bank 0x%x address 0x%p\n",
11727Sjchu 		    reg_bank, pxu_p->px_address[reg_bank]);
11827Sjchu 	}
11927Sjchu 
12027Sjchu 	return (DDI_SUCCESS);
12127Sjchu 
12227Sjchu fail:
12327Sjchu 	cmn_err(CE_WARN, "%s%d: unable to map reg entry %d\n",
12427Sjchu 	    ddi_driver_name(dip), ddi_get_instance(dip), reg_bank);
12527Sjchu 
12627Sjchu 	for (reg_bank--; reg_bank >= PX_REG_CSR; reg_bank--) {
12727Sjchu 		pxu_p->px_address[reg_bank] = NULL;
12827Sjchu 		ddi_regs_map_free(&pxu_p->px_ac[reg_bank]);
12927Sjchu 	}
13027Sjchu 
13127Sjchu 	return (DDI_FAILURE);
13227Sjchu }
13327Sjchu 
13427Sjchu /*
13527Sjchu  * px_lib_unmap_regs:
13627Sjchu  *
13727Sjchu  * This routine unmaps the registers mapped by map_px_registers.
13827Sjchu  *
13927Sjchu  * used by: px_detach(), and error conditions in px_attach()
14027Sjchu  *
14127Sjchu  * return value: none
14227Sjchu  */
14327Sjchu void
14427Sjchu px_lib_unmap_regs(pxu_t *pxu_p)
14527Sjchu {
14627Sjchu 	int i;
14727Sjchu 
14827Sjchu 	for (i = 0; i < PX_REG_MAX; i++) {
14927Sjchu 		if (pxu_p->px_ac[i])
15027Sjchu 			ddi_regs_map_free(&pxu_p->px_ac[i]);
15127Sjchu 	}
15227Sjchu }
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate int
1550Sstevel@tonic-gate px_lib_dev_init(dev_info_t *dip, devhandle_t *dev_hdl)
1560Sstevel@tonic-gate {
1572509Sschwartz 
1582509Sschwartz 	caddr_t			xbc_csr_base, csr_base;
1590Sstevel@tonic-gate 	px_dvma_range_prop_t	px_dvma_range;
1602509Sschwartz 	pxu_t			*pxu_p;
1612509Sschwartz 	uint8_t			chip_mask;
1622509Sschwartz 	px_t			*px_p = DIP_TO_STATE(dip);
1632509Sschwartz 	px_chip_type_t		chip_type = px_identity_init(px_p);
1640Sstevel@tonic-gate 
1652426Sschwartz 	DBG(DBG_ATTACH, dip, "px_lib_dev_init: dip 0x%p", dip);
1662426Sschwartz 
1672426Sschwartz 	if (chip_type == PX_CHIP_UNIDENTIFIED) {
1682426Sschwartz 		cmn_err(CE_WARN, "%s%d: Unrecognized Hardware Version\n",
1692426Sschwartz 		    NAMEINST(dip));
1700Sstevel@tonic-gate 		return (DDI_FAILURE);
1710Sstevel@tonic-gate 	}
1720Sstevel@tonic-gate 
1732509Sschwartz 	chip_mask = BITMASK(chip_type);
1742426Sschwartz 	px_paddr_mask = (chip_type == PX_CHIP_FIRE) ? MMU_FIRE_PADDR_MASK :
1752426Sschwartz 	    MMU_OBERON_PADDR_MASK;
1762426Sschwartz 
1770Sstevel@tonic-gate 	/*
1780Sstevel@tonic-gate 	 * Allocate platform specific structure and link it to
1790Sstevel@tonic-gate 	 * the px state structure.
1800Sstevel@tonic-gate 	 */
1810Sstevel@tonic-gate 	pxu_p = kmem_zalloc(sizeof (pxu_t), KM_SLEEP);
1822426Sschwartz 	pxu_p->chip_type = chip_type;
1830Sstevel@tonic-gate 	pxu_p->portid  = ddi_getprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
1840Sstevel@tonic-gate 	    "portid", -1);
1850Sstevel@tonic-gate 
18627Sjchu 	/* Map in the registers */
18727Sjchu 	if (px_lib_map_regs(pxu_p, dip) == DDI_FAILURE) {
18827Sjchu 		kmem_free(pxu_p, sizeof (pxu_t));
18927Sjchu 
19027Sjchu 		return (DDI_FAILURE);
19127Sjchu 	}
19227Sjchu 
19327Sjchu 	xbc_csr_base = (caddr_t)pxu_p->px_address[PX_REG_XBC];
19427Sjchu 	csr_base = (caddr_t)pxu_p->px_address[PX_REG_CSR];
19527Sjchu 
1960Sstevel@tonic-gate 	pxu_p->tsb_cookie = iommu_tsb_alloc(pxu_p->portid);
1970Sstevel@tonic-gate 	pxu_p->tsb_size = iommu_tsb_cookie_to_size(pxu_p->tsb_cookie);
1980Sstevel@tonic-gate 	pxu_p->tsb_vaddr = iommu_tsb_cookie_to_va(pxu_p->tsb_cookie);
1990Sstevel@tonic-gate 
2001772Sjl139090 	pxu_p->tsb_paddr = va_to_pa(pxu_p->tsb_vaddr);
2011772Sjl139090 
2020Sstevel@tonic-gate 	/*
2030Sstevel@tonic-gate 	 * Create "virtual-dma" property to support child devices
2040Sstevel@tonic-gate 	 * needing to know DVMA range.
2050Sstevel@tonic-gate 	 */
2060Sstevel@tonic-gate 	px_dvma_range.dvma_base = (uint32_t)px_mmu_dvma_end + 1
2070Sstevel@tonic-gate 	    - ((pxu_p->tsb_size >> 3) << MMU_PAGE_SHIFT);
2080Sstevel@tonic-gate 	px_dvma_range.dvma_len = (uint32_t)
2090Sstevel@tonic-gate 	    px_mmu_dvma_end - px_dvma_range.dvma_base + 1;
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	(void) ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
2120Sstevel@tonic-gate 		"virtual-dma", (caddr_t)&px_dvma_range,
2130Sstevel@tonic-gate 		sizeof (px_dvma_range_prop_t));
2140Sstevel@tonic-gate 	/*
2150Sstevel@tonic-gate 	 * Initilize all fire hardware specific blocks.
2160Sstevel@tonic-gate 	 */
2170Sstevel@tonic-gate 	hvio_cb_init(xbc_csr_base, pxu_p);
2180Sstevel@tonic-gate 	hvio_ib_init(csr_base, pxu_p);
2190Sstevel@tonic-gate 	hvio_pec_init(csr_base, pxu_p);
2200Sstevel@tonic-gate 	hvio_mmu_init(csr_base, pxu_p);
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	px_p->px_plat_p = (void *)pxu_p;
2230Sstevel@tonic-gate 
22427Sjchu 	/*
22527Sjchu 	 * Initialize all the interrupt handlers
22627Sjchu 	 */
2271772Sjl139090 	switch (PX_CHIP_TYPE(pxu_p)) {
2281772Sjl139090 	case PX_CHIP_OBERON:
2292044Sjj156685 		/*
2302044Sjj156685 		 * Oberon hotplug uses SPARE3 field in ILU Error Log Enable
2312044Sjj156685 		 * register to indicate the status of leaf reset,
2322044Sjj156685 		 * we need to preserve the value of this bit, and keep it in
2332044Sjj156685 		 * px_ilu_log_mask to reflect the state of the bit
2342044Sjj156685 		 */
2352044Sjj156685 		if (CSR_BR(csr_base, ILU_ERROR_LOG_ENABLE, SPARE3))
2362044Sjj156685 			px_ilu_log_mask |= (1ull <<
2372044Sjj156685 			    ILU_ERROR_LOG_ENABLE_SPARE3);
2382044Sjj156685 		else
2392044Sjj156685 			px_ilu_log_mask &= ~(1ull <<
2402044Sjj156685 			    ILU_ERROR_LOG_ENABLE_SPARE3);
2412509Sschwartz 
2422509Sschwartz 		px_err_reg_setup_pcie(chip_mask, csr_base, PX_ERR_ENABLE);
2431772Sjl139090 		px_fabric_die_rc_ue |= PCIE_AER_UCE_UC;
2441772Sjl139090 		break;
2451772Sjl139090 
2461772Sjl139090 	case PX_CHIP_FIRE:
2472509Sschwartz 		px_err_reg_setup_pcie(chip_mask, csr_base, PX_ERR_ENABLE);
2481772Sjl139090 		break;
2492509Sschwartz 
2501772Sjl139090 	default:
2511772Sjl139090 		cmn_err(CE_WARN, "%s%d: PX primary bus Unknown\n",
2521772Sjl139090 		    ddi_driver_name(dip), ddi_get_instance(dip));
2531772Sjl139090 		return (DDI_FAILURE);
2541772Sjl139090 	}
25527Sjchu 
2560Sstevel@tonic-gate 	/* Initilize device handle */
2570Sstevel@tonic-gate 	*dev_hdl = (devhandle_t)csr_base;
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	DBG(DBG_ATTACH, dip, "px_lib_dev_init: dev_hdl 0x%llx\n", *dev_hdl);
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	return (DDI_SUCCESS);
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate int
2650Sstevel@tonic-gate px_lib_dev_fini(dev_info_t *dip)
2660Sstevel@tonic-gate {
2672509Sschwartz 	caddr_t			csr_base;
2682509Sschwartz 	uint8_t			chip_mask;
2692509Sschwartz 	px_t			*px_p = DIP_TO_STATE(dip);
2702509Sschwartz 	pxu_t			*pxu_p = (pxu_t *)px_p->px_plat_p;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	DBG(DBG_DETACH, dip, "px_lib_dev_fini: dip 0x%p\n", dip);
2730Sstevel@tonic-gate 
27427Sjchu 	/*
27527Sjchu 	 * Deinitialize all the interrupt handlers
27627Sjchu 	 */
2771772Sjl139090 	switch (PX_CHIP_TYPE(pxu_p)) {
2781772Sjl139090 	case PX_CHIP_OBERON:
2792509Sschwartz 	case PX_CHIP_FIRE:
2802509Sschwartz 		chip_mask = BITMASK(PX_CHIP_TYPE(pxu_p));
2812509Sschwartz 		csr_base = (caddr_t)pxu_p->px_address[PX_REG_CSR];
2822509Sschwartz 		px_err_reg_setup_pcie(chip_mask, csr_base, PX_ERR_DISABLE);
2831772Sjl139090 		break;
2842509Sschwartz 
2851772Sjl139090 	default:
2861772Sjl139090 		cmn_err(CE_WARN, "%s%d: PX primary bus Unknown\n",
2871772Sjl139090 		    ddi_driver_name(dip), ddi_get_instance(dip));
2881772Sjl139090 		return (DDI_FAILURE);
2891772Sjl139090 	}
29027Sjchu 
2910Sstevel@tonic-gate 	iommu_tsb_free(pxu_p->tsb_cookie);
2920Sstevel@tonic-gate 
29327Sjchu 	px_lib_unmap_regs((pxu_t *)px_p->px_plat_p);
29427Sjchu 	kmem_free(px_p->px_plat_p, sizeof (pxu_t));
2950Sstevel@tonic-gate 	px_p->px_plat_p = NULL;
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	return (DDI_SUCCESS);
2980Sstevel@tonic-gate }
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate /*ARGSUSED*/
3010Sstevel@tonic-gate int
3020Sstevel@tonic-gate px_lib_intr_devino_to_sysino(dev_info_t *dip, devino_t devino,
3030Sstevel@tonic-gate     sysino_t *sysino)
3040Sstevel@tonic-gate {
3050Sstevel@tonic-gate 	px_t	*px_p = DIP_TO_STATE(dip);
3060Sstevel@tonic-gate 	pxu_t	*pxu_p = (pxu_t *)px_p->px_plat_p;
3070Sstevel@tonic-gate 	uint64_t	ret;
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_devino_to_sysino: dip 0x%p "
3100Sstevel@tonic-gate 	    "devino 0x%x\n", dip, devino);
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	if ((ret = hvio_intr_devino_to_sysino(DIP_TO_HANDLE(dip),
3130Sstevel@tonic-gate 	    pxu_p, devino, sysino)) != H_EOK) {
3140Sstevel@tonic-gate 		DBG(DBG_LIB_INT, dip,
3150Sstevel@tonic-gate 		    "hvio_intr_devino_to_sysino failed, ret 0x%lx\n", ret);
3160Sstevel@tonic-gate 		return (DDI_FAILURE);
3170Sstevel@tonic-gate 	}
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_devino_to_sysino: sysino 0x%llx\n",
3200Sstevel@tonic-gate 	    *sysino);
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	return (DDI_SUCCESS);
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate /*ARGSUSED*/
3260Sstevel@tonic-gate int
3270Sstevel@tonic-gate px_lib_intr_getvalid(dev_info_t *dip, sysino_t sysino,
3280Sstevel@tonic-gate     intr_valid_state_t *intr_valid_state)
3290Sstevel@tonic-gate {
3300Sstevel@tonic-gate 	uint64_t	ret;
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_getvalid: dip 0x%p sysino 0x%llx\n",
3330Sstevel@tonic-gate 	    dip, sysino);
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	if ((ret = hvio_intr_getvalid(DIP_TO_HANDLE(dip),
3360Sstevel@tonic-gate 	    sysino, intr_valid_state)) != H_EOK) {
3370Sstevel@tonic-gate 		DBG(DBG_LIB_INT, dip, "hvio_intr_getvalid failed, ret 0x%lx\n",
3380Sstevel@tonic-gate 		    ret);
3390Sstevel@tonic-gate 		return (DDI_FAILURE);
3400Sstevel@tonic-gate 	}
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_getvalid: intr_valid_state 0x%x\n",
3430Sstevel@tonic-gate 	    *intr_valid_state);
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	return (DDI_SUCCESS);
3460Sstevel@tonic-gate }
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate /*ARGSUSED*/
3490Sstevel@tonic-gate int
3500Sstevel@tonic-gate px_lib_intr_setvalid(dev_info_t *dip, sysino_t sysino,
3510Sstevel@tonic-gate     intr_valid_state_t intr_valid_state)
3520Sstevel@tonic-gate {
3530Sstevel@tonic-gate 	uint64_t	ret;
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_setvalid: dip 0x%p sysino 0x%llx "
3560Sstevel@tonic-gate 	    "intr_valid_state 0x%x\n", dip, sysino, intr_valid_state);
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	if ((ret = hvio_intr_setvalid(DIP_TO_HANDLE(dip),
3590Sstevel@tonic-gate 	    sysino, intr_valid_state)) != H_EOK) {
3600Sstevel@tonic-gate 		DBG(DBG_LIB_INT, dip, "hvio_intr_setvalid failed, ret 0x%lx\n",
3610Sstevel@tonic-gate 		    ret);
3620Sstevel@tonic-gate 		return (DDI_FAILURE);
3630Sstevel@tonic-gate 	}
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	return (DDI_SUCCESS);
3660Sstevel@tonic-gate }
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate /*ARGSUSED*/
3690Sstevel@tonic-gate int
3700Sstevel@tonic-gate px_lib_intr_getstate(dev_info_t *dip, sysino_t sysino,
3710Sstevel@tonic-gate     intr_state_t *intr_state)
3720Sstevel@tonic-gate {
3730Sstevel@tonic-gate 	uint64_t	ret;
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_getstate: dip 0x%p sysino 0x%llx\n",
3760Sstevel@tonic-gate 	    dip, sysino);
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	if ((ret = hvio_intr_getstate(DIP_TO_HANDLE(dip),
3790Sstevel@tonic-gate 	    sysino, intr_state)) != H_EOK) {
3800Sstevel@tonic-gate 		DBG(DBG_LIB_INT, dip, "hvio_intr_getstate failed, ret 0x%lx\n",
3810Sstevel@tonic-gate 		    ret);
3820Sstevel@tonic-gate 		return (DDI_FAILURE);
3830Sstevel@tonic-gate 	}
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_getstate: intr_state 0x%x\n",
3860Sstevel@tonic-gate 	    *intr_state);
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	return (DDI_SUCCESS);
3890Sstevel@tonic-gate }
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate /*ARGSUSED*/
3920Sstevel@tonic-gate int
3930Sstevel@tonic-gate px_lib_intr_setstate(dev_info_t *dip, sysino_t sysino,
3940Sstevel@tonic-gate     intr_state_t intr_state)
3950Sstevel@tonic-gate {
3960Sstevel@tonic-gate 	uint64_t	ret;
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_setstate: dip 0x%p sysino 0x%llx "
3990Sstevel@tonic-gate 	    "intr_state 0x%x\n", dip, sysino, intr_state);
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 	if ((ret = hvio_intr_setstate(DIP_TO_HANDLE(dip),
4020Sstevel@tonic-gate 	    sysino, intr_state)) != H_EOK) {
4030Sstevel@tonic-gate 		DBG(DBG_LIB_INT, dip, "hvio_intr_setstate failed, ret 0x%lx\n",
4040Sstevel@tonic-gate 		    ret);
4050Sstevel@tonic-gate 		return (DDI_FAILURE);
4060Sstevel@tonic-gate 	}
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	return (DDI_SUCCESS);
4090Sstevel@tonic-gate }
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate /*ARGSUSED*/
4120Sstevel@tonic-gate int
4130Sstevel@tonic-gate px_lib_intr_gettarget(dev_info_t *dip, sysino_t sysino, cpuid_t *cpuid)
4140Sstevel@tonic-gate {
4151772Sjl139090 	px_t		*px_p = DIP_TO_STATE(dip);
4161772Sjl139090 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
4170Sstevel@tonic-gate 	uint64_t	ret;
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_gettarget: dip 0x%p sysino 0x%llx\n",
4200Sstevel@tonic-gate 	    dip, sysino);
4210Sstevel@tonic-gate 
4221772Sjl139090 	if ((ret = hvio_intr_gettarget(DIP_TO_HANDLE(dip), pxu_p,
4230Sstevel@tonic-gate 	    sysino, cpuid)) != H_EOK) {
4240Sstevel@tonic-gate 		DBG(DBG_LIB_INT, dip, "hvio_intr_gettarget failed, ret 0x%lx\n",
4250Sstevel@tonic-gate 		    ret);
4260Sstevel@tonic-gate 		return (DDI_FAILURE);
4270Sstevel@tonic-gate 	}
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_gettarget: cpuid 0x%x\n", cpuid);
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	return (DDI_SUCCESS);
4320Sstevel@tonic-gate }
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate /*ARGSUSED*/
4350Sstevel@tonic-gate int
4360Sstevel@tonic-gate px_lib_intr_settarget(dev_info_t *dip, sysino_t sysino, cpuid_t cpuid)
4370Sstevel@tonic-gate {
4381772Sjl139090 	px_t		*px_p = DIP_TO_STATE(dip);
4391772Sjl139090 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
4400Sstevel@tonic-gate 	uint64_t	ret;
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_settarget: dip 0x%p sysino 0x%llx "
4430Sstevel@tonic-gate 	    "cpuid 0x%x\n", dip, sysino, cpuid);
4440Sstevel@tonic-gate 
4451772Sjl139090 	if ((ret = hvio_intr_settarget(DIP_TO_HANDLE(dip), pxu_p,
4460Sstevel@tonic-gate 	    sysino, cpuid)) != H_EOK) {
4470Sstevel@tonic-gate 		DBG(DBG_LIB_INT, dip, "hvio_intr_settarget failed, ret 0x%lx\n",
4480Sstevel@tonic-gate 		    ret);
4490Sstevel@tonic-gate 		return (DDI_FAILURE);
4500Sstevel@tonic-gate 	}
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	return (DDI_SUCCESS);
4530Sstevel@tonic-gate }
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate /*ARGSUSED*/
4560Sstevel@tonic-gate int
4570Sstevel@tonic-gate px_lib_intr_reset(dev_info_t *dip)
4580Sstevel@tonic-gate {
4590Sstevel@tonic-gate 	devino_t	ino;
4600Sstevel@tonic-gate 	sysino_t	sysino;
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_reset: dip 0x%p\n", dip);
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	/* Reset all Interrupts */
4650Sstevel@tonic-gate 	for (ino = 0; ino < INTERRUPT_MAPPING_ENTRIES; ino++) {
4660Sstevel@tonic-gate 		if (px_lib_intr_devino_to_sysino(dip, ino,
4670Sstevel@tonic-gate 		    &sysino) != DDI_SUCCESS)
4680Sstevel@tonic-gate 			return (BF_FATAL);
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 		if (px_lib_intr_setstate(dip, sysino,
4710Sstevel@tonic-gate 		    INTR_IDLE_STATE) != DDI_SUCCESS)
4720Sstevel@tonic-gate 			return (BF_FATAL);
4730Sstevel@tonic-gate 	}
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	return (BF_NONE);
4760Sstevel@tonic-gate }
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate /*ARGSUSED*/
4790Sstevel@tonic-gate int
4800Sstevel@tonic-gate px_lib_iommu_map(dev_info_t *dip, tsbid_t tsbid, pages_t pages,
4811617Sgovinda     io_attributes_t attr, void *addr, size_t pfn_index, int flags)
4820Sstevel@tonic-gate {
4830Sstevel@tonic-gate 	px_t		*px_p = DIP_TO_STATE(dip);
4840Sstevel@tonic-gate 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
4850Sstevel@tonic-gate 	uint64_t	ret;
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_map: dip 0x%p tsbid 0x%llx "
4881617Sgovinda 	    "pages 0x%x attr 0x%x addr 0x%p pfn_index 0x%llx flags 0x%x\n",
4891617Sgovinda 	    dip, tsbid, pages, attr, addr, pfn_index, flags);
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	if ((ret = hvio_iommu_map(px_p->px_dev_hdl, pxu_p, tsbid, pages,
4921617Sgovinda 	    attr, addr, pfn_index, flags)) != H_EOK) {
4930Sstevel@tonic-gate 		DBG(DBG_LIB_DMA, dip,
4940Sstevel@tonic-gate 		    "px_lib_iommu_map failed, ret 0x%lx\n", ret);
4950Sstevel@tonic-gate 		return (DDI_FAILURE);
4960Sstevel@tonic-gate 	}
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	return (DDI_SUCCESS);
4990Sstevel@tonic-gate }
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate /*ARGSUSED*/
5020Sstevel@tonic-gate int
5030Sstevel@tonic-gate px_lib_iommu_demap(dev_info_t *dip, tsbid_t tsbid, pages_t pages)
5040Sstevel@tonic-gate {
5050Sstevel@tonic-gate 	px_t		*px_p = DIP_TO_STATE(dip);
5060Sstevel@tonic-gate 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
5070Sstevel@tonic-gate 	uint64_t	ret;
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_demap: dip 0x%p tsbid 0x%llx "
5100Sstevel@tonic-gate 	    "pages 0x%x\n", dip, tsbid, pages);
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	if ((ret = hvio_iommu_demap(px_p->px_dev_hdl, pxu_p, tsbid, pages))
5130Sstevel@tonic-gate 	    != H_EOK) {
5140Sstevel@tonic-gate 		DBG(DBG_LIB_DMA, dip,
5150Sstevel@tonic-gate 		    "px_lib_iommu_demap failed, ret 0x%lx\n", ret);
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 		return (DDI_FAILURE);
5180Sstevel@tonic-gate 	}
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	return (DDI_SUCCESS);
5210Sstevel@tonic-gate }
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate /*ARGSUSED*/
5240Sstevel@tonic-gate int
5251617Sgovinda px_lib_iommu_getmap(dev_info_t *dip, tsbid_t tsbid, io_attributes_t *attr_p,
5261617Sgovinda     r_addr_t *r_addr_p)
5270Sstevel@tonic-gate {
5280Sstevel@tonic-gate 	px_t	*px_p = DIP_TO_STATE(dip);
5290Sstevel@tonic-gate 	pxu_t	*pxu_p = (pxu_t *)px_p->px_plat_p;
5300Sstevel@tonic-gate 	uint64_t	ret;
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_getmap: dip 0x%p tsbid 0x%llx\n",
5330Sstevel@tonic-gate 	    dip, tsbid);
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	if ((ret = hvio_iommu_getmap(DIP_TO_HANDLE(dip), pxu_p, tsbid,
5361617Sgovinda 	    attr_p, r_addr_p)) != H_EOK) {
5370Sstevel@tonic-gate 		DBG(DBG_LIB_DMA, dip,
5380Sstevel@tonic-gate 		    "hvio_iommu_getmap failed, ret 0x%lx\n", ret);
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 		return ((ret == H_ENOMAP) ? DDI_DMA_NOMAPPING:DDI_FAILURE);
5410Sstevel@tonic-gate 	}
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_getmap: attr 0x%x r_addr 0x%llx\n",
5441617Sgovinda 	    *attr_p, *r_addr_p);
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 	return (DDI_SUCCESS);
5470Sstevel@tonic-gate }
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate /*
5510Sstevel@tonic-gate  * Checks dma attributes against system bypass ranges
5520Sstevel@tonic-gate  * The bypass range is determined by the hardware. Return them so the
5530Sstevel@tonic-gate  * common code can do generic checking against them.
5540Sstevel@tonic-gate  */
5550Sstevel@tonic-gate /*ARGSUSED*/
5560Sstevel@tonic-gate int
5571772Sjl139090 px_lib_dma_bypass_rngchk(dev_info_t *dip, ddi_dma_attr_t *attr_p,
5581772Sjl139090     uint64_t *lo_p, uint64_t *hi_p)
5590Sstevel@tonic-gate {
5601772Sjl139090 	px_t	*px_p = DIP_TO_STATE(dip);
5611772Sjl139090 	pxu_t	*pxu_p = (pxu_t *)px_p->px_plat_p;
5621772Sjl139090 
5631772Sjl139090 	*lo_p = hvio_get_bypass_base(pxu_p);
5641772Sjl139090 	*hi_p = hvio_get_bypass_end(pxu_p);
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	return (DDI_SUCCESS);
5670Sstevel@tonic-gate }
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate /*ARGSUSED*/
5710Sstevel@tonic-gate int
5721617Sgovinda px_lib_iommu_getbypass(dev_info_t *dip, r_addr_t ra, io_attributes_t attr,
5731617Sgovinda     io_addr_t *io_addr_p)
5740Sstevel@tonic-gate {
5750Sstevel@tonic-gate 	uint64_t	ret;
5761772Sjl139090 	px_t	*px_p = DIP_TO_STATE(dip);
5771772Sjl139090 	pxu_t	*pxu_p = (pxu_t *)px_p->px_plat_p;
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_getbypass: dip 0x%p ra 0x%llx "
5801617Sgovinda 	    "attr 0x%x\n", dip, ra, attr);
5810Sstevel@tonic-gate 
5821772Sjl139090 	if ((ret = hvio_iommu_getbypass(DIP_TO_HANDLE(dip), pxu_p, ra,
5831772Sjl139090 	    attr, io_addr_p)) != H_EOK) {
5840Sstevel@tonic-gate 		DBG(DBG_LIB_DMA, dip,
5850Sstevel@tonic-gate 		    "hvio_iommu_getbypass failed, ret 0x%lx\n", ret);
5860Sstevel@tonic-gate 		return (DDI_FAILURE);
5870Sstevel@tonic-gate 	}
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_getbypass: io_addr 0x%llx\n",
5900Sstevel@tonic-gate 	    *io_addr_p);
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	return (DDI_SUCCESS);
5930Sstevel@tonic-gate }
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate /*
5960Sstevel@tonic-gate  * bus dma sync entry point.
5970Sstevel@tonic-gate  */
5980Sstevel@tonic-gate /*ARGSUSED*/
5990Sstevel@tonic-gate int
6000Sstevel@tonic-gate px_lib_dma_sync(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle,
6011617Sgovinda     off_t off, size_t len, uint_t cache_flags)
6020Sstevel@tonic-gate {
6030Sstevel@tonic-gate 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
6041772Sjl139090 	px_t	*px_p = DIP_TO_STATE(dip);
6051772Sjl139090 	pxu_t	*pxu_p = (pxu_t *)px_p->px_plat_p;
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	DBG(DBG_LIB_DMA, dip, "px_lib_dma_sync: dip 0x%p rdip 0x%p "
6080Sstevel@tonic-gate 	    "handle 0x%llx off 0x%x len 0x%x flags 0x%x\n",
6090Sstevel@tonic-gate 	    dip, rdip, handle, off, len, cache_flags);
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	/*
6121772Sjl139090 	 * No flush needed for Oberon
6131772Sjl139090 	 */
6141772Sjl139090 	if (PX_CHIP_TYPE(pxu_p) == PX_CHIP_OBERON)
6151772Sjl139090 		return (DDI_SUCCESS);
6161772Sjl139090 
6171772Sjl139090 	/*
6180Sstevel@tonic-gate 	 * jbus_stst_order is found only in certain cpu modules.
6190Sstevel@tonic-gate 	 * Just return success if not present.
6200Sstevel@tonic-gate 	 */
6210Sstevel@tonic-gate 	if (&jbus_stst_order == NULL)
6220Sstevel@tonic-gate 		return (DDI_SUCCESS);
6230Sstevel@tonic-gate 
624909Segillett 	if (!(mp->dmai_flags & PX_DMAI_FLAGS_INUSE)) {
62527Sjchu 		cmn_err(CE_WARN, "%s%d: Unbound dma handle %p.",
62627Sjchu 		    ddi_driver_name(rdip), ddi_get_instance(rdip), (void *)mp);
62727Sjchu 
6280Sstevel@tonic-gate 		return (DDI_FAILURE);
6290Sstevel@tonic-gate 	}
6300Sstevel@tonic-gate 
631909Segillett 	if (mp->dmai_flags & PX_DMAI_FLAGS_NOSYNC)
6320Sstevel@tonic-gate 		return (DDI_SUCCESS);
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	/*
6350Sstevel@tonic-gate 	 * No flush needed when sending data from memory to device.
6360Sstevel@tonic-gate 	 * Nothing to do to "sync" memory to what device would already see.
6370Sstevel@tonic-gate 	 */
6380Sstevel@tonic-gate 	if (!(mp->dmai_rflags & DDI_DMA_READ) ||
6390Sstevel@tonic-gate 	    ((cache_flags & PX_DMA_SYNC_DDI_FLAGS) == DDI_DMA_SYNC_FORDEV))
6400Sstevel@tonic-gate 		return (DDI_SUCCESS);
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	/*
6430Sstevel@tonic-gate 	 * Perform necessary cpu workaround to ensure jbus ordering.
6440Sstevel@tonic-gate 	 * CPU's internal "invalidate FIFOs" are flushed.
6450Sstevel@tonic-gate 	 */
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate #if !defined(lint)
6480Sstevel@tonic-gate 	kpreempt_disable();
6490Sstevel@tonic-gate #endif
6500Sstevel@tonic-gate 	jbus_stst_order();
6510Sstevel@tonic-gate #if !defined(lint)
6520Sstevel@tonic-gate 	kpreempt_enable();
6530Sstevel@tonic-gate #endif
6540Sstevel@tonic-gate 	return (DDI_SUCCESS);
6550Sstevel@tonic-gate }
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate /*
6580Sstevel@tonic-gate  * MSIQ Functions:
6590Sstevel@tonic-gate  */
6600Sstevel@tonic-gate /*ARGSUSED*/
6610Sstevel@tonic-gate int
6620Sstevel@tonic-gate px_lib_msiq_init(dev_info_t *dip)
6630Sstevel@tonic-gate {
6640Sstevel@tonic-gate 	px_t		*px_p = DIP_TO_STATE(dip);
6650Sstevel@tonic-gate 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
6660Sstevel@tonic-gate 	px_msiq_state_t	*msiq_state_p = &px_p->px_ib_p->ib_msiq_state;
6670Sstevel@tonic-gate 	caddr_t		msiq_addr;
6680Sstevel@tonic-gate 	px_dvma_addr_t	pg_index;
6690Sstevel@tonic-gate 	size_t		size;
6700Sstevel@tonic-gate 	int		ret;
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_init: dip 0x%p\n", dip);
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 	/*
6750Sstevel@tonic-gate 	 * Map the EQ memory into the Fire MMU (has to be 512KB aligned)
6760Sstevel@tonic-gate 	 * and then initialize the base address register.
6770Sstevel@tonic-gate 	 *
6780Sstevel@tonic-gate 	 * Allocate entries from Fire IOMMU so that the resulting address
6790Sstevel@tonic-gate 	 * is properly aligned.  Calculate the index of the first allocated
6800Sstevel@tonic-gate 	 * entry.  Note: The size of the mapping is assumed to be a multiple
6810Sstevel@tonic-gate 	 * of the page size.
6820Sstevel@tonic-gate 	 */
6830Sstevel@tonic-gate 	msiq_addr = (caddr_t)(((uint64_t)msiq_state_p->msiq_buf_p +
6840Sstevel@tonic-gate 	    (MMU_PAGE_SIZE - 1)) >> MMU_PAGE_SHIFT << MMU_PAGE_SHIFT);
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 	size = msiq_state_p->msiq_cnt *
6870Sstevel@tonic-gate 	    msiq_state_p->msiq_rec_cnt * sizeof (msiq_rec_t);
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	pxu_p->msiq_mapped_p = vmem_xalloc(px_p->px_mmu_p->mmu_dvma_map,
6900Sstevel@tonic-gate 	    size, (512 * 1024), 0, 0, NULL, NULL, VM_NOSLEEP | VM_BESTFIT);
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	if (pxu_p->msiq_mapped_p == NULL)
6930Sstevel@tonic-gate 		return (DDI_FAILURE);
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	pg_index = MMU_PAGE_INDEX(px_p->px_mmu_p,
6960Sstevel@tonic-gate 	    MMU_BTOP((ulong_t)pxu_p->msiq_mapped_p));
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 	if ((ret = px_lib_iommu_map(px_p->px_dip, PCI_TSBID(0, pg_index),
6990Sstevel@tonic-gate 	    MMU_BTOP(size), PCI_MAP_ATTR_WRITE, (void *)msiq_addr, 0,
7000Sstevel@tonic-gate 	    MMU_MAP_BUF)) != DDI_SUCCESS) {
7010Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip,
7020Sstevel@tonic-gate 		    "hvio_msiq_init failed, ret 0x%lx\n", ret);
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 		(void) px_lib_msiq_fini(dip);
7050Sstevel@tonic-gate 		return (DDI_FAILURE);
7060Sstevel@tonic-gate 	}
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	(void) hvio_msiq_init(DIP_TO_HANDLE(dip), pxu_p);
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	return (DDI_SUCCESS);
7110Sstevel@tonic-gate }
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate /*ARGSUSED*/
7140Sstevel@tonic-gate int
7150Sstevel@tonic-gate px_lib_msiq_fini(dev_info_t *dip)
7160Sstevel@tonic-gate {
7170Sstevel@tonic-gate 	px_t		*px_p = DIP_TO_STATE(dip);
7180Sstevel@tonic-gate 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
7190Sstevel@tonic-gate 	px_msiq_state_t	*msiq_state_p = &px_p->px_ib_p->ib_msiq_state;
7200Sstevel@tonic-gate 	px_dvma_addr_t	pg_index;
7210Sstevel@tonic-gate 	size_t		size;
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_fini: dip 0x%p\n", dip);
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 	/*
7260Sstevel@tonic-gate 	 * Unmap and free the EQ memory that had been mapped
7270Sstevel@tonic-gate 	 * into the Fire IOMMU.
7280Sstevel@tonic-gate 	 */
7290Sstevel@tonic-gate 	size = msiq_state_p->msiq_cnt *
7300Sstevel@tonic-gate 	    msiq_state_p->msiq_rec_cnt * sizeof (msiq_rec_t);
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 	pg_index = MMU_PAGE_INDEX(px_p->px_mmu_p,
7330Sstevel@tonic-gate 	    MMU_BTOP((ulong_t)pxu_p->msiq_mapped_p));
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 	(void) px_lib_iommu_demap(px_p->px_dip,
7360Sstevel@tonic-gate 	    PCI_TSBID(0, pg_index), MMU_BTOP(size));
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 	/* Free the entries from the Fire MMU */
7390Sstevel@tonic-gate 	vmem_xfree(px_p->px_mmu_p->mmu_dvma_map,
7400Sstevel@tonic-gate 	    (void *)pxu_p->msiq_mapped_p, size);
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	return (DDI_SUCCESS);
7430Sstevel@tonic-gate }
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate /*ARGSUSED*/
7460Sstevel@tonic-gate int
7470Sstevel@tonic-gate px_lib_msiq_info(dev_info_t *dip, msiqid_t msiq_id, r_addr_t *ra_p,
7480Sstevel@tonic-gate     uint_t *msiq_rec_cnt_p)
7490Sstevel@tonic-gate {
7500Sstevel@tonic-gate 	px_t		*px_p = DIP_TO_STATE(dip);
7510Sstevel@tonic-gate 	px_msiq_state_t	*msiq_state_p = &px_p->px_ib_p->ib_msiq_state;
7520Sstevel@tonic-gate 	uint64_t	*msiq_addr;
7530Sstevel@tonic-gate 	size_t		msiq_size;
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_msiq_info: dip 0x%p msiq_id 0x%x\n",
7560Sstevel@tonic-gate 	    dip, msiq_id);
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 	msiq_addr = (uint64_t *)(((uint64_t)msiq_state_p->msiq_buf_p +
7590Sstevel@tonic-gate 	    (MMU_PAGE_SIZE - 1)) >> MMU_PAGE_SHIFT << MMU_PAGE_SHIFT);
7600Sstevel@tonic-gate 	msiq_size = msiq_state_p->msiq_rec_cnt * sizeof (msiq_rec_t);
7610Sstevel@tonic-gate 	ra_p = (r_addr_t *)((caddr_t)msiq_addr + (msiq_id * msiq_size));
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	*msiq_rec_cnt_p = msiq_state_p->msiq_rec_cnt;
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_msiq_info: ra_p 0x%p msiq_rec_cnt 0x%x\n",
7660Sstevel@tonic-gate 	    ra_p, *msiq_rec_cnt_p);
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 	return (DDI_SUCCESS);
7690Sstevel@tonic-gate }
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate /*ARGSUSED*/
7720Sstevel@tonic-gate int
7730Sstevel@tonic-gate px_lib_msiq_getvalid(dev_info_t *dip, msiqid_t msiq_id,
7740Sstevel@tonic-gate     pci_msiq_valid_state_t *msiq_valid_state)
7750Sstevel@tonic-gate {
7760Sstevel@tonic-gate 	uint64_t	ret;
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_getvalid: dip 0x%p msiq_id 0x%x\n",
7790Sstevel@tonic-gate 	    dip, msiq_id);
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	if ((ret = hvio_msiq_getvalid(DIP_TO_HANDLE(dip),
7820Sstevel@tonic-gate 	    msiq_id, msiq_valid_state)) != H_EOK) {
7830Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip,
7840Sstevel@tonic-gate 		    "hvio_msiq_getvalid failed, ret 0x%lx\n", ret);
7850Sstevel@tonic-gate 		return (DDI_FAILURE);
7860Sstevel@tonic-gate 	}
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_getvalid: msiq_valid_state 0x%x\n",
7890Sstevel@tonic-gate 	    *msiq_valid_state);
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 	return (DDI_SUCCESS);
7920Sstevel@tonic-gate }
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate /*ARGSUSED*/
7950Sstevel@tonic-gate int
7960Sstevel@tonic-gate px_lib_msiq_setvalid(dev_info_t *dip, msiqid_t msiq_id,
7970Sstevel@tonic-gate     pci_msiq_valid_state_t msiq_valid_state)
7980Sstevel@tonic-gate {
7990Sstevel@tonic-gate 	uint64_t	ret;
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_setvalid: dip 0x%p msiq_id 0x%x "
8020Sstevel@tonic-gate 	    "msiq_valid_state 0x%x\n", dip, msiq_id, msiq_valid_state);
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 	if ((ret = hvio_msiq_setvalid(DIP_TO_HANDLE(dip),
8050Sstevel@tonic-gate 	    msiq_id, msiq_valid_state)) != H_EOK) {
8060Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip,
8070Sstevel@tonic-gate 		    "hvio_msiq_setvalid failed, ret 0x%lx\n", ret);
8080Sstevel@tonic-gate 		return (DDI_FAILURE);
8090Sstevel@tonic-gate 	}
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 	return (DDI_SUCCESS);
8120Sstevel@tonic-gate }
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate /*ARGSUSED*/
8150Sstevel@tonic-gate int
8160Sstevel@tonic-gate px_lib_msiq_getstate(dev_info_t *dip, msiqid_t msiq_id,
8170Sstevel@tonic-gate     pci_msiq_state_t *msiq_state)
8180Sstevel@tonic-gate {
8190Sstevel@tonic-gate 	uint64_t	ret;
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_getstate: dip 0x%p msiq_id 0x%x\n",
8220Sstevel@tonic-gate 	    dip, msiq_id);
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	if ((ret = hvio_msiq_getstate(DIP_TO_HANDLE(dip),
8250Sstevel@tonic-gate 	    msiq_id, msiq_state)) != H_EOK) {
8260Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip,
8270Sstevel@tonic-gate 		    "hvio_msiq_getstate failed, ret 0x%lx\n", ret);
8280Sstevel@tonic-gate 		return (DDI_FAILURE);
8290Sstevel@tonic-gate 	}
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_getstate: msiq_state 0x%x\n",
8320Sstevel@tonic-gate 	    *msiq_state);
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 	return (DDI_SUCCESS);
8350Sstevel@tonic-gate }
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate /*ARGSUSED*/
8380Sstevel@tonic-gate int
8390Sstevel@tonic-gate px_lib_msiq_setstate(dev_info_t *dip, msiqid_t msiq_id,
8400Sstevel@tonic-gate     pci_msiq_state_t msiq_state)
8410Sstevel@tonic-gate {
8420Sstevel@tonic-gate 	uint64_t	ret;
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_setstate: dip 0x%p msiq_id 0x%x "
8450Sstevel@tonic-gate 	    "msiq_state 0x%x\n", dip, msiq_id, msiq_state);
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate 	if ((ret = hvio_msiq_setstate(DIP_TO_HANDLE(dip),
8480Sstevel@tonic-gate 	    msiq_id, msiq_state)) != H_EOK) {
8490Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip,
8500Sstevel@tonic-gate 		    "hvio_msiq_setstate failed, ret 0x%lx\n", ret);
8510Sstevel@tonic-gate 		return (DDI_FAILURE);
8520Sstevel@tonic-gate 	}
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 	return (DDI_SUCCESS);
8550Sstevel@tonic-gate }
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate /*ARGSUSED*/
8580Sstevel@tonic-gate int
8590Sstevel@tonic-gate px_lib_msiq_gethead(dev_info_t *dip, msiqid_t msiq_id,
8600Sstevel@tonic-gate     msiqhead_t *msiq_head)
8610Sstevel@tonic-gate {
8620Sstevel@tonic-gate 	uint64_t	ret;
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_gethead: dip 0x%p msiq_id 0x%x\n",
8650Sstevel@tonic-gate 	    dip, msiq_id);
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	if ((ret = hvio_msiq_gethead(DIP_TO_HANDLE(dip),
8680Sstevel@tonic-gate 	    msiq_id, msiq_head)) != H_EOK) {
8690Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip,
8700Sstevel@tonic-gate 		    "hvio_msiq_gethead failed, ret 0x%lx\n", ret);
8710Sstevel@tonic-gate 		return (DDI_FAILURE);
8720Sstevel@tonic-gate 	}
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_gethead: msiq_head 0x%x\n",
8750Sstevel@tonic-gate 	    *msiq_head);
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 	return (DDI_SUCCESS);
8780Sstevel@tonic-gate }
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate /*ARGSUSED*/
8810Sstevel@tonic-gate int
8820Sstevel@tonic-gate px_lib_msiq_sethead(dev_info_t *dip, msiqid_t msiq_id,
8830Sstevel@tonic-gate     msiqhead_t msiq_head)
8840Sstevel@tonic-gate {
8850Sstevel@tonic-gate 	uint64_t	ret;
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_sethead: dip 0x%p msiq_id 0x%x "
8880Sstevel@tonic-gate 	    "msiq_head 0x%x\n", dip, msiq_id, msiq_head);
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 	if ((ret = hvio_msiq_sethead(DIP_TO_HANDLE(dip),
8910Sstevel@tonic-gate 	    msiq_id, msiq_head)) != H_EOK) {
8920Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip,
8930Sstevel@tonic-gate 		    "hvio_msiq_sethead failed, ret 0x%lx\n", ret);
8940Sstevel@tonic-gate 		return (DDI_FAILURE);
8950Sstevel@tonic-gate 	}
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 	return (DDI_SUCCESS);
8980Sstevel@tonic-gate }
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate /*ARGSUSED*/
9010Sstevel@tonic-gate int
9020Sstevel@tonic-gate px_lib_msiq_gettail(dev_info_t *dip, msiqid_t msiq_id,
9030Sstevel@tonic-gate     msiqtail_t *msiq_tail)
9040Sstevel@tonic-gate {
9050Sstevel@tonic-gate 	uint64_t	ret;
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_gettail: dip 0x%p msiq_id 0x%x\n",
9080Sstevel@tonic-gate 	    dip, msiq_id);
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	if ((ret = hvio_msiq_gettail(DIP_TO_HANDLE(dip),
9110Sstevel@tonic-gate 	    msiq_id, msiq_tail)) != H_EOK) {
9120Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip,
9130Sstevel@tonic-gate 		    "hvio_msiq_gettail failed, ret 0x%lx\n", ret);
9140Sstevel@tonic-gate 		return (DDI_FAILURE);
9150Sstevel@tonic-gate 	}
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_gettail: msiq_tail 0x%x\n",
9180Sstevel@tonic-gate 	    *msiq_tail);
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 	return (DDI_SUCCESS);
9210Sstevel@tonic-gate }
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate /*ARGSUSED*/
9240Sstevel@tonic-gate void
925*2588Segillett px_lib_get_msiq_rec(dev_info_t *dip, msiqhead_t *msiq_head_p,
926*2588Segillett     msiq_rec_t *msiq_rec_p)
9270Sstevel@tonic-gate {
928*2588Segillett 	eq_rec_t	*eq_rec_p = (eq_rec_t *)msiq_head_p;
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_get_msiq_rec: dip 0x%p eq_rec_p 0x%p\n",
9310Sstevel@tonic-gate 	    dip, eq_rec_p);
9320Sstevel@tonic-gate 
933287Smg140465 	if (!eq_rec_p->eq_rec_fmt_type) {
934287Smg140465 		/* Set msiq_rec_type to zero */
935287Smg140465 		msiq_rec_p->msiq_rec_type = 0;
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 		return;
9380Sstevel@tonic-gate 	}
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_get_msiq_rec: EQ RECORD, "
9410Sstevel@tonic-gate 	    "eq_rec_rid 0x%llx eq_rec_fmt_type 0x%llx "
9420Sstevel@tonic-gate 	    "eq_rec_len 0x%llx eq_rec_addr0 0x%llx "
9430Sstevel@tonic-gate 	    "eq_rec_addr1 0x%llx eq_rec_data0 0x%llx "
9440Sstevel@tonic-gate 	    "eq_rec_data1 0x%llx\n", eq_rec_p->eq_rec_rid,
9450Sstevel@tonic-gate 	    eq_rec_p->eq_rec_fmt_type, eq_rec_p->eq_rec_len,
9460Sstevel@tonic-gate 	    eq_rec_p->eq_rec_addr0, eq_rec_p->eq_rec_addr1,
9470Sstevel@tonic-gate 	    eq_rec_p->eq_rec_data0, eq_rec_p->eq_rec_data1);
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate 	/*
9500Sstevel@tonic-gate 	 * Only upper 4 bits of eq_rec_fmt_type is used
9510Sstevel@tonic-gate 	 * to identify the EQ record type.
9520Sstevel@tonic-gate 	 */
9530Sstevel@tonic-gate 	switch (eq_rec_p->eq_rec_fmt_type >> 3) {
9540Sstevel@tonic-gate 	case EQ_REC_MSI32:
9550Sstevel@tonic-gate 		msiq_rec_p->msiq_rec_type = MSI32_REC;
9560Sstevel@tonic-gate 
957225Sess 		msiq_rec_p->msiq_rec_data.msi.msi_data =
958225Sess 		    eq_rec_p->eq_rec_data0;
9590Sstevel@tonic-gate 		break;
9600Sstevel@tonic-gate 	case EQ_REC_MSI64:
9610Sstevel@tonic-gate 		msiq_rec_p->msiq_rec_type = MSI64_REC;
9620Sstevel@tonic-gate 
963225Sess 		msiq_rec_p->msiq_rec_data.msi.msi_data =
964225Sess 		    eq_rec_p->eq_rec_data0;
9650Sstevel@tonic-gate 		break;
9660Sstevel@tonic-gate 	case EQ_REC_MSG:
9670Sstevel@tonic-gate 		msiq_rec_p->msiq_rec_type = MSG_REC;
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 		msiq_rec_p->msiq_rec_data.msg.msg_route =
9700Sstevel@tonic-gate 		    eq_rec_p->eq_rec_fmt_type & 7;
9710Sstevel@tonic-gate 		msiq_rec_p->msiq_rec_data.msg.msg_targ = eq_rec_p->eq_rec_rid;
9720Sstevel@tonic-gate 		msiq_rec_p->msiq_rec_data.msg.msg_code = eq_rec_p->eq_rec_data0;
9730Sstevel@tonic-gate 		break;
9740Sstevel@tonic-gate 	default:
9750Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: px_lib_get_msiq_rec: "
976671Skrishnae 		    "0x%x is an unknown EQ record type",
9770Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip),
978671Skrishnae 		    (int)eq_rec_p->eq_rec_fmt_type);
9790Sstevel@tonic-gate 		break;
9800Sstevel@tonic-gate 	}
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate 	msiq_rec_p->msiq_rec_rid = eq_rec_p->eq_rec_rid;
9830Sstevel@tonic-gate 	msiq_rec_p->msiq_rec_msi_addr = ((eq_rec_p->eq_rec_addr1 << 16) |
9840Sstevel@tonic-gate 	    (eq_rec_p->eq_rec_addr0 << 2));
9850Sstevel@tonic-gate 
986287Smg140465 	/* Zero out eq_rec_fmt_type field */
987287Smg140465 	eq_rec_p->eq_rec_fmt_type = 0;
9880Sstevel@tonic-gate }
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate /*
9910Sstevel@tonic-gate  * MSI Functions:
9920Sstevel@tonic-gate  */
9930Sstevel@tonic-gate /*ARGSUSED*/
9940Sstevel@tonic-gate int
9950Sstevel@tonic-gate px_lib_msi_init(dev_info_t *dip)
9960Sstevel@tonic-gate {
9970Sstevel@tonic-gate 	px_t		*px_p = DIP_TO_STATE(dip);
9980Sstevel@tonic-gate 	px_msi_state_t	*msi_state_p = &px_p->px_ib_p->ib_msi_state;
9990Sstevel@tonic-gate 	uint64_t	ret;
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_init: dip 0x%p\n", dip);
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 	if ((ret = hvio_msi_init(DIP_TO_HANDLE(dip),
10040Sstevel@tonic-gate 	    msi_state_p->msi_addr32, msi_state_p->msi_addr64)) != H_EOK) {
10050Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip, "px_lib_msi_init failed, ret 0x%lx\n",
10060Sstevel@tonic-gate 		    ret);
10070Sstevel@tonic-gate 		return (DDI_FAILURE);
10080Sstevel@tonic-gate 	}
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 	return (DDI_SUCCESS);
10110Sstevel@tonic-gate }
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate /*ARGSUSED*/
10140Sstevel@tonic-gate int
10150Sstevel@tonic-gate px_lib_msi_getmsiq(dev_info_t *dip, msinum_t msi_num,
10160Sstevel@tonic-gate     msiqid_t *msiq_id)
10170Sstevel@tonic-gate {
10180Sstevel@tonic-gate 	uint64_t	ret;
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getmsiq: dip 0x%p msi_num 0x%x\n",
10210Sstevel@tonic-gate 	    dip, msi_num);
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 	if ((ret = hvio_msi_getmsiq(DIP_TO_HANDLE(dip),
10240Sstevel@tonic-gate 	    msi_num, msiq_id)) != H_EOK) {
10250Sstevel@tonic-gate 		DBG(DBG_LIB_MSI, dip,
10260Sstevel@tonic-gate 		    "hvio_msi_getmsiq failed, ret 0x%lx\n", ret);
10270Sstevel@tonic-gate 		return (DDI_FAILURE);
10280Sstevel@tonic-gate 	}
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getmsiq: msiq_id 0x%x\n",
10310Sstevel@tonic-gate 	    *msiq_id);
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate 	return (DDI_SUCCESS);
10340Sstevel@tonic-gate }
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate /*ARGSUSED*/
10370Sstevel@tonic-gate int
10380Sstevel@tonic-gate px_lib_msi_setmsiq(dev_info_t *dip, msinum_t msi_num,
10390Sstevel@tonic-gate     msiqid_t msiq_id, msi_type_t msitype)
10400Sstevel@tonic-gate {
10410Sstevel@tonic-gate 	uint64_t	ret;
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_setmsiq: dip 0x%p msi_num 0x%x "
10440Sstevel@tonic-gate 	    "msq_id 0x%x\n", dip, msi_num, msiq_id);
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate 	if ((ret = hvio_msi_setmsiq(DIP_TO_HANDLE(dip),
10470Sstevel@tonic-gate 	    msi_num, msiq_id)) != H_EOK) {
10480Sstevel@tonic-gate 		DBG(DBG_LIB_MSI, dip,
10490Sstevel@tonic-gate 		    "hvio_msi_setmsiq failed, ret 0x%lx\n", ret);
10500Sstevel@tonic-gate 		return (DDI_FAILURE);
10510Sstevel@tonic-gate 	}
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 	return (DDI_SUCCESS);
10540Sstevel@tonic-gate }
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate /*ARGSUSED*/
10570Sstevel@tonic-gate int
10580Sstevel@tonic-gate px_lib_msi_getvalid(dev_info_t *dip, msinum_t msi_num,
10590Sstevel@tonic-gate     pci_msi_valid_state_t *msi_valid_state)
10600Sstevel@tonic-gate {
10610Sstevel@tonic-gate 	uint64_t	ret;
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getvalid: dip 0x%p msi_num 0x%x\n",
10640Sstevel@tonic-gate 	    dip, msi_num);
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 	if ((ret = hvio_msi_getvalid(DIP_TO_HANDLE(dip),
10670Sstevel@tonic-gate 	    msi_num, msi_valid_state)) != H_EOK) {
10680Sstevel@tonic-gate 		DBG(DBG_LIB_MSI, dip,
10690Sstevel@tonic-gate 		    "hvio_msi_getvalid failed, ret 0x%lx\n", ret);
10700Sstevel@tonic-gate 		return (DDI_FAILURE);
10710Sstevel@tonic-gate 	}
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getvalid: msiq_id 0x%x\n",
10740Sstevel@tonic-gate 	    *msi_valid_state);
10750Sstevel@tonic-gate 
10760Sstevel@tonic-gate 	return (DDI_SUCCESS);
10770Sstevel@tonic-gate }
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate /*ARGSUSED*/
10800Sstevel@tonic-gate int
10810Sstevel@tonic-gate px_lib_msi_setvalid(dev_info_t *dip, msinum_t msi_num,
10820Sstevel@tonic-gate     pci_msi_valid_state_t msi_valid_state)
10830Sstevel@tonic-gate {
10840Sstevel@tonic-gate 	uint64_t	ret;
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_setvalid: dip 0x%p msi_num 0x%x "
10870Sstevel@tonic-gate 	    "msi_valid_state 0x%x\n", dip, msi_num, msi_valid_state);
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate 	if ((ret = hvio_msi_setvalid(DIP_TO_HANDLE(dip),
10900Sstevel@tonic-gate 	    msi_num, msi_valid_state)) != H_EOK) {
10910Sstevel@tonic-gate 		DBG(DBG_LIB_MSI, dip,
10920Sstevel@tonic-gate 		    "hvio_msi_setvalid failed, ret 0x%lx\n", ret);
10930Sstevel@tonic-gate 		return (DDI_FAILURE);
10940Sstevel@tonic-gate 	}
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 	return (DDI_SUCCESS);
10970Sstevel@tonic-gate }
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate /*ARGSUSED*/
11000Sstevel@tonic-gate int
11010Sstevel@tonic-gate px_lib_msi_getstate(dev_info_t *dip, msinum_t msi_num,
11020Sstevel@tonic-gate     pci_msi_state_t *msi_state)
11030Sstevel@tonic-gate {
11040Sstevel@tonic-gate 	uint64_t	ret;
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getstate: dip 0x%p msi_num 0x%x\n",
11070Sstevel@tonic-gate 	    dip, msi_num);
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 	if ((ret = hvio_msi_getstate(DIP_TO_HANDLE(dip),
11100Sstevel@tonic-gate 	    msi_num, msi_state)) != H_EOK) {
11110Sstevel@tonic-gate 		DBG(DBG_LIB_MSI, dip,
11120Sstevel@tonic-gate 		    "hvio_msi_getstate failed, ret 0x%lx\n", ret);
11130Sstevel@tonic-gate 		return (DDI_FAILURE);
11140Sstevel@tonic-gate 	}
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getstate: msi_state 0x%x\n",
11170Sstevel@tonic-gate 	    *msi_state);
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 	return (DDI_SUCCESS);
11200Sstevel@tonic-gate }
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate /*ARGSUSED*/
11230Sstevel@tonic-gate int
11240Sstevel@tonic-gate px_lib_msi_setstate(dev_info_t *dip, msinum_t msi_num,
11250Sstevel@tonic-gate     pci_msi_state_t msi_state)
11260Sstevel@tonic-gate {
11270Sstevel@tonic-gate 	uint64_t	ret;
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_setstate: dip 0x%p msi_num 0x%x "
11300Sstevel@tonic-gate 	    "msi_state 0x%x\n", dip, msi_num, msi_state);
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate 	if ((ret = hvio_msi_setstate(DIP_TO_HANDLE(dip),
11330Sstevel@tonic-gate 	    msi_num, msi_state)) != H_EOK) {
11340Sstevel@tonic-gate 		DBG(DBG_LIB_MSI, dip,
11350Sstevel@tonic-gate 		    "hvio_msi_setstate failed, ret 0x%lx\n", ret);
11360Sstevel@tonic-gate 		return (DDI_FAILURE);
11370Sstevel@tonic-gate 	}
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate 	return (DDI_SUCCESS);
11400Sstevel@tonic-gate }
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate /*
11430Sstevel@tonic-gate  * MSG Functions:
11440Sstevel@tonic-gate  */
11450Sstevel@tonic-gate /*ARGSUSED*/
11460Sstevel@tonic-gate int
11470Sstevel@tonic-gate px_lib_msg_getmsiq(dev_info_t *dip, pcie_msg_type_t msg_type,
11480Sstevel@tonic-gate     msiqid_t *msiq_id)
11490Sstevel@tonic-gate {
11500Sstevel@tonic-gate 	uint64_t	ret;
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 	DBG(DBG_LIB_MSG, dip, "px_lib_msg_getmsiq: dip 0x%p msg_type 0x%x\n",
11530Sstevel@tonic-gate 	    dip, msg_type);
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 	if ((ret = hvio_msg_getmsiq(DIP_TO_HANDLE(dip),
11560Sstevel@tonic-gate 	    msg_type, msiq_id)) != H_EOK) {
11570Sstevel@tonic-gate 		DBG(DBG_LIB_MSG, dip,
11580Sstevel@tonic-gate 		    "hvio_msg_getmsiq failed, ret 0x%lx\n", ret);
11590Sstevel@tonic-gate 		return (DDI_FAILURE);
11600Sstevel@tonic-gate 	}
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msg_getmsiq: msiq_id 0x%x\n",
11630Sstevel@tonic-gate 	    *msiq_id);
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate 	return (DDI_SUCCESS);
11660Sstevel@tonic-gate }
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate /*ARGSUSED*/
11690Sstevel@tonic-gate int
11700Sstevel@tonic-gate px_lib_msg_setmsiq(dev_info_t *dip, pcie_msg_type_t msg_type,
11710Sstevel@tonic-gate     msiqid_t msiq_id)
11720Sstevel@tonic-gate {
11730Sstevel@tonic-gate 	uint64_t	ret;
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate 	DBG(DBG_LIB_MSG, dip, "px_lib_msi_setstate: dip 0x%p msg_type 0x%x "
11760Sstevel@tonic-gate 	    "msiq_id 0x%x\n", dip, msg_type, msiq_id);
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 	if ((ret = hvio_msg_setmsiq(DIP_TO_HANDLE(dip),
11790Sstevel@tonic-gate 	    msg_type, msiq_id)) != H_EOK) {
11800Sstevel@tonic-gate 		DBG(DBG_LIB_MSG, dip,
11810Sstevel@tonic-gate 		    "hvio_msg_setmsiq failed, ret 0x%lx\n", ret);
11820Sstevel@tonic-gate 		return (DDI_FAILURE);
11830Sstevel@tonic-gate 	}
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate 	return (DDI_SUCCESS);
11860Sstevel@tonic-gate }
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate /*ARGSUSED*/
11890Sstevel@tonic-gate int
11900Sstevel@tonic-gate px_lib_msg_getvalid(dev_info_t *dip, pcie_msg_type_t msg_type,
11910Sstevel@tonic-gate     pcie_msg_valid_state_t *msg_valid_state)
11920Sstevel@tonic-gate {
11930Sstevel@tonic-gate 	uint64_t	ret;
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 	DBG(DBG_LIB_MSG, dip, "px_lib_msg_getvalid: dip 0x%p msg_type 0x%x\n",
11960Sstevel@tonic-gate 	    dip, msg_type);
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate 	if ((ret = hvio_msg_getvalid(DIP_TO_HANDLE(dip), msg_type,
11990Sstevel@tonic-gate 	    msg_valid_state)) != H_EOK) {
12000Sstevel@tonic-gate 		DBG(DBG_LIB_MSG, dip,
12010Sstevel@tonic-gate 		    "hvio_msg_getvalid failed, ret 0x%lx\n", ret);
12020Sstevel@tonic-gate 		return (DDI_FAILURE);
12030Sstevel@tonic-gate 	}
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msg_getvalid: msg_valid_state 0x%x\n",
12060Sstevel@tonic-gate 	    *msg_valid_state);
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate 	return (DDI_SUCCESS);
12090Sstevel@tonic-gate }
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate /*ARGSUSED*/
12120Sstevel@tonic-gate int
12130Sstevel@tonic-gate px_lib_msg_setvalid(dev_info_t *dip, pcie_msg_type_t msg_type,
12140Sstevel@tonic-gate     pcie_msg_valid_state_t msg_valid_state)
12150Sstevel@tonic-gate {
12160Sstevel@tonic-gate 	uint64_t	ret;
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate 	DBG(DBG_LIB_MSG, dip, "px_lib_msg_setvalid: dip 0x%p msg_type 0x%x "
12190Sstevel@tonic-gate 	    "msg_valid_state 0x%x\n", dip, msg_type, msg_valid_state);
12200Sstevel@tonic-gate 
12210Sstevel@tonic-gate 	if ((ret = hvio_msg_setvalid(DIP_TO_HANDLE(dip), msg_type,
12220Sstevel@tonic-gate 	    msg_valid_state)) != H_EOK) {
12230Sstevel@tonic-gate 		DBG(DBG_LIB_MSG, dip,
12240Sstevel@tonic-gate 		    "hvio_msg_setvalid failed, ret 0x%lx\n", ret);
12250Sstevel@tonic-gate 		return (DDI_FAILURE);
12260Sstevel@tonic-gate 	}
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate 	return (DDI_SUCCESS);
12290Sstevel@tonic-gate }
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate /*
12320Sstevel@tonic-gate  * Suspend/Resume Functions:
12330Sstevel@tonic-gate  * Currently unsupported by hypervisor
12340Sstevel@tonic-gate  */
12350Sstevel@tonic-gate int
12360Sstevel@tonic-gate px_lib_suspend(dev_info_t *dip)
12370Sstevel@tonic-gate {
12380Sstevel@tonic-gate 	px_t		*px_p = DIP_TO_STATE(dip);
12390Sstevel@tonic-gate 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
12401648Sjchu 	px_cb_t		*cb_p = PX2CB(px_p);
12410Sstevel@tonic-gate 	devhandle_t	dev_hdl, xbus_dev_hdl;
12421648Sjchu 	uint64_t	ret = H_EOK;
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate 	DBG(DBG_DETACH, dip, "px_lib_suspend: dip 0x%p\n", dip);
12450Sstevel@tonic-gate 
124627Sjchu 	dev_hdl = (devhandle_t)pxu_p->px_address[PX_REG_CSR];
124727Sjchu 	xbus_dev_hdl = (devhandle_t)pxu_p->px_address[PX_REG_XBC];
12480Sstevel@tonic-gate 
12491648Sjchu 	if ((ret = hvio_suspend(dev_hdl, pxu_p)) != H_EOK)
12501648Sjchu 		goto fail;
12511648Sjchu 
12521648Sjchu 	if (--cb_p->attachcnt == 0) {
12531648Sjchu 		ret = hvio_cb_suspend(xbus_dev_hdl, pxu_p);
12541648Sjchu 		if (ret != H_EOK)
12551648Sjchu 			cb_p->attachcnt++;
12560Sstevel@tonic-gate 	}
12570Sstevel@tonic-gate 
12581648Sjchu fail:
12590Sstevel@tonic-gate 	return ((ret != H_EOK) ? DDI_FAILURE: DDI_SUCCESS);
12600Sstevel@tonic-gate }
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate void
12630Sstevel@tonic-gate px_lib_resume(dev_info_t *dip)
12640Sstevel@tonic-gate {
12650Sstevel@tonic-gate 	px_t		*px_p = DIP_TO_STATE(dip);
12660Sstevel@tonic-gate 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
12671648Sjchu 	px_cb_t		*cb_p = PX2CB(px_p);
12680Sstevel@tonic-gate 	devhandle_t	dev_hdl, xbus_dev_hdl;
12690Sstevel@tonic-gate 	devino_t	pec_ino = px_p->px_inos[PX_INTR_PEC];
12700Sstevel@tonic-gate 	devino_t	xbc_ino = px_p->px_inos[PX_INTR_XBC];
12710Sstevel@tonic-gate 
12720Sstevel@tonic-gate 	DBG(DBG_ATTACH, dip, "px_lib_resume: dip 0x%p\n", dip);
12730Sstevel@tonic-gate 
127427Sjchu 	dev_hdl = (devhandle_t)pxu_p->px_address[PX_REG_CSR];
127527Sjchu 	xbus_dev_hdl = (devhandle_t)pxu_p->px_address[PX_REG_XBC];
12760Sstevel@tonic-gate 
12771648Sjchu 	if (++cb_p->attachcnt == 1)
12780Sstevel@tonic-gate 		hvio_cb_resume(dev_hdl, xbus_dev_hdl, xbc_ino, pxu_p);
12790Sstevel@tonic-gate 
12801648Sjchu 	hvio_resume(dev_hdl, pec_ino, pxu_p);
12810Sstevel@tonic-gate }
12820Sstevel@tonic-gate 
12831772Sjl139090 /*
12841772Sjl139090  * Generate a unique Oberon UBC ID based on the Logicial System Board and
12851772Sjl139090  * the IO Channel from the portid property field.
12861772Sjl139090  */
12871772Sjl139090 static uint64_t
12881772Sjl139090 oberon_get_ubc_id(dev_info_t *dip)
12891772Sjl139090 {
12901772Sjl139090 	px_t	*px_p = DIP_TO_STATE(dip);
12911772Sjl139090 	pxu_t	*pxu_p = (pxu_t *)px_p->px_plat_p;
12921772Sjl139090 	uint64_t	ubc_id;
12931772Sjl139090 
12941772Sjl139090 	/*
12951772Sjl139090 	 * Generate a unique 6 bit UBC ID using the 2 IO_Channel#[1:0] bits and
12961772Sjl139090 	 * the 4 LSB_ID[3:0] bits from the Oberon's portid property.
12971772Sjl139090 	 */
12981772Sjl139090 	ubc_id = (((pxu_p->portid >> OBERON_PORT_ID_IOC) &
12991772Sjl139090 	    OBERON_PORT_ID_IOC_MASK) | (((pxu_p->portid >>
13001772Sjl139090 	    OBERON_PORT_ID_LSB) & OBERON_PORT_ID_LSB_MASK)
13011772Sjl139090 	    << OBERON_UBC_ID_LSB));
13021772Sjl139090 
13031772Sjl139090 	return (ubc_id);
13041772Sjl139090 }
13051772Sjl139090 
13061772Sjl139090 /*
13071772Sjl139090  * Oberon does not have a UBC scratch register, so alloc an array of scratch
13081772Sjl139090  * registers when needed and use a unique UBC ID as an index. This code
13091772Sjl139090  * can be simplified if we use a pre-allocated array. They are currently
13101772Sjl139090  * being dynamically allocated because it's only needed by the Oberon.
13111772Sjl139090  */
13121772Sjl139090 static void
13131772Sjl139090 oberon_set_cb(dev_info_t *dip, uint64_t val)
13141772Sjl139090 {
13151772Sjl139090 	uint64_t	ubc_id;
13161772Sjl139090 
13171772Sjl139090 	if (px_oberon_ubc_scratch_regs == NULL)
13181772Sjl139090 		px_oberon_ubc_scratch_regs =
13191772Sjl139090 		    (uint64_t *)kmem_zalloc(sizeof (uint64_t)*
13201772Sjl139090 		    OBERON_UBC_ID_MAX, KM_SLEEP);
13211772Sjl139090 
13221772Sjl139090 	ubc_id = oberon_get_ubc_id(dip);
13231772Sjl139090 
13241772Sjl139090 	px_oberon_ubc_scratch_regs[ubc_id] = val;
13251772Sjl139090 
13261772Sjl139090 	/*
13271772Sjl139090 	 * Check if any scratch registers are still in use. If all scratch
13281772Sjl139090 	 * registers are currently set to zero, then deallocate the scratch
13291772Sjl139090 	 * register array.
13301772Sjl139090 	 */
13311772Sjl139090 	for (ubc_id = 0; ubc_id < OBERON_UBC_ID_MAX; ubc_id++) {
13321772Sjl139090 		if (px_oberon_ubc_scratch_regs[ubc_id] != NULL)
13331772Sjl139090 			return;
13341772Sjl139090 	}
13351772Sjl139090 
13361772Sjl139090 	/*
13371772Sjl139090 	 * All scratch registers are set to zero so deallocate the scratch
13381772Sjl139090 	 * register array and set the pointer to NULL.
13391772Sjl139090 	 */
13401772Sjl139090 	kmem_free(px_oberon_ubc_scratch_regs,
13411772Sjl139090 	    (sizeof (uint64_t)*OBERON_UBC_ID_MAX));
13421772Sjl139090 
13431772Sjl139090 	px_oberon_ubc_scratch_regs = NULL;
13441772Sjl139090 }
13451772Sjl139090 
13461772Sjl139090 /*
13471772Sjl139090  * Oberon does not have a UBC scratch register, so use an allocated array of
13481772Sjl139090  * scratch registers and use the unique UBC ID as an index into that array.
13491772Sjl139090  */
13501772Sjl139090 static uint64_t
13511772Sjl139090 oberon_get_cb(dev_info_t *dip)
13521772Sjl139090 {
13531772Sjl139090 	uint64_t	ubc_id;
13541772Sjl139090 
13551772Sjl139090 	if (px_oberon_ubc_scratch_regs == NULL)
13561772Sjl139090 		return (0);
13571772Sjl139090 
13581772Sjl139090 	ubc_id = oberon_get_ubc_id(dip);
13591772Sjl139090 
13601772Sjl139090 	return (px_oberon_ubc_scratch_regs[ubc_id]);
13611772Sjl139090 }
13621772Sjl139090 
13631772Sjl139090 /*
13641772Sjl139090  * Misc Functions:
13651772Sjl139090  * Currently unsupported by hypervisor
13661772Sjl139090  */
13671772Sjl139090 static uint64_t
13681772Sjl139090 px_get_cb(dev_info_t *dip)
13691772Sjl139090 {
13701772Sjl139090 	px_t	*px_p = DIP_TO_STATE(dip);
13711772Sjl139090 	pxu_t	*pxu_p = (pxu_t *)px_p->px_plat_p;
13721772Sjl139090 
13731772Sjl139090 	/*
13741772Sjl139090 	 * Oberon does not currently have Scratchpad registers.
13751772Sjl139090 	 */
13761772Sjl139090 	if (PX_CHIP_TYPE(pxu_p) == PX_CHIP_OBERON)
13771772Sjl139090 		return (oberon_get_cb(dip));
13781772Sjl139090 
13791772Sjl139090 	return (CSR_XR((caddr_t)pxu_p->px_address[PX_REG_XBC], JBUS_SCRATCH_1));
13801772Sjl139090 }
13811772Sjl139090 
13821772Sjl139090 static void
13831772Sjl139090 px_set_cb(dev_info_t *dip, uint64_t val)
13841772Sjl139090 {
13851772Sjl139090 	px_t	*px_p = DIP_TO_STATE(dip);
13861772Sjl139090 	pxu_t	*pxu_p = (pxu_t *)px_p->px_plat_p;
13871772Sjl139090 
13881772Sjl139090 	/*
13891772Sjl139090 	 * Oberon does not currently have Scratchpad registers.
13901772Sjl139090 	 */
13911772Sjl139090 	if (PX_CHIP_TYPE(pxu_p) == PX_CHIP_OBERON) {
13921772Sjl139090 		oberon_set_cb(dip, val);
13931772Sjl139090 		return;
13941772Sjl139090 	}
13951772Sjl139090 
13961772Sjl139090 	CSR_XS((caddr_t)pxu_p->px_address[PX_REG_XBC], JBUS_SCRATCH_1, val);
13971772Sjl139090 }
13981772Sjl139090 
13990Sstevel@tonic-gate /*ARGSUSED*/
14000Sstevel@tonic-gate int
14010Sstevel@tonic-gate px_lib_map_vconfig(dev_info_t *dip,
14020Sstevel@tonic-gate 	ddi_map_req_t *mp, pci_config_offset_t off,
14030Sstevel@tonic-gate 		pci_regspec_t *rp, caddr_t *addrp)
14040Sstevel@tonic-gate {
14050Sstevel@tonic-gate 	/*
14060Sstevel@tonic-gate 	 * No special config space access services in this layer.
14070Sstevel@tonic-gate 	 */
14080Sstevel@tonic-gate 	return (DDI_FAILURE);
14090Sstevel@tonic-gate }
14100Sstevel@tonic-gate 
1411624Sschwartz void
1412677Sjchu px_lib_map_attr_check(ddi_map_req_t *mp)
1413677Sjchu {
1414677Sjchu 	ddi_acc_hdl_t *hp = mp->map_handlep;
1415677Sjchu 
1416677Sjchu 	/* fire does not accept byte masks from PIO store merge */
1417677Sjchu 	if (hp->ah_acc.devacc_attr_dataorder == DDI_STORECACHING_OK_ACC)
1418677Sjchu 		hp->ah_acc.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
1419677Sjchu }
1420677Sjchu 
1421677Sjchu void
1422624Sschwartz px_lib_clr_errs(px_t *px_p)
142327Sjchu {
1424624Sschwartz 	px_pec_t	*pec_p = px_p->px_pec_p;
142527Sjchu 	dev_info_t	*rpdip = px_p->px_dip;
142627Sjchu 	int		err = PX_OK, ret;
142727Sjchu 	int		acctype = pec_p->pec_safeacc_type;
142827Sjchu 	ddi_fm_error_t	derr;
142927Sjchu 
143027Sjchu 	/* Create the derr */
143127Sjchu 	bzero(&derr, sizeof (ddi_fm_error_t));
143227Sjchu 	derr.fme_version = DDI_FME_VERSION;
143327Sjchu 	derr.fme_ena = fm_ena_generate(0, FM_ENA_FMT1);
143427Sjchu 	derr.fme_flag = acctype;
143527Sjchu 
143627Sjchu 	if (acctype == DDI_FM_ERR_EXPECTED) {
143727Sjchu 		derr.fme_status = DDI_FM_NONFATAL;
143827Sjchu 		ndi_fm_acc_err_set(pec_p->pec_acc_hdl, &derr);
143927Sjchu 	}
144027Sjchu 
14411648Sjchu 	mutex_enter(&px_p->px_fm_mutex);
144227Sjchu 
144327Sjchu 	/* send ereport/handle/clear fire registers */
144427Sjchu 	err = px_err_handle(px_p, &derr, PX_LIB_CALL, B_TRUE);
144527Sjchu 
144627Sjchu 	/* Check all child devices for errors */
144727Sjchu 	ret = ndi_fm_handler_dispatch(rpdip, NULL, &derr);
144827Sjchu 
14491648Sjchu 	mutex_exit(&px_p->px_fm_mutex);
145027Sjchu 
145127Sjchu 	/*
145227Sjchu 	 * PX_FATAL_HW indicates a condition recovered from Fatal-Reset,
145327Sjchu 	 * therefore it does not cause panic.
145427Sjchu 	 */
145527Sjchu 	if ((err & (PX_FATAL_GOS | PX_FATAL_SW)) || (ret == DDI_FM_FATAL))
1456677Sjchu 		PX_FM_PANIC("Fatal System Port Error has occurred\n");
145727Sjchu }
145827Sjchu 
14590Sstevel@tonic-gate #ifdef  DEBUG
14600Sstevel@tonic-gate int	px_peekfault_cnt = 0;
14610Sstevel@tonic-gate int	px_pokefault_cnt = 0;
14620Sstevel@tonic-gate #endif  /* DEBUG */
14630Sstevel@tonic-gate 
14640Sstevel@tonic-gate /*ARGSUSED*/
14650Sstevel@tonic-gate static int
14660Sstevel@tonic-gate px_lib_do_poke(dev_info_t *dip, dev_info_t *rdip,
14670Sstevel@tonic-gate     peekpoke_ctlops_t *in_args)
14680Sstevel@tonic-gate {
14690Sstevel@tonic-gate 	px_t *px_p = DIP_TO_STATE(dip);
14700Sstevel@tonic-gate 	px_pec_t *pec_p = px_p->px_pec_p;
14710Sstevel@tonic-gate 	int err = DDI_SUCCESS;
14720Sstevel@tonic-gate 	on_trap_data_t otd;
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate 	mutex_enter(&pec_p->pec_pokefault_mutex);
14750Sstevel@tonic-gate 	pec_p->pec_ontrap_data = &otd;
147627Sjchu 	pec_p->pec_safeacc_type = DDI_FM_ERR_POKE;
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate 	/* Set up protected environment. */
14790Sstevel@tonic-gate 	if (!on_trap(&otd, OT_DATA_ACCESS)) {
14800Sstevel@tonic-gate 		uintptr_t tramp = otd.ot_trampoline;
14810Sstevel@tonic-gate 
14820Sstevel@tonic-gate 		otd.ot_trampoline = (uintptr_t)&poke_fault;
14830Sstevel@tonic-gate 		err = do_poke(in_args->size, (void *)in_args->dev_addr,
14840Sstevel@tonic-gate 		    (void *)in_args->host_addr);
14850Sstevel@tonic-gate 		otd.ot_trampoline = tramp;
14860Sstevel@tonic-gate 	} else
14870Sstevel@tonic-gate 		err = DDI_FAILURE;
14880Sstevel@tonic-gate 
1489624Sschwartz 	px_lib_clr_errs(px_p);
149027Sjchu 
14910Sstevel@tonic-gate 	if (otd.ot_trap & OT_DATA_ACCESS)
14920Sstevel@tonic-gate 		err = DDI_FAILURE;
14930Sstevel@tonic-gate 
14940Sstevel@tonic-gate 	/* Take down protected environment. */
14950Sstevel@tonic-gate 	no_trap();
14960Sstevel@tonic-gate 
14970Sstevel@tonic-gate 	pec_p->pec_ontrap_data = NULL;
149827Sjchu 	pec_p->pec_safeacc_type = DDI_FM_ERR_UNEXPECTED;
14990Sstevel@tonic-gate 	mutex_exit(&pec_p->pec_pokefault_mutex);
15000Sstevel@tonic-gate 
15010Sstevel@tonic-gate #ifdef  DEBUG
15020Sstevel@tonic-gate 	if (err == DDI_FAILURE)
15030Sstevel@tonic-gate 		px_pokefault_cnt++;
15040Sstevel@tonic-gate #endif
15050Sstevel@tonic-gate 	return (err);
15060Sstevel@tonic-gate }
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate /*ARGSUSED*/
15090Sstevel@tonic-gate static int
15100Sstevel@tonic-gate px_lib_do_caut_put(dev_info_t *dip, dev_info_t *rdip,
15110Sstevel@tonic-gate     peekpoke_ctlops_t *cautacc_ctlops_arg)
15120Sstevel@tonic-gate {
15130Sstevel@tonic-gate 	size_t size = cautacc_ctlops_arg->size;
15140Sstevel@tonic-gate 	uintptr_t dev_addr = cautacc_ctlops_arg->dev_addr;
15150Sstevel@tonic-gate 	uintptr_t host_addr = cautacc_ctlops_arg->host_addr;
15160Sstevel@tonic-gate 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)cautacc_ctlops_arg->handle;
15170Sstevel@tonic-gate 	size_t repcount = cautacc_ctlops_arg->repcount;
15180Sstevel@tonic-gate 	uint_t flags = cautacc_ctlops_arg->flags;
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 	px_t *px_p = DIP_TO_STATE(dip);
15210Sstevel@tonic-gate 	px_pec_t *pec_p = px_p->px_pec_p;
15220Sstevel@tonic-gate 	int err = DDI_SUCCESS;
15230Sstevel@tonic-gate 
152427Sjchu 	/*
152527Sjchu 	 * Note that i_ndi_busop_access_enter ends up grabbing the pokefault
152627Sjchu 	 * mutex.
152727Sjchu 	 */
15280Sstevel@tonic-gate 	i_ndi_busop_access_enter(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp);
15290Sstevel@tonic-gate 
153027Sjchu 	pec_p->pec_ontrap_data = (on_trap_data_t *)hp->ahi_err->err_ontrap;
153127Sjchu 	pec_p->pec_safeacc_type = DDI_FM_ERR_EXPECTED;
153227Sjchu 	hp->ahi_err->err_expected = DDI_FM_ERR_EXPECTED;
15330Sstevel@tonic-gate 
15340Sstevel@tonic-gate 	if (!i_ddi_ontrap((ddi_acc_handle_t)hp)) {
15350Sstevel@tonic-gate 		for (; repcount; repcount--) {
15360Sstevel@tonic-gate 			switch (size) {
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate 			case sizeof (uint8_t):
15390Sstevel@tonic-gate 				i_ddi_put8(hp, (uint8_t *)dev_addr,
15400Sstevel@tonic-gate 				    *(uint8_t *)host_addr);
15410Sstevel@tonic-gate 				break;
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate 			case sizeof (uint16_t):
15440Sstevel@tonic-gate 				i_ddi_put16(hp, (uint16_t *)dev_addr,
15450Sstevel@tonic-gate 				    *(uint16_t *)host_addr);
15460Sstevel@tonic-gate 				break;
15470Sstevel@tonic-gate 
15480Sstevel@tonic-gate 			case sizeof (uint32_t):
15490Sstevel@tonic-gate 				i_ddi_put32(hp, (uint32_t *)dev_addr,
15500Sstevel@tonic-gate 				    *(uint32_t *)host_addr);
15510Sstevel@tonic-gate 				break;
15520Sstevel@tonic-gate 
15530Sstevel@tonic-gate 			case sizeof (uint64_t):
15540Sstevel@tonic-gate 				i_ddi_put64(hp, (uint64_t *)dev_addr,
15550Sstevel@tonic-gate 				    *(uint64_t *)host_addr);
15560Sstevel@tonic-gate 				break;
15570Sstevel@tonic-gate 			}
15580Sstevel@tonic-gate 
15590Sstevel@tonic-gate 			host_addr += size;
15600Sstevel@tonic-gate 
15610Sstevel@tonic-gate 			if (flags == DDI_DEV_AUTOINCR)
15620Sstevel@tonic-gate 				dev_addr += size;
15630Sstevel@tonic-gate 
1564624Sschwartz 			px_lib_clr_errs(px_p);
156527Sjchu 
15660Sstevel@tonic-gate 			if (pec_p->pec_ontrap_data->ot_trap & OT_DATA_ACCESS) {
15670Sstevel@tonic-gate 				err = DDI_FAILURE;
15680Sstevel@tonic-gate #ifdef  DEBUG
15690Sstevel@tonic-gate 				px_pokefault_cnt++;
15700Sstevel@tonic-gate #endif
15710Sstevel@tonic-gate 				break;
15720Sstevel@tonic-gate 			}
15730Sstevel@tonic-gate 		}
15740Sstevel@tonic-gate 	}
15750Sstevel@tonic-gate 
15760Sstevel@tonic-gate 	i_ddi_notrap((ddi_acc_handle_t)hp);
15770Sstevel@tonic-gate 	pec_p->pec_ontrap_data = NULL;
157827Sjchu 	pec_p->pec_safeacc_type = DDI_FM_ERR_UNEXPECTED;
15790Sstevel@tonic-gate 	i_ndi_busop_access_exit(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp);
15800Sstevel@tonic-gate 	hp->ahi_err->err_expected = DDI_FM_ERR_UNEXPECTED;
15810Sstevel@tonic-gate 
15820Sstevel@tonic-gate 	return (err);
15830Sstevel@tonic-gate }
15840Sstevel@tonic-gate 
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate int
15870Sstevel@tonic-gate px_lib_ctlops_poke(dev_info_t *dip, dev_info_t *rdip,
15880Sstevel@tonic-gate     peekpoke_ctlops_t *in_args)
15890Sstevel@tonic-gate {
15900Sstevel@tonic-gate 	return (in_args->handle ? px_lib_do_caut_put(dip, rdip, in_args) :
15910Sstevel@tonic-gate 	    px_lib_do_poke(dip, rdip, in_args));
15920Sstevel@tonic-gate }
15930Sstevel@tonic-gate 
15940Sstevel@tonic-gate 
15950Sstevel@tonic-gate /*ARGSUSED*/
15960Sstevel@tonic-gate static int
15970Sstevel@tonic-gate px_lib_do_peek(dev_info_t *dip, peekpoke_ctlops_t *in_args)
15980Sstevel@tonic-gate {
159927Sjchu 	px_t *px_p = DIP_TO_STATE(dip);
160027Sjchu 	px_pec_t *pec_p = px_p->px_pec_p;
16010Sstevel@tonic-gate 	int err = DDI_SUCCESS;
16020Sstevel@tonic-gate 	on_trap_data_t otd;
16030Sstevel@tonic-gate 
160427Sjchu 	mutex_enter(&pec_p->pec_pokefault_mutex);
160527Sjchu 	pec_p->pec_safeacc_type = DDI_FM_ERR_PEEK;
160627Sjchu 
16070Sstevel@tonic-gate 	if (!on_trap(&otd, OT_DATA_ACCESS)) {
16080Sstevel@tonic-gate 		uintptr_t tramp = otd.ot_trampoline;
16090Sstevel@tonic-gate 
16100Sstevel@tonic-gate 		otd.ot_trampoline = (uintptr_t)&peek_fault;
16110Sstevel@tonic-gate 		err = do_peek(in_args->size, (void *)in_args->dev_addr,
16120Sstevel@tonic-gate 		    (void *)in_args->host_addr);
16130Sstevel@tonic-gate 		otd.ot_trampoline = tramp;
16140Sstevel@tonic-gate 	} else
16150Sstevel@tonic-gate 		err = DDI_FAILURE;
16160Sstevel@tonic-gate 
16170Sstevel@tonic-gate 	no_trap();
161827Sjchu 	pec_p->pec_safeacc_type = DDI_FM_ERR_UNEXPECTED;
161927Sjchu 	mutex_exit(&pec_p->pec_pokefault_mutex);
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate #ifdef  DEBUG
16220Sstevel@tonic-gate 	if (err == DDI_FAILURE)
16230Sstevel@tonic-gate 		px_peekfault_cnt++;
16240Sstevel@tonic-gate #endif
16250Sstevel@tonic-gate 	return (err);
16260Sstevel@tonic-gate }
16270Sstevel@tonic-gate 
16280Sstevel@tonic-gate 
16290Sstevel@tonic-gate static int
16300Sstevel@tonic-gate px_lib_do_caut_get(dev_info_t *dip, peekpoke_ctlops_t *cautacc_ctlops_arg)
16310Sstevel@tonic-gate {
16320Sstevel@tonic-gate 	size_t size = cautacc_ctlops_arg->size;
16330Sstevel@tonic-gate 	uintptr_t dev_addr = cautacc_ctlops_arg->dev_addr;
16340Sstevel@tonic-gate 	uintptr_t host_addr = cautacc_ctlops_arg->host_addr;
16350Sstevel@tonic-gate 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)cautacc_ctlops_arg->handle;
16360Sstevel@tonic-gate 	size_t repcount = cautacc_ctlops_arg->repcount;
16370Sstevel@tonic-gate 	uint_t flags = cautacc_ctlops_arg->flags;
16380Sstevel@tonic-gate 
16390Sstevel@tonic-gate 	px_t *px_p = DIP_TO_STATE(dip);
16400Sstevel@tonic-gate 	px_pec_t *pec_p = px_p->px_pec_p;
16410Sstevel@tonic-gate 	int err = DDI_SUCCESS;
16420Sstevel@tonic-gate 
164327Sjchu 	/*
164427Sjchu 	 * Note that i_ndi_busop_access_enter ends up grabbing the pokefault
164527Sjchu 	 * mutex.
164627Sjchu 	 */
164727Sjchu 	i_ndi_busop_access_enter(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp);
164827Sjchu 
164927Sjchu 	pec_p->pec_ontrap_data = (on_trap_data_t *)hp->ahi_err->err_ontrap;
165027Sjchu 	pec_p->pec_safeacc_type = DDI_FM_ERR_EXPECTED;
16510Sstevel@tonic-gate 	hp->ahi_err->err_expected = DDI_FM_ERR_EXPECTED;
16520Sstevel@tonic-gate 
16530Sstevel@tonic-gate 	if (repcount == 1) {
16540Sstevel@tonic-gate 		if (!i_ddi_ontrap((ddi_acc_handle_t)hp)) {
16550Sstevel@tonic-gate 			i_ddi_caut_get(size, (void *)dev_addr,
16560Sstevel@tonic-gate 			    (void *)host_addr);
16570Sstevel@tonic-gate 		} else {
16580Sstevel@tonic-gate 			int i;
16590Sstevel@tonic-gate 			uint8_t *ff_addr = (uint8_t *)host_addr;
16600Sstevel@tonic-gate 			for (i = 0; i < size; i++)
16610Sstevel@tonic-gate 				*ff_addr++ = 0xff;
16620Sstevel@tonic-gate 
16630Sstevel@tonic-gate 			err = DDI_FAILURE;
16640Sstevel@tonic-gate #ifdef  DEBUG
16650Sstevel@tonic-gate 			px_peekfault_cnt++;
16660Sstevel@tonic-gate #endif
16670Sstevel@tonic-gate 		}
16680Sstevel@tonic-gate 	} else {
16690Sstevel@tonic-gate 		if (!i_ddi_ontrap((ddi_acc_handle_t)hp)) {
16700Sstevel@tonic-gate 			for (; repcount; repcount--) {
16710Sstevel@tonic-gate 				i_ddi_caut_get(size, (void *)dev_addr,
16720Sstevel@tonic-gate 				    (void *)host_addr);
16730Sstevel@tonic-gate 
16740Sstevel@tonic-gate 				host_addr += size;
16750Sstevel@tonic-gate 
16760Sstevel@tonic-gate 				if (flags == DDI_DEV_AUTOINCR)
16770Sstevel@tonic-gate 					dev_addr += size;
16780Sstevel@tonic-gate 			}
16790Sstevel@tonic-gate 		} else {
16800Sstevel@tonic-gate 			err = DDI_FAILURE;
16810Sstevel@tonic-gate #ifdef  DEBUG
16820Sstevel@tonic-gate 			px_peekfault_cnt++;
16830Sstevel@tonic-gate #endif
16840Sstevel@tonic-gate 		}
16850Sstevel@tonic-gate 	}
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate 	i_ddi_notrap((ddi_acc_handle_t)hp);
16880Sstevel@tonic-gate 	pec_p->pec_ontrap_data = NULL;
168927Sjchu 	pec_p->pec_safeacc_type = DDI_FM_ERR_UNEXPECTED;
16900Sstevel@tonic-gate 	i_ndi_busop_access_exit(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp);
16910Sstevel@tonic-gate 	hp->ahi_err->err_expected = DDI_FM_ERR_UNEXPECTED;
16920Sstevel@tonic-gate 
16930Sstevel@tonic-gate 	return (err);
16940Sstevel@tonic-gate }
16950Sstevel@tonic-gate 
16960Sstevel@tonic-gate /*ARGSUSED*/
16970Sstevel@tonic-gate int
16980Sstevel@tonic-gate px_lib_ctlops_peek(dev_info_t *dip, dev_info_t *rdip,
16990Sstevel@tonic-gate     peekpoke_ctlops_t *in_args, void *result)
17000Sstevel@tonic-gate {
17010Sstevel@tonic-gate 	result = (void *)in_args->host_addr;
17020Sstevel@tonic-gate 	return (in_args->handle ? px_lib_do_caut_get(dip, in_args) :
17030Sstevel@tonic-gate 	    px_lib_do_peek(dip, in_args));
17040Sstevel@tonic-gate }
1705118Sjchu 
17060Sstevel@tonic-gate /*
17070Sstevel@tonic-gate  * implements PPM interface
17080Sstevel@tonic-gate  */
17090Sstevel@tonic-gate int
17100Sstevel@tonic-gate px_lib_pmctl(int cmd, px_t *px_p)
17110Sstevel@tonic-gate {
17120Sstevel@tonic-gate 	ASSERT((cmd & ~PPMREQ_MASK) == PPMREQ);
17130Sstevel@tonic-gate 	switch (cmd) {
17140Sstevel@tonic-gate 	case PPMREQ_PRE_PWR_OFF:
17150Sstevel@tonic-gate 		/*
17160Sstevel@tonic-gate 		 * Currently there is no device power management for
17170Sstevel@tonic-gate 		 * the root complex (fire). When there is we need to make
17180Sstevel@tonic-gate 		 * sure that it is at full power before trying to send the
17190Sstevel@tonic-gate 		 * PME_Turn_Off message.
17200Sstevel@tonic-gate 		 */
17210Sstevel@tonic-gate 		DBG(DBG_PWR, px_p->px_dip,
17220Sstevel@tonic-gate 		    "ioctl: request to send PME_Turn_Off\n");
17230Sstevel@tonic-gate 		return (px_goto_l23ready(px_p));
17240Sstevel@tonic-gate 
17250Sstevel@tonic-gate 	case PPMREQ_PRE_PWR_ON:
1726118Sjchu 		DBG(DBG_PWR, px_p->px_dip, "ioctl: PRE_PWR_ON request\n");
1727118Sjchu 		return (px_pre_pwron_check(px_p));
1728118Sjchu 
17290Sstevel@tonic-gate 	case PPMREQ_POST_PWR_ON:
1730118Sjchu 		DBG(DBG_PWR, px_p->px_dip, "ioctl: POST_PWR_ON request\n");
1731118Sjchu 		return (px_goto_l0(px_p));
17320Sstevel@tonic-gate 
17330Sstevel@tonic-gate 	default:
17340Sstevel@tonic-gate 		return (DDI_FAILURE);
17350Sstevel@tonic-gate 	}
17360Sstevel@tonic-gate }
17370Sstevel@tonic-gate 
17380Sstevel@tonic-gate /*
17390Sstevel@tonic-gate  * sends PME_Turn_Off message to put the link in L2/L3 ready state.
17400Sstevel@tonic-gate  * called by px_ioctl.
17410Sstevel@tonic-gate  * returns DDI_SUCCESS or DDI_FAILURE
17420Sstevel@tonic-gate  * 1. Wait for link to be in L1 state (link status reg)
17430Sstevel@tonic-gate  * 2. write to PME_Turn_off reg to boradcast
17440Sstevel@tonic-gate  * 3. set timeout
17450Sstevel@tonic-gate  * 4. If timeout, return failure.
17460Sstevel@tonic-gate  * 5. If PM_TO_Ack, wait till link is in L2/L3 ready
17470Sstevel@tonic-gate  */
17480Sstevel@tonic-gate static int
17490Sstevel@tonic-gate px_goto_l23ready(px_t *px_p)
17500Sstevel@tonic-gate {
17510Sstevel@tonic-gate 	pcie_pwr_t	*pwr_p;
175227Sjchu 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
175327Sjchu 	caddr_t	csr_base = (caddr_t)pxu_p->px_address[PX_REG_CSR];
17540Sstevel@tonic-gate 	int		ret = DDI_SUCCESS;
17550Sstevel@tonic-gate 	clock_t		end, timeleft;
1756118Sjchu 	int		mutex_held = 1;
17570Sstevel@tonic-gate 
17580Sstevel@tonic-gate 	/* If no PM info, return failure */
17590Sstevel@tonic-gate 	if (!PCIE_PMINFO(px_p->px_dip) ||
17600Sstevel@tonic-gate 	    !(pwr_p = PCIE_NEXUS_PMINFO(px_p->px_dip)))
17610Sstevel@tonic-gate 		return (DDI_FAILURE);
17620Sstevel@tonic-gate 
17630Sstevel@tonic-gate 	mutex_enter(&pwr_p->pwr_lock);
1764118Sjchu 	mutex_enter(&px_p->px_l23ready_lock);
17650Sstevel@tonic-gate 	/* Clear the PME_To_ACK receieved flag */
1766118Sjchu 	px_p->px_pm_flags &= ~PX_PMETOACK_RECVD;
1767287Smg140465 	/*
1768287Smg140465 	 * When P25 is the downstream device, after receiving
1769287Smg140465 	 * PME_To_ACK, fire will go to Detect state, which causes
1770287Smg140465 	 * the link down event. Inform FMA that this is expected.
1771287Smg140465 	 * In case of all other cards complaint with the pci express
1772287Smg140465 	 * spec, this will happen when the power is re-applied. FMA
1773287Smg140465 	 * code will clear this flag after one instance of LDN. Since
1774287Smg140465 	 * there will not be a LDN event for the spec compliant cards,
1775287Smg140465 	 * we need to clear the flag after receiving PME_To_ACK.
1776287Smg140465 	 */
1777287Smg140465 	px_p->px_pm_flags |= PX_LDN_EXPECTED;
17780Sstevel@tonic-gate 	if (px_send_pme_turnoff(csr_base) != DDI_SUCCESS) {
17790Sstevel@tonic-gate 		ret = DDI_FAILURE;
17800Sstevel@tonic-gate 		goto l23ready_done;
17810Sstevel@tonic-gate 	}
1782118Sjchu 	px_p->px_pm_flags |= PX_PME_TURNOFF_PENDING;
17830Sstevel@tonic-gate 
17840Sstevel@tonic-gate 	end = ddi_get_lbolt() + drv_usectohz(px_pme_to_ack_timeout);
1785118Sjchu 	while (!(px_p->px_pm_flags & PX_PMETOACK_RECVD)) {
1786118Sjchu 		timeleft = cv_timedwait(&px_p->px_l23ready_cv,
1787118Sjchu 		    &px_p->px_l23ready_lock, end);
17880Sstevel@tonic-gate 		/*
17890Sstevel@tonic-gate 		 * if cv_timedwait returns -1, it is either
17900Sstevel@tonic-gate 		 * 1) timed out or
17910Sstevel@tonic-gate 		 * 2) there was a pre-mature wakeup but by the time
17920Sstevel@tonic-gate 		 * cv_timedwait is called again end < lbolt i.e.
17930Sstevel@tonic-gate 		 * end is in the past.
17940Sstevel@tonic-gate 		 * 3) By the time we make first cv_timedwait call,
17950Sstevel@tonic-gate 		 * end < lbolt is true.
17960Sstevel@tonic-gate 		 */
17970Sstevel@tonic-gate 		if (timeleft == -1)
17980Sstevel@tonic-gate 			break;
17990Sstevel@tonic-gate 	}
1800118Sjchu 	if (!(px_p->px_pm_flags & PX_PMETOACK_RECVD)) {
18010Sstevel@tonic-gate 		/*
18020Sstevel@tonic-gate 		 * Either timedout or interrupt didn't get a
18030Sstevel@tonic-gate 		 * chance to grab the mutex and set the flag.
18040Sstevel@tonic-gate 		 * release the mutex and delay for sometime.
18050Sstevel@tonic-gate 		 * This will 1) give a chance for interrupt to
18060Sstevel@tonic-gate 		 * set the flag 2) creates a delay between two
18070Sstevel@tonic-gate 		 * consequetive requests.
18080Sstevel@tonic-gate 		 */
1809118Sjchu 		mutex_exit(&px_p->px_l23ready_lock);
18101147Sjchu 		delay(drv_usectohz(50 * PX_MSEC_TO_USEC));
1811118Sjchu 		mutex_held = 0;
1812118Sjchu 		if (!(px_p->px_pm_flags & PX_PMETOACK_RECVD)) {
18130Sstevel@tonic-gate 			ret = DDI_FAILURE;
18140Sstevel@tonic-gate 			DBG(DBG_PWR, px_p->px_dip, " Timed out while waiting"
18150Sstevel@tonic-gate 			    " for PME_TO_ACK\n");
18160Sstevel@tonic-gate 		}
18170Sstevel@tonic-gate 	}
1818287Smg140465 	px_p->px_pm_flags &=
1819287Smg140465 	    ~(PX_PME_TURNOFF_PENDING | PX_PMETOACK_RECVD | PX_LDN_EXPECTED);
18200Sstevel@tonic-gate 
18210Sstevel@tonic-gate l23ready_done:
1822118Sjchu 	if (mutex_held)
1823118Sjchu 		mutex_exit(&px_p->px_l23ready_lock);
1824118Sjchu 	/*
1825118Sjchu 	 * Wait till link is in L1 idle, if sending PME_Turn_Off
1826118Sjchu 	 * was succesful.
1827118Sjchu 	 */
1828118Sjchu 	if (ret == DDI_SUCCESS) {
1829118Sjchu 		if (px_link_wait4l1idle(csr_base) != DDI_SUCCESS) {
1830118Sjchu 			DBG(DBG_PWR, px_p->px_dip, " Link is not at L1"
1831287Smg140465 			    " even though we received PME_To_ACK.\n");
1832287Smg140465 			/*
1833287Smg140465 			 * Workaround for hardware bug with P25.
1834287Smg140465 			 * Due to a hardware bug with P25, link state
1835287Smg140465 			 * will be Detect state rather than L1 after
1836287Smg140465 			 * link is transitioned to L23Ready state. Since
1837287Smg140465 			 * we don't know whether link is L23ready state
1838287Smg140465 			 * without Fire's state being L1_idle, we delay
1839287Smg140465 			 * here just to make sure that we wait till link
1840287Smg140465 			 * is transitioned to L23Ready state.
1841287Smg140465 			 */
18421147Sjchu 			delay(drv_usectohz(100 * PX_MSEC_TO_USEC));
1843287Smg140465 		}
1844287Smg140465 		pwr_p->pwr_link_lvl = PM_LEVEL_L3;
1845118Sjchu 
1846118Sjchu 	}
18470Sstevel@tonic-gate 	mutex_exit(&pwr_p->pwr_lock);
18480Sstevel@tonic-gate 	return (ret);
18490Sstevel@tonic-gate }
18500Sstevel@tonic-gate 
1851118Sjchu /*
1852118Sjchu  * Message interrupt handler intended to be shared for both
1853118Sjchu  * PME and PME_TO_ACK msg handling, currently only handles
1854118Sjchu  * PME_To_ACK message.
1855118Sjchu  */
1856118Sjchu uint_t
1857118Sjchu px_pmeq_intr(caddr_t arg)
1858118Sjchu {
1859118Sjchu 	px_t	*px_p = (px_t *)arg;
1860118Sjchu 
1861287Smg140465 	DBG(DBG_PWR, px_p->px_dip, " PME_To_ACK received \n");
1862118Sjchu 	mutex_enter(&px_p->px_l23ready_lock);
1863118Sjchu 	cv_broadcast(&px_p->px_l23ready_cv);
1864118Sjchu 	if (px_p->px_pm_flags & PX_PME_TURNOFF_PENDING) {
1865118Sjchu 		px_p->px_pm_flags |= PX_PMETOACK_RECVD;
1866118Sjchu 	} else {
1867118Sjchu 		/*
1868118Sjchu 		 * This maybe the second ack received. If so then,
1869118Sjchu 		 * we should be receiving it during wait4L1 stage.
1870118Sjchu 		 */
1871118Sjchu 		px_p->px_pmetoack_ignored++;
1872118Sjchu 	}
1873118Sjchu 	mutex_exit(&px_p->px_l23ready_lock);
1874118Sjchu 	return (DDI_INTR_CLAIMED);
1875118Sjchu }
1876118Sjchu 
1877118Sjchu static int
1878118Sjchu px_pre_pwron_check(px_t *px_p)
1879118Sjchu {
1880118Sjchu 	pcie_pwr_t	*pwr_p;
1881118Sjchu 
1882118Sjchu 	/* If no PM info, return failure */
1883118Sjchu 	if (!PCIE_PMINFO(px_p->px_dip) ||
1884118Sjchu 	    !(pwr_p = PCIE_NEXUS_PMINFO(px_p->px_dip)))
1885118Sjchu 		return (DDI_FAILURE);
1886118Sjchu 
1887287Smg140465 	/*
1888287Smg140465 	 * For the spec compliant downstream cards link down
1889287Smg140465 	 * is expected when the device is powered on.
1890287Smg140465 	 */
1891287Smg140465 	px_p->px_pm_flags |= PX_LDN_EXPECTED;
1892118Sjchu 	return (pwr_p->pwr_link_lvl == PM_LEVEL_L3 ? DDI_SUCCESS : DDI_FAILURE);
1893118Sjchu }
1894118Sjchu 
1895118Sjchu static int
1896118Sjchu px_goto_l0(px_t *px_p)
1897118Sjchu {
1898118Sjchu 	pcie_pwr_t	*pwr_p;
1899118Sjchu 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
1900118Sjchu 	caddr_t csr_base = (caddr_t)pxu_p->px_address[PX_REG_CSR];
1901118Sjchu 	int		ret = DDI_SUCCESS;
19021147Sjchu 	uint64_t	time_spent = 0;
1903118Sjchu 
1904118Sjchu 	/* If no PM info, return failure */
1905118Sjchu 	if (!PCIE_PMINFO(px_p->px_dip) ||
1906118Sjchu 	    !(pwr_p = PCIE_NEXUS_PMINFO(px_p->px_dip)))
1907118Sjchu 		return (DDI_FAILURE);
1908118Sjchu 
1909118Sjchu 	mutex_enter(&pwr_p->pwr_lock);
1910287Smg140465 	/*
19111147Sjchu 	 * The following link retrain activity will cause LDN and LUP event.
19121147Sjchu 	 * Receiving LDN prior to receiving LUP is expected, not an error in
19131147Sjchu 	 * this case.  Receiving LUP indicates link is fully up to support
19141147Sjchu 	 * powering up down stream device, and of course any further LDN and
19151147Sjchu 	 * LUP outside this context will be error.
1916287Smg140465 	 */
19171147Sjchu 	px_p->px_lup_pending = 1;
1918118Sjchu 	if (px_link_retrain(csr_base) != DDI_SUCCESS) {
1919118Sjchu 		ret = DDI_FAILURE;
1920118Sjchu 		goto l0_done;
1921118Sjchu 	}
1922118Sjchu 
19231147Sjchu 	/* LUP event takes the order of 15ms amount of time to occur */
19241147Sjchu 	for (; px_p->px_lup_pending && (time_spent < px_lup_poll_to);
19251147Sjchu 	    time_spent += px_lup_poll_interval)
19261147Sjchu 		drv_usecwait(px_lup_poll_interval);
19271147Sjchu 	if (px_p->px_lup_pending)
19281147Sjchu 		ret = DDI_FAILURE;
1929118Sjchu l0_done:
1930287Smg140465 	px_enable_detect_quiet(csr_base);
1931118Sjchu 	if (ret == DDI_SUCCESS)
1932287Smg140465 		pwr_p->pwr_link_lvl = PM_LEVEL_L0;
1933118Sjchu 	mutex_exit(&pwr_p->pwr_lock);
1934118Sjchu 	return (ret);
1935118Sjchu }
1936118Sjchu 
19370Sstevel@tonic-gate /*
19380Sstevel@tonic-gate  * Extract the drivers binding name to identify which chip we're binding to.
19390Sstevel@tonic-gate  * Whenever a new bus bridge is created, the driver alias entry should be
19400Sstevel@tonic-gate  * added here to identify the device if needed.  If a device isn't added,
19410Sstevel@tonic-gate  * the identity defaults to PX_CHIP_UNIDENTIFIED.
19420Sstevel@tonic-gate  */
19430Sstevel@tonic-gate static uint32_t
19442426Sschwartz px_identity_init(px_t *px_p)
19450Sstevel@tonic-gate {
19460Sstevel@tonic-gate 	dev_info_t	*dip = px_p->px_dip;
19470Sstevel@tonic-gate 	char		*name = ddi_binding_name(dip);
19480Sstevel@tonic-gate 	uint32_t	revision = 0;
19490Sstevel@tonic-gate 
19500Sstevel@tonic-gate 	revision = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
19510Sstevel@tonic-gate 	    "module-revision#", 0);
19520Sstevel@tonic-gate 
19530Sstevel@tonic-gate 	/* Check for Fire driver binding name */
19542426Sschwartz 	if (strcmp(name, "pciex108e,80f0") == 0) {
19552426Sschwartz 		DBG(DBG_ATTACH, dip, "px_identity_init: %s%d: "
19562426Sschwartz 		    "(FIRE), module-revision %d\n", NAMEINST(dip),
19572426Sschwartz 		    revision);
19582426Sschwartz 
19592426Sschwartz 		return ((revision >= FIRE_MOD_REV_20) ?
19602426Sschwartz 		    PX_CHIP_FIRE : PX_CHIP_UNIDENTIFIED);
19610Sstevel@tonic-gate 	}
19620Sstevel@tonic-gate 
19631772Sjl139090 	/* Check for Oberon driver binding name */
19641772Sjl139090 	if (strcmp(name, "pciex108e,80f8") == 0) {
19652426Sschwartz 		DBG(DBG_ATTACH, dip, "px_identity_init: %s%d: "
19662426Sschwartz 		    "(OBERON), module-revision %d\n", NAMEINST(dip),
19672426Sschwartz 		    revision);
19682426Sschwartz 
19692426Sschwartz 		return (PX_CHIP_OBERON);
19701772Sjl139090 	}
19711772Sjl139090 
19720Sstevel@tonic-gate 	DBG(DBG_ATTACH, dip, "%s%d: Unknown PCI Express Host bridge %s %x\n",
19730Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), name, revision);
19740Sstevel@tonic-gate 
19750Sstevel@tonic-gate 	return (PX_CHIP_UNIDENTIFIED);
19760Sstevel@tonic-gate }
197727Sjchu 
197827Sjchu int
197927Sjchu px_err_add_intr(px_fault_t *px_fault_p)
198027Sjchu {
198127Sjchu 	dev_info_t	*dip = px_fault_p->px_fh_dip;
198227Sjchu 	px_t		*px_p = DIP_TO_STATE(dip);
198327Sjchu 
198427Sjchu 	VERIFY(add_ivintr(px_fault_p->px_fh_sysino, PX_ERR_PIL,
198527Sjchu 		px_fault_p->px_err_func, (caddr_t)px_fault_p, NULL) == 0);
198627Sjchu 
198727Sjchu 	px_ib_intr_enable(px_p, intr_dist_cpuid(), px_fault_p->px_intr_ino);
198827Sjchu 
198927Sjchu 	return (DDI_SUCCESS);
199027Sjchu }
199127Sjchu 
199227Sjchu void
199327Sjchu px_err_rem_intr(px_fault_t *px_fault_p)
199427Sjchu {
199527Sjchu 	dev_info_t	*dip = px_fault_p->px_fh_dip;
199627Sjchu 	px_t		*px_p = DIP_TO_STATE(dip);
199727Sjchu 
199827Sjchu 	px_ib_intr_disable(px_p->px_ib_p, px_fault_p->px_intr_ino,
199927Sjchu 		IB_INTR_WAIT);
2000965Sgovinda 
2001965Sgovinda 	rem_ivintr(px_fault_p->px_fh_sysino, NULL);
200227Sjchu }
200327Sjchu 
20041648Sjchu /*
20051648Sjchu  * px_cb_add_intr() - Called from attach(9E) to create CB if not yet
20061648Sjchu  * created, to add CB interrupt vector always, but enable only once.
20071648Sjchu  */
20081648Sjchu int
20091648Sjchu px_cb_add_intr(px_fault_t *fault_p)
20101648Sjchu {
20111648Sjchu 	px_t		*px_p = DIP_TO_STATE(fault_p->px_fh_dip);
20121648Sjchu 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
20131772Sjl139090 	px_cb_t		*cb_p = (px_cb_t *)px_get_cb(fault_p->px_fh_dip);
20141648Sjchu 	px_cb_list_t	*pxl, *pxl_new;
20151648Sjchu 	cpuid_t		cpuid;
20161648Sjchu 
20171648Sjchu 
20181648Sjchu 	if (cb_p == NULL) {
20191648Sjchu 		cb_p = kmem_zalloc(sizeof (px_cb_t), KM_SLEEP);
20201648Sjchu 		mutex_init(&cb_p->cb_mutex, NULL, MUTEX_DRIVER, NULL);
20211648Sjchu 		cb_p->px_cb_func = px_cb_intr;
20221648Sjchu 		pxu_p->px_cb_p = cb_p;
20231772Sjl139090 		px_set_cb(fault_p->px_fh_dip, (uint64_t)cb_p);
20242509Sschwartz 
20252509Sschwartz 		/* px_lib_dev_init allows only FIRE and OBERON */
20262509Sschwartz 		px_err_reg_enable(
20272509Sschwartz 		    (pxu_p->chip_type == PX_CHIP_FIRE) ?
20282509Sschwartz 			PX_ERR_JBC : PX_ERR_UBC,
20292509Sschwartz 		    pxu_p->px_address[PX_REG_XBC]);
20301648Sjchu 	} else
20311648Sjchu 		pxu_p->px_cb_p = cb_p;
20321648Sjchu 
20331648Sjchu 	mutex_enter(&cb_p->cb_mutex);
20341648Sjchu 
20351648Sjchu 	VERIFY(add_ivintr(fault_p->px_fh_sysino, PX_ERR_PIL,
20361648Sjchu 	    cb_p->px_cb_func, (caddr_t)cb_p, NULL) == 0);
20371648Sjchu 
20381648Sjchu 	if (cb_p->pxl == NULL) {
20391648Sjchu 
20401648Sjchu 		cpuid = intr_dist_cpuid(),
20411648Sjchu 		px_ib_intr_enable(px_p, cpuid, fault_p->px_intr_ino);
20421648Sjchu 
20431648Sjchu 		pxl = kmem_zalloc(sizeof (px_cb_list_t), KM_SLEEP);
20441648Sjchu 		pxl->pxp = px_p;
20451648Sjchu 
20461648Sjchu 		cb_p->pxl = pxl;
20471648Sjchu 		cb_p->sysino = fault_p->px_fh_sysino;
20481648Sjchu 		cb_p->cpuid = cpuid;
20491648Sjchu 
20501648Sjchu 	} else {
20511648Sjchu 		/*
20521648Sjchu 		 * Find the last pxl or
20531648Sjchu 		 * stop short at encoutering a redundent, or
20541648Sjchu 		 * both.
20551648Sjchu 		 */
20561648Sjchu 		pxl = cb_p->pxl;
20571648Sjchu 		for (; !(pxl->pxp == px_p) && pxl->next; pxl = pxl->next);
20581648Sjchu 		if (pxl->pxp == px_p) {
20591648Sjchu 			cmn_err(CE_WARN, "px_cb_add_intr: reregister sysino "
20601650Sjchu 			    "%lx by px_p 0x%p\n", cb_p->sysino, (void *)px_p);
20611648Sjchu 			return (DDI_FAILURE);
20621648Sjchu 		}
20631648Sjchu 
20641648Sjchu 		/* add to linked list */
20651648Sjchu 		pxl_new = kmem_zalloc(sizeof (px_cb_list_t), KM_SLEEP);
20661648Sjchu 		pxl_new->pxp = px_p;
20671648Sjchu 		pxl->next = pxl_new;
20681648Sjchu 	}
20691648Sjchu 	cb_p->attachcnt++;
20701648Sjchu 
20711648Sjchu 	mutex_exit(&cb_p->cb_mutex);
20721648Sjchu 
20731648Sjchu 	return (DDI_SUCCESS);
20741648Sjchu }
20751648Sjchu 
20761648Sjchu /*
20771648Sjchu  * px_cb_rem_intr() - Called from detach(9E) to remove its CB
20781648Sjchu  * interrupt vector, to shift proxy to the next available px,
20791648Sjchu  * or disable CB interrupt when itself is the last.
20801648Sjchu  */
20811648Sjchu void
20821648Sjchu px_cb_rem_intr(px_fault_t *fault_p)
20831648Sjchu {
20841648Sjchu 	px_t		*px_p = DIP_TO_STATE(fault_p->px_fh_dip), *pxp;
20851648Sjchu 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
20861648Sjchu 	px_cb_t		*cb_p = PX2CB(px_p);
20871648Sjchu 	px_cb_list_t	*pxl, *prev;
20881648Sjchu 	px_fault_t	*f_p;
20891648Sjchu 
20901648Sjchu 	ASSERT(cb_p->pxl);
20911648Sjchu 
20921648Sjchu 	/* De-list the target px, move the next px up */
20931648Sjchu 
20941648Sjchu 	mutex_enter(&cb_p->cb_mutex);
20951648Sjchu 
20961648Sjchu 	pxl = cb_p->pxl;
20971648Sjchu 	if (pxl->pxp == px_p) {
20981648Sjchu 		cb_p->pxl = pxl->next;
20991648Sjchu 	} else {
21001648Sjchu 		prev = pxl;
21011648Sjchu 		pxl = pxl->next;
21021648Sjchu 		for (; pxl && (pxl->pxp != px_p); prev = pxl, pxl = pxl->next);
21031648Sjchu 		if (!pxl) {
21041648Sjchu 			cmn_err(CE_WARN, "px_cb_rem_intr: can't find px_p 0x%p "
21051650Sjchu 			    "in registered CB list.", (void *)px_p);
21061648Sjchu 			return;
21071648Sjchu 		}
21081648Sjchu 		prev->next = pxl->next;
21091648Sjchu 	}
21101648Sjchu 	kmem_free(pxl, sizeof (px_cb_list_t));
21111648Sjchu 
21121648Sjchu 	if (fault_p->px_fh_sysino == cb_p->sysino) {
21131648Sjchu 		px_ib_intr_disable(px_p->px_ib_p, fault_p->px_intr_ino,
21141648Sjchu 		    IB_INTR_WAIT);
21151648Sjchu 
21161648Sjchu 		if (cb_p->pxl) {
21171648Sjchu 			pxp = cb_p->pxl->pxp;
21181648Sjchu 			f_p = &pxp->px_cb_fault;
21191648Sjchu 			cb_p->sysino = f_p->px_fh_sysino;
21201648Sjchu 
21211648Sjchu 			PX_INTR_ENABLE(pxp->px_dip, cb_p->sysino, cb_p->cpuid);
21221650Sjchu 			(void) px_lib_intr_setstate(pxp->px_dip, cb_p->sysino,
21231648Sjchu 			    INTR_IDLE_STATE);
21241648Sjchu 		}
21251648Sjchu 	}
21261648Sjchu 
21271648Sjchu 	rem_ivintr(fault_p->px_fh_sysino, NULL);
21281648Sjchu 	pxu_p->px_cb_p = NULL;
21291648Sjchu 	cb_p->attachcnt--;
21301648Sjchu 	if (cb_p->pxl) {
21311648Sjchu 		mutex_exit(&cb_p->cb_mutex);
21321648Sjchu 		return;
21331648Sjchu 	}
21341648Sjchu 	mutex_exit(&cb_p->cb_mutex);
21351648Sjchu 
21362509Sschwartz 	/* px_lib_dev_init allows only FIRE and OBERON */
21372509Sschwartz 	px_err_reg_disable(
21382509Sschwartz 	    (pxu_p->chip_type == PX_CHIP_FIRE) ? PX_ERR_JBC : PX_ERR_UBC,
21392509Sschwartz 	    pxu_p->px_address[PX_REG_XBC]);
21402509Sschwartz 
21411648Sjchu 	mutex_destroy(&cb_p->cb_mutex);
21421772Sjl139090 	px_set_cb(fault_p->px_fh_dip, 0ull);
21431648Sjchu 	kmem_free(cb_p, sizeof (px_cb_t));
21441648Sjchu }
21451648Sjchu 
21461648Sjchu /*
21471648Sjchu  * px_cb_intr() - sun4u only,  CB interrupt dispatcher
21481648Sjchu  */
21491648Sjchu uint_t
21501648Sjchu px_cb_intr(caddr_t arg)
21511648Sjchu {
21521648Sjchu 	px_cb_t		*cb_p = (px_cb_t *)arg;
21531648Sjchu 	px_cb_list_t	*pxl = cb_p->pxl;
21541648Sjchu 	px_t		*pxp = pxl ? pxl->pxp : NULL;
21551648Sjchu 	px_fault_t	*fault_p;
21561648Sjchu 
21571648Sjchu 	while (pxl && pxp && (pxp->px_state != PX_ATTACHED)) {
21581648Sjchu 		pxl = pxl->next;
21591648Sjchu 		pxp = (pxl) ? pxl->pxp : NULL;
21601648Sjchu 	}
21611648Sjchu 
21621648Sjchu 	if (pxp) {
21631648Sjchu 		fault_p = &pxp->px_cb_fault;
21641648Sjchu 		return (fault_p->px_err_func((caddr_t)fault_p));
21651648Sjchu 	} else
21661648Sjchu 		return (DDI_INTR_UNCLAIMED);
21671648Sjchu }
21681648Sjchu 
21691648Sjchu /*
21701648Sjchu  * px_cb_intr_redist() - sun4u only, CB interrupt redistribution
21711648Sjchu  */
21721648Sjchu void
21731648Sjchu px_cb_intr_redist(px_t	*px_p)
21741648Sjchu {
21751648Sjchu 	px_fault_t	*f_p = &px_p->px_cb_fault;
21761648Sjchu 	px_cb_t		*cb_p = PX2CB(px_p);
21771648Sjchu 	devino_t	ino = px_p->px_inos[PX_INTR_XBC];
21781648Sjchu 	cpuid_t		cpuid;
21791648Sjchu 
21801648Sjchu 	mutex_enter(&cb_p->cb_mutex);
21811648Sjchu 
21821648Sjchu 	if (cb_p->sysino != f_p->px_fh_sysino) {
21831648Sjchu 		mutex_exit(&cb_p->cb_mutex);
21841648Sjchu 		return;
21851648Sjchu 	}
21861648Sjchu 
21871648Sjchu 	cb_p->cpuid = cpuid = intr_dist_cpuid();
21881648Sjchu 	px_ib_intr_dist_en(px_p->px_dip, cpuid, ino, B_FALSE);
21891648Sjchu 
21901648Sjchu 	mutex_exit(&cb_p->cb_mutex);
21911648Sjchu }
21921648Sjchu 
219327Sjchu #ifdef FMA
219427Sjchu void
219527Sjchu px_fill_rc_status(px_fault_t *px_fault_p, pciex_rc_error_regs_t *rc_status)
219627Sjchu {
219727Sjchu 	/* populate the rc_status by reading the registers - TBD */
219827Sjchu }
219927Sjchu #endif /* FMA */
2200383Set142600 
2201383Set142600 /*
2202383Set142600  * Unprotected raw reads/writes of fabric device's config space.
2203383Set142600  * Only used for temporary PCI-E Fabric Error Handling.
2204383Set142600  */
2205383Set142600 uint32_t
22061648Sjchu px_fab_get(px_t *px_p, pcie_req_id_t bdf, uint16_t offset)
22071648Sjchu {
2208383Set142600 	px_ranges_t	*rp = px_p->px_ranges_p;
2209383Set142600 	uint64_t	range_prop, base_addr;
2210383Set142600 	int		bank = PCI_REG_ADDR_G(PCI_ADDR_CONFIG);
2211383Set142600 	uint32_t	val;
2212383Set142600 
2213383Set142600 	/* Get Fire's Physical Base Address */
22141772Sjl139090 	range_prop = px_get_range_prop(px_p, rp, bank);
2215383Set142600 
2216383Set142600 	/* Get config space first. */
2217383Set142600 	base_addr = range_prop + PX_BDF_TO_CFGADDR(bdf, offset);
2218383Set142600 
2219383Set142600 	val = ldphysio(base_addr);
2220383Set142600 
2221383Set142600 	return (LE_32(val));
2222383Set142600 }
2223383Set142600 
2224383Set142600 void
2225383Set142600 px_fab_set(px_t *px_p, pcie_req_id_t bdf, uint16_t offset,
2226383Set142600     uint32_t val) {
2227383Set142600 	px_ranges_t	*rp = px_p->px_ranges_p;
2228383Set142600 	uint64_t	range_prop, base_addr;
2229383Set142600 	int		bank = PCI_REG_ADDR_G(PCI_ADDR_CONFIG);
2230383Set142600 
2231383Set142600 	/* Get Fire's Physical Base Address */
22321772Sjl139090 	range_prop = px_get_range_prop(px_p, rp, bank);
2233383Set142600 
2234383Set142600 	/* Get config space first. */
2235383Set142600 	base_addr = range_prop + PX_BDF_TO_CFGADDR(bdf, offset);
2236383Set142600 
2237383Set142600 	stphysio(base_addr, LE_32(val));
2238383Set142600 }
2239435Sjchu 
2240435Sjchu /*
2241435Sjchu  * cpr callback
2242435Sjchu  *
2243435Sjchu  * disable fabric error msg interrupt prior to suspending
2244435Sjchu  * all device drivers; re-enable fabric error msg interrupt
2245435Sjchu  * after all devices are resumed.
2246435Sjchu  */
2247435Sjchu static boolean_t
2248435Sjchu px_cpr_callb(void *arg, int code)
2249435Sjchu {
2250435Sjchu 	px_t		*px_p = (px_t *)arg;
2251435Sjchu 	px_ib_t		*ib_p = px_p->px_ib_p;
2252435Sjchu 	px_pec_t	*pec_p = px_p->px_pec_p;
2253435Sjchu 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
2254435Sjchu 	caddr_t		csr_base;
2255435Sjchu 	devino_t	ce_ino, nf_ino, f_ino;
2256435Sjchu 	px_ib_ino_info_t	*ce_ino_p, *nf_ino_p, *f_ino_p;
2257435Sjchu 	uint64_t	imu_log_enable, imu_intr_enable;
2258435Sjchu 	uint64_t	imu_log_mask, imu_intr_mask;
2259435Sjchu 
2260435Sjchu 	ce_ino = px_msiqid_to_devino(px_p, pec_p->pec_corr_msg_msiq_id);
2261435Sjchu 	nf_ino = px_msiqid_to_devino(px_p, pec_p->pec_non_fatal_msg_msiq_id);
2262435Sjchu 	f_ino = px_msiqid_to_devino(px_p, pec_p->pec_fatal_msg_msiq_id);
2263435Sjchu 	csr_base = (caddr_t)pxu_p->px_address[PX_REG_CSR];
2264435Sjchu 
2265435Sjchu 	imu_log_enable = CSR_XR(csr_base, IMU_ERROR_LOG_ENABLE);
2266435Sjchu 	imu_intr_enable = CSR_XR(csr_base, IMU_INTERRUPT_ENABLE);
2267435Sjchu 
2268435Sjchu 	imu_log_mask = BITMASK(IMU_ERROR_LOG_ENABLE_FATAL_MES_NOT_EN_LOG_EN) |
2269435Sjchu 	    BITMASK(IMU_ERROR_LOG_ENABLE_NONFATAL_MES_NOT_EN_LOG_EN) |
2270435Sjchu 	    BITMASK(IMU_ERROR_LOG_ENABLE_COR_MES_NOT_EN_LOG_EN);
2271435Sjchu 
2272435Sjchu 	imu_intr_mask =
2273435Sjchu 	    BITMASK(IMU_INTERRUPT_ENABLE_FATAL_MES_NOT_EN_S_INT_EN) |
2274435Sjchu 	    BITMASK(IMU_INTERRUPT_ENABLE_NONFATAL_MES_NOT_EN_S_INT_EN) |
2275435Sjchu 	    BITMASK(IMU_INTERRUPT_ENABLE_COR_MES_NOT_EN_S_INT_EN) |
2276435Sjchu 	    BITMASK(IMU_INTERRUPT_ENABLE_FATAL_MES_NOT_EN_P_INT_EN) |
2277435Sjchu 	    BITMASK(IMU_INTERRUPT_ENABLE_NONFATAL_MES_NOT_EN_P_INT_EN) |
2278435Sjchu 	    BITMASK(IMU_INTERRUPT_ENABLE_COR_MES_NOT_EN_P_INT_EN);
2279435Sjchu 
2280435Sjchu 	switch (code) {
2281435Sjchu 	case CB_CODE_CPR_CHKPT:
2282435Sjchu 		/* disable imu rbne on corr/nonfatal/fatal errors */
2283435Sjchu 		CSR_XS(csr_base, IMU_ERROR_LOG_ENABLE,
2284435Sjchu 		    imu_log_enable & (~imu_log_mask));
2285435Sjchu 
2286435Sjchu 		CSR_XS(csr_base, IMU_INTERRUPT_ENABLE,
2287435Sjchu 		    imu_intr_enable & (~imu_intr_mask));
2288435Sjchu 
2289435Sjchu 		/* disable CORR intr mapping */
2290435Sjchu 		px_ib_intr_disable(ib_p, ce_ino, IB_INTR_NOWAIT);
2291435Sjchu 
2292435Sjchu 		/* disable NON FATAL intr mapping */
2293435Sjchu 		px_ib_intr_disable(ib_p, nf_ino, IB_INTR_NOWAIT);
2294435Sjchu 
2295435Sjchu 		/* disable FATAL intr mapping */
2296435Sjchu 		px_ib_intr_disable(ib_p, f_ino, IB_INTR_NOWAIT);
2297435Sjchu 
2298435Sjchu 		break;
2299435Sjchu 
2300435Sjchu 	case CB_CODE_CPR_RESUME:
2301435Sjchu 		mutex_enter(&ib_p->ib_ino_lst_mutex);
2302435Sjchu 
2303435Sjchu 		ce_ino_p = px_ib_locate_ino(ib_p, ce_ino);
2304435Sjchu 		nf_ino_p = px_ib_locate_ino(ib_p, nf_ino);
2305435Sjchu 		f_ino_p = px_ib_locate_ino(ib_p, f_ino);
2306435Sjchu 
2307435Sjchu 		/* enable CORR intr mapping */
2308435Sjchu 		if (ce_ino_p)
2309435Sjchu 			px_ib_intr_enable(px_p, ce_ino_p->ino_cpuid, ce_ino);
2310435Sjchu 		else
2311435Sjchu 			cmn_err(CE_WARN, "px_cpr_callb: RESUME unable to "
2312435Sjchu 			    "reenable PCIe Correctable msg intr.\n");
2313435Sjchu 
2314435Sjchu 		/* enable NON FATAL intr mapping */
2315435Sjchu 		if (nf_ino_p)
2316435Sjchu 			px_ib_intr_enable(px_p, nf_ino_p->ino_cpuid, nf_ino);
2317435Sjchu 		else
2318435Sjchu 			cmn_err(CE_WARN, "px_cpr_callb: RESUME unable to "
2319435Sjchu 			    "reenable PCIe Non Fatal msg intr.\n");
2320435Sjchu 
2321435Sjchu 		/* enable FATAL intr mapping */
2322435Sjchu 		if (f_ino_p)
2323435Sjchu 			px_ib_intr_enable(px_p, f_ino_p->ino_cpuid, f_ino);
2324435Sjchu 		else
2325435Sjchu 			cmn_err(CE_WARN, "px_cpr_callb: RESUME unable to "
2326435Sjchu 			    "reenable PCIe Fatal msg intr.\n");
2327435Sjchu 
2328435Sjchu 		mutex_exit(&ib_p->ib_ino_lst_mutex);
2329435Sjchu 
2330435Sjchu 		/* enable corr/nonfatal/fatal not enable error */
2331435Sjchu 		CSR_XS(csr_base, IMU_ERROR_LOG_ENABLE, (imu_log_enable |
2332435Sjchu 		    (imu_log_mask & px_imu_log_mask)));
2333435Sjchu 		CSR_XS(csr_base, IMU_INTERRUPT_ENABLE, (imu_intr_enable |
2334435Sjchu 		    (imu_intr_mask & px_imu_intr_mask)));
2335435Sjchu 
2336435Sjchu 		break;
2337435Sjchu 	}
2338435Sjchu 
2339435Sjchu 	return (B_TRUE);
2340435Sjchu }
2341435Sjchu 
23422053Sschwartz uint64_t
23432053Sschwartz px_get_rng_parent_hi_mask(px_t *px_p)
23442053Sschwartz {
23452053Sschwartz 	pxu_t *pxu_p = (pxu_t *)px_p->px_plat_p;
23462053Sschwartz 	uint64_t mask;
23472053Sschwartz 
23482053Sschwartz 	switch (PX_CHIP_TYPE(pxu_p)) {
23492053Sschwartz 	case PX_CHIP_OBERON:
23502053Sschwartz 		mask = OBERON_RANGE_PROP_MASK;
23512053Sschwartz 		break;
23522053Sschwartz 	case PX_CHIP_FIRE:
23532053Sschwartz 		mask = PX_RANGE_PROP_MASK;
23542053Sschwartz 		break;
23552053Sschwartz 	default:
23562053Sschwartz 		mask = PX_RANGE_PROP_MASK;
23572053Sschwartz 	}
23582053Sschwartz 
23592053Sschwartz 	return (mask);
23602053Sschwartz }
23612053Sschwartz 
2362435Sjchu /*
23631772Sjl139090  * fetch chip's range propery's value
23641772Sjl139090  */
23651772Sjl139090 uint64_t
23661772Sjl139090 px_get_range_prop(px_t *px_p, px_ranges_t *rp, int bank)
23671772Sjl139090 {
23681772Sjl139090 	uint64_t mask, range_prop;
23691772Sjl139090 
23702053Sschwartz 	mask = px_get_rng_parent_hi_mask(px_p);
23711772Sjl139090 	range_prop = (((uint64_t)(rp[bank].parent_high & mask)) << 32) |
23721772Sjl139090 		rp[bank].parent_low;
23731772Sjl139090 
23741772Sjl139090 	return (range_prop);
23751772Sjl139090 }
23761772Sjl139090 
23771772Sjl139090 /*
2378435Sjchu  * add cpr callback
2379435Sjchu  */
2380435Sjchu void
2381435Sjchu px_cpr_add_callb(px_t *px_p)
2382435Sjchu {
2383435Sjchu 	px_p->px_cprcb_id = callb_add(px_cpr_callb, (void *)px_p,
2384435Sjchu 	CB_CL_CPR_POST_USER, "px_cpr");
2385435Sjchu }
2386435Sjchu 
2387435Sjchu /*
2388435Sjchu  * remove cpr callback
2389435Sjchu  */
2390435Sjchu void
2391435Sjchu px_cpr_rem_callb(px_t *px_p)
2392435Sjchu {
2393435Sjchu 	(void) callb_delete(px_p->px_cprcb_id);
2394435Sjchu }
23951531Skini 
23961531Skini /*ARGSUSED*/
23971772Sjl139090 static uint_t
23981772Sjl139090 px_hp_intr(caddr_t arg1, caddr_t arg2)
23991772Sjl139090 {
24001772Sjl139090 	px_t *px_p = (px_t *)arg1;
24011772Sjl139090 	int rval;
24021772Sjl139090 
24031772Sjl139090 	rval = pciehpc_intr(px_p->px_dip);
24041772Sjl139090 
24051772Sjl139090 #ifdef  DEBUG
24061772Sjl139090 	if (rval == DDI_INTR_UNCLAIMED)
24071772Sjl139090 	    cmn_err(CE_WARN, "%s%d: UNCLAIMED intr\n",
24081772Sjl139090 		ddi_driver_name(px_p->px_dip),
24091772Sjl139090 		ddi_get_instance(px_p->px_dip));
24101772Sjl139090 #endif
24111772Sjl139090 
24121772Sjl139090 	return (rval);
24131772Sjl139090 }
24141772Sjl139090 
24151531Skini int
24161531Skini px_lib_hotplug_init(dev_info_t *dip, void *arg)
24171531Skini {
24181772Sjl139090 	px_t	*px_p = DIP_TO_STATE(dip);
24191772Sjl139090 	uint64_t ret;
24201772Sjl139090 
24211772Sjl139090 	if ((ret = hvio_hotplug_init(dip, arg)) == DDI_SUCCESS) {
24221772Sjl139090 		sysino_t sysino;
24231772Sjl139090 
24241772Sjl139090 		if (px_lib_intr_devino_to_sysino(px_p->px_dip,
24251772Sjl139090 		    px_p->px_inos[PX_INTR_HOTPLUG], &sysino) !=
24261772Sjl139090 		    DDI_SUCCESS) {
24271772Sjl139090 #ifdef	DEBUG
24281772Sjl139090 			cmn_err(CE_WARN, "%s%d: devino_to_sysino fails\n",
24291772Sjl139090 			    ddi_driver_name(px_p->px_dip),
24301772Sjl139090 			    ddi_get_instance(px_p->px_dip));
24311772Sjl139090 #endif
24321772Sjl139090 			return (DDI_FAILURE);
24331772Sjl139090 		}
24341772Sjl139090 
24351772Sjl139090 		VERIFY(add_ivintr(sysino, PX_PCIEHP_PIL,
24361772Sjl139090 		    (intrfunc)px_hp_intr, (caddr_t)px_p, NULL) == 0);
24371772Sjl139090 	}
24381772Sjl139090 
24391772Sjl139090 	return (ret);
24401531Skini }
24411531Skini 
24421531Skini void
24431531Skini px_lib_hotplug_uninit(dev_info_t *dip)
24441531Skini {
24451772Sjl139090 	if (hvio_hotplug_uninit(dip) == DDI_SUCCESS) {
24461772Sjl139090 		px_t	*px_p = DIP_TO_STATE(dip);
24471772Sjl139090 		sysino_t sysino;
24481772Sjl139090 
24491772Sjl139090 		if (px_lib_intr_devino_to_sysino(px_p->px_dip,
24501772Sjl139090 		    px_p->px_inos[PX_INTR_HOTPLUG], &sysino) !=
24511772Sjl139090 		    DDI_SUCCESS) {
24521772Sjl139090 #ifdef	DEBUG
24531772Sjl139090 			cmn_err(CE_WARN, "%s%d: devino_to_sysino fails\n",
24541772Sjl139090 			    ddi_driver_name(px_p->px_dip),
24551772Sjl139090 			    ddi_get_instance(px_p->px_dip));
24561772Sjl139090 #endif
24571772Sjl139090 			return;
24581772Sjl139090 		}
24591772Sjl139090 
24601772Sjl139090 		rem_ivintr(sysino, NULL);
24611772Sjl139090 	}
24621531Skini }
24632476Sdwoods 
24642476Sdwoods boolean_t
24652476Sdwoods px_lib_is_in_drain_state(px_t *px_p)
24662476Sdwoods {
24672476Sdwoods 	pxu_t 	*pxu_p = (pxu_t *)px_p->px_plat_p;
24682476Sdwoods 	caddr_t csr_base = (caddr_t)pxu_p->px_address[PX_REG_CSR];
24692476Sdwoods 	uint64_t drain_status;
24702476Sdwoods 
24712476Sdwoods 	if (PX_CHIP_TYPE(pxu_p) == PX_CHIP_OBERON) {
24722476Sdwoods 		drain_status = CSR_BR(csr_base, DRAIN_CONTROL_STATUS, DRAIN);
24732476Sdwoods 	} else {
24742476Sdwoods 		drain_status = CSR_BR(csr_base, TLU_STATUS, DRAIN);
24752476Sdwoods 	}
24762476Sdwoods 
24772476Sdwoods 	return (drain_status);
24782476Sdwoods }
2479