xref: /onnv-gate/usr/src/uts/sun4u/io/px/px_lib4u.c (revision 7403:59d121d61d7d)
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 /*
226313Skrishnae  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/kmem.h>
280Sstevel@tonic-gate #include <sys/conf.h>
290Sstevel@tonic-gate #include <sys/ddi.h>
300Sstevel@tonic-gate #include <sys/sunddi.h>
316313Skrishnae #include <sys/sunndi.h>
3227Sjchu #include <sys/fm/protocol.h>
3327Sjchu #include <sys/fm/util.h>
340Sstevel@tonic-gate #include <sys/modctl.h>
350Sstevel@tonic-gate #include <sys/disp.h>
360Sstevel@tonic-gate #include <sys/stat.h>
370Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
380Sstevel@tonic-gate #include <sys/vmem.h>
390Sstevel@tonic-gate #include <sys/iommutsb.h>
400Sstevel@tonic-gate #include <sys/cpuvar.h>
4127Sjchu #include <sys/ivintr.h>
42383Set142600 #include <sys/byteorder.h>
431531Skini #include <sys/hotplug/pci/pciehpc.h>
443623Sjchu #include <sys/spl.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",
886313Skrishnae 	    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 
2115328Sdanice 	(void) ddi_prop_update_int_array(DDI_DEV_T_NONE, dip,
2125328Sdanice 	    "virtual-dma", (int *)&px_dvma_range,
2135328Sdanice 	    sizeof (px_dvma_range_prop_t) / sizeof (int));
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 		break;
2441772Sjl139090 
2451772Sjl139090 	case PX_CHIP_FIRE:
2462509Sschwartz 		px_err_reg_setup_pcie(chip_mask, csr_base, PX_ERR_ENABLE);
2471772Sjl139090 		break;
2482509Sschwartz 
2491772Sjl139090 	default:
2501772Sjl139090 		cmn_err(CE_WARN, "%s%d: PX primary bus Unknown\n",
2511772Sjl139090 		    ddi_driver_name(dip), ddi_get_instance(dip));
2521772Sjl139090 		return (DDI_FAILURE);
2531772Sjl139090 	}
25427Sjchu 
2550Sstevel@tonic-gate 	/* Initilize device handle */
2560Sstevel@tonic-gate 	*dev_hdl = (devhandle_t)csr_base;
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	DBG(DBG_ATTACH, dip, "px_lib_dev_init: dev_hdl 0x%llx\n", *dev_hdl);
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	return (DDI_SUCCESS);
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate int
2640Sstevel@tonic-gate px_lib_dev_fini(dev_info_t *dip)
2650Sstevel@tonic-gate {
2662509Sschwartz 	caddr_t			csr_base;
2672509Sschwartz 	uint8_t			chip_mask;
2682509Sschwartz 	px_t			*px_p = DIP_TO_STATE(dip);
2692509Sschwartz 	pxu_t			*pxu_p = (pxu_t *)px_p->px_plat_p;
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	DBG(DBG_DETACH, dip, "px_lib_dev_fini: dip 0x%p\n", dip);
2720Sstevel@tonic-gate 
27327Sjchu 	/*
27427Sjchu 	 * Deinitialize all the interrupt handlers
27527Sjchu 	 */
2761772Sjl139090 	switch (PX_CHIP_TYPE(pxu_p)) {
2771772Sjl139090 	case PX_CHIP_OBERON:
2782509Sschwartz 	case PX_CHIP_FIRE:
2792509Sschwartz 		chip_mask = BITMASK(PX_CHIP_TYPE(pxu_p));
2802509Sschwartz 		csr_base = (caddr_t)pxu_p->px_address[PX_REG_CSR];
2812509Sschwartz 		px_err_reg_setup_pcie(chip_mask, csr_base, PX_ERR_DISABLE);
2821772Sjl139090 		break;
2832509Sschwartz 
2841772Sjl139090 	default:
2851772Sjl139090 		cmn_err(CE_WARN, "%s%d: PX primary bus Unknown\n",
2861772Sjl139090 		    ddi_driver_name(dip), ddi_get_instance(dip));
2871772Sjl139090 		return (DDI_FAILURE);
2881772Sjl139090 	}
28927Sjchu 
2900Sstevel@tonic-gate 	iommu_tsb_free(pxu_p->tsb_cookie);
2910Sstevel@tonic-gate 
29227Sjchu 	px_lib_unmap_regs((pxu_t *)px_p->px_plat_p);
29327Sjchu 	kmem_free(px_p->px_plat_p, sizeof (pxu_t));
2940Sstevel@tonic-gate 	px_p->px_plat_p = NULL;
2955328Sdanice 	(void) ddi_prop_remove(DDI_DEV_T_NONE, dip, "virtual-dma");
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 	px_dvma_addr_t	pg_index;
668*7403SAlan.Adamson@Sun.COM 	size_t		q_sz = msiq_state_p->msiq_rec_cnt * sizeof (msiq_rec_t);
6690Sstevel@tonic-gate 	size_t		size;
670*7403SAlan.Adamson@Sun.COM 	int		i, ret;
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_init: dip 0x%p\n", dip);
6730Sstevel@tonic-gate 
674*7403SAlan.Adamson@Sun.COM 	/* must aligned on q_sz (happens to be !!! page) boundary */
675*7403SAlan.Adamson@Sun.COM 	ASSERT(q_sz == 8 * 1024);
676*7403SAlan.Adamson@Sun.COM 
6770Sstevel@tonic-gate 	/*
6780Sstevel@tonic-gate 	 * Map the EQ memory into the Fire MMU (has to be 512KB aligned)
6790Sstevel@tonic-gate 	 * and then initialize the base address register.
6800Sstevel@tonic-gate 	 *
6810Sstevel@tonic-gate 	 * Allocate entries from Fire IOMMU so that the resulting address
6820Sstevel@tonic-gate 	 * is properly aligned.  Calculate the index of the first allocated
6830Sstevel@tonic-gate 	 * entry.  Note: The size of the mapping is assumed to be a multiple
6840Sstevel@tonic-gate 	 * of the page size.
6850Sstevel@tonic-gate 	 */
686*7403SAlan.Adamson@Sun.COM 	size = msiq_state_p->msiq_cnt * q_sz;
687*7403SAlan.Adamson@Sun.COM 
688*7403SAlan.Adamson@Sun.COM 	msiq_state_p->msiq_buf_p = kmem_zalloc(size, KM_SLEEP);
689*7403SAlan.Adamson@Sun.COM 
690*7403SAlan.Adamson@Sun.COM 	for (i = 0; i < msiq_state_p->msiq_cnt; i++)
691*7403SAlan.Adamson@Sun.COM 		msiq_state_p->msiq_p[i].msiq_base_p = (msiqhead_t *)
692*7403SAlan.Adamson@Sun.COM 		    ((caddr_t)msiq_state_p->msiq_buf_p + (i * q_sz));
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 	pxu_p->msiq_mapped_p = vmem_xalloc(px_p->px_mmu_p->mmu_dvma_map,
6950Sstevel@tonic-gate 	    size, (512 * 1024), 0, 0, NULL, NULL, VM_NOSLEEP | VM_BESTFIT);
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 	if (pxu_p->msiq_mapped_p == NULL)
6980Sstevel@tonic-gate 		return (DDI_FAILURE);
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	pg_index = MMU_PAGE_INDEX(px_p->px_mmu_p,
7010Sstevel@tonic-gate 	    MMU_BTOP((ulong_t)pxu_p->msiq_mapped_p));
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	if ((ret = px_lib_iommu_map(px_p->px_dip, PCI_TSBID(0, pg_index),
7042755Segillett 	    MMU_BTOP(size), PCI_MAP_ATTR_WRITE, msiq_state_p->msiq_buf_p,
7052755Segillett 	    0, MMU_MAP_BUF)) != DDI_SUCCESS) {
7060Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip,
7076983Sanbui 		    "px_lib_msiq_init: px_lib_iommu_map failed, "
7086983Sanbui 		    "ret 0x%lx\n", ret);
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 		(void) px_lib_msiq_fini(dip);
7110Sstevel@tonic-gate 		return (DDI_FAILURE);
7120Sstevel@tonic-gate 	}
7130Sstevel@tonic-gate 
7147124Sanbui 	if ((ret = hvio_msiq_init(DIP_TO_HANDLE(dip),
7157124Sanbui 	    pxu_p)) != H_EOK) {
7167124Sanbui 		DBG(DBG_LIB_MSIQ, dip,
7177124Sanbui 		    "hvio_msiq_init failed, ret 0x%lx\n", ret);
7187124Sanbui 
7197124Sanbui 		(void) px_lib_msiq_fini(dip);
7207124Sanbui 		return (DDI_FAILURE);
7217124Sanbui 	}
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 	return (DDI_SUCCESS);
7240Sstevel@tonic-gate }
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate /*ARGSUSED*/
7270Sstevel@tonic-gate int
7280Sstevel@tonic-gate px_lib_msiq_fini(dev_info_t *dip)
7290Sstevel@tonic-gate {
7300Sstevel@tonic-gate 	px_t		*px_p = DIP_TO_STATE(dip);
7310Sstevel@tonic-gate 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
7320Sstevel@tonic-gate 	px_msiq_state_t	*msiq_state_p = &px_p->px_ib_p->ib_msiq_state;
7330Sstevel@tonic-gate 	px_dvma_addr_t	pg_index;
7340Sstevel@tonic-gate 	size_t		size;
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_fini: dip 0x%p\n", dip);
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 	/*
7390Sstevel@tonic-gate 	 * Unmap and free the EQ memory that had been mapped
7400Sstevel@tonic-gate 	 * into the Fire IOMMU.
7410Sstevel@tonic-gate 	 */
7420Sstevel@tonic-gate 	size = msiq_state_p->msiq_cnt *
7430Sstevel@tonic-gate 	    msiq_state_p->msiq_rec_cnt * sizeof (msiq_rec_t);
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 	pg_index = MMU_PAGE_INDEX(px_p->px_mmu_p,
7460Sstevel@tonic-gate 	    MMU_BTOP((ulong_t)pxu_p->msiq_mapped_p));
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 	(void) px_lib_iommu_demap(px_p->px_dip,
7490Sstevel@tonic-gate 	    PCI_TSBID(0, pg_index), MMU_BTOP(size));
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	/* Free the entries from the Fire MMU */
7520Sstevel@tonic-gate 	vmem_xfree(px_p->px_mmu_p->mmu_dvma_map,
7530Sstevel@tonic-gate 	    (void *)pxu_p->msiq_mapped_p, size);
7540Sstevel@tonic-gate 
755*7403SAlan.Adamson@Sun.COM 	kmem_free(msiq_state_p->msiq_buf_p, msiq_state_p->msiq_cnt *
756*7403SAlan.Adamson@Sun.COM 	    msiq_state_p->msiq_rec_cnt * sizeof (msiq_rec_t));
757*7403SAlan.Adamson@Sun.COM 
7580Sstevel@tonic-gate 	return (DDI_SUCCESS);
7590Sstevel@tonic-gate }
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate /*ARGSUSED*/
7620Sstevel@tonic-gate int
7630Sstevel@tonic-gate px_lib_msiq_info(dev_info_t *dip, msiqid_t msiq_id, r_addr_t *ra_p,
7640Sstevel@tonic-gate     uint_t *msiq_rec_cnt_p)
7650Sstevel@tonic-gate {
7660Sstevel@tonic-gate 	px_t		*px_p = DIP_TO_STATE(dip);
7670Sstevel@tonic-gate 	px_msiq_state_t	*msiq_state_p = &px_p->px_ib_p->ib_msiq_state;
7680Sstevel@tonic-gate 	size_t		msiq_size;
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_msiq_info: dip 0x%p msiq_id 0x%x\n",
7710Sstevel@tonic-gate 	    dip, msiq_id);
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	msiq_size = msiq_state_p->msiq_rec_cnt * sizeof (msiq_rec_t);
7742755Segillett 	ra_p = (r_addr_t *)((caddr_t)msiq_state_p->msiq_buf_p +
7752755Segillett 	    (msiq_id * msiq_size));
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 	*msiq_rec_cnt_p = msiq_state_p->msiq_rec_cnt;
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_msiq_info: ra_p 0x%p msiq_rec_cnt 0x%x\n",
7800Sstevel@tonic-gate 	    ra_p, *msiq_rec_cnt_p);
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 	return (DDI_SUCCESS);
7830Sstevel@tonic-gate }
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate /*ARGSUSED*/
7860Sstevel@tonic-gate int
7870Sstevel@tonic-gate px_lib_msiq_getvalid(dev_info_t *dip, msiqid_t msiq_id,
7880Sstevel@tonic-gate     pci_msiq_valid_state_t *msiq_valid_state)
7890Sstevel@tonic-gate {
7900Sstevel@tonic-gate 	uint64_t	ret;
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_getvalid: dip 0x%p msiq_id 0x%x\n",
7930Sstevel@tonic-gate 	    dip, msiq_id);
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	if ((ret = hvio_msiq_getvalid(DIP_TO_HANDLE(dip),
7960Sstevel@tonic-gate 	    msiq_id, msiq_valid_state)) != H_EOK) {
7970Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip,
7980Sstevel@tonic-gate 		    "hvio_msiq_getvalid failed, ret 0x%lx\n", ret);
7990Sstevel@tonic-gate 		return (DDI_FAILURE);
8000Sstevel@tonic-gate 	}
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_getvalid: msiq_valid_state 0x%x\n",
8030Sstevel@tonic-gate 	    *msiq_valid_state);
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	return (DDI_SUCCESS);
8060Sstevel@tonic-gate }
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate /*ARGSUSED*/
8090Sstevel@tonic-gate int
8100Sstevel@tonic-gate px_lib_msiq_setvalid(dev_info_t *dip, msiqid_t msiq_id,
8110Sstevel@tonic-gate     pci_msiq_valid_state_t msiq_valid_state)
8120Sstevel@tonic-gate {
8130Sstevel@tonic-gate 	uint64_t	ret;
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_setvalid: dip 0x%p msiq_id 0x%x "
8160Sstevel@tonic-gate 	    "msiq_valid_state 0x%x\n", dip, msiq_id, msiq_valid_state);
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	if ((ret = hvio_msiq_setvalid(DIP_TO_HANDLE(dip),
8190Sstevel@tonic-gate 	    msiq_id, msiq_valid_state)) != H_EOK) {
8200Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip,
8210Sstevel@tonic-gate 		    "hvio_msiq_setvalid failed, ret 0x%lx\n", ret);
8220Sstevel@tonic-gate 		return (DDI_FAILURE);
8230Sstevel@tonic-gate 	}
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	return (DDI_SUCCESS);
8260Sstevel@tonic-gate }
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate /*ARGSUSED*/
8290Sstevel@tonic-gate int
8300Sstevel@tonic-gate px_lib_msiq_getstate(dev_info_t *dip, msiqid_t msiq_id,
8310Sstevel@tonic-gate     pci_msiq_state_t *msiq_state)
8320Sstevel@tonic-gate {
8330Sstevel@tonic-gate 	uint64_t	ret;
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_getstate: dip 0x%p msiq_id 0x%x\n",
8360Sstevel@tonic-gate 	    dip, msiq_id);
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 	if ((ret = hvio_msiq_getstate(DIP_TO_HANDLE(dip),
8390Sstevel@tonic-gate 	    msiq_id, msiq_state)) != H_EOK) {
8400Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip,
8410Sstevel@tonic-gate 		    "hvio_msiq_getstate failed, ret 0x%lx\n", ret);
8420Sstevel@tonic-gate 		return (DDI_FAILURE);
8430Sstevel@tonic-gate 	}
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_getstate: msiq_state 0x%x\n",
8460Sstevel@tonic-gate 	    *msiq_state);
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	return (DDI_SUCCESS);
8490Sstevel@tonic-gate }
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate /*ARGSUSED*/
8520Sstevel@tonic-gate int
8530Sstevel@tonic-gate px_lib_msiq_setstate(dev_info_t *dip, msiqid_t msiq_id,
8540Sstevel@tonic-gate     pci_msiq_state_t msiq_state)
8550Sstevel@tonic-gate {
8560Sstevel@tonic-gate 	uint64_t	ret;
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_setstate: dip 0x%p msiq_id 0x%x "
8590Sstevel@tonic-gate 	    "msiq_state 0x%x\n", dip, msiq_id, msiq_state);
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	if ((ret = hvio_msiq_setstate(DIP_TO_HANDLE(dip),
8620Sstevel@tonic-gate 	    msiq_id, msiq_state)) != H_EOK) {
8630Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip,
8640Sstevel@tonic-gate 		    "hvio_msiq_setstate failed, ret 0x%lx\n", ret);
8650Sstevel@tonic-gate 		return (DDI_FAILURE);
8660Sstevel@tonic-gate 	}
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate 	return (DDI_SUCCESS);
8690Sstevel@tonic-gate }
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate /*ARGSUSED*/
8720Sstevel@tonic-gate int
8730Sstevel@tonic-gate px_lib_msiq_gethead(dev_info_t *dip, msiqid_t msiq_id,
8740Sstevel@tonic-gate     msiqhead_t *msiq_head)
8750Sstevel@tonic-gate {
8760Sstevel@tonic-gate 	uint64_t	ret;
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_gethead: dip 0x%p msiq_id 0x%x\n",
8790Sstevel@tonic-gate 	    dip, msiq_id);
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 	if ((ret = hvio_msiq_gethead(DIP_TO_HANDLE(dip),
8820Sstevel@tonic-gate 	    msiq_id, msiq_head)) != H_EOK) {
8830Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip,
8840Sstevel@tonic-gate 		    "hvio_msiq_gethead failed, ret 0x%lx\n", ret);
8850Sstevel@tonic-gate 		return (DDI_FAILURE);
8860Sstevel@tonic-gate 	}
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_gethead: msiq_head 0x%x\n",
8890Sstevel@tonic-gate 	    *msiq_head);
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 	return (DDI_SUCCESS);
8920Sstevel@tonic-gate }
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate /*ARGSUSED*/
8950Sstevel@tonic-gate int
8960Sstevel@tonic-gate px_lib_msiq_sethead(dev_info_t *dip, msiqid_t msiq_id,
8970Sstevel@tonic-gate     msiqhead_t msiq_head)
8980Sstevel@tonic-gate {
8990Sstevel@tonic-gate 	uint64_t	ret;
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_sethead: dip 0x%p msiq_id 0x%x "
9020Sstevel@tonic-gate 	    "msiq_head 0x%x\n", dip, msiq_id, msiq_head);
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 	if ((ret = hvio_msiq_sethead(DIP_TO_HANDLE(dip),
9050Sstevel@tonic-gate 	    msiq_id, msiq_head)) != H_EOK) {
9060Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip,
9070Sstevel@tonic-gate 		    "hvio_msiq_sethead failed, ret 0x%lx\n", ret);
9080Sstevel@tonic-gate 		return (DDI_FAILURE);
9090Sstevel@tonic-gate 	}
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 	return (DDI_SUCCESS);
9120Sstevel@tonic-gate }
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate /*ARGSUSED*/
9150Sstevel@tonic-gate int
9160Sstevel@tonic-gate px_lib_msiq_gettail(dev_info_t *dip, msiqid_t msiq_id,
9170Sstevel@tonic-gate     msiqtail_t *msiq_tail)
9180Sstevel@tonic-gate {
9190Sstevel@tonic-gate 	uint64_t	ret;
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_gettail: dip 0x%p msiq_id 0x%x\n",
9220Sstevel@tonic-gate 	    dip, msiq_id);
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 	if ((ret = hvio_msiq_gettail(DIP_TO_HANDLE(dip),
9250Sstevel@tonic-gate 	    msiq_id, msiq_tail)) != H_EOK) {
9260Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip,
9270Sstevel@tonic-gate 		    "hvio_msiq_gettail failed, ret 0x%lx\n", ret);
9280Sstevel@tonic-gate 		return (DDI_FAILURE);
9290Sstevel@tonic-gate 	}
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_gettail: msiq_tail 0x%x\n",
9320Sstevel@tonic-gate 	    *msiq_tail);
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 	return (DDI_SUCCESS);
9350Sstevel@tonic-gate }
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate /*ARGSUSED*/
9380Sstevel@tonic-gate void
9392588Segillett px_lib_get_msiq_rec(dev_info_t *dip, msiqhead_t *msiq_head_p,
9402588Segillett     msiq_rec_t *msiq_rec_p)
9410Sstevel@tonic-gate {
9422588Segillett 	eq_rec_t	*eq_rec_p = (eq_rec_t *)msiq_head_p;
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_get_msiq_rec: dip 0x%p eq_rec_p 0x%p\n",
9450Sstevel@tonic-gate 	    dip, eq_rec_p);
9460Sstevel@tonic-gate 
947287Smg140465 	if (!eq_rec_p->eq_rec_fmt_type) {
948287Smg140465 		/* Set msiq_rec_type to zero */
949287Smg140465 		msiq_rec_p->msiq_rec_type = 0;
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 		return;
9520Sstevel@tonic-gate 	}
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_get_msiq_rec: EQ RECORD, "
9550Sstevel@tonic-gate 	    "eq_rec_rid 0x%llx eq_rec_fmt_type 0x%llx "
9560Sstevel@tonic-gate 	    "eq_rec_len 0x%llx eq_rec_addr0 0x%llx "
9570Sstevel@tonic-gate 	    "eq_rec_addr1 0x%llx eq_rec_data0 0x%llx "
9580Sstevel@tonic-gate 	    "eq_rec_data1 0x%llx\n", eq_rec_p->eq_rec_rid,
9590Sstevel@tonic-gate 	    eq_rec_p->eq_rec_fmt_type, eq_rec_p->eq_rec_len,
9600Sstevel@tonic-gate 	    eq_rec_p->eq_rec_addr0, eq_rec_p->eq_rec_addr1,
9610Sstevel@tonic-gate 	    eq_rec_p->eq_rec_data0, eq_rec_p->eq_rec_data1);
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate 	/*
9640Sstevel@tonic-gate 	 * Only upper 4 bits of eq_rec_fmt_type is used
9650Sstevel@tonic-gate 	 * to identify the EQ record type.
9660Sstevel@tonic-gate 	 */
9670Sstevel@tonic-gate 	switch (eq_rec_p->eq_rec_fmt_type >> 3) {
9680Sstevel@tonic-gate 	case EQ_REC_MSI32:
9690Sstevel@tonic-gate 		msiq_rec_p->msiq_rec_type = MSI32_REC;
9700Sstevel@tonic-gate 
971225Sess 		msiq_rec_p->msiq_rec_data.msi.msi_data =
972225Sess 		    eq_rec_p->eq_rec_data0;
9730Sstevel@tonic-gate 		break;
9740Sstevel@tonic-gate 	case EQ_REC_MSI64:
9750Sstevel@tonic-gate 		msiq_rec_p->msiq_rec_type = MSI64_REC;
9760Sstevel@tonic-gate 
977225Sess 		msiq_rec_p->msiq_rec_data.msi.msi_data =
978225Sess 		    eq_rec_p->eq_rec_data0;
9790Sstevel@tonic-gate 		break;
9800Sstevel@tonic-gate 	case EQ_REC_MSG:
9810Sstevel@tonic-gate 		msiq_rec_p->msiq_rec_type = MSG_REC;
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 		msiq_rec_p->msiq_rec_data.msg.msg_route =
9840Sstevel@tonic-gate 		    eq_rec_p->eq_rec_fmt_type & 7;
9850Sstevel@tonic-gate 		msiq_rec_p->msiq_rec_data.msg.msg_targ = eq_rec_p->eq_rec_rid;
9860Sstevel@tonic-gate 		msiq_rec_p->msiq_rec_data.msg.msg_code = eq_rec_p->eq_rec_data0;
9870Sstevel@tonic-gate 		break;
9880Sstevel@tonic-gate 	default:
9890Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: px_lib_get_msiq_rec: "
990671Skrishnae 		    "0x%x is an unknown EQ record type",
9910Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip),
992671Skrishnae 		    (int)eq_rec_p->eq_rec_fmt_type);
9930Sstevel@tonic-gate 		break;
9940Sstevel@tonic-gate 	}
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 	msiq_rec_p->msiq_rec_rid = eq_rec_p->eq_rec_rid;
9970Sstevel@tonic-gate 	msiq_rec_p->msiq_rec_msi_addr = ((eq_rec_p->eq_rec_addr1 << 16) |
9980Sstevel@tonic-gate 	    (eq_rec_p->eq_rec_addr0 << 2));
9992973Sgovinda }
10002973Sgovinda 
10012973Sgovinda /*ARGSUSED*/
10022973Sgovinda void
10032973Sgovinda px_lib_clr_msiq_rec(dev_info_t *dip, msiqhead_t *msiq_head_p)
10042973Sgovinda {
10052973Sgovinda 	eq_rec_t	*eq_rec_p = (eq_rec_t *)msiq_head_p;
10062973Sgovinda 
10072973Sgovinda 	DBG(DBG_LIB_MSIQ, dip, "px_lib_clr_msiq_rec: dip 0x%p eq_rec_p 0x%p\n",
10082973Sgovinda 	    dip, eq_rec_p);
10092973Sgovinda 
10102973Sgovinda 	if (eq_rec_p->eq_rec_fmt_type) {
10112973Sgovinda 		/* Zero out eq_rec_fmt_type field */
10122973Sgovinda 		eq_rec_p->eq_rec_fmt_type = 0;
10132973Sgovinda 	}
10140Sstevel@tonic-gate }
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate /*
10170Sstevel@tonic-gate  * MSI Functions:
10180Sstevel@tonic-gate  */
10190Sstevel@tonic-gate /*ARGSUSED*/
10200Sstevel@tonic-gate int
10210Sstevel@tonic-gate px_lib_msi_init(dev_info_t *dip)
10220Sstevel@tonic-gate {
10230Sstevel@tonic-gate 	px_t		*px_p = DIP_TO_STATE(dip);
10240Sstevel@tonic-gate 	px_msi_state_t	*msi_state_p = &px_p->px_ib_p->ib_msi_state;
10250Sstevel@tonic-gate 	uint64_t	ret;
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_init: dip 0x%p\n", dip);
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 	if ((ret = hvio_msi_init(DIP_TO_HANDLE(dip),
10300Sstevel@tonic-gate 	    msi_state_p->msi_addr32, msi_state_p->msi_addr64)) != H_EOK) {
10310Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip, "px_lib_msi_init failed, ret 0x%lx\n",
10320Sstevel@tonic-gate 		    ret);
10330Sstevel@tonic-gate 		return (DDI_FAILURE);
10340Sstevel@tonic-gate 	}
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 	return (DDI_SUCCESS);
10370Sstevel@tonic-gate }
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate /*ARGSUSED*/
10400Sstevel@tonic-gate int
10410Sstevel@tonic-gate px_lib_msi_getmsiq(dev_info_t *dip, msinum_t msi_num,
10420Sstevel@tonic-gate     msiqid_t *msiq_id)
10430Sstevel@tonic-gate {
10440Sstevel@tonic-gate 	uint64_t	ret;
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getmsiq: dip 0x%p msi_num 0x%x\n",
10470Sstevel@tonic-gate 	    dip, msi_num);
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 	if ((ret = hvio_msi_getmsiq(DIP_TO_HANDLE(dip),
10500Sstevel@tonic-gate 	    msi_num, msiq_id)) != H_EOK) {
10510Sstevel@tonic-gate 		DBG(DBG_LIB_MSI, dip,
10520Sstevel@tonic-gate 		    "hvio_msi_getmsiq failed, ret 0x%lx\n", ret);
10530Sstevel@tonic-gate 		return (DDI_FAILURE);
10540Sstevel@tonic-gate 	}
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getmsiq: msiq_id 0x%x\n",
10570Sstevel@tonic-gate 	    *msiq_id);
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 	return (DDI_SUCCESS);
10600Sstevel@tonic-gate }
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate /*ARGSUSED*/
10630Sstevel@tonic-gate int
10640Sstevel@tonic-gate px_lib_msi_setmsiq(dev_info_t *dip, msinum_t msi_num,
10650Sstevel@tonic-gate     msiqid_t msiq_id, msi_type_t msitype)
10660Sstevel@tonic-gate {
10670Sstevel@tonic-gate 	uint64_t	ret;
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_setmsiq: dip 0x%p msi_num 0x%x "
10700Sstevel@tonic-gate 	    "msq_id 0x%x\n", dip, msi_num, msiq_id);
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 	if ((ret = hvio_msi_setmsiq(DIP_TO_HANDLE(dip),
10730Sstevel@tonic-gate 	    msi_num, msiq_id)) != H_EOK) {
10740Sstevel@tonic-gate 		DBG(DBG_LIB_MSI, dip,
10750Sstevel@tonic-gate 		    "hvio_msi_setmsiq failed, ret 0x%lx\n", ret);
10760Sstevel@tonic-gate 		return (DDI_FAILURE);
10770Sstevel@tonic-gate 	}
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 	return (DDI_SUCCESS);
10800Sstevel@tonic-gate }
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate /*ARGSUSED*/
10830Sstevel@tonic-gate int
10840Sstevel@tonic-gate px_lib_msi_getvalid(dev_info_t *dip, msinum_t msi_num,
10850Sstevel@tonic-gate     pci_msi_valid_state_t *msi_valid_state)
10860Sstevel@tonic-gate {
10870Sstevel@tonic-gate 	uint64_t	ret;
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getvalid: dip 0x%p msi_num 0x%x\n",
10900Sstevel@tonic-gate 	    dip, msi_num);
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate 	if ((ret = hvio_msi_getvalid(DIP_TO_HANDLE(dip),
10930Sstevel@tonic-gate 	    msi_num, msi_valid_state)) != H_EOK) {
10940Sstevel@tonic-gate 		DBG(DBG_LIB_MSI, dip,
10950Sstevel@tonic-gate 		    "hvio_msi_getvalid failed, ret 0x%lx\n", ret);
10960Sstevel@tonic-gate 		return (DDI_FAILURE);
10970Sstevel@tonic-gate 	}
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getvalid: msiq_id 0x%x\n",
11000Sstevel@tonic-gate 	    *msi_valid_state);
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 	return (DDI_SUCCESS);
11030Sstevel@tonic-gate }
11040Sstevel@tonic-gate 
11050Sstevel@tonic-gate /*ARGSUSED*/
11060Sstevel@tonic-gate int
11070Sstevel@tonic-gate px_lib_msi_setvalid(dev_info_t *dip, msinum_t msi_num,
11080Sstevel@tonic-gate     pci_msi_valid_state_t msi_valid_state)
11090Sstevel@tonic-gate {
11100Sstevel@tonic-gate 	uint64_t	ret;
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_setvalid: dip 0x%p msi_num 0x%x "
11130Sstevel@tonic-gate 	    "msi_valid_state 0x%x\n", dip, msi_num, msi_valid_state);
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 	if ((ret = hvio_msi_setvalid(DIP_TO_HANDLE(dip),
11160Sstevel@tonic-gate 	    msi_num, msi_valid_state)) != H_EOK) {
11170Sstevel@tonic-gate 		DBG(DBG_LIB_MSI, dip,
11180Sstevel@tonic-gate 		    "hvio_msi_setvalid failed, ret 0x%lx\n", ret);
11190Sstevel@tonic-gate 		return (DDI_FAILURE);
11200Sstevel@tonic-gate 	}
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 	return (DDI_SUCCESS);
11230Sstevel@tonic-gate }
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate /*ARGSUSED*/
11260Sstevel@tonic-gate int
11270Sstevel@tonic-gate px_lib_msi_getstate(dev_info_t *dip, msinum_t msi_num,
11280Sstevel@tonic-gate     pci_msi_state_t *msi_state)
11290Sstevel@tonic-gate {
11300Sstevel@tonic-gate 	uint64_t	ret;
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getstate: dip 0x%p msi_num 0x%x\n",
11330Sstevel@tonic-gate 	    dip, msi_num);
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 	if ((ret = hvio_msi_getstate(DIP_TO_HANDLE(dip),
11360Sstevel@tonic-gate 	    msi_num, msi_state)) != H_EOK) {
11370Sstevel@tonic-gate 		DBG(DBG_LIB_MSI, dip,
11380Sstevel@tonic-gate 		    "hvio_msi_getstate failed, ret 0x%lx\n", ret);
11390Sstevel@tonic-gate 		return (DDI_FAILURE);
11400Sstevel@tonic-gate 	}
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getstate: msi_state 0x%x\n",
11430Sstevel@tonic-gate 	    *msi_state);
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 	return (DDI_SUCCESS);
11460Sstevel@tonic-gate }
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate /*ARGSUSED*/
11490Sstevel@tonic-gate int
11500Sstevel@tonic-gate px_lib_msi_setstate(dev_info_t *dip, msinum_t msi_num,
11510Sstevel@tonic-gate     pci_msi_state_t msi_state)
11520Sstevel@tonic-gate {
11530Sstevel@tonic-gate 	uint64_t	ret;
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_setstate: dip 0x%p msi_num 0x%x "
11560Sstevel@tonic-gate 	    "msi_state 0x%x\n", dip, msi_num, msi_state);
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 	if ((ret = hvio_msi_setstate(DIP_TO_HANDLE(dip),
11590Sstevel@tonic-gate 	    msi_num, msi_state)) != H_EOK) {
11600Sstevel@tonic-gate 		DBG(DBG_LIB_MSI, dip,
11610Sstevel@tonic-gate 		    "hvio_msi_setstate failed, ret 0x%lx\n", ret);
11620Sstevel@tonic-gate 		return (DDI_FAILURE);
11630Sstevel@tonic-gate 	}
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate 	return (DDI_SUCCESS);
11660Sstevel@tonic-gate }
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate /*
11690Sstevel@tonic-gate  * MSG Functions:
11700Sstevel@tonic-gate  */
11710Sstevel@tonic-gate /*ARGSUSED*/
11720Sstevel@tonic-gate int
11730Sstevel@tonic-gate px_lib_msg_getmsiq(dev_info_t *dip, pcie_msg_type_t msg_type,
11740Sstevel@tonic-gate     msiqid_t *msiq_id)
11750Sstevel@tonic-gate {
11760Sstevel@tonic-gate 	uint64_t	ret;
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 	DBG(DBG_LIB_MSG, dip, "px_lib_msg_getmsiq: dip 0x%p msg_type 0x%x\n",
11790Sstevel@tonic-gate 	    dip, msg_type);
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 	if ((ret = hvio_msg_getmsiq(DIP_TO_HANDLE(dip),
11820Sstevel@tonic-gate 	    msg_type, msiq_id)) != H_EOK) {
11830Sstevel@tonic-gate 		DBG(DBG_LIB_MSG, dip,
11840Sstevel@tonic-gate 		    "hvio_msg_getmsiq failed, ret 0x%lx\n", ret);
11850Sstevel@tonic-gate 		return (DDI_FAILURE);
11860Sstevel@tonic-gate 	}
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msg_getmsiq: msiq_id 0x%x\n",
11890Sstevel@tonic-gate 	    *msiq_id);
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate 	return (DDI_SUCCESS);
11920Sstevel@tonic-gate }
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate /*ARGSUSED*/
11950Sstevel@tonic-gate int
11960Sstevel@tonic-gate px_lib_msg_setmsiq(dev_info_t *dip, pcie_msg_type_t msg_type,
11970Sstevel@tonic-gate     msiqid_t msiq_id)
11980Sstevel@tonic-gate {
11990Sstevel@tonic-gate 	uint64_t	ret;
12000Sstevel@tonic-gate 
12010Sstevel@tonic-gate 	DBG(DBG_LIB_MSG, dip, "px_lib_msi_setstate: dip 0x%p msg_type 0x%x "
12020Sstevel@tonic-gate 	    "msiq_id 0x%x\n", dip, msg_type, msiq_id);
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 	if ((ret = hvio_msg_setmsiq(DIP_TO_HANDLE(dip),
12050Sstevel@tonic-gate 	    msg_type, msiq_id)) != H_EOK) {
12060Sstevel@tonic-gate 		DBG(DBG_LIB_MSG, dip,
12070Sstevel@tonic-gate 		    "hvio_msg_setmsiq failed, ret 0x%lx\n", ret);
12080Sstevel@tonic-gate 		return (DDI_FAILURE);
12090Sstevel@tonic-gate 	}
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 	return (DDI_SUCCESS);
12120Sstevel@tonic-gate }
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate /*ARGSUSED*/
12150Sstevel@tonic-gate int
12160Sstevel@tonic-gate px_lib_msg_getvalid(dev_info_t *dip, pcie_msg_type_t msg_type,
12170Sstevel@tonic-gate     pcie_msg_valid_state_t *msg_valid_state)
12180Sstevel@tonic-gate {
12190Sstevel@tonic-gate 	uint64_t	ret;
12200Sstevel@tonic-gate 
12210Sstevel@tonic-gate 	DBG(DBG_LIB_MSG, dip, "px_lib_msg_getvalid: dip 0x%p msg_type 0x%x\n",
12220Sstevel@tonic-gate 	    dip, msg_type);
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate 	if ((ret = hvio_msg_getvalid(DIP_TO_HANDLE(dip), msg_type,
12250Sstevel@tonic-gate 	    msg_valid_state)) != H_EOK) {
12260Sstevel@tonic-gate 		DBG(DBG_LIB_MSG, dip,
12270Sstevel@tonic-gate 		    "hvio_msg_getvalid failed, ret 0x%lx\n", ret);
12280Sstevel@tonic-gate 		return (DDI_FAILURE);
12290Sstevel@tonic-gate 	}
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msg_getvalid: msg_valid_state 0x%x\n",
12320Sstevel@tonic-gate 	    *msg_valid_state);
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate 	return (DDI_SUCCESS);
12350Sstevel@tonic-gate }
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate /*ARGSUSED*/
12380Sstevel@tonic-gate int
12390Sstevel@tonic-gate px_lib_msg_setvalid(dev_info_t *dip, pcie_msg_type_t msg_type,
12400Sstevel@tonic-gate     pcie_msg_valid_state_t msg_valid_state)
12410Sstevel@tonic-gate {
12420Sstevel@tonic-gate 	uint64_t	ret;
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate 	DBG(DBG_LIB_MSG, dip, "px_lib_msg_setvalid: dip 0x%p msg_type 0x%x "
12450Sstevel@tonic-gate 	    "msg_valid_state 0x%x\n", dip, msg_type, msg_valid_state);
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate 	if ((ret = hvio_msg_setvalid(DIP_TO_HANDLE(dip), msg_type,
12480Sstevel@tonic-gate 	    msg_valid_state)) != H_EOK) {
12490Sstevel@tonic-gate 		DBG(DBG_LIB_MSG, dip,
12500Sstevel@tonic-gate 		    "hvio_msg_setvalid failed, ret 0x%lx\n", ret);
12510Sstevel@tonic-gate 		return (DDI_FAILURE);
12520Sstevel@tonic-gate 	}
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate 	return (DDI_SUCCESS);
12550Sstevel@tonic-gate }
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate /*
12580Sstevel@tonic-gate  * Suspend/Resume Functions:
12590Sstevel@tonic-gate  * Currently unsupported by hypervisor
12600Sstevel@tonic-gate  */
12610Sstevel@tonic-gate int
12620Sstevel@tonic-gate px_lib_suspend(dev_info_t *dip)
12630Sstevel@tonic-gate {
12640Sstevel@tonic-gate 	px_t		*px_p = DIP_TO_STATE(dip);
12650Sstevel@tonic-gate 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
12661648Sjchu 	px_cb_t		*cb_p = PX2CB(px_p);
12670Sstevel@tonic-gate 	devhandle_t	dev_hdl, xbus_dev_hdl;
12681648Sjchu 	uint64_t	ret = H_EOK;
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate 	DBG(DBG_DETACH, dip, "px_lib_suspend: dip 0x%p\n", dip);
12710Sstevel@tonic-gate 
127227Sjchu 	dev_hdl = (devhandle_t)pxu_p->px_address[PX_REG_CSR];
127327Sjchu 	xbus_dev_hdl = (devhandle_t)pxu_p->px_address[PX_REG_XBC];
12740Sstevel@tonic-gate 
12751648Sjchu 	if ((ret = hvio_suspend(dev_hdl, pxu_p)) != H_EOK)
12761648Sjchu 		goto fail;
12771648Sjchu 
12781648Sjchu 	if (--cb_p->attachcnt == 0) {
12791648Sjchu 		ret = hvio_cb_suspend(xbus_dev_hdl, pxu_p);
12801648Sjchu 		if (ret != H_EOK)
12811648Sjchu 			cb_p->attachcnt++;
12820Sstevel@tonic-gate 	}
12833274Set142600 	pxu_p->cpr_flag = PX_ENTERED_CPR;
12840Sstevel@tonic-gate 
12851648Sjchu fail:
12860Sstevel@tonic-gate 	return ((ret != H_EOK) ? DDI_FAILURE: DDI_SUCCESS);
12870Sstevel@tonic-gate }
12880Sstevel@tonic-gate 
12890Sstevel@tonic-gate void
12900Sstevel@tonic-gate px_lib_resume(dev_info_t *dip)
12910Sstevel@tonic-gate {
12920Sstevel@tonic-gate 	px_t		*px_p = DIP_TO_STATE(dip);
12930Sstevel@tonic-gate 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
12941648Sjchu 	px_cb_t		*cb_p = PX2CB(px_p);
12950Sstevel@tonic-gate 	devhandle_t	dev_hdl, xbus_dev_hdl;
12960Sstevel@tonic-gate 	devino_t	pec_ino = px_p->px_inos[PX_INTR_PEC];
12970Sstevel@tonic-gate 	devino_t	xbc_ino = px_p->px_inos[PX_INTR_XBC];
12980Sstevel@tonic-gate 
12990Sstevel@tonic-gate 	DBG(DBG_ATTACH, dip, "px_lib_resume: dip 0x%p\n", dip);
13000Sstevel@tonic-gate 
130127Sjchu 	dev_hdl = (devhandle_t)pxu_p->px_address[PX_REG_CSR];
130227Sjchu 	xbus_dev_hdl = (devhandle_t)pxu_p->px_address[PX_REG_XBC];
13030Sstevel@tonic-gate 
13041648Sjchu 	if (++cb_p->attachcnt == 1)
13050Sstevel@tonic-gate 		hvio_cb_resume(dev_hdl, xbus_dev_hdl, xbc_ino, pxu_p);
13060Sstevel@tonic-gate 
13071648Sjchu 	hvio_resume(dev_hdl, pec_ino, pxu_p);
13080Sstevel@tonic-gate }
13090Sstevel@tonic-gate 
13101772Sjl139090 /*
13111772Sjl139090  * Generate a unique Oberon UBC ID based on the Logicial System Board and
13121772Sjl139090  * the IO Channel from the portid property field.
13131772Sjl139090  */
13141772Sjl139090 static uint64_t
13151772Sjl139090 oberon_get_ubc_id(dev_info_t *dip)
13161772Sjl139090 {
13171772Sjl139090 	px_t	*px_p = DIP_TO_STATE(dip);
13181772Sjl139090 	pxu_t	*pxu_p = (pxu_t *)px_p->px_plat_p;
13191772Sjl139090 	uint64_t	ubc_id;
13201772Sjl139090 
13211772Sjl139090 	/*
13221772Sjl139090 	 * Generate a unique 6 bit UBC ID using the 2 IO_Channel#[1:0] bits and
13231772Sjl139090 	 * the 4 LSB_ID[3:0] bits from the Oberon's portid property.
13241772Sjl139090 	 */
13251772Sjl139090 	ubc_id = (((pxu_p->portid >> OBERON_PORT_ID_IOC) &
13261772Sjl139090 	    OBERON_PORT_ID_IOC_MASK) | (((pxu_p->portid >>
13271772Sjl139090 	    OBERON_PORT_ID_LSB) & OBERON_PORT_ID_LSB_MASK)
13281772Sjl139090 	    << OBERON_UBC_ID_LSB));
13291772Sjl139090 
13301772Sjl139090 	return (ubc_id);
13311772Sjl139090 }
13321772Sjl139090 
13331772Sjl139090 /*
13341772Sjl139090  * Oberon does not have a UBC scratch register, so alloc an array of scratch
13351772Sjl139090  * registers when needed and use a unique UBC ID as an index. This code
13361772Sjl139090  * can be simplified if we use a pre-allocated array. They are currently
13371772Sjl139090  * being dynamically allocated because it's only needed by the Oberon.
13381772Sjl139090  */
13391772Sjl139090 static void
13401772Sjl139090 oberon_set_cb(dev_info_t *dip, uint64_t val)
13411772Sjl139090 {
13421772Sjl139090 	uint64_t	ubc_id;
13431772Sjl139090 
13441772Sjl139090 	if (px_oberon_ubc_scratch_regs == NULL)
13451772Sjl139090 		px_oberon_ubc_scratch_regs =
13461772Sjl139090 		    (uint64_t *)kmem_zalloc(sizeof (uint64_t)*
13471772Sjl139090 		    OBERON_UBC_ID_MAX, KM_SLEEP);
13481772Sjl139090 
13491772Sjl139090 	ubc_id = oberon_get_ubc_id(dip);
13501772Sjl139090 
13511772Sjl139090 	px_oberon_ubc_scratch_regs[ubc_id] = val;
13521772Sjl139090 
13531772Sjl139090 	/*
13541772Sjl139090 	 * Check if any scratch registers are still in use. If all scratch
13551772Sjl139090 	 * registers are currently set to zero, then deallocate the scratch
13561772Sjl139090 	 * register array.
13571772Sjl139090 	 */
13581772Sjl139090 	for (ubc_id = 0; ubc_id < OBERON_UBC_ID_MAX; ubc_id++) {
13591772Sjl139090 		if (px_oberon_ubc_scratch_regs[ubc_id] != NULL)
13601772Sjl139090 			return;
13611772Sjl139090 	}
13621772Sjl139090 
13631772Sjl139090 	/*
13641772Sjl139090 	 * All scratch registers are set to zero so deallocate the scratch
13651772Sjl139090 	 * register array and set the pointer to NULL.
13661772Sjl139090 	 */
13671772Sjl139090 	kmem_free(px_oberon_ubc_scratch_regs,
13681772Sjl139090 	    (sizeof (uint64_t)*OBERON_UBC_ID_MAX));
13691772Sjl139090 
13701772Sjl139090 	px_oberon_ubc_scratch_regs = NULL;
13711772Sjl139090 }
13721772Sjl139090 
13731772Sjl139090 /*
13741772Sjl139090  * Oberon does not have a UBC scratch register, so use an allocated array of
13751772Sjl139090  * scratch registers and use the unique UBC ID as an index into that array.
13761772Sjl139090  */
13771772Sjl139090 static uint64_t
13781772Sjl139090 oberon_get_cb(dev_info_t *dip)
13791772Sjl139090 {
13801772Sjl139090 	uint64_t	ubc_id;
13811772Sjl139090 
13821772Sjl139090 	if (px_oberon_ubc_scratch_regs == NULL)
13831772Sjl139090 		return (0);
13841772Sjl139090 
13851772Sjl139090 	ubc_id = oberon_get_ubc_id(dip);
13861772Sjl139090 
13871772Sjl139090 	return (px_oberon_ubc_scratch_regs[ubc_id]);
13881772Sjl139090 }
13891772Sjl139090 
13901772Sjl139090 /*
13911772Sjl139090  * Misc Functions:
13921772Sjl139090  * Currently unsupported by hypervisor
13931772Sjl139090  */
13941772Sjl139090 static uint64_t
13951772Sjl139090 px_get_cb(dev_info_t *dip)
13961772Sjl139090 {
13971772Sjl139090 	px_t	*px_p = DIP_TO_STATE(dip);
13981772Sjl139090 	pxu_t	*pxu_p = (pxu_t *)px_p->px_plat_p;
13991772Sjl139090 
14001772Sjl139090 	/*
14011772Sjl139090 	 * Oberon does not currently have Scratchpad registers.
14021772Sjl139090 	 */
14031772Sjl139090 	if (PX_CHIP_TYPE(pxu_p) == PX_CHIP_OBERON)
14041772Sjl139090 		return (oberon_get_cb(dip));
14051772Sjl139090 
14061772Sjl139090 	return (CSR_XR((caddr_t)pxu_p->px_address[PX_REG_XBC], JBUS_SCRATCH_1));
14071772Sjl139090 }
14081772Sjl139090 
14091772Sjl139090 static void
14101772Sjl139090 px_set_cb(dev_info_t *dip, uint64_t val)
14111772Sjl139090 {
14121772Sjl139090 	px_t	*px_p = DIP_TO_STATE(dip);
14131772Sjl139090 	pxu_t	*pxu_p = (pxu_t *)px_p->px_plat_p;
14141772Sjl139090 
14151772Sjl139090 	/*
14161772Sjl139090 	 * Oberon does not currently have Scratchpad registers.
14171772Sjl139090 	 */
14181772Sjl139090 	if (PX_CHIP_TYPE(pxu_p) == PX_CHIP_OBERON) {
14191772Sjl139090 		oberon_set_cb(dip, val);
14201772Sjl139090 		return;
14211772Sjl139090 	}
14221772Sjl139090 
14231772Sjl139090 	CSR_XS((caddr_t)pxu_p->px_address[PX_REG_XBC], JBUS_SCRATCH_1, val);
14241772Sjl139090 }
14251772Sjl139090 
14260Sstevel@tonic-gate /*ARGSUSED*/
14270Sstevel@tonic-gate int
14280Sstevel@tonic-gate px_lib_map_vconfig(dev_info_t *dip,
14290Sstevel@tonic-gate 	ddi_map_req_t *mp, pci_config_offset_t off,
14300Sstevel@tonic-gate 		pci_regspec_t *rp, caddr_t *addrp)
14310Sstevel@tonic-gate {
14320Sstevel@tonic-gate 	/*
14330Sstevel@tonic-gate 	 * No special config space access services in this layer.
14340Sstevel@tonic-gate 	 */
14350Sstevel@tonic-gate 	return (DDI_FAILURE);
14360Sstevel@tonic-gate }
14370Sstevel@tonic-gate 
1438624Sschwartz void
1439677Sjchu px_lib_map_attr_check(ddi_map_req_t *mp)
1440677Sjchu {
1441677Sjchu 	ddi_acc_hdl_t *hp = mp->map_handlep;
1442677Sjchu 
1443677Sjchu 	/* fire does not accept byte masks from PIO store merge */
1444677Sjchu 	if (hp->ah_acc.devacc_attr_dataorder == DDI_STORECACHING_OK_ACC)
1445677Sjchu 		hp->ah_acc.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
1446677Sjchu }
1447677Sjchu 
14483274Set142600 /* This function is called only by poke, caut put and pxtool poke. */
1449677Sjchu void
14503274Set142600 px_lib_clr_errs(px_t *px_p, dev_info_t *rdip, uint64_t addr)
145127Sjchu {
1452624Sschwartz 	px_pec_t	*pec_p = px_p->px_pec_p;
145327Sjchu 	dev_info_t	*rpdip = px_p->px_dip;
14543274Set142600 	int		rc_err, fab_err, i;
145527Sjchu 	int		acctype = pec_p->pec_safeacc_type;
145627Sjchu 	ddi_fm_error_t	derr;
14573274Set142600 	px_ranges_t	*ranges_p;
14583274Set142600 	int		range_len;
14593274Set142600 	uint32_t	addr_high, addr_low;
14603274Set142600 	pcie_req_id_t	bdf = 0;
146127Sjchu 
146227Sjchu 	/* Create the derr */
146327Sjchu 	bzero(&derr, sizeof (ddi_fm_error_t));
146427Sjchu 	derr.fme_version = DDI_FME_VERSION;
146527Sjchu 	derr.fme_ena = fm_ena_generate(0, FM_ENA_FMT1);
146627Sjchu 	derr.fme_flag = acctype;
146727Sjchu 
146827Sjchu 	if (acctype == DDI_FM_ERR_EXPECTED) {
146927Sjchu 		derr.fme_status = DDI_FM_NONFATAL;
147027Sjchu 		ndi_fm_acc_err_set(pec_p->pec_acc_hdl, &derr);
147127Sjchu 	}
147227Sjchu 
14736313Skrishnae 	if (px_fm_enter(px_p) != DDI_SUCCESS)
14746313Skrishnae 		return;
147527Sjchu 
147627Sjchu 	/* send ereport/handle/clear fire registers */
14773274Set142600 	rc_err = px_err_cmn_intr(px_p, &derr, PX_LIB_CALL, PX_FM_BLOCK_ALL);
14783274Set142600 
14793274Set142600 	/* Figure out if this is a cfg or mem32 access */
14803274Set142600 	addr_high = (uint32_t)(addr >> 32);
14813274Set142600 	addr_low = (uint32_t)addr;
14823274Set142600 	range_len = px_p->px_ranges_length / sizeof (px_ranges_t);
14833274Set142600 	i = 0;
14843274Set142600 	for (ranges_p = px_p->px_ranges_p; i < range_len; i++, ranges_p++) {
14853274Set142600 		if (ranges_p->parent_high == addr_high) {
14863274Set142600 			switch (ranges_p->child_high & PCI_ADDR_MASK) {
14873274Set142600 			case PCI_ADDR_CONFIG:
14883274Set142600 				bdf = (pcie_req_id_t)(addr_low >> 12);
14893274Set142600 				addr_low = 0;
14903274Set142600 				break;
14913274Set142600 			case PCI_ADDR_MEM32:
14923274Set142600 				if (rdip)
14936313Skrishnae 					bdf = PCI_GET_BDF(rdip);
14943274Set142600 				else
14953274Set142600 					bdf = NULL;
14963274Set142600 				break;
14973274Set142600 			}
14983274Set142600 			break;
14993274Set142600 		}
15003274Set142600 	}
15013274Set142600 
15023274Set142600 	px_rp_en_q(px_p, bdf, addr_low, NULL);
15033274Set142600 
15043274Set142600 	/*
15053274Set142600 	 * XXX - Current code scans the fabric for all px_tool accesses.
15063274Set142600 	 * In future, do not scan fabric for px_tool access to IO Root Nexus
15073274Set142600 	 */
15086313Skrishnae 	fab_err = px_scan_fabric(px_p, rpdip, &derr);
15096313Skrishnae 
15106313Skrishnae 	px_err_panic(rc_err, PX_RC, fab_err, B_TRUE);
15116313Skrishnae 	px_fm_exit(px_p);
15126313Skrishnae 	px_err_panic(rc_err, PX_RC, fab_err, B_FALSE);
151327Sjchu }
151427Sjchu 
15150Sstevel@tonic-gate #ifdef  DEBUG
15160Sstevel@tonic-gate int	px_peekfault_cnt = 0;
15170Sstevel@tonic-gate int	px_pokefault_cnt = 0;
15180Sstevel@tonic-gate #endif  /* DEBUG */
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate /*ARGSUSED*/
15210Sstevel@tonic-gate static int
15220Sstevel@tonic-gate px_lib_do_poke(dev_info_t *dip, dev_info_t *rdip,
15230Sstevel@tonic-gate     peekpoke_ctlops_t *in_args)
15240Sstevel@tonic-gate {
15250Sstevel@tonic-gate 	px_t *px_p = DIP_TO_STATE(dip);
15260Sstevel@tonic-gate 	px_pec_t *pec_p = px_p->px_pec_p;
15270Sstevel@tonic-gate 	int err = DDI_SUCCESS;
15280Sstevel@tonic-gate 	on_trap_data_t otd;
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate 	mutex_enter(&pec_p->pec_pokefault_mutex);
15310Sstevel@tonic-gate 	pec_p->pec_ontrap_data = &otd;
153227Sjchu 	pec_p->pec_safeacc_type = DDI_FM_ERR_POKE;
15330Sstevel@tonic-gate 
15340Sstevel@tonic-gate 	/* Set up protected environment. */
15350Sstevel@tonic-gate 	if (!on_trap(&otd, OT_DATA_ACCESS)) {
15360Sstevel@tonic-gate 		uintptr_t tramp = otd.ot_trampoline;
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate 		otd.ot_trampoline = (uintptr_t)&poke_fault;
15390Sstevel@tonic-gate 		err = do_poke(in_args->size, (void *)in_args->dev_addr,
15400Sstevel@tonic-gate 		    (void *)in_args->host_addr);
15410Sstevel@tonic-gate 		otd.ot_trampoline = tramp;
15420Sstevel@tonic-gate 	} else
15430Sstevel@tonic-gate 		err = DDI_FAILURE;
15440Sstevel@tonic-gate 
15453274Set142600 	px_lib_clr_errs(px_p, rdip, in_args->dev_addr);
154627Sjchu 
15470Sstevel@tonic-gate 	if (otd.ot_trap & OT_DATA_ACCESS)
15480Sstevel@tonic-gate 		err = DDI_FAILURE;
15490Sstevel@tonic-gate 
15500Sstevel@tonic-gate 	/* Take down protected environment. */
15510Sstevel@tonic-gate 	no_trap();
15520Sstevel@tonic-gate 
15530Sstevel@tonic-gate 	pec_p->pec_ontrap_data = NULL;
155427Sjchu 	pec_p->pec_safeacc_type = DDI_FM_ERR_UNEXPECTED;
15550Sstevel@tonic-gate 	mutex_exit(&pec_p->pec_pokefault_mutex);
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate #ifdef  DEBUG
15580Sstevel@tonic-gate 	if (err == DDI_FAILURE)
15590Sstevel@tonic-gate 		px_pokefault_cnt++;
15600Sstevel@tonic-gate #endif
15610Sstevel@tonic-gate 	return (err);
15620Sstevel@tonic-gate }
15630Sstevel@tonic-gate 
15640Sstevel@tonic-gate /*ARGSUSED*/
15650Sstevel@tonic-gate static int
15660Sstevel@tonic-gate px_lib_do_caut_put(dev_info_t *dip, dev_info_t *rdip,
15670Sstevel@tonic-gate     peekpoke_ctlops_t *cautacc_ctlops_arg)
15680Sstevel@tonic-gate {
15690Sstevel@tonic-gate 	size_t size = cautacc_ctlops_arg->size;
15700Sstevel@tonic-gate 	uintptr_t dev_addr = cautacc_ctlops_arg->dev_addr;
15710Sstevel@tonic-gate 	uintptr_t host_addr = cautacc_ctlops_arg->host_addr;
15720Sstevel@tonic-gate 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)cautacc_ctlops_arg->handle;
15730Sstevel@tonic-gate 	size_t repcount = cautacc_ctlops_arg->repcount;
15740Sstevel@tonic-gate 	uint_t flags = cautacc_ctlops_arg->flags;
15750Sstevel@tonic-gate 
15760Sstevel@tonic-gate 	px_t *px_p = DIP_TO_STATE(dip);
15770Sstevel@tonic-gate 	px_pec_t *pec_p = px_p->px_pec_p;
15780Sstevel@tonic-gate 	int err = DDI_SUCCESS;
15790Sstevel@tonic-gate 
158027Sjchu 	/*
158127Sjchu 	 * Note that i_ndi_busop_access_enter ends up grabbing the pokefault
158227Sjchu 	 * mutex.
158327Sjchu 	 */
15840Sstevel@tonic-gate 	i_ndi_busop_access_enter(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp);
15850Sstevel@tonic-gate 
158627Sjchu 	pec_p->pec_ontrap_data = (on_trap_data_t *)hp->ahi_err->err_ontrap;
158727Sjchu 	pec_p->pec_safeacc_type = DDI_FM_ERR_EXPECTED;
158827Sjchu 	hp->ahi_err->err_expected = DDI_FM_ERR_EXPECTED;
15890Sstevel@tonic-gate 
15900Sstevel@tonic-gate 	if (!i_ddi_ontrap((ddi_acc_handle_t)hp)) {
15910Sstevel@tonic-gate 		for (; repcount; repcount--) {
15920Sstevel@tonic-gate 			switch (size) {
15930Sstevel@tonic-gate 
15940Sstevel@tonic-gate 			case sizeof (uint8_t):
15950Sstevel@tonic-gate 				i_ddi_put8(hp, (uint8_t *)dev_addr,
15960Sstevel@tonic-gate 				    *(uint8_t *)host_addr);
15970Sstevel@tonic-gate 				break;
15980Sstevel@tonic-gate 
15990Sstevel@tonic-gate 			case sizeof (uint16_t):
16000Sstevel@tonic-gate 				i_ddi_put16(hp, (uint16_t *)dev_addr,
16010Sstevel@tonic-gate 				    *(uint16_t *)host_addr);
16020Sstevel@tonic-gate 				break;
16030Sstevel@tonic-gate 
16040Sstevel@tonic-gate 			case sizeof (uint32_t):
16050Sstevel@tonic-gate 				i_ddi_put32(hp, (uint32_t *)dev_addr,
16060Sstevel@tonic-gate 				    *(uint32_t *)host_addr);
16070Sstevel@tonic-gate 				break;
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate 			case sizeof (uint64_t):
16100Sstevel@tonic-gate 				i_ddi_put64(hp, (uint64_t *)dev_addr,
16110Sstevel@tonic-gate 				    *(uint64_t *)host_addr);
16120Sstevel@tonic-gate 				break;
16130Sstevel@tonic-gate 			}
16140Sstevel@tonic-gate 
16150Sstevel@tonic-gate 			host_addr += size;
16160Sstevel@tonic-gate 
16170Sstevel@tonic-gate 			if (flags == DDI_DEV_AUTOINCR)
16180Sstevel@tonic-gate 				dev_addr += size;
16190Sstevel@tonic-gate 
16203274Set142600 			px_lib_clr_errs(px_p, rdip, dev_addr);
162127Sjchu 
16220Sstevel@tonic-gate 			if (pec_p->pec_ontrap_data->ot_trap & OT_DATA_ACCESS) {
16230Sstevel@tonic-gate 				err = DDI_FAILURE;
16240Sstevel@tonic-gate #ifdef  DEBUG
16250Sstevel@tonic-gate 				px_pokefault_cnt++;
16260Sstevel@tonic-gate #endif
16270Sstevel@tonic-gate 				break;
16280Sstevel@tonic-gate 			}
16290Sstevel@tonic-gate 		}
16300Sstevel@tonic-gate 	}
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate 	i_ddi_notrap((ddi_acc_handle_t)hp);
16330Sstevel@tonic-gate 	pec_p->pec_ontrap_data = NULL;
163427Sjchu 	pec_p->pec_safeacc_type = DDI_FM_ERR_UNEXPECTED;
16350Sstevel@tonic-gate 	i_ndi_busop_access_exit(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp);
16360Sstevel@tonic-gate 	hp->ahi_err->err_expected = DDI_FM_ERR_UNEXPECTED;
16370Sstevel@tonic-gate 
16380Sstevel@tonic-gate 	return (err);
16390Sstevel@tonic-gate }
16400Sstevel@tonic-gate 
16410Sstevel@tonic-gate 
16420Sstevel@tonic-gate int
16430Sstevel@tonic-gate px_lib_ctlops_poke(dev_info_t *dip, dev_info_t *rdip,
16440Sstevel@tonic-gate     peekpoke_ctlops_t *in_args)
16450Sstevel@tonic-gate {
16460Sstevel@tonic-gate 	return (in_args->handle ? px_lib_do_caut_put(dip, rdip, in_args) :
16470Sstevel@tonic-gate 	    px_lib_do_poke(dip, rdip, in_args));
16480Sstevel@tonic-gate }
16490Sstevel@tonic-gate 
16500Sstevel@tonic-gate 
16510Sstevel@tonic-gate /*ARGSUSED*/
16520Sstevel@tonic-gate static int
16530Sstevel@tonic-gate px_lib_do_peek(dev_info_t *dip, peekpoke_ctlops_t *in_args)
16540Sstevel@tonic-gate {
165527Sjchu 	px_t *px_p = DIP_TO_STATE(dip);
165627Sjchu 	px_pec_t *pec_p = px_p->px_pec_p;
16570Sstevel@tonic-gate 	int err = DDI_SUCCESS;
16580Sstevel@tonic-gate 	on_trap_data_t otd;
16590Sstevel@tonic-gate 
166027Sjchu 	mutex_enter(&pec_p->pec_pokefault_mutex);
16616313Skrishnae 	if (px_fm_enter(px_p) != DDI_SUCCESS)
16626313Skrishnae 		return (DDI_FAILURE);
166327Sjchu 	pec_p->pec_safeacc_type = DDI_FM_ERR_PEEK;
16646313Skrishnae 	px_fm_exit(px_p);
166527Sjchu 
16660Sstevel@tonic-gate 	if (!on_trap(&otd, OT_DATA_ACCESS)) {
16670Sstevel@tonic-gate 		uintptr_t tramp = otd.ot_trampoline;
16680Sstevel@tonic-gate 
16690Sstevel@tonic-gate 		otd.ot_trampoline = (uintptr_t)&peek_fault;
16700Sstevel@tonic-gate 		err = do_peek(in_args->size, (void *)in_args->dev_addr,
16710Sstevel@tonic-gate 		    (void *)in_args->host_addr);
16720Sstevel@tonic-gate 		otd.ot_trampoline = tramp;
16730Sstevel@tonic-gate 	} else
16740Sstevel@tonic-gate 		err = DDI_FAILURE;
16750Sstevel@tonic-gate 
16760Sstevel@tonic-gate 	no_trap();
167727Sjchu 	pec_p->pec_safeacc_type = DDI_FM_ERR_UNEXPECTED;
167827Sjchu 	mutex_exit(&pec_p->pec_pokefault_mutex);
16790Sstevel@tonic-gate 
16800Sstevel@tonic-gate #ifdef  DEBUG
16810Sstevel@tonic-gate 	if (err == DDI_FAILURE)
16820Sstevel@tonic-gate 		px_peekfault_cnt++;
16830Sstevel@tonic-gate #endif
16840Sstevel@tonic-gate 	return (err);
16850Sstevel@tonic-gate }
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate 
16880Sstevel@tonic-gate static int
16890Sstevel@tonic-gate px_lib_do_caut_get(dev_info_t *dip, peekpoke_ctlops_t *cautacc_ctlops_arg)
16900Sstevel@tonic-gate {
16910Sstevel@tonic-gate 	size_t size = cautacc_ctlops_arg->size;
16920Sstevel@tonic-gate 	uintptr_t dev_addr = cautacc_ctlops_arg->dev_addr;
16930Sstevel@tonic-gate 	uintptr_t host_addr = cautacc_ctlops_arg->host_addr;
16940Sstevel@tonic-gate 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)cautacc_ctlops_arg->handle;
16950Sstevel@tonic-gate 	size_t repcount = cautacc_ctlops_arg->repcount;
16960Sstevel@tonic-gate 	uint_t flags = cautacc_ctlops_arg->flags;
16970Sstevel@tonic-gate 
16980Sstevel@tonic-gate 	px_t *px_p = DIP_TO_STATE(dip);
16990Sstevel@tonic-gate 	px_pec_t *pec_p = px_p->px_pec_p;
17000Sstevel@tonic-gate 	int err = DDI_SUCCESS;
17010Sstevel@tonic-gate 
170227Sjchu 	/*
170327Sjchu 	 * Note that i_ndi_busop_access_enter ends up grabbing the pokefault
170427Sjchu 	 * mutex.
170527Sjchu 	 */
170627Sjchu 	i_ndi_busop_access_enter(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp);
170727Sjchu 
170827Sjchu 	pec_p->pec_ontrap_data = (on_trap_data_t *)hp->ahi_err->err_ontrap;
170927Sjchu 	pec_p->pec_safeacc_type = DDI_FM_ERR_EXPECTED;
17100Sstevel@tonic-gate 	hp->ahi_err->err_expected = DDI_FM_ERR_EXPECTED;
17110Sstevel@tonic-gate 
17120Sstevel@tonic-gate 	if (repcount == 1) {
17130Sstevel@tonic-gate 		if (!i_ddi_ontrap((ddi_acc_handle_t)hp)) {
17140Sstevel@tonic-gate 			i_ddi_caut_get(size, (void *)dev_addr,
17150Sstevel@tonic-gate 			    (void *)host_addr);
17160Sstevel@tonic-gate 		} else {
17170Sstevel@tonic-gate 			int i;
17180Sstevel@tonic-gate 			uint8_t *ff_addr = (uint8_t *)host_addr;
17190Sstevel@tonic-gate 			for (i = 0; i < size; i++)
17200Sstevel@tonic-gate 				*ff_addr++ = 0xff;
17210Sstevel@tonic-gate 
17220Sstevel@tonic-gate 			err = DDI_FAILURE;
17230Sstevel@tonic-gate #ifdef  DEBUG
17240Sstevel@tonic-gate 			px_peekfault_cnt++;
17250Sstevel@tonic-gate #endif
17260Sstevel@tonic-gate 		}
17270Sstevel@tonic-gate 	} else {
17280Sstevel@tonic-gate 		if (!i_ddi_ontrap((ddi_acc_handle_t)hp)) {
17290Sstevel@tonic-gate 			for (; repcount; repcount--) {
17300Sstevel@tonic-gate 				i_ddi_caut_get(size, (void *)dev_addr,
17310Sstevel@tonic-gate 				    (void *)host_addr);
17320Sstevel@tonic-gate 
17330Sstevel@tonic-gate 				host_addr += size;
17340Sstevel@tonic-gate 
17350Sstevel@tonic-gate 				if (flags == DDI_DEV_AUTOINCR)
17360Sstevel@tonic-gate 					dev_addr += size;
17370Sstevel@tonic-gate 			}
17380Sstevel@tonic-gate 		} else {
17390Sstevel@tonic-gate 			err = DDI_FAILURE;
17400Sstevel@tonic-gate #ifdef  DEBUG
17410Sstevel@tonic-gate 			px_peekfault_cnt++;
17420Sstevel@tonic-gate #endif
17430Sstevel@tonic-gate 		}
17440Sstevel@tonic-gate 	}
17450Sstevel@tonic-gate 
17460Sstevel@tonic-gate 	i_ddi_notrap((ddi_acc_handle_t)hp);
17470Sstevel@tonic-gate 	pec_p->pec_ontrap_data = NULL;
174827Sjchu 	pec_p->pec_safeacc_type = DDI_FM_ERR_UNEXPECTED;
17490Sstevel@tonic-gate 	i_ndi_busop_access_exit(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp);
17500Sstevel@tonic-gate 	hp->ahi_err->err_expected = DDI_FM_ERR_UNEXPECTED;
17510Sstevel@tonic-gate 
17520Sstevel@tonic-gate 	return (err);
17530Sstevel@tonic-gate }
17540Sstevel@tonic-gate 
17550Sstevel@tonic-gate /*ARGSUSED*/
17560Sstevel@tonic-gate int
17570Sstevel@tonic-gate px_lib_ctlops_peek(dev_info_t *dip, dev_info_t *rdip,
17580Sstevel@tonic-gate     peekpoke_ctlops_t *in_args, void *result)
17590Sstevel@tonic-gate {
17600Sstevel@tonic-gate 	result = (void *)in_args->host_addr;
17610Sstevel@tonic-gate 	return (in_args->handle ? px_lib_do_caut_get(dip, in_args) :
17620Sstevel@tonic-gate 	    px_lib_do_peek(dip, in_args));
17630Sstevel@tonic-gate }
1764118Sjchu 
17650Sstevel@tonic-gate /*
17660Sstevel@tonic-gate  * implements PPM interface
17670Sstevel@tonic-gate  */
17680Sstevel@tonic-gate int
17690Sstevel@tonic-gate px_lib_pmctl(int cmd, px_t *px_p)
17700Sstevel@tonic-gate {
17710Sstevel@tonic-gate 	ASSERT((cmd & ~PPMREQ_MASK) == PPMREQ);
17720Sstevel@tonic-gate 	switch (cmd) {
17730Sstevel@tonic-gate 	case PPMREQ_PRE_PWR_OFF:
17740Sstevel@tonic-gate 		/*
17750Sstevel@tonic-gate 		 * Currently there is no device power management for
17760Sstevel@tonic-gate 		 * the root complex (fire). When there is we need to make
17770Sstevel@tonic-gate 		 * sure that it is at full power before trying to send the
17780Sstevel@tonic-gate 		 * PME_Turn_Off message.
17790Sstevel@tonic-gate 		 */
17800Sstevel@tonic-gate 		DBG(DBG_PWR, px_p->px_dip,
17810Sstevel@tonic-gate 		    "ioctl: request to send PME_Turn_Off\n");
17820Sstevel@tonic-gate 		return (px_goto_l23ready(px_p));
17830Sstevel@tonic-gate 
17840Sstevel@tonic-gate 	case PPMREQ_PRE_PWR_ON:
1785118Sjchu 		DBG(DBG_PWR, px_p->px_dip, "ioctl: PRE_PWR_ON request\n");
1786118Sjchu 		return (px_pre_pwron_check(px_p));
1787118Sjchu 
17880Sstevel@tonic-gate 	case PPMREQ_POST_PWR_ON:
1789118Sjchu 		DBG(DBG_PWR, px_p->px_dip, "ioctl: POST_PWR_ON request\n");
1790118Sjchu 		return (px_goto_l0(px_p));
17910Sstevel@tonic-gate 
17920Sstevel@tonic-gate 	default:
17930Sstevel@tonic-gate 		return (DDI_FAILURE);
17940Sstevel@tonic-gate 	}
17950Sstevel@tonic-gate }
17960Sstevel@tonic-gate 
17970Sstevel@tonic-gate /*
17980Sstevel@tonic-gate  * sends PME_Turn_Off message to put the link in L2/L3 ready state.
17990Sstevel@tonic-gate  * called by px_ioctl.
18000Sstevel@tonic-gate  * returns DDI_SUCCESS or DDI_FAILURE
18010Sstevel@tonic-gate  * 1. Wait for link to be in L1 state (link status reg)
18020Sstevel@tonic-gate  * 2. write to PME_Turn_off reg to boradcast
18030Sstevel@tonic-gate  * 3. set timeout
18040Sstevel@tonic-gate  * 4. If timeout, return failure.
18050Sstevel@tonic-gate  * 5. If PM_TO_Ack, wait till link is in L2/L3 ready
18060Sstevel@tonic-gate  */
18070Sstevel@tonic-gate static int
18080Sstevel@tonic-gate px_goto_l23ready(px_t *px_p)
18090Sstevel@tonic-gate {
18100Sstevel@tonic-gate 	pcie_pwr_t	*pwr_p;
181127Sjchu 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
181227Sjchu 	caddr_t	csr_base = (caddr_t)pxu_p->px_address[PX_REG_CSR];
18130Sstevel@tonic-gate 	int		ret = DDI_SUCCESS;
18140Sstevel@tonic-gate 	clock_t		end, timeleft;
1815118Sjchu 	int		mutex_held = 1;
18160Sstevel@tonic-gate 
18170Sstevel@tonic-gate 	/* If no PM info, return failure */
18180Sstevel@tonic-gate 	if (!PCIE_PMINFO(px_p->px_dip) ||
18190Sstevel@tonic-gate 	    !(pwr_p = PCIE_NEXUS_PMINFO(px_p->px_dip)))
18200Sstevel@tonic-gate 		return (DDI_FAILURE);
18210Sstevel@tonic-gate 
18220Sstevel@tonic-gate 	mutex_enter(&pwr_p->pwr_lock);
1823118Sjchu 	mutex_enter(&px_p->px_l23ready_lock);
18240Sstevel@tonic-gate 	/* Clear the PME_To_ACK receieved flag */
1825118Sjchu 	px_p->px_pm_flags &= ~PX_PMETOACK_RECVD;
1826287Smg140465 	/*
1827287Smg140465 	 * When P25 is the downstream device, after receiving
1828287Smg140465 	 * PME_To_ACK, fire will go to Detect state, which causes
1829287Smg140465 	 * the link down event. Inform FMA that this is expected.
1830287Smg140465 	 * In case of all other cards complaint with the pci express
1831287Smg140465 	 * spec, this will happen when the power is re-applied. FMA
1832287Smg140465 	 * code will clear this flag after one instance of LDN. Since
1833287Smg140465 	 * there will not be a LDN event for the spec compliant cards,
1834287Smg140465 	 * we need to clear the flag after receiving PME_To_ACK.
1835287Smg140465 	 */
1836287Smg140465 	px_p->px_pm_flags |= PX_LDN_EXPECTED;
18370Sstevel@tonic-gate 	if (px_send_pme_turnoff(csr_base) != DDI_SUCCESS) {
18380Sstevel@tonic-gate 		ret = DDI_FAILURE;
18390Sstevel@tonic-gate 		goto l23ready_done;
18400Sstevel@tonic-gate 	}
1841118Sjchu 	px_p->px_pm_flags |= PX_PME_TURNOFF_PENDING;
18420Sstevel@tonic-gate 
18430Sstevel@tonic-gate 	end = ddi_get_lbolt() + drv_usectohz(px_pme_to_ack_timeout);
1844118Sjchu 	while (!(px_p->px_pm_flags & PX_PMETOACK_RECVD)) {
1845118Sjchu 		timeleft = cv_timedwait(&px_p->px_l23ready_cv,
1846118Sjchu 		    &px_p->px_l23ready_lock, end);
18470Sstevel@tonic-gate 		/*
18480Sstevel@tonic-gate 		 * if cv_timedwait returns -1, it is either
18490Sstevel@tonic-gate 		 * 1) timed out or
18500Sstevel@tonic-gate 		 * 2) there was a pre-mature wakeup but by the time
18510Sstevel@tonic-gate 		 * cv_timedwait is called again end < lbolt i.e.
18520Sstevel@tonic-gate 		 * end is in the past.
18530Sstevel@tonic-gate 		 * 3) By the time we make first cv_timedwait call,
18540Sstevel@tonic-gate 		 * end < lbolt is true.
18550Sstevel@tonic-gate 		 */
18560Sstevel@tonic-gate 		if (timeleft == -1)
18570Sstevel@tonic-gate 			break;
18580Sstevel@tonic-gate 	}
1859118Sjchu 	if (!(px_p->px_pm_flags & PX_PMETOACK_RECVD)) {
18600Sstevel@tonic-gate 		/*
18610Sstevel@tonic-gate 		 * Either timedout or interrupt didn't get a
18620Sstevel@tonic-gate 		 * chance to grab the mutex and set the flag.
18630Sstevel@tonic-gate 		 * release the mutex and delay for sometime.
18640Sstevel@tonic-gate 		 * This will 1) give a chance for interrupt to
18650Sstevel@tonic-gate 		 * set the flag 2) creates a delay between two
18660Sstevel@tonic-gate 		 * consequetive requests.
18670Sstevel@tonic-gate 		 */
1868118Sjchu 		mutex_exit(&px_p->px_l23ready_lock);
18691147Sjchu 		delay(drv_usectohz(50 * PX_MSEC_TO_USEC));
1870118Sjchu 		mutex_held = 0;
1871118Sjchu 		if (!(px_p->px_pm_flags & PX_PMETOACK_RECVD)) {
18720Sstevel@tonic-gate 			ret = DDI_FAILURE;
18730Sstevel@tonic-gate 			DBG(DBG_PWR, px_p->px_dip, " Timed out while waiting"
18740Sstevel@tonic-gate 			    " for PME_TO_ACK\n");
18750Sstevel@tonic-gate 		}
18760Sstevel@tonic-gate 	}
1877287Smg140465 	px_p->px_pm_flags &=
1878287Smg140465 	    ~(PX_PME_TURNOFF_PENDING | PX_PMETOACK_RECVD | PX_LDN_EXPECTED);
18790Sstevel@tonic-gate 
18800Sstevel@tonic-gate l23ready_done:
1881118Sjchu 	if (mutex_held)
1882118Sjchu 		mutex_exit(&px_p->px_l23ready_lock);
1883118Sjchu 	/*
1884118Sjchu 	 * Wait till link is in L1 idle, if sending PME_Turn_Off
1885118Sjchu 	 * was succesful.
1886118Sjchu 	 */
1887118Sjchu 	if (ret == DDI_SUCCESS) {
1888118Sjchu 		if (px_link_wait4l1idle(csr_base) != DDI_SUCCESS) {
1889118Sjchu 			DBG(DBG_PWR, px_p->px_dip, " Link is not at L1"
1890287Smg140465 			    " even though we received PME_To_ACK.\n");
1891287Smg140465 			/*
1892287Smg140465 			 * Workaround for hardware bug with P25.
1893287Smg140465 			 * Due to a hardware bug with P25, link state
1894287Smg140465 			 * will be Detect state rather than L1 after
1895287Smg140465 			 * link is transitioned to L23Ready state. Since
1896287Smg140465 			 * we don't know whether link is L23ready state
1897287Smg140465 			 * without Fire's state being L1_idle, we delay
1898287Smg140465 			 * here just to make sure that we wait till link
1899287Smg140465 			 * is transitioned to L23Ready state.
1900287Smg140465 			 */
19011147Sjchu 			delay(drv_usectohz(100 * PX_MSEC_TO_USEC));
1902287Smg140465 		}
1903287Smg140465 		pwr_p->pwr_link_lvl = PM_LEVEL_L3;
1904118Sjchu 
1905118Sjchu 	}
19060Sstevel@tonic-gate 	mutex_exit(&pwr_p->pwr_lock);
19070Sstevel@tonic-gate 	return (ret);
19080Sstevel@tonic-gate }
19090Sstevel@tonic-gate 
1910118Sjchu /*
1911118Sjchu  * Message interrupt handler intended to be shared for both
1912118Sjchu  * PME and PME_TO_ACK msg handling, currently only handles
1913118Sjchu  * PME_To_ACK message.
1914118Sjchu  */
1915118Sjchu uint_t
1916118Sjchu px_pmeq_intr(caddr_t arg)
1917118Sjchu {
1918118Sjchu 	px_t	*px_p = (px_t *)arg;
1919118Sjchu 
1920287Smg140465 	DBG(DBG_PWR, px_p->px_dip, " PME_To_ACK received \n");
1921118Sjchu 	mutex_enter(&px_p->px_l23ready_lock);
1922118Sjchu 	cv_broadcast(&px_p->px_l23ready_cv);
1923118Sjchu 	if (px_p->px_pm_flags & PX_PME_TURNOFF_PENDING) {
1924118Sjchu 		px_p->px_pm_flags |= PX_PMETOACK_RECVD;
1925118Sjchu 	} else {
1926118Sjchu 		/*
1927118Sjchu 		 * This maybe the second ack received. If so then,
1928118Sjchu 		 * we should be receiving it during wait4L1 stage.
1929118Sjchu 		 */
1930118Sjchu 		px_p->px_pmetoack_ignored++;
1931118Sjchu 	}
1932118Sjchu 	mutex_exit(&px_p->px_l23ready_lock);
1933118Sjchu 	return (DDI_INTR_CLAIMED);
1934118Sjchu }
1935118Sjchu 
1936118Sjchu static int
1937118Sjchu px_pre_pwron_check(px_t *px_p)
1938118Sjchu {
1939118Sjchu 	pcie_pwr_t	*pwr_p;
1940118Sjchu 
1941118Sjchu 	/* If no PM info, return failure */
1942118Sjchu 	if (!PCIE_PMINFO(px_p->px_dip) ||
1943118Sjchu 	    !(pwr_p = PCIE_NEXUS_PMINFO(px_p->px_dip)))
1944118Sjchu 		return (DDI_FAILURE);
1945118Sjchu 
1946287Smg140465 	/*
1947287Smg140465 	 * For the spec compliant downstream cards link down
1948287Smg140465 	 * is expected when the device is powered on.
1949287Smg140465 	 */
1950287Smg140465 	px_p->px_pm_flags |= PX_LDN_EXPECTED;
1951118Sjchu 	return (pwr_p->pwr_link_lvl == PM_LEVEL_L3 ? DDI_SUCCESS : DDI_FAILURE);
1952118Sjchu }
1953118Sjchu 
1954118Sjchu static int
1955118Sjchu px_goto_l0(px_t *px_p)
1956118Sjchu {
1957118Sjchu 	pcie_pwr_t	*pwr_p;
1958118Sjchu 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
1959118Sjchu 	caddr_t csr_base = (caddr_t)pxu_p->px_address[PX_REG_CSR];
1960118Sjchu 	int		ret = DDI_SUCCESS;
19611147Sjchu 	uint64_t	time_spent = 0;
1962118Sjchu 
1963118Sjchu 	/* If no PM info, return failure */
1964118Sjchu 	if (!PCIE_PMINFO(px_p->px_dip) ||
1965118Sjchu 	    !(pwr_p = PCIE_NEXUS_PMINFO(px_p->px_dip)))
1966118Sjchu 		return (DDI_FAILURE);
1967118Sjchu 
1968118Sjchu 	mutex_enter(&pwr_p->pwr_lock);
1969287Smg140465 	/*
19701147Sjchu 	 * The following link retrain activity will cause LDN and LUP event.
19711147Sjchu 	 * Receiving LDN prior to receiving LUP is expected, not an error in
19721147Sjchu 	 * this case.  Receiving LUP indicates link is fully up to support
19731147Sjchu 	 * powering up down stream device, and of course any further LDN and
19741147Sjchu 	 * LUP outside this context will be error.
1975287Smg140465 	 */
19761147Sjchu 	px_p->px_lup_pending = 1;
1977118Sjchu 	if (px_link_retrain(csr_base) != DDI_SUCCESS) {
1978118Sjchu 		ret = DDI_FAILURE;
1979118Sjchu 		goto l0_done;
1980118Sjchu 	}
1981118Sjchu 
19821147Sjchu 	/* LUP event takes the order of 15ms amount of time to occur */
19831147Sjchu 	for (; px_p->px_lup_pending && (time_spent < px_lup_poll_to);
19841147Sjchu 	    time_spent += px_lup_poll_interval)
19851147Sjchu 		drv_usecwait(px_lup_poll_interval);
19861147Sjchu 	if (px_p->px_lup_pending)
19871147Sjchu 		ret = DDI_FAILURE;
1988118Sjchu l0_done:
1989287Smg140465 	px_enable_detect_quiet(csr_base);
1990118Sjchu 	if (ret == DDI_SUCCESS)
1991287Smg140465 		pwr_p->pwr_link_lvl = PM_LEVEL_L0;
1992118Sjchu 	mutex_exit(&pwr_p->pwr_lock);
1993118Sjchu 	return (ret);
1994118Sjchu }
1995118Sjchu 
19960Sstevel@tonic-gate /*
19970Sstevel@tonic-gate  * Extract the drivers binding name to identify which chip we're binding to.
19980Sstevel@tonic-gate  * Whenever a new bus bridge is created, the driver alias entry should be
19990Sstevel@tonic-gate  * added here to identify the device if needed.  If a device isn't added,
20000Sstevel@tonic-gate  * the identity defaults to PX_CHIP_UNIDENTIFIED.
20010Sstevel@tonic-gate  */
20020Sstevel@tonic-gate static uint32_t
20032426Sschwartz px_identity_init(px_t *px_p)
20040Sstevel@tonic-gate {
20050Sstevel@tonic-gate 	dev_info_t	*dip = px_p->px_dip;
20060Sstevel@tonic-gate 	char		*name = ddi_binding_name(dip);
20070Sstevel@tonic-gate 	uint32_t	revision = 0;
20080Sstevel@tonic-gate 
20090Sstevel@tonic-gate 	revision = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
20100Sstevel@tonic-gate 	    "module-revision#", 0);
20110Sstevel@tonic-gate 
20120Sstevel@tonic-gate 	/* Check for Fire driver binding name */
20132426Sschwartz 	if (strcmp(name, "pciex108e,80f0") == 0) {
20142426Sschwartz 		DBG(DBG_ATTACH, dip, "px_identity_init: %s%d: "
20152426Sschwartz 		    "(FIRE), module-revision %d\n", NAMEINST(dip),
20162426Sschwartz 		    revision);
20172426Sschwartz 
20182426Sschwartz 		return ((revision >= FIRE_MOD_REV_20) ?
20192426Sschwartz 		    PX_CHIP_FIRE : PX_CHIP_UNIDENTIFIED);
20200Sstevel@tonic-gate 	}
20210Sstevel@tonic-gate 
20221772Sjl139090 	/* Check for Oberon driver binding name */
20231772Sjl139090 	if (strcmp(name, "pciex108e,80f8") == 0) {
20242426Sschwartz 		DBG(DBG_ATTACH, dip, "px_identity_init: %s%d: "
20252426Sschwartz 		    "(OBERON), module-revision %d\n", NAMEINST(dip),
20262426Sschwartz 		    revision);
20272426Sschwartz 
20282426Sschwartz 		return (PX_CHIP_OBERON);
20291772Sjl139090 	}
20301772Sjl139090 
20310Sstevel@tonic-gate 	DBG(DBG_ATTACH, dip, "%s%d: Unknown PCI Express Host bridge %s %x\n",
20320Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), name, revision);
20330Sstevel@tonic-gate 
20340Sstevel@tonic-gate 	return (PX_CHIP_UNIDENTIFIED);
20350Sstevel@tonic-gate }
203627Sjchu 
203727Sjchu int
203827Sjchu px_err_add_intr(px_fault_t *px_fault_p)
203927Sjchu {
204027Sjchu 	dev_info_t	*dip = px_fault_p->px_fh_dip;
204127Sjchu 	px_t		*px_p = DIP_TO_STATE(dip);
204227Sjchu 
204327Sjchu 	VERIFY(add_ivintr(px_fault_p->px_fh_sysino, PX_ERR_PIL,
20442973Sgovinda 	    (intrfunc)px_fault_p->px_err_func, (caddr_t)px_fault_p,
20452973Sgovinda 	    NULL, NULL) == 0);
204627Sjchu 
204727Sjchu 	px_ib_intr_enable(px_p, intr_dist_cpuid(), px_fault_p->px_intr_ino);
204827Sjchu 
204927Sjchu 	return (DDI_SUCCESS);
205027Sjchu }
205127Sjchu 
205227Sjchu void
205327Sjchu px_err_rem_intr(px_fault_t *px_fault_p)
205427Sjchu {
205527Sjchu 	dev_info_t	*dip = px_fault_p->px_fh_dip;
205627Sjchu 	px_t		*px_p = DIP_TO_STATE(dip);
205727Sjchu 
205827Sjchu 	px_ib_intr_disable(px_p->px_ib_p, px_fault_p->px_intr_ino,
20596313Skrishnae 	    IB_INTR_WAIT);
2060965Sgovinda 
20612973Sgovinda 	VERIFY(rem_ivintr(px_fault_p->px_fh_sysino, PX_ERR_PIL) == 0);
206227Sjchu }
206327Sjchu 
20641648Sjchu /*
20653623Sjchu  * px_cb_intr_redist() - sun4u only, CB interrupt redistribution
20663623Sjchu  */
20673623Sjchu void
20683623Sjchu px_cb_intr_redist(void *arg)
20693623Sjchu {
20703623Sjchu 	px_cb_t		*cb_p = (px_cb_t *)arg;
20713623Sjchu 	px_cb_list_t	*pxl;
20723623Sjchu 	px_t		*pxp = NULL;
20733623Sjchu 	px_fault_t	*f_p = NULL;
20743623Sjchu 	uint32_t	new_cpuid;
20753623Sjchu 	intr_valid_state_t	enabled = 0;
20763623Sjchu 
20773623Sjchu 	mutex_enter(&cb_p->cb_mutex);
20783623Sjchu 
20793623Sjchu 	pxl = cb_p->pxl;
20803623Sjchu 	if (!pxl)
20813623Sjchu 		goto cb_done;
20823623Sjchu 
20833623Sjchu 	pxp = pxl->pxp;
20843623Sjchu 	f_p = &pxp->px_cb_fault;
20853623Sjchu 	for (; pxl && (f_p->px_fh_sysino != cb_p->sysino); ) {
20863623Sjchu 		pxl = pxl->next;
20873623Sjchu 		pxp = pxl->pxp;
20883623Sjchu 		f_p = &pxp->px_cb_fault;
20893623Sjchu 	}
20903623Sjchu 	if (pxl == NULL)
20913623Sjchu 		goto cb_done;
20923623Sjchu 
20933623Sjchu 	new_cpuid =  intr_dist_cpuid();
20943623Sjchu 	if (new_cpuid == cb_p->cpuid)
20953623Sjchu 		goto cb_done;
20963623Sjchu 
20973623Sjchu 	if ((px_lib_intr_getvalid(pxp->px_dip, f_p->px_fh_sysino, &enabled)
20983623Sjchu 	    != DDI_SUCCESS) || !enabled) {
20993623Sjchu 		DBG(DBG_IB, pxp->px_dip, "px_cb_intr_redist: CB not enabled, "
21003623Sjchu 		    "sysino(0x%x)\n", f_p->px_fh_sysino);
21013623Sjchu 		goto cb_done;
21023623Sjchu 	}
21033623Sjchu 
21043623Sjchu 	PX_INTR_DISABLE(pxp->px_dip, f_p->px_fh_sysino);
21053623Sjchu 
21063623Sjchu 	cb_p->cpuid = new_cpuid;
21073623Sjchu 	cb_p->sysino = f_p->px_fh_sysino;
21083623Sjchu 	PX_INTR_ENABLE(pxp->px_dip, cb_p->sysino, cb_p->cpuid);
21093623Sjchu 
21103623Sjchu cb_done:
21113623Sjchu 	mutex_exit(&cb_p->cb_mutex);
21123623Sjchu }
21133623Sjchu 
21143623Sjchu /*
21151648Sjchu  * px_cb_add_intr() - Called from attach(9E) to create CB if not yet
21161648Sjchu  * created, to add CB interrupt vector always, but enable only once.
21171648Sjchu  */
21181648Sjchu int
21191648Sjchu px_cb_add_intr(px_fault_t *fault_p)
21201648Sjchu {
21211648Sjchu 	px_t		*px_p = DIP_TO_STATE(fault_p->px_fh_dip);
21221648Sjchu 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
21231772Sjl139090 	px_cb_t		*cb_p = (px_cb_t *)px_get_cb(fault_p->px_fh_dip);
21241648Sjchu 	px_cb_list_t	*pxl, *pxl_new;
21253623Sjchu 	boolean_t	is_proxy = B_FALSE;
21263623Sjchu 
21273623Sjchu 	/* create cb */
21281648Sjchu 	if (cb_p == NULL) {
21291648Sjchu 		cb_p = kmem_zalloc(sizeof (px_cb_t), KM_SLEEP);
21303623Sjchu 
21313623Sjchu 		mutex_init(&cb_p->cb_mutex, NULL, MUTEX_DRIVER,
21323623Sjchu 		    (void *) ipltospl(FM_ERR_PIL));
21333623Sjchu 
21341648Sjchu 		cb_p->px_cb_func = px_cb_intr;
21351648Sjchu 		pxu_p->px_cb_p = cb_p;
21361772Sjl139090 		px_set_cb(fault_p->px_fh_dip, (uint64_t)cb_p);
21372509Sschwartz 
21382509Sschwartz 		/* px_lib_dev_init allows only FIRE and OBERON */
21392509Sschwartz 		px_err_reg_enable(
21402509Sschwartz 		    (pxu_p->chip_type == PX_CHIP_FIRE) ?
21416313Skrishnae 		    PX_ERR_JBC : PX_ERR_UBC,
21422509Sschwartz 		    pxu_p->px_address[PX_REG_XBC]);
21431648Sjchu 	} else
21441648Sjchu 		pxu_p->px_cb_p = cb_p;
21451648Sjchu 
21463623Sjchu 	/* register cb interrupt */
21471648Sjchu 	VERIFY(add_ivintr(fault_p->px_fh_sysino, PX_ERR_PIL,
21482973Sgovinda 	    (intrfunc)cb_p->px_cb_func, (caddr_t)cb_p, NULL, NULL) == 0);
21491648Sjchu 
21503623Sjchu 
21513623Sjchu 	/* update cb list */
21523623Sjchu 	mutex_enter(&cb_p->cb_mutex);
21531648Sjchu 	if (cb_p->pxl == NULL) {
21543623Sjchu 		is_proxy = B_TRUE;
21551648Sjchu 		pxl = kmem_zalloc(sizeof (px_cb_list_t), KM_SLEEP);
21561648Sjchu 		pxl->pxp = px_p;
21571648Sjchu 		cb_p->pxl = pxl;
21581648Sjchu 		cb_p->sysino = fault_p->px_fh_sysino;
21593623Sjchu 		cb_p->cpuid = intr_dist_cpuid();
21601648Sjchu 	} else {
21611648Sjchu 		/*
21621648Sjchu 		 * Find the last pxl or
21633623Sjchu 		 * stop short at encountering a redundent entry, or
21641648Sjchu 		 * both.
21651648Sjchu 		 */
21661648Sjchu 		pxl = cb_p->pxl;
21676313Skrishnae 		for (; !(pxl->pxp == px_p) && pxl->next; pxl = pxl->next) {};
21683623Sjchu 		ASSERT(pxl->pxp != px_p);
21691648Sjchu 
21701648Sjchu 		/* add to linked list */
21711648Sjchu 		pxl_new = kmem_zalloc(sizeof (px_cb_list_t), KM_SLEEP);
21721648Sjchu 		pxl_new->pxp = px_p;
21731648Sjchu 		pxl->next = pxl_new;
21741648Sjchu 	}
21751648Sjchu 	cb_p->attachcnt++;
21761648Sjchu 	mutex_exit(&cb_p->cb_mutex);
21771648Sjchu 
21783623Sjchu 	if (is_proxy) {
21793623Sjchu 		/* add to interrupt redistribution list */
21803623Sjchu 		intr_dist_add(px_cb_intr_redist, cb_p);
21813623Sjchu 
21823623Sjchu 		/* enable cb hw interrupt */
21833623Sjchu 		px_ib_intr_enable(px_p, cb_p->cpuid, fault_p->px_intr_ino);
21843623Sjchu 	}
21853623Sjchu 
21861648Sjchu 	return (DDI_SUCCESS);
21871648Sjchu }
21881648Sjchu 
21891648Sjchu /*
21901648Sjchu  * px_cb_rem_intr() - Called from detach(9E) to remove its CB
21911648Sjchu  * interrupt vector, to shift proxy to the next available px,
21921648Sjchu  * or disable CB interrupt when itself is the last.
21931648Sjchu  */
21941648Sjchu void
21951648Sjchu px_cb_rem_intr(px_fault_t *fault_p)
21961648Sjchu {
21971648Sjchu 	px_t		*px_p = DIP_TO_STATE(fault_p->px_fh_dip), *pxp;
21981648Sjchu 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
21991648Sjchu 	px_cb_t		*cb_p = PX2CB(px_p);
22001648Sjchu 	px_cb_list_t	*pxl, *prev;
22011648Sjchu 	px_fault_t	*f_p;
22021648Sjchu 
22031648Sjchu 	ASSERT(cb_p->pxl);
22041648Sjchu 
22053623Sjchu 	/* find and remove this px, and update cb list */
22061648Sjchu 	mutex_enter(&cb_p->cb_mutex);
22071648Sjchu 
22081648Sjchu 	pxl = cb_p->pxl;
22091648Sjchu 	if (pxl->pxp == px_p) {
22101648Sjchu 		cb_p->pxl = pxl->next;
22111648Sjchu 	} else {
22121648Sjchu 		prev = pxl;
22131648Sjchu 		pxl = pxl->next;
22146313Skrishnae 		for (; pxl && (pxl->pxp != px_p); prev = pxl, pxl = pxl->next) {
22156313Skrishnae 		};
22161648Sjchu 		if (!pxl) {
22171648Sjchu 			cmn_err(CE_WARN, "px_cb_rem_intr: can't find px_p 0x%p "
22181650Sjchu 			    "in registered CB list.", (void *)px_p);
22193623Sjchu 			mutex_exit(&cb_p->cb_mutex);
22201648Sjchu 			return;
22211648Sjchu 		}
22221648Sjchu 		prev->next = pxl->next;
22231648Sjchu 	}
22243623Sjchu 	pxu_p->px_cb_p = NULL;
22253623Sjchu 	cb_p->attachcnt--;
22261648Sjchu 	kmem_free(pxl, sizeof (px_cb_list_t));
22273623Sjchu 	mutex_exit(&cb_p->cb_mutex);
22283623Sjchu 
22293623Sjchu 	/* disable cb hw interrupt */
22303623Sjchu 	if (fault_p->px_fh_sysino == cb_p->sysino)
22311648Sjchu 		px_ib_intr_disable(px_p->px_ib_p, fault_p->px_intr_ino,
22321648Sjchu 		    IB_INTR_WAIT);
22331648Sjchu 
22343623Sjchu 	/* if last px, remove from interrupt redistribution list */
22353623Sjchu 	if (cb_p->pxl == NULL)
22363623Sjchu 		intr_dist_rem(px_cb_intr_redist, cb_p);
22373623Sjchu 
22383623Sjchu 	/* de-register interrupt */
22393623Sjchu 	VERIFY(rem_ivintr(fault_p->px_fh_sysino, PX_ERR_PIL) == 0);
22403623Sjchu 
22413623Sjchu 	/* if not last px, assign next px to manage cb */
22423623Sjchu 	mutex_enter(&cb_p->cb_mutex);
22433623Sjchu 	if (cb_p->pxl) {
22443623Sjchu 		if (fault_p->px_fh_sysino == cb_p->sysino) {
22451648Sjchu 			pxp = cb_p->pxl->pxp;
22461648Sjchu 			f_p = &pxp->px_cb_fault;
22471648Sjchu 			cb_p->sysino = f_p->px_fh_sysino;
22481648Sjchu 
22491648Sjchu 			PX_INTR_ENABLE(pxp->px_dip, cb_p->sysino, cb_p->cpuid);
22501650Sjchu 			(void) px_lib_intr_setstate(pxp->px_dip, cb_p->sysino,
22511648Sjchu 			    INTR_IDLE_STATE);
22521648Sjchu 		}
22531648Sjchu 		mutex_exit(&cb_p->cb_mutex);
22541648Sjchu 		return;
22551648Sjchu 	}
22563623Sjchu 
22573623Sjchu 	/* clean up after the last px */
22581648Sjchu 	mutex_exit(&cb_p->cb_mutex);
22591648Sjchu 
22602509Sschwartz 	/* px_lib_dev_init allows only FIRE and OBERON */
22612509Sschwartz 	px_err_reg_disable(
22622509Sschwartz 	    (pxu_p->chip_type == PX_CHIP_FIRE) ? PX_ERR_JBC : PX_ERR_UBC,
22632509Sschwartz 	    pxu_p->px_address[PX_REG_XBC]);
22642509Sschwartz 
22651648Sjchu 	mutex_destroy(&cb_p->cb_mutex);
22661772Sjl139090 	px_set_cb(fault_p->px_fh_dip, 0ull);
22671648Sjchu 	kmem_free(cb_p, sizeof (px_cb_t));
22681648Sjchu }
22691648Sjchu 
22701648Sjchu /*
22711648Sjchu  * px_cb_intr() - sun4u only,  CB interrupt dispatcher
22721648Sjchu  */
22731648Sjchu uint_t
22741648Sjchu px_cb_intr(caddr_t arg)
22751648Sjchu {
22761648Sjchu 	px_cb_t		*cb_p = (px_cb_t *)arg;
22773623Sjchu 	px_t		*pxp;
22783623Sjchu 	px_fault_t	*f_p;
22793623Sjchu 	int		ret;
22803354Sjl139090 
22811648Sjchu 	mutex_enter(&cb_p->cb_mutex);
22821648Sjchu 
22833623Sjchu 	if (!cb_p->pxl) {
22841648Sjchu 		mutex_exit(&cb_p->cb_mutex);
22853623Sjchu 		return (DDI_INTR_UNCLAIMED);
22861648Sjchu 	}
22871648Sjchu 
22883623Sjchu 	pxp = cb_p->pxl->pxp;
22893623Sjchu 	f_p = &pxp->px_cb_fault;
22903623Sjchu 
22913623Sjchu 	ret = f_p->px_err_func((caddr_t)f_p);
22921648Sjchu 
22931648Sjchu 	mutex_exit(&cb_p->cb_mutex);
22943623Sjchu 	return (ret);
22951648Sjchu }
22961648Sjchu 
22973623Sjchu #ifdef	FMA
229827Sjchu void
229927Sjchu px_fill_rc_status(px_fault_t *px_fault_p, pciex_rc_error_regs_t *rc_status)
230027Sjchu {
230127Sjchu 	/* populate the rc_status by reading the registers - TBD */
230227Sjchu }
230327Sjchu #endif /* FMA */
2304383Set142600 
2305383Set142600 /*
2306383Set142600  * Unprotected raw reads/writes of fabric device's config space.
2307383Set142600  * Only used for temporary PCI-E Fabric Error Handling.
2308383Set142600  */
2309383Set142600 uint32_t
23101648Sjchu px_fab_get(px_t *px_p, pcie_req_id_t bdf, uint16_t offset)
23111648Sjchu {
2312383Set142600 	px_ranges_t	*rp = px_p->px_ranges_p;
2313383Set142600 	uint64_t	range_prop, base_addr;
2314383Set142600 	int		bank = PCI_REG_ADDR_G(PCI_ADDR_CONFIG);
2315383Set142600 	uint32_t	val;
2316383Set142600 
2317383Set142600 	/* Get Fire's Physical Base Address */
23181772Sjl139090 	range_prop = px_get_range_prop(px_p, rp, bank);
2319383Set142600 
2320383Set142600 	/* Get config space first. */
2321383Set142600 	base_addr = range_prop + PX_BDF_TO_CFGADDR(bdf, offset);
2322383Set142600 
2323383Set142600 	val = ldphysio(base_addr);
2324383Set142600 
2325383Set142600 	return (LE_32(val));
2326383Set142600 }
2327383Set142600 
2328383Set142600 void
2329383Set142600 px_fab_set(px_t *px_p, pcie_req_id_t bdf, uint16_t offset,
2330383Set142600     uint32_t val) {
2331383Set142600 	px_ranges_t	*rp = px_p->px_ranges_p;
2332383Set142600 	uint64_t	range_prop, base_addr;
2333383Set142600 	int		bank = PCI_REG_ADDR_G(PCI_ADDR_CONFIG);
2334383Set142600 
2335383Set142600 	/* Get Fire's Physical Base Address */
23361772Sjl139090 	range_prop = px_get_range_prop(px_p, rp, bank);
2337383Set142600 
2338383Set142600 	/* Get config space first. */
2339383Set142600 	base_addr = range_prop + PX_BDF_TO_CFGADDR(bdf, offset);
2340383Set142600 
2341383Set142600 	stphysio(base_addr, LE_32(val));
2342383Set142600 }
2343435Sjchu 
2344435Sjchu /*
2345435Sjchu  * cpr callback
2346435Sjchu  *
2347435Sjchu  * disable fabric error msg interrupt prior to suspending
2348435Sjchu  * all device drivers; re-enable fabric error msg interrupt
2349435Sjchu  * after all devices are resumed.
2350435Sjchu  */
2351435Sjchu static boolean_t
2352435Sjchu px_cpr_callb(void *arg, int code)
2353435Sjchu {
2354435Sjchu 	px_t		*px_p = (px_t *)arg;
2355435Sjchu 	px_ib_t		*ib_p = px_p->px_ib_p;
2356435Sjchu 	px_pec_t	*pec_p = px_p->px_pec_p;
2357435Sjchu 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
2358435Sjchu 	caddr_t		csr_base;
2359435Sjchu 	devino_t	ce_ino, nf_ino, f_ino;
23602973Sgovinda 	px_ino_t	*ce_ino_p, *nf_ino_p, *f_ino_p;
2361435Sjchu 	uint64_t	imu_log_enable, imu_intr_enable;
2362435Sjchu 	uint64_t	imu_log_mask, imu_intr_mask;
2363435Sjchu 
2364435Sjchu 	ce_ino = px_msiqid_to_devino(px_p, pec_p->pec_corr_msg_msiq_id);
2365435Sjchu 	nf_ino = px_msiqid_to_devino(px_p, pec_p->pec_non_fatal_msg_msiq_id);
2366435Sjchu 	f_ino = px_msiqid_to_devino(px_p, pec_p->pec_fatal_msg_msiq_id);
2367435Sjchu 	csr_base = (caddr_t)pxu_p->px_address[PX_REG_CSR];
2368435Sjchu 
2369435Sjchu 	imu_log_enable = CSR_XR(csr_base, IMU_ERROR_LOG_ENABLE);
2370435Sjchu 	imu_intr_enable = CSR_XR(csr_base, IMU_INTERRUPT_ENABLE);
2371435Sjchu 
2372435Sjchu 	imu_log_mask = BITMASK(IMU_ERROR_LOG_ENABLE_FATAL_MES_NOT_EN_LOG_EN) |
2373435Sjchu 	    BITMASK(IMU_ERROR_LOG_ENABLE_NONFATAL_MES_NOT_EN_LOG_EN) |
2374435Sjchu 	    BITMASK(IMU_ERROR_LOG_ENABLE_COR_MES_NOT_EN_LOG_EN);
2375435Sjchu 
2376435Sjchu 	imu_intr_mask =
2377435Sjchu 	    BITMASK(IMU_INTERRUPT_ENABLE_FATAL_MES_NOT_EN_S_INT_EN) |
2378435Sjchu 	    BITMASK(IMU_INTERRUPT_ENABLE_NONFATAL_MES_NOT_EN_S_INT_EN) |
2379435Sjchu 	    BITMASK(IMU_INTERRUPT_ENABLE_COR_MES_NOT_EN_S_INT_EN) |
2380435Sjchu 	    BITMASK(IMU_INTERRUPT_ENABLE_FATAL_MES_NOT_EN_P_INT_EN) |
2381435Sjchu 	    BITMASK(IMU_INTERRUPT_ENABLE_NONFATAL_MES_NOT_EN_P_INT_EN) |
2382435Sjchu 	    BITMASK(IMU_INTERRUPT_ENABLE_COR_MES_NOT_EN_P_INT_EN);
2383435Sjchu 
2384435Sjchu 	switch (code) {
2385435Sjchu 	case CB_CODE_CPR_CHKPT:
2386435Sjchu 		/* disable imu rbne on corr/nonfatal/fatal errors */
2387435Sjchu 		CSR_XS(csr_base, IMU_ERROR_LOG_ENABLE,
2388435Sjchu 		    imu_log_enable & (~imu_log_mask));
2389435Sjchu 
2390435Sjchu 		CSR_XS(csr_base, IMU_INTERRUPT_ENABLE,
2391435Sjchu 		    imu_intr_enable & (~imu_intr_mask));
2392435Sjchu 
2393435Sjchu 		/* disable CORR intr mapping */
2394435Sjchu 		px_ib_intr_disable(ib_p, ce_ino, IB_INTR_NOWAIT);
2395435Sjchu 
2396435Sjchu 		/* disable NON FATAL intr mapping */
2397435Sjchu 		px_ib_intr_disable(ib_p, nf_ino, IB_INTR_NOWAIT);
2398435Sjchu 
2399435Sjchu 		/* disable FATAL intr mapping */
2400435Sjchu 		px_ib_intr_disable(ib_p, f_ino, IB_INTR_NOWAIT);
2401435Sjchu 
2402435Sjchu 		break;
2403435Sjchu 
2404435Sjchu 	case CB_CODE_CPR_RESUME:
24053274Set142600 		pxu_p->cpr_flag = PX_NOT_CPR;
2406435Sjchu 		mutex_enter(&ib_p->ib_ino_lst_mutex);
2407435Sjchu 
2408435Sjchu 		ce_ino_p = px_ib_locate_ino(ib_p, ce_ino);
2409435Sjchu 		nf_ino_p = px_ib_locate_ino(ib_p, nf_ino);
2410435Sjchu 		f_ino_p = px_ib_locate_ino(ib_p, f_ino);
2411435Sjchu 
2412435Sjchu 		/* enable CORR intr mapping */
2413435Sjchu 		if (ce_ino_p)
2414435Sjchu 			px_ib_intr_enable(px_p, ce_ino_p->ino_cpuid, ce_ino);
2415435Sjchu 		else
2416435Sjchu 			cmn_err(CE_WARN, "px_cpr_callb: RESUME unable to "
2417435Sjchu 			    "reenable PCIe Correctable msg intr.\n");
2418435Sjchu 
2419435Sjchu 		/* enable NON FATAL intr mapping */
2420435Sjchu 		if (nf_ino_p)
2421435Sjchu 			px_ib_intr_enable(px_p, nf_ino_p->ino_cpuid, nf_ino);
2422435Sjchu 		else
2423435Sjchu 			cmn_err(CE_WARN, "px_cpr_callb: RESUME unable to "
2424435Sjchu 			    "reenable PCIe Non Fatal msg intr.\n");
2425435Sjchu 
2426435Sjchu 		/* enable FATAL intr mapping */
2427435Sjchu 		if (f_ino_p)
2428435Sjchu 			px_ib_intr_enable(px_p, f_ino_p->ino_cpuid, f_ino);
2429435Sjchu 		else
2430435Sjchu 			cmn_err(CE_WARN, "px_cpr_callb: RESUME unable to "
2431435Sjchu 			    "reenable PCIe Fatal msg intr.\n");
2432435Sjchu 
2433435Sjchu 		mutex_exit(&ib_p->ib_ino_lst_mutex);
2434435Sjchu 
2435435Sjchu 		/* enable corr/nonfatal/fatal not enable error */
2436435Sjchu 		CSR_XS(csr_base, IMU_ERROR_LOG_ENABLE, (imu_log_enable |
2437435Sjchu 		    (imu_log_mask & px_imu_log_mask)));
2438435Sjchu 		CSR_XS(csr_base, IMU_INTERRUPT_ENABLE, (imu_intr_enable |
2439435Sjchu 		    (imu_intr_mask & px_imu_intr_mask)));
2440435Sjchu 
2441435Sjchu 		break;
2442435Sjchu 	}
2443435Sjchu 
2444435Sjchu 	return (B_TRUE);
2445435Sjchu }
2446435Sjchu 
24472053Sschwartz uint64_t
24482053Sschwartz px_get_rng_parent_hi_mask(px_t *px_p)
24492053Sschwartz {
24502053Sschwartz 	pxu_t *pxu_p = (pxu_t *)px_p->px_plat_p;
24512053Sschwartz 	uint64_t mask;
24522053Sschwartz 
24532053Sschwartz 	switch (PX_CHIP_TYPE(pxu_p)) {
24542053Sschwartz 	case PX_CHIP_OBERON:
24552053Sschwartz 		mask = OBERON_RANGE_PROP_MASK;
24562053Sschwartz 		break;
24572053Sschwartz 	case PX_CHIP_FIRE:
24582053Sschwartz 		mask = PX_RANGE_PROP_MASK;
24592053Sschwartz 		break;
24602053Sschwartz 	default:
24612053Sschwartz 		mask = PX_RANGE_PROP_MASK;
24622053Sschwartz 	}
24632053Sschwartz 
24642053Sschwartz 	return (mask);
24652053Sschwartz }
24662053Sschwartz 
2467435Sjchu /*
24681772Sjl139090  * fetch chip's range propery's value
24691772Sjl139090  */
24701772Sjl139090 uint64_t
24711772Sjl139090 px_get_range_prop(px_t *px_p, px_ranges_t *rp, int bank)
24721772Sjl139090 {
24731772Sjl139090 	uint64_t mask, range_prop;
24741772Sjl139090 
24752053Sschwartz 	mask = px_get_rng_parent_hi_mask(px_p);
24761772Sjl139090 	range_prop = (((uint64_t)(rp[bank].parent_high & mask)) << 32) |
24776313Skrishnae 	    rp[bank].parent_low;
24781772Sjl139090 
24791772Sjl139090 	return (range_prop);
24801772Sjl139090 }
24811772Sjl139090 
24821772Sjl139090 /*
2483435Sjchu  * add cpr callback
2484435Sjchu  */
2485435Sjchu void
2486435Sjchu px_cpr_add_callb(px_t *px_p)
2487435Sjchu {
2488435Sjchu 	px_p->px_cprcb_id = callb_add(px_cpr_callb, (void *)px_p,
24896313Skrishnae 	    CB_CL_CPR_POST_USER, "px_cpr");
2490435Sjchu }
2491435Sjchu 
2492435Sjchu /*
2493435Sjchu  * remove cpr callback
2494435Sjchu  */
2495435Sjchu void
2496435Sjchu px_cpr_rem_callb(px_t *px_p)
2497435Sjchu {
2498435Sjchu 	(void) callb_delete(px_p->px_cprcb_id);
2499435Sjchu }
25001531Skini 
25011531Skini /*ARGSUSED*/
25021772Sjl139090 static uint_t
25031772Sjl139090 px_hp_intr(caddr_t arg1, caddr_t arg2)
25041772Sjl139090 {
25054701Sgovinda 	px_t	*px_p = (px_t *)arg1;
25064701Sgovinda 	pxu_t 	*pxu_p = (pxu_t *)px_p->px_plat_p;
25074701Sgovinda 	int	rval;
25081772Sjl139090 
25091772Sjl139090 	rval = pciehpc_intr(px_p->px_dip);
25101772Sjl139090 
25111772Sjl139090 #ifdef  DEBUG
25121772Sjl139090 	if (rval == DDI_INTR_UNCLAIMED)
25136313Skrishnae 		cmn_err(CE_WARN, "%s%d: UNCLAIMED intr\n",
25146313Skrishnae 		    ddi_driver_name(px_p->px_dip),
25156313Skrishnae 		    ddi_get_instance(px_p->px_dip));
25161772Sjl139090 #endif
25171772Sjl139090 
25184701Sgovinda 	/* Set the interrupt state to idle */
25194701Sgovinda 	if (px_lib_intr_setstate(px_p->px_dip,
25204701Sgovinda 	    pxu_p->hp_sysino, INTR_IDLE_STATE) != DDI_SUCCESS)
25214701Sgovinda 		return (DDI_INTR_UNCLAIMED);
25224701Sgovinda 
25231772Sjl139090 	return (rval);
25241772Sjl139090 }
25251772Sjl139090 
25261531Skini int
25271531Skini px_lib_hotplug_init(dev_info_t *dip, void *arg)
25281531Skini {
25291772Sjl139090 	px_t	*px_p = DIP_TO_STATE(dip);
25304701Sgovinda 	pxu_t 	*pxu_p = (pxu_t *)px_p->px_plat_p;
25311772Sjl139090 	uint64_t ret;
25321772Sjl139090 
25331772Sjl139090 	if ((ret = hvio_hotplug_init(dip, arg)) == DDI_SUCCESS) {
25341772Sjl139090 		if (px_lib_intr_devino_to_sysino(px_p->px_dip,
25354701Sgovinda 		    px_p->px_inos[PX_INTR_HOTPLUG], &pxu_p->hp_sysino) !=
25361772Sjl139090 		    DDI_SUCCESS) {
25371772Sjl139090 #ifdef	DEBUG
25381772Sjl139090 			cmn_err(CE_WARN, "%s%d: devino_to_sysino fails\n",
25391772Sjl139090 			    ddi_driver_name(px_p->px_dip),
25401772Sjl139090 			    ddi_get_instance(px_p->px_dip));
25411772Sjl139090 #endif
25421772Sjl139090 			return (DDI_FAILURE);
25431772Sjl139090 		}
25441772Sjl139090 
25454701Sgovinda 		VERIFY(add_ivintr(pxu_p->hp_sysino, PX_PCIEHP_PIL,
25462973Sgovinda 		    (intrfunc)px_hp_intr, (caddr_t)px_p, NULL, NULL) == 0);
25473953Sscarter 
25483953Sscarter 		px_ib_intr_enable(px_p, intr_dist_cpuid(),
25493953Sscarter 		    px_p->px_inos[PX_INTR_HOTPLUG]);
25501772Sjl139090 	}
25511772Sjl139090 
25521772Sjl139090 	return (ret);
25531531Skini }
25541531Skini 
25551531Skini void
25561531Skini px_lib_hotplug_uninit(dev_info_t *dip)
25571531Skini {
25581772Sjl139090 	if (hvio_hotplug_uninit(dip) == DDI_SUCCESS) {
25591772Sjl139090 		px_t	*px_p = DIP_TO_STATE(dip);
25604701Sgovinda 		pxu_t 	*pxu_p = (pxu_t *)px_p->px_plat_p;
25611772Sjl139090 
25623953Sscarter 		px_ib_intr_disable(px_p->px_ib_p,
25633953Sscarter 		    px_p->px_inos[PX_INTR_HOTPLUG], IB_INTR_WAIT);
25643953Sscarter 
25654701Sgovinda 		VERIFY(rem_ivintr(pxu_p->hp_sysino, PX_PCIEHP_PIL) == 0);
25661772Sjl139090 	}
25671531Skini }
25682476Sdwoods 
25693953Sscarter /*
25703953Sscarter  * px_hp_intr_redist() - sun4u only, HP interrupt redistribution
25713953Sscarter  */
25723953Sscarter void
25733953Sscarter px_hp_intr_redist(px_t *px_p)
25743953Sscarter {
25753953Sscarter 	if (px_p && (px_p->px_dev_caps & PX_HOTPLUG_CAPABLE)) {
25763953Sscarter 		px_ib_intr_dist_en(px_p->px_dip, intr_dist_cpuid(),
25773953Sscarter 		    px_p->px_inos[PX_INTR_HOTPLUG], B_FALSE);
25783953Sscarter 	}
25793953Sscarter }
25803953Sscarter 
25812476Sdwoods boolean_t
25822476Sdwoods px_lib_is_in_drain_state(px_t *px_p)
25832476Sdwoods {
25842476Sdwoods 	pxu_t 	*pxu_p = (pxu_t *)px_p->px_plat_p;
25852476Sdwoods 	caddr_t csr_base = (caddr_t)pxu_p->px_address[PX_REG_CSR];
25862476Sdwoods 	uint64_t drain_status;
25872476Sdwoods 
25882476Sdwoods 	if (PX_CHIP_TYPE(pxu_p) == PX_CHIP_OBERON) {
25892476Sdwoods 		drain_status = CSR_BR(csr_base, DRAIN_CONTROL_STATUS, DRAIN);
25902476Sdwoods 	} else {
25912476Sdwoods 		drain_status = CSR_BR(csr_base, TLU_STATUS, DRAIN);
25922476Sdwoods 	}
25932476Sdwoods 
25942476Sdwoods 	return (drain_status);
25952476Sdwoods }
25963613Set142600 
25973613Set142600 pcie_req_id_t
25983613Set142600 px_lib_get_bdf(px_t *px_p)
25993613Set142600 {
26003613Set142600 	pxu_t 	*pxu_p = (pxu_t *)px_p->px_plat_p;
26013613Set142600 	caddr_t csr_base = (caddr_t)pxu_p->px_address[PX_REG_CSR];
26023613Set142600 	pcie_req_id_t bdf;
26033613Set142600 
26043613Set142600 	bdf = CSR_BR(csr_base, DMC_PCI_EXPRESS_CONFIGURATION, REQ_ID);
26053613Set142600 
26063613Set142600 	return (bdf);
26073613Set142600 }
2608