xref: /onnv-gate/usr/src/uts/sun4u/io/pci/pci_intr.c (revision 2973:55b674bffad9)
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
223*2973Sgovinda pci_spurintr(ib_ino_pil_t *ipil_p) {
224*2973Sgovinda 	ib_ino_info_t	*ino_p = ipil_p->ipil_ino_p;
225*2973Sgovinda 	ih_t		*ih_p = ipil_p->ipil_ih_start;
226*2973Sgovinda 	pci_t		*pci_p = ino_p->ino_ib_p->ib_pci_p;
227*2973Sgovinda 	char		*err_fmt_str;
228*2973Sgovinda 	boolean_t	blocked = B_FALSE;
229*2973Sgovinda 	int		i;
2300Sstevel@tonic-gate 
231*2973Sgovinda 	if (ino_p->ino_unclaimed_intrs > pci_unclaimed_intr_max)
2320Sstevel@tonic-gate 		return (DDI_INTR_CLAIMED);
2330Sstevel@tonic-gate 
234*2973Sgovinda 	if (!ino_p->ino_unclaimed_intrs)
2350Sstevel@tonic-gate 		ino_p->ino_spurintr_begin = ddi_get_lbolt();
2360Sstevel@tonic-gate 
237*2973Sgovinda 	ino_p->ino_unclaimed_intrs++;
2380Sstevel@tonic-gate 
239*2973Sgovinda 	if (ino_p->ino_unclaimed_intrs <= pci_unclaimed_intr_max)
2400Sstevel@tonic-gate 		goto clear;
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	if (drv_hztousec(ddi_get_lbolt() - ino_p->ino_spurintr_begin)
2430Sstevel@tonic-gate 	    > pci_spurintr_duration) {
244*2973Sgovinda 		ino_p->ino_unclaimed_intrs = 0;
2450Sstevel@tonic-gate 		goto clear;
2460Sstevel@tonic-gate 	}
2470Sstevel@tonic-gate 	err_fmt_str = "%s%d: ino 0x%x blocked";
2482234Sdanice 	blocked = B_TRUE;
2490Sstevel@tonic-gate 	goto warn;
2500Sstevel@tonic-gate clear:
2512234Sdanice 	if (!pci_spurintr_msgs) { /* tomatillo errata #71 spurious mondo */
2522234Sdanice 		/* clear the pending state */
2532234Sdanice 		IB_INO_INTR_CLEAR(ino_p->ino_clr_reg);
2540Sstevel@tonic-gate 		return (DDI_INTR_CLAIMED);
2552234Sdanice 	}
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	err_fmt_str = "!%s%d: spurious interrupt from ino 0x%x";
2580Sstevel@tonic-gate warn:
2590Sstevel@tonic-gate 	cmn_err(CE_WARN, err_fmt_str, NAMEINST(pci_p->pci_dip), ino_p->ino_ino);
260*2973Sgovinda 	for (i = 0; i < ipil_p->ipil_ih_size; i++, ih_p = ih_p->ih_next)
2610Sstevel@tonic-gate 		cmn_err(CE_CONT, "!%s-%d#%x ", NAMEINST(ih_p->ih_dip),
2620Sstevel@tonic-gate 		    ih_p->ih_inum);
2630Sstevel@tonic-gate 	cmn_err(CE_CONT, "!\n");
2642234Sdanice 	if (blocked == B_FALSE)  /* clear the pending state */
2652234Sdanice 		IB_INO_INTR_CLEAR(ino_p->ino_clr_reg);
2662234Sdanice 
2670Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
2680Sstevel@tonic-gate }
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate /*
2710Sstevel@tonic-gate  * pci_intr_wrapper
2720Sstevel@tonic-gate  *
2730Sstevel@tonic-gate  * This routine is used as wrapper around interrupt handlers installed by child
2740Sstevel@tonic-gate  * device drivers.  This routine invokes the driver interrupt handlers and
2750Sstevel@tonic-gate  * examines the return codes.
2760Sstevel@tonic-gate  * There is a count of unclaimed interrupts kept on a per-ino basis. If at
2770Sstevel@tonic-gate  * least one handler claims the interrupt then the counter is halved and the
2780Sstevel@tonic-gate  * interrupt state machine is idled. If no handler claims the interrupt then
2790Sstevel@tonic-gate  * the counter is incremented by one and the state machine is idled.
2800Sstevel@tonic-gate  * If the count ever reaches the limit value set by pci_unclaimed_intr_max
2810Sstevel@tonic-gate  * then the interrupt state machine is not idled thus preventing any further
2820Sstevel@tonic-gate  * interrupts on that ino. The state machine will only be idled again if a
2830Sstevel@tonic-gate  * handler is subsequently added or removed.
2840Sstevel@tonic-gate  *
2850Sstevel@tonic-gate  * return value: DDI_INTR_CLAIMED if any handlers claimed the interrupt,
2860Sstevel@tonic-gate  * DDI_INTR_UNCLAIMED otherwise.
2870Sstevel@tonic-gate  */
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate extern uint64_t intr_get_time(void);
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate uint_t
2920Sstevel@tonic-gate pci_intr_wrapper(caddr_t arg)
2930Sstevel@tonic-gate {
294*2973Sgovinda 	ib_ino_pil_t	*ipil_p = (ib_ino_pil_t *)arg;
295*2973Sgovinda 	ib_ino_info_t	*ino_p = ipil_p->ipil_ino_p;
296*2973Sgovinda 	uint_t		result = 0, r = DDI_INTR_UNCLAIMED;
297*2973Sgovinda 	pci_t		*pci_p = ino_p->ino_ib_p->ib_pci_p;
298*2973Sgovinda 	pbm_t		*pbm_p = pci_p->pci_pbm_p;
299*2973Sgovinda 	ih_t		*ih_p = ipil_p->ipil_ih_start;
300*2973Sgovinda 	int		i;
3010Sstevel@tonic-gate 
302*2973Sgovinda 	for (i = 0; i < ipil_p->ipil_ih_size; i++, ih_p = ih_p->ih_next) {
3030Sstevel@tonic-gate 		dev_info_t *dip = ih_p->ih_dip;
3040Sstevel@tonic-gate 		uint_t (*handler)() = ih_p->ih_handler;
3050Sstevel@tonic-gate 		caddr_t arg1 = ih_p->ih_handler_arg1;
3060Sstevel@tonic-gate 		caddr_t arg2 = ih_p->ih_handler_arg2;
3070Sstevel@tonic-gate 		ddi_acc_handle_t cfg_hdl = ih_p->ih_config_handle;
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 		if (pci_intr_dma_sync && cfg_hdl && pbm_p->pbm_sync_reg_pa) {
3100Sstevel@tonic-gate 			(void) pci_config_get16(cfg_hdl, PCI_CONF_VENID);
3110Sstevel@tonic-gate 			pci_pbm_dma_sync(pbm_p, ino_p->ino_ino);
3120Sstevel@tonic-gate 		}
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 		if (ih_p->ih_intr_state == PCI_INTR_STATE_DISABLE) {
3150Sstevel@tonic-gate 			DEBUG3(DBG_INTR, pci_p->pci_dip,
3160Sstevel@tonic-gate 			    "pci_intr_wrapper: %s%d interrupt %d is disabled\n",
3170Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip),
3180Sstevel@tonic-gate 			    ino_p->ino_ino);
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 			continue;
3210Sstevel@tonic-gate 		}
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 		DTRACE_PROBE4(interrupt__start, dev_info_t, dip,
3240Sstevel@tonic-gate 		    void *, handler, caddr_t, arg1, caddr_t, arg2);
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 		r = (*handler)(arg1, arg2);
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 		/*
3290Sstevel@tonic-gate 		 * Account for time used by this interrupt. Protect against
3300Sstevel@tonic-gate 		 * conflicting writes to ih_ticks from ib_intr_dist_all() by
3310Sstevel@tonic-gate 		 * using atomic ops.
3320Sstevel@tonic-gate 		 */
3330Sstevel@tonic-gate 
334*2973Sgovinda 		if (ipil_p->ipil_pil <= LOCK_LEVEL)
3350Sstevel@tonic-gate 			atomic_add_64(&ih_p->ih_ticks, intr_get_time());
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 		DTRACE_PROBE4(interrupt__complete, dev_info_t, dip,
3380Sstevel@tonic-gate 		    void *, handler, caddr_t, arg1, int, r);
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 		result += r;
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 		if (pci_check_all_handlers)
3430Sstevel@tonic-gate 			continue;
3440Sstevel@tonic-gate 		if (result)
3450Sstevel@tonic-gate 			break;
3460Sstevel@tonic-gate 	}
3470Sstevel@tonic-gate 
348*2973Sgovinda 	if (result)
349*2973Sgovinda 		ino_p->ino_claimed |= (1 << ipil_p->ipil_pil);
350*2973Sgovinda 
351*2973Sgovinda 	/* Interrupt can only be cleared after all pil levels are handled */
352*2973Sgovinda 	if (ipil_p->ipil_pil != ino_p->ino_lopil)
353*2973Sgovinda 		return (DDI_INTR_CLAIMED);
3540Sstevel@tonic-gate 
355*2973Sgovinda 	if (!ino_p->ino_claimed)
356*2973Sgovinda 		return (pci_spurintr(ipil_p));
357*2973Sgovinda 
358*2973Sgovinda 	ino_p->ino_unclaimed_intrs = 0;
359*2973Sgovinda 	ino_p->ino_claimed = 0;
360*2973Sgovinda 
361*2973Sgovinda 	/* Clear the pending state */
362*2973Sgovinda 	IB_INO_INTR_CLEAR(ino_p->ino_clr_reg);
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
3650Sstevel@tonic-gate }
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate dev_info_t *
3680Sstevel@tonic-gate get_my_childs_dip(dev_info_t *dip, dev_info_t *rdip)
3690Sstevel@tonic-gate {
3700Sstevel@tonic-gate 	dev_info_t *cdip = rdip;
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	for (; ddi_get_parent(cdip) != dip; cdip = ddi_get_parent(cdip))
3730Sstevel@tonic-gate 		;
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	return (cdip);
3760Sstevel@tonic-gate }
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate /* default class to pil value mapping */
3790Sstevel@tonic-gate pci_class_val_t pci_default_pil [] = {
3800Sstevel@tonic-gate 	{0x000000, 0xff0000, 0x1},	/* Class code for pre-2.0 devices */
3810Sstevel@tonic-gate 	{0x010000, 0xff0000, 0x4},	/* Mass Storage Controller */
3820Sstevel@tonic-gate 	{0x020000, 0xff0000, 0x6},	/* Network Controller */
3830Sstevel@tonic-gate 	{0x030000, 0xff0000, 0x9},	/* Display Controller */
3840Sstevel@tonic-gate 	{0x040000, 0xff0000, 0x9},	/* Multimedia Controller */
3850Sstevel@tonic-gate 	{0x050000, 0xff0000, 0xb},	/* Memory Controller */
3860Sstevel@tonic-gate 	{0x060000, 0xff0000, 0xb},	/* Bridge Controller */
3870Sstevel@tonic-gate 	{0x0c0000, 0xffff00, 0x9},	/* Serial Bus, FireWire (IEEE 1394) */
3880Sstevel@tonic-gate 	{0x0c0100, 0xffff00, 0x4},	/* Serial Bus, ACCESS.bus */
3890Sstevel@tonic-gate 	{0x0c0200, 0xffff00, 0x4},	/* Serial Bus, SSA */
3900Sstevel@tonic-gate 	{0x0c0300, 0xffff00, 0x9},	/* Serial Bus Universal Serial Bus */
3910Sstevel@tonic-gate 	{0x0c0400, 0xffff00, 0x6},	/* Serial Bus, Fibre Channel */
3920Sstevel@tonic-gate 	{0x0c0600, 0xffff00, 0x6}	/* Serial Bus, Infiniband */
3930Sstevel@tonic-gate };
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate /*
3960Sstevel@tonic-gate  * Default class to intr_weight value mapping (% of CPU).  A driver.conf
3970Sstevel@tonic-gate  * entry on or above the pci node like
3980Sstevel@tonic-gate  *
3990Sstevel@tonic-gate  *	pci-class-intr-weights= 0x020000, 0xff0000, 30;
4000Sstevel@tonic-gate  *
4010Sstevel@tonic-gate  * can be used to augment or override entries in the default table below.
4020Sstevel@tonic-gate  *
4030Sstevel@tonic-gate  * NB: The values below give NICs preference on redistribution, and provide
4040Sstevel@tonic-gate  * NICs some isolation from other interrupt sources. We need better interfaces
4050Sstevel@tonic-gate  * that allow the NIC driver to identify a specific NIC instance as high
4060Sstevel@tonic-gate  * bandwidth, and thus deserving of separation from other low bandwidth
4070Sstevel@tonic-gate  * NICs additional isolation from other interrupt sources.
4080Sstevel@tonic-gate  *
4090Sstevel@tonic-gate  * NB: We treat Infiniband like a NIC.
4100Sstevel@tonic-gate  */
4110Sstevel@tonic-gate pci_class_val_t pci_default_intr_weight [] = {
4120Sstevel@tonic-gate 	{0x020000, 0xff0000, 35},	/* Network Controller */
4130Sstevel@tonic-gate 	{0x010000, 0xff0000, 10},	/* Mass Storage Controller */
4140Sstevel@tonic-gate 	{0x0c0400, 0xffff00, 10},	/* Serial Bus, Fibre Channel */
4150Sstevel@tonic-gate 	{0x0c0600, 0xffff00, 50}	/* Serial Bus, Infiniband */
4160Sstevel@tonic-gate };
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate static uint32_t
4190Sstevel@tonic-gate pci_match_class_val(uint32_t key, pci_class_val_t *rec_p, int nrec,
4200Sstevel@tonic-gate     uint32_t default_val)
4210Sstevel@tonic-gate {
4220Sstevel@tonic-gate 	int i;
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	for (i = 0; i < nrec; rec_p++, i++) {
4250Sstevel@tonic-gate 		if ((rec_p->class_code & rec_p->class_mask) ==
4260Sstevel@tonic-gate 		    (key & rec_p->class_mask))
4270Sstevel@tonic-gate 			return (rec_p->class_val);
4280Sstevel@tonic-gate 	}
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	return (default_val);
4310Sstevel@tonic-gate }
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate /*
4340Sstevel@tonic-gate  * Return the configuration value, based on class code and sub class code,
4350Sstevel@tonic-gate  * from the specified property based or default pci_class_val_t table.
4360Sstevel@tonic-gate  */
4370Sstevel@tonic-gate uint32_t
4380Sstevel@tonic-gate pci_class_to_val(dev_info_t *rdip, char *property_name, pci_class_val_t *rec_p,
4390Sstevel@tonic-gate     int nrec, uint32_t default_val)
4400Sstevel@tonic-gate {
4410Sstevel@tonic-gate 	int property_len;
4420Sstevel@tonic-gate 	uint32_t class_code;
4430Sstevel@tonic-gate 	pci_class_val_t *conf;
4440Sstevel@tonic-gate 	uint32_t val = default_val;
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	/*
4470Sstevel@tonic-gate 	 * Use the "class-code" property to get the base and sub class
4480Sstevel@tonic-gate 	 * codes for the requesting device.
4490Sstevel@tonic-gate 	 */
4500Sstevel@tonic-gate 	class_code = (uint32_t)ddi_prop_get_int(DDI_DEV_T_ANY, rdip,
4510Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "class-code", -1);
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	if (class_code == -1)
4540Sstevel@tonic-gate 		return (val);
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 	/* look up the val from the default table */
4570Sstevel@tonic-gate 	val = pci_match_class_val(class_code, rec_p, nrec, val);
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	/* see if there is a more specific property specified value */
4610Sstevel@tonic-gate 	if (ddi_getlongprop(DDI_DEV_T_ANY, rdip, DDI_PROP_NOTPROM,
4620Sstevel@tonic-gate 	    property_name, (caddr_t)&conf, &property_len))
4630Sstevel@tonic-gate 			return (val);
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	if ((property_len % sizeof (pci_class_val_t)) == 0)
4660Sstevel@tonic-gate 		val = pci_match_class_val(class_code, conf,
4670Sstevel@tonic-gate 		    property_len / sizeof (pci_class_val_t), val);
4680Sstevel@tonic-gate 	kmem_free(conf, property_len);
4690Sstevel@tonic-gate 	return (val);
4700Sstevel@tonic-gate }
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate /* pci_class_to_pil: return the pil for a given PCI device. */
4730Sstevel@tonic-gate uint32_t
4740Sstevel@tonic-gate pci_class_to_pil(dev_info_t *rdip)
4750Sstevel@tonic-gate {
4760Sstevel@tonic-gate 	uint32_t pil;
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	/* default pil is 0 (uninitialized) */
4790Sstevel@tonic-gate 	pil = pci_class_to_val(rdip,
4800Sstevel@tonic-gate 	    "pci-class-priorities", pci_default_pil,
4810Sstevel@tonic-gate 	    sizeof (pci_default_pil) / sizeof (pci_class_val_t), 0);
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	/* range check the result */
4840Sstevel@tonic-gate 	if (pil >= 0xf)
4850Sstevel@tonic-gate 		pil = 0;
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	return (pil);
4880Sstevel@tonic-gate }
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate /* pci_class_to_intr_weight: return the intr_weight for a given PCI device. */
4910Sstevel@tonic-gate int32_t
4920Sstevel@tonic-gate pci_class_to_intr_weight(dev_info_t *rdip)
4930Sstevel@tonic-gate {
4940Sstevel@tonic-gate 	int32_t intr_weight;
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	/* default weight is 0% */
4970Sstevel@tonic-gate 	intr_weight = pci_class_to_val(rdip,
4980Sstevel@tonic-gate 	    "pci-class-intr-weights", pci_default_intr_weight,
4990Sstevel@tonic-gate 	    sizeof (pci_default_intr_weight) / sizeof (pci_class_val_t), 0);
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	/* range check the result */
5020Sstevel@tonic-gate 	if (intr_weight < 0)
5030Sstevel@tonic-gate 		intr_weight = 0;
5040Sstevel@tonic-gate 	if (intr_weight > 1000)
5050Sstevel@tonic-gate 		intr_weight = 1000;
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	return (intr_weight);
5080Sstevel@tonic-gate }
5090Sstevel@tonic-gate 
51066Sesolom static struct {
51166Sesolom 	kstat_named_t pciintr_ks_name;
51266Sesolom 	kstat_named_t pciintr_ks_type;
51366Sesolom 	kstat_named_t pciintr_ks_cpu;
51466Sesolom 	kstat_named_t pciintr_ks_pil;
51566Sesolom 	kstat_named_t pciintr_ks_time;
51666Sesolom 	kstat_named_t pciintr_ks_ino;
51766Sesolom 	kstat_named_t pciintr_ks_cookie;
51866Sesolom 	kstat_named_t pciintr_ks_devpath;
51966Sesolom 	kstat_named_t pciintr_ks_buspath;
52066Sesolom } pciintr_ks_template = {
52166Sesolom 	{ "name",	KSTAT_DATA_CHAR },
52266Sesolom 	{ "type",	KSTAT_DATA_CHAR },
52366Sesolom 	{ "cpu",	KSTAT_DATA_UINT64 },
52466Sesolom 	{ "pil",	KSTAT_DATA_UINT64 },
52566Sesolom 	{ "time",	KSTAT_DATA_UINT64 },
52666Sesolom 	{ "ino",	KSTAT_DATA_UINT64 },
52766Sesolom 	{ "cookie",	KSTAT_DATA_UINT64 },
52866Sesolom 	{ "devpath",	KSTAT_DATA_STRING },
52966Sesolom 	{ "buspath",	KSTAT_DATA_STRING },
53066Sesolom };
53166Sesolom static uint32_t pciintr_ks_instance;
5321811Sesolom static char ih_devpath[MAXPATHLEN];
5331811Sesolom static char ih_buspath[MAXPATHLEN];
53466Sesolom 
53566Sesolom kmutex_t pciintr_ks_template_lock;
53666Sesolom 
53766Sesolom int
53866Sesolom pci_ks_update(kstat_t *ksp, int rw)
53966Sesolom {
540*2973Sgovinda 	ih_t		*ih_p = ksp->ks_private;
541*2973Sgovinda 	int	maxlen = sizeof (pciintr_ks_template.pciintr_ks_name.value.c);
542*2973Sgovinda 	ib_ino_pil_t	*ipil_p = ih_p->ih_ipil_p;
543*2973Sgovinda 	ib_ino_info_t	*ino_p = ipil_p->ipil_ino_p;
544*2973Sgovinda 	ib_t		*ib_p = ino_p->ino_ib_p;
545*2973Sgovinda 	pci_t		*pci_p = ib_p->ib_pci_p;
546*2973Sgovinda 	ib_ino_t	ino;
54766Sesolom 
548*2973Sgovinda 	ino = ino_p->ino_ino;
54966Sesolom 
55066Sesolom 	(void) snprintf(pciintr_ks_template.pciintr_ks_name.value.c, maxlen,
55166Sesolom 	    "%s%d", ddi_driver_name(ih_p->ih_dip),
55266Sesolom 	    ddi_get_instance(ih_p->ih_dip));
55366Sesolom 
55466Sesolom 	(void) ddi_pathname(ih_p->ih_dip, ih_devpath);
55566Sesolom 	(void) ddi_pathname(pci_p->pci_dip, ih_buspath);
55666Sesolom 	kstat_named_setstr(&pciintr_ks_template.pciintr_ks_devpath, ih_devpath);
55766Sesolom 	kstat_named_setstr(&pciintr_ks_template.pciintr_ks_buspath, ih_buspath);
55866Sesolom 
5591087Sschwartz 	if (ih_p->ih_intr_state == PCI_INTR_STATE_ENABLE) {
5601087Sschwartz 		(void) strcpy(pciintr_ks_template.pciintr_ks_type.value.c,
5611087Sschwartz 		    "fixed");
5621087Sschwartz 		pciintr_ks_template.pciintr_ks_cpu.value.ui64 =
563*2973Sgovinda 		    ino_p->ino_cpuid;
5641087Sschwartz 		pciintr_ks_template.pciintr_ks_pil.value.ui64 =
565*2973Sgovinda 		    ipil_p->ipil_pil;
5661087Sschwartz 		pciintr_ks_template.pciintr_ks_time.value.ui64 = ih_p->ih_nsec +
5671087Sschwartz 		    (uint64_t)tick2ns((hrtime_t)ih_p->ih_ticks,
568*2973Sgovinda 		    ino_p->ino_cpuid);
5691087Sschwartz 		pciintr_ks_template.pciintr_ks_ino.value.ui64 = ino;
5701087Sschwartz 		pciintr_ks_template.pciintr_ks_cookie.value.ui64 =
5711087Sschwartz 			IB_INO_TO_MONDO(ib_p, ino);
5721087Sschwartz 	} else {
5731087Sschwartz 		(void) strcpy(pciintr_ks_template.pciintr_ks_type.value.c,
5741087Sschwartz 		    "disabled");
5751087Sschwartz 		pciintr_ks_template.pciintr_ks_cpu.value.ui64 = 0;
5761087Sschwartz 		pciintr_ks_template.pciintr_ks_pil.value.ui64 = 0;
5771087Sschwartz 		pciintr_ks_template.pciintr_ks_time.value.ui64 = 0;
5781087Sschwartz 		pciintr_ks_template.pciintr_ks_ino.value.ui64 = 0;
5791087Sschwartz 		pciintr_ks_template.pciintr_ks_cookie.value.ui64 = 0;
5801087Sschwartz 	}
5811087Sschwartz 
58266Sesolom 	return (0);
58366Sesolom }
58466Sesolom 
5850Sstevel@tonic-gate int
5860Sstevel@tonic-gate pci_add_intr(dev_info_t *dip, dev_info_t *rdip, ddi_intr_handle_impl_t *hdlp)
5870Sstevel@tonic-gate {
588*2973Sgovinda 	pci_t		*pci_p = get_pci_soft_state(ddi_get_instance(dip));
589*2973Sgovinda 	ib_t		*ib_p = pci_p->pci_ib_p;
590*2973Sgovinda 	cb_t		*cb_p = pci_p->pci_cb_p;
591*2973Sgovinda 	ih_t		*ih_p;
592*2973Sgovinda 	ib_ino_t	ino;
593*2973Sgovinda 	ib_ino_info_t	*ino_p;	/* pulse interrupts have no ino */
594*2973Sgovinda 	ib_ino_pil_t	*ipil_p, *ipil_list;
595*2973Sgovinda 	ib_mondo_t	mondo;
596*2973Sgovinda 	uint32_t	cpu_id;
597*2973Sgovinda 	int		ret;
598*2973Sgovinda 	int32_t		weight;
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	ino = IB_MONDO_TO_INO(hdlp->ih_vector);
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	DEBUG3(DBG_A_INTX, dip, "pci_add_intr: rdip=%s%d ino=%x\n",
6030Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip), ino);
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	if (ino > ib_p->ib_max_ino) {
6060Sstevel@tonic-gate 		DEBUG1(DBG_A_INTX, dip, "ino %x is invalid\n", ino);
6070Sstevel@tonic-gate 		return (DDI_INTR_NOTFOUND);
6080Sstevel@tonic-gate 	}
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	if (hdlp->ih_vector & PCI_PULSE_INO) {
6110Sstevel@tonic-gate 		volatile uint64_t *map_reg_addr;
6120Sstevel@tonic-gate 		map_reg_addr = ib_intr_map_reg_addr(ib_p, ino);
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 		mondo = pci_xlate_intr(dip, rdip, ib_p, ino);
6150Sstevel@tonic-gate 		if (mondo == 0)
6160Sstevel@tonic-gate 			goto fail1;
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 		hdlp->ih_vector = CB_MONDO_TO_XMONDO(cb_p, mondo);
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 		if (i_ddi_add_ivintr(hdlp) != DDI_SUCCESS)
6210Sstevel@tonic-gate 			goto fail1;
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 		/*
6240Sstevel@tonic-gate 		 * Select cpu and program.
6250Sstevel@tonic-gate 		 *
6260Sstevel@tonic-gate 		 * Since there is no good way to always derive cpuid in
6270Sstevel@tonic-gate 		 * pci_remove_intr for PCI_PULSE_INO (esp. for STARFIRE), we
6280Sstevel@tonic-gate 		 * don't add (or remove) device weight for pulsed interrupt
6290Sstevel@tonic-gate 		 * sources.
6300Sstevel@tonic-gate 		 */
6310Sstevel@tonic-gate 		mutex_enter(&ib_p->ib_intr_lock);
6320Sstevel@tonic-gate 		cpu_id = intr_dist_cpuid();
6330Sstevel@tonic-gate 		*map_reg_addr = ib_get_map_reg(mondo, cpu_id);
6340Sstevel@tonic-gate 		mutex_exit(&ib_p->ib_intr_lock);
6350Sstevel@tonic-gate 		*map_reg_addr;	/* flush previous write */
6360Sstevel@tonic-gate 		goto done;
6370Sstevel@tonic-gate 	}
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 	if ((mondo = pci_xlate_intr(dip, rdip, pci_p->pci_ib_p, ino)) == 0)
6400Sstevel@tonic-gate 		goto fail1;
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	ino = IB_MONDO_TO_INO(mondo);
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
6450Sstevel@tonic-gate 	ih_p = ib_alloc_ih(rdip, hdlp->ih_inum,
6460Sstevel@tonic-gate 	    hdlp->ih_cb_func, hdlp->ih_cb_arg1, hdlp->ih_cb_arg2);
6470Sstevel@tonic-gate 	if (map_pcidev_cfg_reg(dip, rdip, &ih_p->ih_config_handle))
6480Sstevel@tonic-gate 		goto fail2;
6490Sstevel@tonic-gate 
650*2973Sgovinda 	ino_p = ib_locate_ino(ib_p, ino);
651*2973Sgovinda 	ipil_list = ino_p ? ino_p->ino_ipil_p:NULL;
652*2973Sgovinda 
653*2973Sgovinda 	/* Sharing ino */
654*2973Sgovinda 	if (ino_p && (ipil_p = ib_ino_locate_ipil(ino_p, hdlp->ih_pri))) {
655*2973Sgovinda 		if (ib_intr_locate_ih(ipil_p, rdip, hdlp->ih_inum)) {
656*2973Sgovinda 			DEBUG1(DBG_A_INTX, dip, "dup intr #%d\n",
657*2973Sgovinda 			    hdlp->ih_inum);
6580Sstevel@tonic-gate 			goto fail3;
6590Sstevel@tonic-gate 		}
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 		/* add weight to the cpu that we are already targeting */
6620Sstevel@tonic-gate 		cpu_id = ino_p->ino_cpuid;
6630Sstevel@tonic-gate 		weight = pci_class_to_intr_weight(rdip);
6640Sstevel@tonic-gate 		intr_dist_cpuid_add_device_weight(cpu_id, rdip, weight);
6650Sstevel@tonic-gate 
666*2973Sgovinda 		ib_ino_add_intr(pci_p, ipil_p, ih_p);
6670Sstevel@tonic-gate 		goto ino_done;
6680Sstevel@tonic-gate 	}
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	if (hdlp->ih_pri == 0)
6710Sstevel@tonic-gate 		hdlp->ih_pri = pci_class_to_pil(rdip);
6720Sstevel@tonic-gate 
673*2973Sgovinda 	ipil_p = ib_new_ino_pil(ib_p, ino, hdlp->ih_pri, ih_p);
674*2973Sgovinda 	ino_p = ipil_p->ipil_ino_p;
675*2973Sgovinda 
6760Sstevel@tonic-gate 	hdlp->ih_vector = CB_MONDO_TO_XMONDO(cb_p, mondo);
6770Sstevel@tonic-gate 
678909Segillett 	/* Store this global mondo */
679909Segillett 	ino_p->ino_mondo = hdlp->ih_vector;
680909Segillett 
6810Sstevel@tonic-gate 	DEBUG2(DBG_A_INTX, dip, "pci_add_intr:  pil=0x%x mondo=0x%x\n",
6820Sstevel@tonic-gate 	    hdlp->ih_pri, hdlp->ih_vector);
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp,
685*2973Sgovinda 	    (ddi_intr_handler_t *)pci_intr_wrapper, (caddr_t)ipil_p, NULL);
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 	ret = i_ddi_add_ivintr(hdlp);
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	/*
6900Sstevel@tonic-gate 	 * Restore original interrupt handler
6910Sstevel@tonic-gate 	 * and arguments in interrupt handle.
6920Sstevel@tonic-gate 	 */
6930Sstevel@tonic-gate 	DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp, ih_p->ih_handler,
6940Sstevel@tonic-gate 	    ih_p->ih_handler_arg1, ih_p->ih_handler_arg2);
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
6970Sstevel@tonic-gate 		goto fail4;
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	/* Save the pil for this ino */
700*2973Sgovinda 	ipil_p->ipil_pil = hdlp->ih_pri;
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 	/* clear and enable interrupt */
7030Sstevel@tonic-gate 	IB_INO_INTR_CLEAR(ino_p->ino_clr_reg);
7040Sstevel@tonic-gate 
705*2973Sgovinda 	/*
706*2973Sgovinda 	 * Select cpu and compute weight, saving both for sharing and removal.
707*2973Sgovinda 	 */
708*2973Sgovinda 	if (ipil_list == NULL)
709*2973Sgovinda 		ino_p->ino_cpuid = pci_intr_dist_cpuid(ib_p, ino_p);
710*2973Sgovinda 
711*2973Sgovinda 	cpu_id = ino_p->ino_cpuid;
7120Sstevel@tonic-gate 	ino_p->ino_established = 1;
7130Sstevel@tonic-gate 	weight = pci_class_to_intr_weight(rdip);
7140Sstevel@tonic-gate 	intr_dist_cpuid_add_device_weight(cpu_id, rdip, weight);
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate #ifdef _STARFIRE
7170Sstevel@tonic-gate 	cpu_id = pc_translate_tgtid(cb_p->cb_ittrans_cookie, cpu_id,
7180Sstevel@tonic-gate 		IB_GET_MAPREG_INO(ino));
7190Sstevel@tonic-gate #endif /* _STARFIRE */
720*2973Sgovinda 	if (!ipil_list) {
721*2973Sgovinda 		*ino_p->ino_map_reg = ib_get_map_reg(mondo, cpu_id);
722*2973Sgovinda 		*ino_p->ino_map_reg;
723*2973Sgovinda 	}
7240Sstevel@tonic-gate ino_done:
725*2973Sgovinda 	ih_p->ih_ipil_p = ipil_p;
72666Sesolom 	ih_p->ih_ksp = kstat_create("pci_intrs",
72766Sesolom 	    atomic_inc_32_nv(&pciintr_ks_instance), "config", "interrupts",
72866Sesolom 	    KSTAT_TYPE_NAMED,
72966Sesolom 	    sizeof (pciintr_ks_template) / sizeof (kstat_named_t),
73066Sesolom 	    KSTAT_FLAG_VIRTUAL);
73166Sesolom 	if (ih_p->ih_ksp != NULL) {
73266Sesolom 		ih_p->ih_ksp->ks_data_size += MAXPATHLEN * 2;
73366Sesolom 		ih_p->ih_ksp->ks_lock = &pciintr_ks_template_lock;
73466Sesolom 		ih_p->ih_ksp->ks_data = &pciintr_ks_template;
73566Sesolom 		ih_p->ih_ksp->ks_private = ih_p;
73666Sesolom 		ih_p->ih_ksp->ks_update = pci_ks_update;
7370Sstevel@tonic-gate 		kstat_install(ih_p->ih_ksp);
73866Sesolom 	}
7390Sstevel@tonic-gate 	ib_ino_map_reg_share(ib_p, ino, ino_p);
7400Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
7410Sstevel@tonic-gate done:
7420Sstevel@tonic-gate 	DEBUG2(DBG_A_INTX, dip, "done! Interrupt 0x%x pil=%x\n",
7430Sstevel@tonic-gate 		hdlp->ih_vector, hdlp->ih_pri);
7440Sstevel@tonic-gate 	return (DDI_SUCCESS);
7450Sstevel@tonic-gate fail4:
746*2973Sgovinda 	ib_delete_ino_pil(ib_p, ipil_p);
7470Sstevel@tonic-gate fail3:
7480Sstevel@tonic-gate 	if (ih_p->ih_config_handle)
7490Sstevel@tonic-gate 		pci_config_teardown(&ih_p->ih_config_handle);
7500Sstevel@tonic-gate fail2:
7510Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
7520Sstevel@tonic-gate 	kmem_free(ih_p, sizeof (ih_t));
7530Sstevel@tonic-gate fail1:
7540Sstevel@tonic-gate 	DEBUG2(DBG_A_INTX, dip, "Failed! Interrupt 0x%x pil=%x\n",
7550Sstevel@tonic-gate 		hdlp->ih_vector, hdlp->ih_pri);
7560Sstevel@tonic-gate 	return (DDI_FAILURE);
7570Sstevel@tonic-gate }
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate int
7600Sstevel@tonic-gate pci_remove_intr(dev_info_t *dip, dev_info_t *rdip, ddi_intr_handle_impl_t *hdlp)
7610Sstevel@tonic-gate {
762*2973Sgovinda 	pci_t		*pci_p = get_pci_soft_state(ddi_get_instance(dip));
763*2973Sgovinda 	ib_t		*ib_p = pci_p->pci_ib_p;
764*2973Sgovinda 	cb_t		*cb_p = pci_p->pci_cb_p;
765*2973Sgovinda 	ib_ino_t	ino;
766*2973Sgovinda 	ib_mondo_t	mondo;
767*2973Sgovinda 	ib_ino_info_t	*ino_p;	/* non-pulse only */
768*2973Sgovinda 	ib_ino_pil_t	*ipil_p; /* non-pulse only */
769*2973Sgovinda 	ih_t		*ih_p;	/* non-pulse only */
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	ino = IB_MONDO_TO_INO(hdlp->ih_vector);
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	DEBUG3(DBG_R_INTX, dip, "pci_rem_intr: rdip=%s%d ino=%x\n",
7740Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip), ino);
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 	if (hdlp->ih_vector & PCI_PULSE_INO) { /* pulse interrupt */
7770Sstevel@tonic-gate 		volatile uint64_t *map_reg_addr;
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 		/*
7800Sstevel@tonic-gate 		 * No weight was added by pci_add_intr for PCI_PULSE_INO
7810Sstevel@tonic-gate 		 * because it is difficult to determine cpuid here.
7820Sstevel@tonic-gate 		 */
7830Sstevel@tonic-gate 		map_reg_addr = ib_intr_map_reg_addr(ib_p, ino);
7840Sstevel@tonic-gate 		IB_INO_INTR_RESET(map_reg_addr);	/* disable intr */
7850Sstevel@tonic-gate 		*map_reg_addr;
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 		mondo = pci_xlate_intr(dip, rdip, ib_p, ino);
7880Sstevel@tonic-gate 		if (mondo == 0) {
7890Sstevel@tonic-gate 			DEBUG1(DBG_R_INTX, dip,
7900Sstevel@tonic-gate 				"can't get mondo for ino %x\n", ino);
7910Sstevel@tonic-gate 			return (DDI_FAILURE);
7920Sstevel@tonic-gate 		}
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 		if (hdlp->ih_pri == 0)
7950Sstevel@tonic-gate 			hdlp->ih_pri = pci_class_to_pil(rdip);
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 		hdlp->ih_vector = CB_MONDO_TO_XMONDO(cb_p, mondo);
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 		DEBUG2(DBG_R_INTX, dip, "pci_rem_intr: pil=0x%x mondo=0x%x\n",
8000Sstevel@tonic-gate 		    hdlp->ih_pri, hdlp->ih_vector);
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 		i_ddi_rem_ivintr(hdlp);
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 		DEBUG2(DBG_R_INTX, dip, "pulse success mondo=%x reg=%p\n",
8050Sstevel@tonic-gate 			mondo, map_reg_addr);
8060Sstevel@tonic-gate 		return (DDI_SUCCESS);
8070Sstevel@tonic-gate 	}
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	/* Translate the interrupt property */
8100Sstevel@tonic-gate 	mondo = pci_xlate_intr(dip, rdip, pci_p->pci_ib_p, ino);
8110Sstevel@tonic-gate 	if (mondo == 0) {
8120Sstevel@tonic-gate 		DEBUG1(DBG_R_INTX, dip, "can't get mondo for ino %x\n", ino);
8130Sstevel@tonic-gate 		return (DDI_FAILURE);
8140Sstevel@tonic-gate 	}
8150Sstevel@tonic-gate 	ino = IB_MONDO_TO_INO(mondo);
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
8180Sstevel@tonic-gate 	ino_p = ib_locate_ino(ib_p, ino);
8190Sstevel@tonic-gate 	if (!ino_p) {
8200Sstevel@tonic-gate 		int r = cb_remove_xintr(pci_p, dip, rdip, ino, mondo);
8210Sstevel@tonic-gate 		if (r != DDI_SUCCESS)
8220Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d-xintr: ino %x is invalid",
8230Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip), ino);
8240Sstevel@tonic-gate 		mutex_exit(&ib_p->ib_ino_lst_mutex);
8250Sstevel@tonic-gate 		return (r);
8260Sstevel@tonic-gate 	}
8270Sstevel@tonic-gate 
828*2973Sgovinda 	ipil_p = ib_ino_locate_ipil(ino_p, hdlp->ih_pri);
829*2973Sgovinda 	ih_p = ib_intr_locate_ih(ipil_p, rdip, hdlp->ih_inum);
830*2973Sgovinda 	ib_ino_rem_intr(pci_p, ipil_p, ih_p);
8310Sstevel@tonic-gate 	intr_dist_cpuid_rem_device_weight(ino_p->ino_cpuid, rdip);
832*2973Sgovinda 	if (ipil_p->ipil_ih_size == 0) {
8330Sstevel@tonic-gate 		IB_INO_INTR_PEND(ib_clear_intr_reg_addr(ib_p, ino));
8340Sstevel@tonic-gate 		hdlp->ih_vector = CB_MONDO_TO_XMONDO(cb_p, mondo);
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 		i_ddi_rem_ivintr(hdlp);
837*2973Sgovinda 		ib_delete_ino_pil(ib_p, ipil_p);
8380Sstevel@tonic-gate 	}
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate 	/* re-enable interrupt only if mapping register still shared */
841*2973Sgovinda 	if (ib_ino_map_reg_unshare(ib_p, ino, ino_p) || ino_p->ino_ipil_size) {
8420Sstevel@tonic-gate 		IB_INO_INTR_ON(ino_p->ino_map_reg);
8430Sstevel@tonic-gate 		*ino_p->ino_map_reg;
8440Sstevel@tonic-gate 	}
8450Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
8460Sstevel@tonic-gate 
847*2973Sgovinda 	if (ino_p->ino_ipil_size == 0)
8480Sstevel@tonic-gate 		kmem_free(ino_p, sizeof (ib_ino_info_t));
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 	DEBUG1(DBG_R_INTX, dip, "success! mondo=%x\n", mondo);
8510Sstevel@tonic-gate 	return (DDI_SUCCESS);
8520Sstevel@tonic-gate }
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate /*
8550Sstevel@tonic-gate  * free the pci_inos array allocated during pci_intr_setup. the actual
8560Sstevel@tonic-gate  * interrupts are torn down by their respective block destroy routines:
8570Sstevel@tonic-gate  * cb_destroy, pbm_destroy, and ib_destroy.
8580Sstevel@tonic-gate  */
8590Sstevel@tonic-gate void
8600Sstevel@tonic-gate pci_intr_teardown(pci_t *pci_p)
8610Sstevel@tonic-gate {
8620Sstevel@tonic-gate 	kmem_free(pci_p->pci_inos, pci_p->pci_inos_len);
8630Sstevel@tonic-gate 	pci_p->pci_inos = NULL;
8640Sstevel@tonic-gate 	pci_p->pci_inos_len = 0;
8650Sstevel@tonic-gate }
866