xref: /onnv-gate/usr/src/uts/sun4u/io/pci/pci_intr.c (revision 2234:012f68c731a8)
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
51811Sesolom  * Common Development and Distribution License (the "License").
61811Sesolom  * 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 /*
221811Sesolom  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * PCI nexus interrupt handling:
300Sstevel@tonic-gate  *	PCI device interrupt handler wrapper
310Sstevel@tonic-gate  *	pil lookup routine
320Sstevel@tonic-gate  *	PCI device interrupt related initchild code
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #include <sys/types.h>
360Sstevel@tonic-gate #include <sys/kmem.h>
370Sstevel@tonic-gate #include <sys/async.h>
380Sstevel@tonic-gate #include <sys/spl.h>
390Sstevel@tonic-gate #include <sys/sunddi.h>
400Sstevel@tonic-gate #include <sys/machsystm.h>	/* e_ddi_nodeid_to_dip() */
410Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
420Sstevel@tonic-gate #include <sys/pci/pci_obj.h>
430Sstevel@tonic-gate #include <sys/sdt.h>
4466Sesolom #include <sys/clock.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #ifdef _STARFIRE
470Sstevel@tonic-gate #include <sys/starfire.h>
480Sstevel@tonic-gate #endif /* _STARFIRE */
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate  * interrupt jabber:
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * When an interrupt line is jabbering, every time the state machine for the
540Sstevel@tonic-gate  * associated ino is idled, a new mondo will be sent and the ino will go into
550Sstevel@tonic-gate  * the pending state again. The mondo will cause a new call to
560Sstevel@tonic-gate  * pci_intr_wrapper() which normally idles the ino's state machine which would
570Sstevel@tonic-gate  * precipitate another trip round the loop.
580Sstevel@tonic-gate  * The loop can be broken by preventing the ino's state machine from being
590Sstevel@tonic-gate  * idled when an interrupt line is jabbering. See the comment at the
600Sstevel@tonic-gate  * beginning of pci_intr_wrapper() explaining how the 'interrupt jabber
610Sstevel@tonic-gate  * protection' code does this.
620Sstevel@tonic-gate  */
630Sstevel@tonic-gate 
640Sstevel@tonic-gate /*LINTLIBRARY*/
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #ifdef NOT_DEFINED
670Sstevel@tonic-gate /*
680Sstevel@tonic-gate  * This array is used to determine the sparc PIL at the which the
690Sstevel@tonic-gate  * handler for a given INO will execute.  This table is for onboard
700Sstevel@tonic-gate  * devices only.  A different scheme will be used for plug-in cards.
710Sstevel@tonic-gate  */
720Sstevel@tonic-gate 
730Sstevel@tonic-gate uint_t ino_to_pil[] = {
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	/* pil */		/* ino */
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	0, 0, 0, 0,  		/* 0x00 - 0x03: bus A slot 0 int#A, B, C, D */
780Sstevel@tonic-gate 	0, 0, 0, 0,		/* 0x04 - 0x07: bus A slot 1 int#A, B, C, D */
790Sstevel@tonic-gate 	0, 0, 0, 0,  		/* 0x08 - 0x0B: unused */
800Sstevel@tonic-gate 	0, 0, 0, 0,		/* 0x0C - 0x0F: unused */
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	0, 0, 0, 0,  		/* 0x10 - 0x13: bus B slot 0 int#A, B, C, D */
830Sstevel@tonic-gate 	0, 0, 0, 0,		/* 0x14 - 0x17: bus B slot 1 int#A, B, C, D */
840Sstevel@tonic-gate 	0, 0, 0, 0,  		/* 0x18 - 0x1B: bus B slot 2 int#A, B, C, D */
850Sstevel@tonic-gate 	4, 0, 0, 0,		/* 0x1C - 0x1F: bus B slot 3 int#A, B, C, D */
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	4,			/* 0x20: SCSI */
880Sstevel@tonic-gate 	6,			/* 0x21: ethernet */
890Sstevel@tonic-gate 	3,			/* 0x22: parallel port */
900Sstevel@tonic-gate 	9,			/* 0x23: audio record */
910Sstevel@tonic-gate 	9,			/* 0x24: audio playback */
920Sstevel@tonic-gate 	14,			/* 0x25: power fail */
930Sstevel@tonic-gate 	4,			/* 0x26: 2nd SCSI */
940Sstevel@tonic-gate 	8,			/* 0x27: floppy */
950Sstevel@tonic-gate 	14,			/* 0x28: thermal warning */
960Sstevel@tonic-gate 	12,			/* 0x29: keyboard */
970Sstevel@tonic-gate 	12,			/* 0x2A: mouse */
980Sstevel@tonic-gate 	12,			/* 0x2B: serial */
990Sstevel@tonic-gate 	0,			/* 0x2C: timer/counter 0 */
1000Sstevel@tonic-gate 	0,			/* 0x2D: timer/counter 1 */
1010Sstevel@tonic-gate 	14,			/* 0x2E: uncorrectable ECC errors */
1020Sstevel@tonic-gate 	14,			/* 0x2F: correctable ECC errors */
1030Sstevel@tonic-gate 	14,			/* 0x30: PCI bus A error */
1040Sstevel@tonic-gate 	14,			/* 0x31: PCI bus B error */
1050Sstevel@tonic-gate 	14,			/* 0x32: power management wakeup */
1060Sstevel@tonic-gate 	14,			/* 0x33 */
1070Sstevel@tonic-gate 	14,			/* 0x34 */
1080Sstevel@tonic-gate 	14,			/* 0x35 */
1090Sstevel@tonic-gate 	14,			/* 0x36 */
1100Sstevel@tonic-gate 	14,			/* 0x37 */
1110Sstevel@tonic-gate 	14,			/* 0x38 */
1120Sstevel@tonic-gate 	14,			/* 0x39 */
1130Sstevel@tonic-gate 	14,			/* 0x3a */
1140Sstevel@tonic-gate 	14,			/* 0x3b */
1150Sstevel@tonic-gate 	14,			/* 0x3c */
1160Sstevel@tonic-gate 	14,			/* 0x3d */
1170Sstevel@tonic-gate 	14,			/* 0x3e */
1180Sstevel@tonic-gate 	14,			/* 0x3f */
1190Sstevel@tonic-gate 	14			/* 0x40 */
1200Sstevel@tonic-gate };
1210Sstevel@tonic-gate #endif /* NOT_DEFINED */
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate #define	PCI_SIMBA_VENID		0x108e	/* vendor id for simba */
1250Sstevel@tonic-gate #define	PCI_SIMBA_DEVID		0x5000	/* device id for simba */
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate /*
1280Sstevel@tonic-gate  * map_pcidev_cfg_reg - create mapping to pci device configuration registers
1290Sstevel@tonic-gate  *			if we have a simba AND a pci to pci bridge along the
1300Sstevel@tonic-gate  *			device path.
1310Sstevel@tonic-gate  *			Called with corresponding mutexes held!!
1320Sstevel@tonic-gate  *
1330Sstevel@tonic-gate  * XXX	  XXX	XXX	The purpose of this routine is to overcome a hardware
1340Sstevel@tonic-gate  *			defect in Sabre CPU and Simba bridge configuration
1350Sstevel@tonic-gate  *			which does not drain DMA write data stalled in
1360Sstevel@tonic-gate  *			PCI to PCI bridges (such as the DEC bridge) beyond
1370Sstevel@tonic-gate  *			Simba. This routine will setup the data structures
1380Sstevel@tonic-gate  *			to allow the pci_intr_wrapper to perform a manual
1390Sstevel@tonic-gate  *			drain data operation before passing the control to
1400Sstevel@tonic-gate  *			interrupt handlers of device drivers.
1410Sstevel@tonic-gate  * return value:
1420Sstevel@tonic-gate  * DDI_SUCCESS
1430Sstevel@tonic-gate  * DDI_FAILURE		if unable to create mapping
1440Sstevel@tonic-gate  */
1450Sstevel@tonic-gate static int
1460Sstevel@tonic-gate map_pcidev_cfg_reg(dev_info_t *dip, dev_info_t *rdip, ddi_acc_handle_t *hdl_p)
1470Sstevel@tonic-gate {
1480Sstevel@tonic-gate 	dev_info_t *cdip;
1490Sstevel@tonic-gate 	dev_info_t *pci_dip = NULL;
1500Sstevel@tonic-gate 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
1510Sstevel@tonic-gate 	int simba_found = 0, pci_bridge_found = 0;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	for (cdip = rdip; cdip && cdip != dip; cdip = ddi_get_parent(cdip)) {
1540Sstevel@tonic-gate 		ddi_acc_handle_t config_handle;
1550Sstevel@tonic-gate 		uint32_t vendor_id = ddi_getprop(DDI_DEV_T_ANY, cdip,
1560Sstevel@tonic-gate 			DDI_PROP_DONTPASS, "vendor-id", 0xffff);
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 		DEBUG4(DBG_A_INTX, pci_p->pci_dip,
1590Sstevel@tonic-gate 			"map dev cfg reg for %s%d: @%s%d\n",
1600Sstevel@tonic-gate 			ddi_driver_name(rdip), ddi_get_instance(rdip),
1610Sstevel@tonic-gate 			ddi_driver_name(cdip), ddi_get_instance(cdip));
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 		if (ddi_prop_exists(DDI_DEV_T_ANY, cdip, DDI_PROP_DONTPASS,
1640Sstevel@tonic-gate 				"no-dma-interrupt-sync"))
1650Sstevel@tonic-gate 			continue;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 		/* continue to search up-stream if not a PCI device */
1680Sstevel@tonic-gate 		if (vendor_id == 0xffff)
1690Sstevel@tonic-gate 			continue;
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 		/* record the deepest pci device */
1720Sstevel@tonic-gate 		if (!pci_dip)
1730Sstevel@tonic-gate 			pci_dip = cdip;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 		/* look for simba */
1760Sstevel@tonic-gate 		if (vendor_id == PCI_SIMBA_VENID) {
1770Sstevel@tonic-gate 			uint32_t device_id = ddi_getprop(DDI_DEV_T_ANY,
1780Sstevel@tonic-gate 			    cdip, DDI_PROP_DONTPASS, "device-id", -1);
1790Sstevel@tonic-gate 			if (device_id == PCI_SIMBA_DEVID) {
1800Sstevel@tonic-gate 				simba_found = 1;
1810Sstevel@tonic-gate 				DEBUG0(DBG_A_INTX, pci_p->pci_dip,
1820Sstevel@tonic-gate 					"\tFound simba\n");
1830Sstevel@tonic-gate 				continue; /* do not check bridge if simba */
1840Sstevel@tonic-gate 			}
1850Sstevel@tonic-gate 		}
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 		/* look for pci to pci bridge */
1880Sstevel@tonic-gate 		if (pci_config_setup(cdip, &config_handle) != DDI_SUCCESS) {
1890Sstevel@tonic-gate 			cmn_err(CE_WARN,
1900Sstevel@tonic-gate 			    "%s%d: can't get brdg cfg space for %s%d\n",
1910Sstevel@tonic-gate 				ddi_driver_name(dip), ddi_get_instance(dip),
1920Sstevel@tonic-gate 				ddi_driver_name(cdip), ddi_get_instance(cdip));
1930Sstevel@tonic-gate 			return (DDI_FAILURE);
1940Sstevel@tonic-gate 		}
1950Sstevel@tonic-gate 		if (pci_config_get8(config_handle, PCI_CONF_BASCLASS)
1960Sstevel@tonic-gate 		    == PCI_CLASS_BRIDGE) {
1970Sstevel@tonic-gate 			DEBUG0(DBG_A_INTX, pci_p->pci_dip,
1980Sstevel@tonic-gate 				"\tFound PCI to xBus bridge\n");
1990Sstevel@tonic-gate 			pci_bridge_found = 1;
2000Sstevel@tonic-gate 		}
2010Sstevel@tonic-gate 		pci_config_teardown(&config_handle);
2020Sstevel@tonic-gate 	}
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	if (!pci_bridge_found)
2050Sstevel@tonic-gate 		return (DDI_SUCCESS);
2060Sstevel@tonic-gate 	if (!simba_found && (CHIP_TYPE(pci_p) < PCI_CHIP_SCHIZO))
2070Sstevel@tonic-gate 		return (DDI_SUCCESS);
2080Sstevel@tonic-gate 	if (pci_config_setup(pci_dip, hdl_p) != DDI_SUCCESS) {
2090Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: can not get config space for %s%d\n",
2100Sstevel@tonic-gate 			ddi_driver_name(dip), ddi_get_instance(dip),
2110Sstevel@tonic-gate 			ddi_driver_name(cdip), ddi_get_instance(cdip));
2120Sstevel@tonic-gate 		return (DDI_FAILURE);
2130Sstevel@tonic-gate 	}
2140Sstevel@tonic-gate 	return (DDI_SUCCESS);
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate /*
2180Sstevel@tonic-gate  * If the unclaimed interrupt count has reached the limit set by
2190Sstevel@tonic-gate  * pci_unclaimed_intr_max within the time limit, then all interrupts
2200Sstevel@tonic-gate  * on this ino is blocked by not idling the interrupt state machine.
2210Sstevel@tonic-gate  */
2220Sstevel@tonic-gate static int
2230Sstevel@tonic-gate pci_spurintr(ib_ino_info_t *ino_p) {
2240Sstevel@tonic-gate 	int i;
2250Sstevel@tonic-gate 	ih_t *ih_p = ino_p->ino_ih_start;
2260Sstevel@tonic-gate 	pci_t *pci_p = ino_p->ino_ib_p->ib_pci_p;
2270Sstevel@tonic-gate 	char *err_fmt_str;
228*2234Sdanice 	boolean_t blocked = B_FALSE;
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	if (ino_p->ino_unclaimed > pci_unclaimed_intr_max)
2310Sstevel@tonic-gate 		return (DDI_INTR_CLAIMED);
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	if (!ino_p->ino_unclaimed)
2340Sstevel@tonic-gate 		ino_p->ino_spurintr_begin = ddi_get_lbolt();
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	ino_p->ino_unclaimed++;
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	if (ino_p->ino_unclaimed <= pci_unclaimed_intr_max)
2390Sstevel@tonic-gate 		goto clear;
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	if (drv_hztousec(ddi_get_lbolt() - ino_p->ino_spurintr_begin)
2420Sstevel@tonic-gate 	    > pci_spurintr_duration) {
2430Sstevel@tonic-gate 		ino_p->ino_unclaimed = 0;
2440Sstevel@tonic-gate 		goto clear;
2450Sstevel@tonic-gate 	}
2460Sstevel@tonic-gate 	err_fmt_str = "%s%d: ino 0x%x blocked";
247*2234Sdanice 	blocked = B_TRUE;
2480Sstevel@tonic-gate 	goto warn;
2490Sstevel@tonic-gate clear:
250*2234Sdanice 	if (!pci_spurintr_msgs) { /* tomatillo errata #71 spurious mondo */
251*2234Sdanice 		/* clear the pending state */
252*2234Sdanice 		IB_INO_INTR_CLEAR(ino_p->ino_clr_reg);
2530Sstevel@tonic-gate 		return (DDI_INTR_CLAIMED);
254*2234Sdanice 	}
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	err_fmt_str = "!%s%d: spurious interrupt from ino 0x%x";
2570Sstevel@tonic-gate warn:
2580Sstevel@tonic-gate 	cmn_err(CE_WARN, err_fmt_str, NAMEINST(pci_p->pci_dip), ino_p->ino_ino);
2590Sstevel@tonic-gate 	for (i = 0; i < ino_p->ino_ih_size; i++, ih_p = ih_p->ih_next)
2600Sstevel@tonic-gate 		cmn_err(CE_CONT, "!%s-%d#%x ", NAMEINST(ih_p->ih_dip),
2610Sstevel@tonic-gate 		    ih_p->ih_inum);
2620Sstevel@tonic-gate 	cmn_err(CE_CONT, "!\n");
263*2234Sdanice 	if (blocked == B_FALSE)  /* clear the pending state */
264*2234Sdanice 		IB_INO_INTR_CLEAR(ino_p->ino_clr_reg);
265*2234Sdanice 
2660Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate /*
2700Sstevel@tonic-gate  * pci_intr_wrapper
2710Sstevel@tonic-gate  *
2720Sstevel@tonic-gate  * This routine is used as wrapper around interrupt handlers installed by child
2730Sstevel@tonic-gate  * device drivers.  This routine invokes the driver interrupt handlers and
2740Sstevel@tonic-gate  * examines the return codes.
2750Sstevel@tonic-gate  * There is a count of unclaimed interrupts kept on a per-ino basis. If at
2760Sstevel@tonic-gate  * least one handler claims the interrupt then the counter is halved and the
2770Sstevel@tonic-gate  * interrupt state machine is idled. If no handler claims the interrupt then
2780Sstevel@tonic-gate  * the counter is incremented by one and the state machine is idled.
2790Sstevel@tonic-gate  * If the count ever reaches the limit value set by pci_unclaimed_intr_max
2800Sstevel@tonic-gate  * then the interrupt state machine is not idled thus preventing any further
2810Sstevel@tonic-gate  * interrupts on that ino. The state machine will only be idled again if a
2820Sstevel@tonic-gate  * handler is subsequently added or removed.
2830Sstevel@tonic-gate  *
2840Sstevel@tonic-gate  * return value: DDI_INTR_CLAIMED if any handlers claimed the interrupt,
2850Sstevel@tonic-gate  * DDI_INTR_UNCLAIMED otherwise.
2860Sstevel@tonic-gate  */
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate extern uint64_t intr_get_time(void);
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate uint_t
2910Sstevel@tonic-gate pci_intr_wrapper(caddr_t arg)
2920Sstevel@tonic-gate {
2930Sstevel@tonic-gate 	ib_ino_info_t *ino_p = (ib_ino_info_t *)arg;
2940Sstevel@tonic-gate 	uint_t result = 0, r;
2950Sstevel@tonic-gate 	pci_t *pci_p = ino_p->ino_ib_p->ib_pci_p;
2960Sstevel@tonic-gate 	pbm_t *pbm_p = pci_p->pci_pbm_p;
2970Sstevel@tonic-gate 	ih_t *ih_p = ino_p->ino_ih_start;
2980Sstevel@tonic-gate 	int i;
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	for (i = 0; i < ino_p->ino_ih_size; i++, ih_p = ih_p->ih_next) {
3010Sstevel@tonic-gate 		dev_info_t *dip = ih_p->ih_dip;
3020Sstevel@tonic-gate 		uint_t (*handler)() = ih_p->ih_handler;
3030Sstevel@tonic-gate 		caddr_t arg1 = ih_p->ih_handler_arg1;
3040Sstevel@tonic-gate 		caddr_t arg2 = ih_p->ih_handler_arg2;
3050Sstevel@tonic-gate 		ddi_acc_handle_t cfg_hdl = ih_p->ih_config_handle;
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 		if (pci_intr_dma_sync && cfg_hdl && pbm_p->pbm_sync_reg_pa) {
3080Sstevel@tonic-gate 			(void) pci_config_get16(cfg_hdl, PCI_CONF_VENID);
3090Sstevel@tonic-gate 			pci_pbm_dma_sync(pbm_p, ino_p->ino_ino);
3100Sstevel@tonic-gate 		}
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 		if (ih_p->ih_intr_state == PCI_INTR_STATE_DISABLE) {
3130Sstevel@tonic-gate 			DEBUG3(DBG_INTR, pci_p->pci_dip,
3140Sstevel@tonic-gate 			    "pci_intr_wrapper: %s%d interrupt %d is disabled\n",
3150Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip),
3160Sstevel@tonic-gate 			    ino_p->ino_ino);
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 			continue;
3190Sstevel@tonic-gate 		}
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 		DTRACE_PROBE4(interrupt__start, dev_info_t, dip,
3220Sstevel@tonic-gate 		    void *, handler, caddr_t, arg1, caddr_t, arg2);
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 		r = (*handler)(arg1, arg2);
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 		/*
3270Sstevel@tonic-gate 		 * Account for time used by this interrupt. Protect against
3280Sstevel@tonic-gate 		 * conflicting writes to ih_ticks from ib_intr_dist_all() by
3290Sstevel@tonic-gate 		 * using atomic ops.
3300Sstevel@tonic-gate 		 */
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 		if (ino_p->ino_pil <= LOCK_LEVEL)
3330Sstevel@tonic-gate 			atomic_add_64(&ih_p->ih_ticks, intr_get_time());
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 		DTRACE_PROBE4(interrupt__complete, dev_info_t, dip,
3360Sstevel@tonic-gate 		    void *, handler, caddr_t, arg1, int, r);
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 		result += r;
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 		if (pci_check_all_handlers)
3410Sstevel@tonic-gate 			continue;
3420Sstevel@tonic-gate 		if (result)
3430Sstevel@tonic-gate 			break;
3440Sstevel@tonic-gate 	}
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	if (!result)
3470Sstevel@tonic-gate 		return (pci_spurintr(ino_p));
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	ino_p->ino_unclaimed = 0;
3500Sstevel@tonic-gate 	IB_INO_INTR_CLEAR(ino_p->ino_clr_reg);  /* clear the pending state */
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
3530Sstevel@tonic-gate }
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate dev_info_t *
3560Sstevel@tonic-gate get_my_childs_dip(dev_info_t *dip, dev_info_t *rdip)
3570Sstevel@tonic-gate {
3580Sstevel@tonic-gate 	dev_info_t *cdip = rdip;
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	for (; ddi_get_parent(cdip) != dip; cdip = ddi_get_parent(cdip))
3610Sstevel@tonic-gate 		;
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	return (cdip);
3640Sstevel@tonic-gate }
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate /* default class to pil value mapping */
3670Sstevel@tonic-gate pci_class_val_t pci_default_pil [] = {
3680Sstevel@tonic-gate 	{0x000000, 0xff0000, 0x1},	/* Class code for pre-2.0 devices */
3690Sstevel@tonic-gate 	{0x010000, 0xff0000, 0x4},	/* Mass Storage Controller */
3700Sstevel@tonic-gate 	{0x020000, 0xff0000, 0x6},	/* Network Controller */
3710Sstevel@tonic-gate 	{0x030000, 0xff0000, 0x9},	/* Display Controller */
3720Sstevel@tonic-gate 	{0x040000, 0xff0000, 0x9},	/* Multimedia Controller */
3730Sstevel@tonic-gate 	{0x050000, 0xff0000, 0xb},	/* Memory Controller */
3740Sstevel@tonic-gate 	{0x060000, 0xff0000, 0xb},	/* Bridge Controller */
3750Sstevel@tonic-gate 	{0x0c0000, 0xffff00, 0x9},	/* Serial Bus, FireWire (IEEE 1394) */
3760Sstevel@tonic-gate 	{0x0c0100, 0xffff00, 0x4},	/* Serial Bus, ACCESS.bus */
3770Sstevel@tonic-gate 	{0x0c0200, 0xffff00, 0x4},	/* Serial Bus, SSA */
3780Sstevel@tonic-gate 	{0x0c0300, 0xffff00, 0x9},	/* Serial Bus Universal Serial Bus */
3790Sstevel@tonic-gate 	{0x0c0400, 0xffff00, 0x6},	/* Serial Bus, Fibre Channel */
3800Sstevel@tonic-gate 	{0x0c0600, 0xffff00, 0x6}	/* Serial Bus, Infiniband */
3810Sstevel@tonic-gate };
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate /*
3840Sstevel@tonic-gate  * Default class to intr_weight value mapping (% of CPU).  A driver.conf
3850Sstevel@tonic-gate  * entry on or above the pci node like
3860Sstevel@tonic-gate  *
3870Sstevel@tonic-gate  *	pci-class-intr-weights= 0x020000, 0xff0000, 30;
3880Sstevel@tonic-gate  *
3890Sstevel@tonic-gate  * can be used to augment or override entries in the default table below.
3900Sstevel@tonic-gate  *
3910Sstevel@tonic-gate  * NB: The values below give NICs preference on redistribution, and provide
3920Sstevel@tonic-gate  * NICs some isolation from other interrupt sources. We need better interfaces
3930Sstevel@tonic-gate  * that allow the NIC driver to identify a specific NIC instance as high
3940Sstevel@tonic-gate  * bandwidth, and thus deserving of separation from other low bandwidth
3950Sstevel@tonic-gate  * NICs additional isolation from other interrupt sources.
3960Sstevel@tonic-gate  *
3970Sstevel@tonic-gate  * NB: We treat Infiniband like a NIC.
3980Sstevel@tonic-gate  */
3990Sstevel@tonic-gate pci_class_val_t pci_default_intr_weight [] = {
4000Sstevel@tonic-gate 	{0x020000, 0xff0000, 35},	/* Network Controller */
4010Sstevel@tonic-gate 	{0x010000, 0xff0000, 10},	/* Mass Storage Controller */
4020Sstevel@tonic-gate 	{0x0c0400, 0xffff00, 10},	/* Serial Bus, Fibre Channel */
4030Sstevel@tonic-gate 	{0x0c0600, 0xffff00, 50}	/* Serial Bus, Infiniband */
4040Sstevel@tonic-gate };
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate static uint32_t
4070Sstevel@tonic-gate pci_match_class_val(uint32_t key, pci_class_val_t *rec_p, int nrec,
4080Sstevel@tonic-gate     uint32_t default_val)
4090Sstevel@tonic-gate {
4100Sstevel@tonic-gate 	int i;
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	for (i = 0; i < nrec; rec_p++, i++) {
4130Sstevel@tonic-gate 		if ((rec_p->class_code & rec_p->class_mask) ==
4140Sstevel@tonic-gate 		    (key & rec_p->class_mask))
4150Sstevel@tonic-gate 			return (rec_p->class_val);
4160Sstevel@tonic-gate 	}
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	return (default_val);
4190Sstevel@tonic-gate }
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate /*
4220Sstevel@tonic-gate  * Return the configuration value, based on class code and sub class code,
4230Sstevel@tonic-gate  * from the specified property based or default pci_class_val_t table.
4240Sstevel@tonic-gate  */
4250Sstevel@tonic-gate uint32_t
4260Sstevel@tonic-gate pci_class_to_val(dev_info_t *rdip, char *property_name, pci_class_val_t *rec_p,
4270Sstevel@tonic-gate     int nrec, uint32_t default_val)
4280Sstevel@tonic-gate {
4290Sstevel@tonic-gate 	int property_len;
4300Sstevel@tonic-gate 	uint32_t class_code;
4310Sstevel@tonic-gate 	pci_class_val_t *conf;
4320Sstevel@tonic-gate 	uint32_t val = default_val;
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	/*
4350Sstevel@tonic-gate 	 * Use the "class-code" property to get the base and sub class
4360Sstevel@tonic-gate 	 * codes for the requesting device.
4370Sstevel@tonic-gate 	 */
4380Sstevel@tonic-gate 	class_code = (uint32_t)ddi_prop_get_int(DDI_DEV_T_ANY, rdip,
4390Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "class-code", -1);
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	if (class_code == -1)
4420Sstevel@tonic-gate 		return (val);
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	/* look up the val from the default table */
4450Sstevel@tonic-gate 	val = pci_match_class_val(class_code, rec_p, nrec, val);
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	/* see if there is a more specific property specified value */
4490Sstevel@tonic-gate 	if (ddi_getlongprop(DDI_DEV_T_ANY, rdip, DDI_PROP_NOTPROM,
4500Sstevel@tonic-gate 	    property_name, (caddr_t)&conf, &property_len))
4510Sstevel@tonic-gate 			return (val);
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	if ((property_len % sizeof (pci_class_val_t)) == 0)
4540Sstevel@tonic-gate 		val = pci_match_class_val(class_code, conf,
4550Sstevel@tonic-gate 		    property_len / sizeof (pci_class_val_t), val);
4560Sstevel@tonic-gate 	kmem_free(conf, property_len);
4570Sstevel@tonic-gate 	return (val);
4580Sstevel@tonic-gate }
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate /* pci_class_to_pil: return the pil for a given PCI device. */
4610Sstevel@tonic-gate uint32_t
4620Sstevel@tonic-gate pci_class_to_pil(dev_info_t *rdip)
4630Sstevel@tonic-gate {
4640Sstevel@tonic-gate 	uint32_t pil;
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	/* default pil is 0 (uninitialized) */
4670Sstevel@tonic-gate 	pil = pci_class_to_val(rdip,
4680Sstevel@tonic-gate 	    "pci-class-priorities", pci_default_pil,
4690Sstevel@tonic-gate 	    sizeof (pci_default_pil) / sizeof (pci_class_val_t), 0);
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	/* range check the result */
4720Sstevel@tonic-gate 	if (pil >= 0xf)
4730Sstevel@tonic-gate 		pil = 0;
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	return (pil);
4760Sstevel@tonic-gate }
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate /* pci_class_to_intr_weight: return the intr_weight for a given PCI device. */
4790Sstevel@tonic-gate int32_t
4800Sstevel@tonic-gate pci_class_to_intr_weight(dev_info_t *rdip)
4810Sstevel@tonic-gate {
4820Sstevel@tonic-gate 	int32_t intr_weight;
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	/* default weight is 0% */
4850Sstevel@tonic-gate 	intr_weight = pci_class_to_val(rdip,
4860Sstevel@tonic-gate 	    "pci-class-intr-weights", pci_default_intr_weight,
4870Sstevel@tonic-gate 	    sizeof (pci_default_intr_weight) / sizeof (pci_class_val_t), 0);
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	/* range check the result */
4900Sstevel@tonic-gate 	if (intr_weight < 0)
4910Sstevel@tonic-gate 		intr_weight = 0;
4920Sstevel@tonic-gate 	if (intr_weight > 1000)
4930Sstevel@tonic-gate 		intr_weight = 1000;
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	return (intr_weight);
4960Sstevel@tonic-gate }
4970Sstevel@tonic-gate 
49866Sesolom static struct {
49966Sesolom 	kstat_named_t pciintr_ks_name;
50066Sesolom 	kstat_named_t pciintr_ks_type;
50166Sesolom 	kstat_named_t pciintr_ks_cpu;
50266Sesolom 	kstat_named_t pciintr_ks_pil;
50366Sesolom 	kstat_named_t pciintr_ks_time;
50466Sesolom 	kstat_named_t pciintr_ks_ino;
50566Sesolom 	kstat_named_t pciintr_ks_cookie;
50666Sesolom 	kstat_named_t pciintr_ks_devpath;
50766Sesolom 	kstat_named_t pciintr_ks_buspath;
50866Sesolom } pciintr_ks_template = {
50966Sesolom 	{ "name",	KSTAT_DATA_CHAR },
51066Sesolom 	{ "type",	KSTAT_DATA_CHAR },
51166Sesolom 	{ "cpu",	KSTAT_DATA_UINT64 },
51266Sesolom 	{ "pil",	KSTAT_DATA_UINT64 },
51366Sesolom 	{ "time",	KSTAT_DATA_UINT64 },
51466Sesolom 	{ "ino",	KSTAT_DATA_UINT64 },
51566Sesolom 	{ "cookie",	KSTAT_DATA_UINT64 },
51666Sesolom 	{ "devpath",	KSTAT_DATA_STRING },
51766Sesolom 	{ "buspath",	KSTAT_DATA_STRING },
51866Sesolom };
51966Sesolom static uint32_t pciintr_ks_instance;
5201811Sesolom static char ih_devpath[MAXPATHLEN];
5211811Sesolom static char ih_buspath[MAXPATHLEN];
52266Sesolom 
52366Sesolom kmutex_t pciintr_ks_template_lock;
52466Sesolom 
52566Sesolom int
52666Sesolom pci_ks_update(kstat_t *ksp, int rw)
52766Sesolom {
52866Sesolom 	ih_t *ih_p = ksp->ks_private;
52966Sesolom 	int maxlen = sizeof (pciintr_ks_template.pciintr_ks_name.value.c);
53066Sesolom 	ib_t *ib_p = ih_p->ih_ino_p->ino_ib_p;
53166Sesolom 	pci_t *pci_p = ib_p->ib_pci_p;
53266Sesolom 	ib_ino_t ino;
53366Sesolom 
53466Sesolom 	ino = ih_p->ih_ino_p->ino_ino;
53566Sesolom 
53666Sesolom 	(void) snprintf(pciintr_ks_template.pciintr_ks_name.value.c, maxlen,
53766Sesolom 	    "%s%d", ddi_driver_name(ih_p->ih_dip),
53866Sesolom 	    ddi_get_instance(ih_p->ih_dip));
53966Sesolom 
54066Sesolom 	(void) ddi_pathname(ih_p->ih_dip, ih_devpath);
54166Sesolom 	(void) ddi_pathname(pci_p->pci_dip, ih_buspath);
54266Sesolom 	kstat_named_setstr(&pciintr_ks_template.pciintr_ks_devpath, ih_devpath);
54366Sesolom 	kstat_named_setstr(&pciintr_ks_template.pciintr_ks_buspath, ih_buspath);
54466Sesolom 
5451087Sschwartz 	if (ih_p->ih_intr_state == PCI_INTR_STATE_ENABLE) {
5461087Sschwartz 		(void) strcpy(pciintr_ks_template.pciintr_ks_type.value.c,
5471087Sschwartz 		    "fixed");
5481087Sschwartz 		pciintr_ks_template.pciintr_ks_cpu.value.ui64 =
5491087Sschwartz 		    ih_p->ih_ino_p->ino_cpuid;
5501087Sschwartz 		pciintr_ks_template.pciintr_ks_pil.value.ui64 =
5511087Sschwartz 		    ih_p->ih_ino_p->ino_pil;
5521087Sschwartz 		pciintr_ks_template.pciintr_ks_time.value.ui64 = ih_p->ih_nsec +
5531087Sschwartz 		    (uint64_t)tick2ns((hrtime_t)ih_p->ih_ticks,
5541087Sschwartz 			ih_p->ih_ino_p->ino_cpuid);
5551087Sschwartz 		pciintr_ks_template.pciintr_ks_ino.value.ui64 = ino;
5561087Sschwartz 		pciintr_ks_template.pciintr_ks_cookie.value.ui64 =
5571087Sschwartz 			IB_INO_TO_MONDO(ib_p, ino);
5581087Sschwartz 	} else {
5591087Sschwartz 		(void) strcpy(pciintr_ks_template.pciintr_ks_type.value.c,
5601087Sschwartz 		    "disabled");
5611087Sschwartz 		pciintr_ks_template.pciintr_ks_cpu.value.ui64 = 0;
5621087Sschwartz 		pciintr_ks_template.pciintr_ks_pil.value.ui64 = 0;
5631087Sschwartz 		pciintr_ks_template.pciintr_ks_time.value.ui64 = 0;
5641087Sschwartz 		pciintr_ks_template.pciintr_ks_ino.value.ui64 = 0;
5651087Sschwartz 		pciintr_ks_template.pciintr_ks_cookie.value.ui64 = 0;
5661087Sschwartz 	}
5671087Sschwartz 
56866Sesolom 	return (0);
56966Sesolom }
57066Sesolom 
5710Sstevel@tonic-gate int
5720Sstevel@tonic-gate pci_add_intr(dev_info_t *dip, dev_info_t *rdip, ddi_intr_handle_impl_t *hdlp)
5730Sstevel@tonic-gate {
5740Sstevel@tonic-gate 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
5750Sstevel@tonic-gate 	ib_t *ib_p = pci_p->pci_ib_p;
5760Sstevel@tonic-gate 	cb_t *cb_p = pci_p->pci_cb_p;
5770Sstevel@tonic-gate 	ih_t *ih_p;
5780Sstevel@tonic-gate 	ib_ino_t ino;
5790Sstevel@tonic-gate 	ib_ino_info_t *ino_p;		/* pulse interrupts have no ino */
5800Sstevel@tonic-gate 	ib_mondo_t mondo;
5810Sstevel@tonic-gate 	uint32_t cpu_id;
5820Sstevel@tonic-gate 	int ret;
5830Sstevel@tonic-gate 	int32_t weight;
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 	ino = IB_MONDO_TO_INO(hdlp->ih_vector);
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	DEBUG3(DBG_A_INTX, dip, "pci_add_intr: rdip=%s%d ino=%x\n",
5880Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip), ino);
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 	if (ino > ib_p->ib_max_ino) {
5910Sstevel@tonic-gate 		DEBUG1(DBG_A_INTX, dip, "ino %x is invalid\n", ino);
5920Sstevel@tonic-gate 		return (DDI_INTR_NOTFOUND);
5930Sstevel@tonic-gate 	}
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	if (hdlp->ih_vector & PCI_PULSE_INO) {
5960Sstevel@tonic-gate 		volatile uint64_t *map_reg_addr;
5970Sstevel@tonic-gate 		map_reg_addr = ib_intr_map_reg_addr(ib_p, ino);
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 		mondo = pci_xlate_intr(dip, rdip, ib_p, ino);
6000Sstevel@tonic-gate 		if (mondo == 0)
6010Sstevel@tonic-gate 			goto fail1;
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 		hdlp->ih_vector = CB_MONDO_TO_XMONDO(cb_p, mondo);
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 		if (i_ddi_add_ivintr(hdlp) != DDI_SUCCESS)
6060Sstevel@tonic-gate 			goto fail1;
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 		/*
6090Sstevel@tonic-gate 		 * Select cpu and program.
6100Sstevel@tonic-gate 		 *
6110Sstevel@tonic-gate 		 * Since there is no good way to always derive cpuid in
6120Sstevel@tonic-gate 		 * pci_remove_intr for PCI_PULSE_INO (esp. for STARFIRE), we
6130Sstevel@tonic-gate 		 * don't add (or remove) device weight for pulsed interrupt
6140Sstevel@tonic-gate 		 * sources.
6150Sstevel@tonic-gate 		 */
6160Sstevel@tonic-gate 		mutex_enter(&ib_p->ib_intr_lock);
6170Sstevel@tonic-gate 		cpu_id = intr_dist_cpuid();
6180Sstevel@tonic-gate 		*map_reg_addr = ib_get_map_reg(mondo, cpu_id);
6190Sstevel@tonic-gate 		mutex_exit(&ib_p->ib_intr_lock);
6200Sstevel@tonic-gate 		*map_reg_addr;	/* flush previous write */
6210Sstevel@tonic-gate 		goto done;
6220Sstevel@tonic-gate 	}
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	if ((mondo = pci_xlate_intr(dip, rdip, pci_p->pci_ib_p, ino)) == 0)
6250Sstevel@tonic-gate 		goto fail1;
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	ino = IB_MONDO_TO_INO(mondo);
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
6300Sstevel@tonic-gate 	ih_p = ib_alloc_ih(rdip, hdlp->ih_inum,
6310Sstevel@tonic-gate 	    hdlp->ih_cb_func, hdlp->ih_cb_arg1, hdlp->ih_cb_arg2);
6320Sstevel@tonic-gate 	if (map_pcidev_cfg_reg(dip, rdip, &ih_p->ih_config_handle))
6330Sstevel@tonic-gate 		goto fail2;
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	if (ino_p = ib_locate_ino(ib_p, ino)) {		/* sharing ino */
6360Sstevel@tonic-gate 		uint32_t intr_index = hdlp->ih_inum;
6370Sstevel@tonic-gate 		if (ib_ino_locate_intr(ino_p, rdip, intr_index)) {
6380Sstevel@tonic-gate 			DEBUG1(DBG_A_INTX, dip, "dup intr #%d\n", intr_index);
6390Sstevel@tonic-gate 			goto fail3;
6400Sstevel@tonic-gate 		}
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 		/* add weight to the cpu that we are already targeting */
6430Sstevel@tonic-gate 		cpu_id = ino_p->ino_cpuid;
6440Sstevel@tonic-gate 		weight = pci_class_to_intr_weight(rdip);
6450Sstevel@tonic-gate 		intr_dist_cpuid_add_device_weight(cpu_id, rdip, weight);
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 		ib_ino_add_intr(pci_p, ino_p, ih_p);
6480Sstevel@tonic-gate 		goto ino_done;
6490Sstevel@tonic-gate 	}
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 	ino_p = ib_new_ino(ib_p, ino, ih_p);
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	if (hdlp->ih_pri == 0)
6540Sstevel@tonic-gate 		hdlp->ih_pri = pci_class_to_pil(rdip);
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 	hdlp->ih_vector = CB_MONDO_TO_XMONDO(cb_p, mondo);
6570Sstevel@tonic-gate 
658909Segillett 	/* Store this global mondo */
659909Segillett 	ino_p->ino_mondo = hdlp->ih_vector;
660909Segillett 
6610Sstevel@tonic-gate 	DEBUG2(DBG_A_INTX, dip, "pci_add_intr:  pil=0x%x mondo=0x%x\n",
6620Sstevel@tonic-gate 	    hdlp->ih_pri, hdlp->ih_vector);
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp,
6650Sstevel@tonic-gate 	    (ddi_intr_handler_t *)pci_intr_wrapper, (caddr_t)ino_p, NULL);
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 	ret = i_ddi_add_ivintr(hdlp);
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	/*
6700Sstevel@tonic-gate 	 * Restore original interrupt handler
6710Sstevel@tonic-gate 	 * and arguments in interrupt handle.
6720Sstevel@tonic-gate 	 */
6730Sstevel@tonic-gate 	DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp, ih_p->ih_handler,
6740Sstevel@tonic-gate 	    ih_p->ih_handler_arg1, ih_p->ih_handler_arg2);
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
6770Sstevel@tonic-gate 		goto fail4;
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	/* Save the pil for this ino */
6800Sstevel@tonic-gate 	ino_p->ino_pil = hdlp->ih_pri;
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 	/* clear and enable interrupt */
6830Sstevel@tonic-gate 	IB_INO_INTR_CLEAR(ino_p->ino_clr_reg);
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 	/* select cpu and compute weight, saving both for sharing and removal */
6860Sstevel@tonic-gate 	cpu_id = pci_intr_dist_cpuid(ib_p, ino_p);
6870Sstevel@tonic-gate 	ino_p->ino_cpuid = cpu_id;
6880Sstevel@tonic-gate 	ino_p->ino_established = 1;
6890Sstevel@tonic-gate 	weight = pci_class_to_intr_weight(rdip);
6900Sstevel@tonic-gate 	intr_dist_cpuid_add_device_weight(cpu_id, rdip, weight);
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate #ifdef _STARFIRE
6930Sstevel@tonic-gate 	cpu_id = pc_translate_tgtid(cb_p->cb_ittrans_cookie, cpu_id,
6940Sstevel@tonic-gate 		IB_GET_MAPREG_INO(ino));
6950Sstevel@tonic-gate #endif /* _STARFIRE */
6960Sstevel@tonic-gate 	*ino_p->ino_map_reg = ib_get_map_reg(mondo, cpu_id);
6970Sstevel@tonic-gate 	*ino_p->ino_map_reg;
6980Sstevel@tonic-gate ino_done:
6990Sstevel@tonic-gate 	ih_p->ih_ino_p = ino_p;
70066Sesolom 	ih_p->ih_ksp = kstat_create("pci_intrs",
70166Sesolom 	    atomic_inc_32_nv(&pciintr_ks_instance), "config", "interrupts",
70266Sesolom 	    KSTAT_TYPE_NAMED,
70366Sesolom 	    sizeof (pciintr_ks_template) / sizeof (kstat_named_t),
70466Sesolom 	    KSTAT_FLAG_VIRTUAL);
70566Sesolom 	if (ih_p->ih_ksp != NULL) {
70666Sesolom 		ih_p->ih_ksp->ks_data_size += MAXPATHLEN * 2;
70766Sesolom 		ih_p->ih_ksp->ks_lock = &pciintr_ks_template_lock;
70866Sesolom 		ih_p->ih_ksp->ks_data = &pciintr_ks_template;
70966Sesolom 		ih_p->ih_ksp->ks_private = ih_p;
71066Sesolom 		ih_p->ih_ksp->ks_update = pci_ks_update;
7110Sstevel@tonic-gate 		kstat_install(ih_p->ih_ksp);
71266Sesolom 	}
7130Sstevel@tonic-gate 	ib_ino_map_reg_share(ib_p, ino, ino_p);
7140Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
7150Sstevel@tonic-gate done:
7160Sstevel@tonic-gate 	DEBUG2(DBG_A_INTX, dip, "done! Interrupt 0x%x pil=%x\n",
7170Sstevel@tonic-gate 		hdlp->ih_vector, hdlp->ih_pri);
7180Sstevel@tonic-gate 	return (DDI_SUCCESS);
7190Sstevel@tonic-gate fail4:
7200Sstevel@tonic-gate 	ib_delete_ino(ib_p, ino_p);
7210Sstevel@tonic-gate fail3:
7220Sstevel@tonic-gate 	if (ih_p->ih_config_handle)
7230Sstevel@tonic-gate 		pci_config_teardown(&ih_p->ih_config_handle);
7240Sstevel@tonic-gate fail2:
7250Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
7260Sstevel@tonic-gate 	kmem_free(ih_p, sizeof (ih_t));
7270Sstevel@tonic-gate fail1:
7280Sstevel@tonic-gate 	DEBUG2(DBG_A_INTX, dip, "Failed! Interrupt 0x%x pil=%x\n",
7290Sstevel@tonic-gate 		hdlp->ih_vector, hdlp->ih_pri);
7300Sstevel@tonic-gate 	return (DDI_FAILURE);
7310Sstevel@tonic-gate }
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate int
7340Sstevel@tonic-gate pci_remove_intr(dev_info_t *dip, dev_info_t *rdip, ddi_intr_handle_impl_t *hdlp)
7350Sstevel@tonic-gate {
7360Sstevel@tonic-gate 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
7370Sstevel@tonic-gate 	ib_t *ib_p = pci_p->pci_ib_p;
7380Sstevel@tonic-gate 	cb_t *cb_p = pci_p->pci_cb_p;
7390Sstevel@tonic-gate 	ib_ino_t ino;
7400Sstevel@tonic-gate 	ib_mondo_t mondo;
7410Sstevel@tonic-gate 	ib_ino_info_t *ino_p;	/* non-pulse only */
7420Sstevel@tonic-gate 	ih_t *ih_p;		/* non-pulse only */
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	ino = IB_MONDO_TO_INO(hdlp->ih_vector);
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 	DEBUG3(DBG_R_INTX, dip, "pci_rem_intr: rdip=%s%d ino=%x\n",
7470Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip), ino);
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 	if (hdlp->ih_vector & PCI_PULSE_INO) { /* pulse interrupt */
7500Sstevel@tonic-gate 		volatile uint64_t *map_reg_addr;
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 		/*
7530Sstevel@tonic-gate 		 * No weight was added by pci_add_intr for PCI_PULSE_INO
7540Sstevel@tonic-gate 		 * because it is difficult to determine cpuid here.
7550Sstevel@tonic-gate 		 */
7560Sstevel@tonic-gate 		map_reg_addr = ib_intr_map_reg_addr(ib_p, ino);
7570Sstevel@tonic-gate 		IB_INO_INTR_RESET(map_reg_addr);	/* disable intr */
7580Sstevel@tonic-gate 		*map_reg_addr;
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 		mondo = pci_xlate_intr(dip, rdip, ib_p, ino);
7610Sstevel@tonic-gate 		if (mondo == 0) {
7620Sstevel@tonic-gate 			DEBUG1(DBG_R_INTX, dip,
7630Sstevel@tonic-gate 				"can't get mondo for ino %x\n", ino);
7640Sstevel@tonic-gate 			return (DDI_FAILURE);
7650Sstevel@tonic-gate 		}
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 		if (hdlp->ih_pri == 0)
7680Sstevel@tonic-gate 			hdlp->ih_pri = pci_class_to_pil(rdip);
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 		hdlp->ih_vector = CB_MONDO_TO_XMONDO(cb_p, mondo);
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 		DEBUG2(DBG_R_INTX, dip, "pci_rem_intr: pil=0x%x mondo=0x%x\n",
7730Sstevel@tonic-gate 		    hdlp->ih_pri, hdlp->ih_vector);
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 		i_ddi_rem_ivintr(hdlp);
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 		DEBUG2(DBG_R_INTX, dip, "pulse success mondo=%x reg=%p\n",
7780Sstevel@tonic-gate 			mondo, map_reg_addr);
7790Sstevel@tonic-gate 		return (DDI_SUCCESS);
7800Sstevel@tonic-gate 	}
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 	/* Translate the interrupt property */
7830Sstevel@tonic-gate 	mondo = pci_xlate_intr(dip, rdip, pci_p->pci_ib_p, ino);
7840Sstevel@tonic-gate 	if (mondo == 0) {
7850Sstevel@tonic-gate 		DEBUG1(DBG_R_INTX, dip, "can't get mondo for ino %x\n", ino);
7860Sstevel@tonic-gate 		return (DDI_FAILURE);
7870Sstevel@tonic-gate 	}
7880Sstevel@tonic-gate 	ino = IB_MONDO_TO_INO(mondo);
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
7910Sstevel@tonic-gate 	ino_p = ib_locate_ino(ib_p, ino);
7920Sstevel@tonic-gate 	if (!ino_p) {
7930Sstevel@tonic-gate 		int r = cb_remove_xintr(pci_p, dip, rdip, ino, mondo);
7940Sstevel@tonic-gate 		if (r != DDI_SUCCESS)
7950Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d-xintr: ino %x is invalid",
7960Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip), ino);
7970Sstevel@tonic-gate 		mutex_exit(&ib_p->ib_ino_lst_mutex);
7980Sstevel@tonic-gate 		return (r);
7990Sstevel@tonic-gate 	}
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	ih_p = ib_ino_locate_intr(ino_p, rdip, hdlp->ih_inum);
8020Sstevel@tonic-gate 	ib_ino_rem_intr(pci_p, ino_p, ih_p);
8030Sstevel@tonic-gate 	intr_dist_cpuid_rem_device_weight(ino_p->ino_cpuid, rdip);
8040Sstevel@tonic-gate 	if (ino_p->ino_ih_size == 0) {
8050Sstevel@tonic-gate 		IB_INO_INTR_PEND(ib_clear_intr_reg_addr(ib_p, ino));
8060Sstevel@tonic-gate 		hdlp->ih_vector = CB_MONDO_TO_XMONDO(cb_p, mondo);
8070Sstevel@tonic-gate 		if (hdlp->ih_pri == 0)
8080Sstevel@tonic-gate 			hdlp->ih_pri = pci_class_to_pil(rdip);
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 		i_ddi_rem_ivintr(hdlp);
8110Sstevel@tonic-gate 		ib_delete_ino(ib_p, ino_p);
8120Sstevel@tonic-gate 	}
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 	/* re-enable interrupt only if mapping register still shared */
8150Sstevel@tonic-gate 	if (ib_ino_map_reg_unshare(ib_p, ino, ino_p)) {
8160Sstevel@tonic-gate 		IB_INO_INTR_ON(ino_p->ino_map_reg);
8170Sstevel@tonic-gate 		*ino_p->ino_map_reg;
8180Sstevel@tonic-gate 	}
8190Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 	if (ino_p->ino_ih_size == 0)
8220Sstevel@tonic-gate 		kmem_free(ino_p, sizeof (ib_ino_info_t));
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	DEBUG1(DBG_R_INTX, dip, "success! mondo=%x\n", mondo);
8250Sstevel@tonic-gate 	return (DDI_SUCCESS);
8260Sstevel@tonic-gate }
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate /*
8290Sstevel@tonic-gate  * free the pci_inos array allocated during pci_intr_setup. the actual
8300Sstevel@tonic-gate  * interrupts are torn down by their respective block destroy routines:
8310Sstevel@tonic-gate  * cb_destroy, pbm_destroy, and ib_destroy.
8320Sstevel@tonic-gate  */
8330Sstevel@tonic-gate void
8340Sstevel@tonic-gate pci_intr_teardown(pci_t *pci_p)
8350Sstevel@tonic-gate {
8360Sstevel@tonic-gate 	kmem_free(pci_p->pci_inos, pci_p->pci_inos_len);
8370Sstevel@tonic-gate 	pci_p->pci_inos = NULL;
8380Sstevel@tonic-gate 	pci_p->pci_inos_len = 0;
8390Sstevel@tonic-gate }
840