xref: /onnv-gate/usr/src/uts/sun4u/io/pci/pci_intr.c (revision 1811:b06ef01e6da1)
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
5*1811Sesolom  * Common Development and Distribution License (the "License").
6*1811Sesolom  * 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 /*
22*1811Sesolom  * 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;
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	if (ino_p->ino_unclaimed > pci_unclaimed_intr_max)
2300Sstevel@tonic-gate 		return (DDI_INTR_CLAIMED);
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	if (!ino_p->ino_unclaimed)
2330Sstevel@tonic-gate 		ino_p->ino_spurintr_begin = ddi_get_lbolt();
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	ino_p->ino_unclaimed++;
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	if (ino_p->ino_unclaimed <= pci_unclaimed_intr_max)
2380Sstevel@tonic-gate 		goto clear;
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	if (drv_hztousec(ddi_get_lbolt() - ino_p->ino_spurintr_begin)
2410Sstevel@tonic-gate 	    > pci_spurintr_duration) {
2420Sstevel@tonic-gate 		ino_p->ino_unclaimed = 0;
2430Sstevel@tonic-gate 		goto clear;
2440Sstevel@tonic-gate 	}
2450Sstevel@tonic-gate 	err_fmt_str = "%s%d: ino 0x%x blocked";
2460Sstevel@tonic-gate 	goto warn;
2470Sstevel@tonic-gate clear:
2480Sstevel@tonic-gate 	IB_INO_INTR_CLEAR(ino_p->ino_clr_reg);  /* clear the pending state */
2490Sstevel@tonic-gate 	if (!pci_spurintr_msgs) /* tomatillo errata #71 spurious mondo */
2500Sstevel@tonic-gate 		return (DDI_INTR_CLAIMED);
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	err_fmt_str = "!%s%d: spurious interrupt from ino 0x%x";
2530Sstevel@tonic-gate warn:
2540Sstevel@tonic-gate 	cmn_err(CE_WARN, err_fmt_str, NAMEINST(pci_p->pci_dip), ino_p->ino_ino);
2550Sstevel@tonic-gate 	for (i = 0; i < ino_p->ino_ih_size; i++, ih_p = ih_p->ih_next)
2560Sstevel@tonic-gate 		cmn_err(CE_CONT, "!%s-%d#%x ", NAMEINST(ih_p->ih_dip),
2570Sstevel@tonic-gate 		    ih_p->ih_inum);
2580Sstevel@tonic-gate 	cmn_err(CE_CONT, "!\n");
2590Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate /*
2630Sstevel@tonic-gate  * pci_intr_wrapper
2640Sstevel@tonic-gate  *
2650Sstevel@tonic-gate  * This routine is used as wrapper around interrupt handlers installed by child
2660Sstevel@tonic-gate  * device drivers.  This routine invokes the driver interrupt handlers and
2670Sstevel@tonic-gate  * examines the return codes.
2680Sstevel@tonic-gate  * There is a count of unclaimed interrupts kept on a per-ino basis. If at
2690Sstevel@tonic-gate  * least one handler claims the interrupt then the counter is halved and the
2700Sstevel@tonic-gate  * interrupt state machine is idled. If no handler claims the interrupt then
2710Sstevel@tonic-gate  * the counter is incremented by one and the state machine is idled.
2720Sstevel@tonic-gate  * If the count ever reaches the limit value set by pci_unclaimed_intr_max
2730Sstevel@tonic-gate  * then the interrupt state machine is not idled thus preventing any further
2740Sstevel@tonic-gate  * interrupts on that ino. The state machine will only be idled again if a
2750Sstevel@tonic-gate  * handler is subsequently added or removed.
2760Sstevel@tonic-gate  *
2770Sstevel@tonic-gate  * return value: DDI_INTR_CLAIMED if any handlers claimed the interrupt,
2780Sstevel@tonic-gate  * DDI_INTR_UNCLAIMED otherwise.
2790Sstevel@tonic-gate  */
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate extern uint64_t intr_get_time(void);
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate uint_t
2840Sstevel@tonic-gate pci_intr_wrapper(caddr_t arg)
2850Sstevel@tonic-gate {
2860Sstevel@tonic-gate 	ib_ino_info_t *ino_p = (ib_ino_info_t *)arg;
2870Sstevel@tonic-gate 	uint_t result = 0, r;
2880Sstevel@tonic-gate 	pci_t *pci_p = ino_p->ino_ib_p->ib_pci_p;
2890Sstevel@tonic-gate 	pbm_t *pbm_p = pci_p->pci_pbm_p;
2900Sstevel@tonic-gate 	ih_t *ih_p = ino_p->ino_ih_start;
2910Sstevel@tonic-gate 	int i;
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	for (i = 0; i < ino_p->ino_ih_size; i++, ih_p = ih_p->ih_next) {
2940Sstevel@tonic-gate 		dev_info_t *dip = ih_p->ih_dip;
2950Sstevel@tonic-gate 		uint_t (*handler)() = ih_p->ih_handler;
2960Sstevel@tonic-gate 		caddr_t arg1 = ih_p->ih_handler_arg1;
2970Sstevel@tonic-gate 		caddr_t arg2 = ih_p->ih_handler_arg2;
2980Sstevel@tonic-gate 		ddi_acc_handle_t cfg_hdl = ih_p->ih_config_handle;
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 		if (pci_intr_dma_sync && cfg_hdl && pbm_p->pbm_sync_reg_pa) {
3010Sstevel@tonic-gate 			(void) pci_config_get16(cfg_hdl, PCI_CONF_VENID);
3020Sstevel@tonic-gate 			pci_pbm_dma_sync(pbm_p, ino_p->ino_ino);
3030Sstevel@tonic-gate 		}
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 		if (ih_p->ih_intr_state == PCI_INTR_STATE_DISABLE) {
3060Sstevel@tonic-gate 			DEBUG3(DBG_INTR, pci_p->pci_dip,
3070Sstevel@tonic-gate 			    "pci_intr_wrapper: %s%d interrupt %d is disabled\n",
3080Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip),
3090Sstevel@tonic-gate 			    ino_p->ino_ino);
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 			continue;
3120Sstevel@tonic-gate 		}
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 		DTRACE_PROBE4(interrupt__start, dev_info_t, dip,
3150Sstevel@tonic-gate 		    void *, handler, caddr_t, arg1, caddr_t, arg2);
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 		r = (*handler)(arg1, arg2);
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 		/*
3200Sstevel@tonic-gate 		 * Account for time used by this interrupt. Protect against
3210Sstevel@tonic-gate 		 * conflicting writes to ih_ticks from ib_intr_dist_all() by
3220Sstevel@tonic-gate 		 * using atomic ops.
3230Sstevel@tonic-gate 		 */
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 		if (ino_p->ino_pil <= LOCK_LEVEL)
3260Sstevel@tonic-gate 			atomic_add_64(&ih_p->ih_ticks, intr_get_time());
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 		DTRACE_PROBE4(interrupt__complete, dev_info_t, dip,
3290Sstevel@tonic-gate 		    void *, handler, caddr_t, arg1, int, r);
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 		result += r;
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 		if (pci_check_all_handlers)
3340Sstevel@tonic-gate 			continue;
3350Sstevel@tonic-gate 		if (result)
3360Sstevel@tonic-gate 			break;
3370Sstevel@tonic-gate 	}
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	if (!result)
3400Sstevel@tonic-gate 		return (pci_spurintr(ino_p));
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	ino_p->ino_unclaimed = 0;
3430Sstevel@tonic-gate 	IB_INO_INTR_CLEAR(ino_p->ino_clr_reg);  /* clear the pending state */
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
3460Sstevel@tonic-gate }
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate dev_info_t *
3490Sstevel@tonic-gate get_my_childs_dip(dev_info_t *dip, dev_info_t *rdip)
3500Sstevel@tonic-gate {
3510Sstevel@tonic-gate 	dev_info_t *cdip = rdip;
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	for (; ddi_get_parent(cdip) != dip; cdip = ddi_get_parent(cdip))
3540Sstevel@tonic-gate 		;
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	return (cdip);
3570Sstevel@tonic-gate }
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate /* default class to pil value mapping */
3600Sstevel@tonic-gate pci_class_val_t pci_default_pil [] = {
3610Sstevel@tonic-gate 	{0x000000, 0xff0000, 0x1},	/* Class code for pre-2.0 devices */
3620Sstevel@tonic-gate 	{0x010000, 0xff0000, 0x4},	/* Mass Storage Controller */
3630Sstevel@tonic-gate 	{0x020000, 0xff0000, 0x6},	/* Network Controller */
3640Sstevel@tonic-gate 	{0x030000, 0xff0000, 0x9},	/* Display Controller */
3650Sstevel@tonic-gate 	{0x040000, 0xff0000, 0x9},	/* Multimedia Controller */
3660Sstevel@tonic-gate 	{0x050000, 0xff0000, 0xb},	/* Memory Controller */
3670Sstevel@tonic-gate 	{0x060000, 0xff0000, 0xb},	/* Bridge Controller */
3680Sstevel@tonic-gate 	{0x0c0000, 0xffff00, 0x9},	/* Serial Bus, FireWire (IEEE 1394) */
3690Sstevel@tonic-gate 	{0x0c0100, 0xffff00, 0x4},	/* Serial Bus, ACCESS.bus */
3700Sstevel@tonic-gate 	{0x0c0200, 0xffff00, 0x4},	/* Serial Bus, SSA */
3710Sstevel@tonic-gate 	{0x0c0300, 0xffff00, 0x9},	/* Serial Bus Universal Serial Bus */
3720Sstevel@tonic-gate 	{0x0c0400, 0xffff00, 0x6},	/* Serial Bus, Fibre Channel */
3730Sstevel@tonic-gate 	{0x0c0600, 0xffff00, 0x6}	/* Serial Bus, Infiniband */
3740Sstevel@tonic-gate };
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate /*
3770Sstevel@tonic-gate  * Default class to intr_weight value mapping (% of CPU).  A driver.conf
3780Sstevel@tonic-gate  * entry on or above the pci node like
3790Sstevel@tonic-gate  *
3800Sstevel@tonic-gate  *	pci-class-intr-weights= 0x020000, 0xff0000, 30;
3810Sstevel@tonic-gate  *
3820Sstevel@tonic-gate  * can be used to augment or override entries in the default table below.
3830Sstevel@tonic-gate  *
3840Sstevel@tonic-gate  * NB: The values below give NICs preference on redistribution, and provide
3850Sstevel@tonic-gate  * NICs some isolation from other interrupt sources. We need better interfaces
3860Sstevel@tonic-gate  * that allow the NIC driver to identify a specific NIC instance as high
3870Sstevel@tonic-gate  * bandwidth, and thus deserving of separation from other low bandwidth
3880Sstevel@tonic-gate  * NICs additional isolation from other interrupt sources.
3890Sstevel@tonic-gate  *
3900Sstevel@tonic-gate  * NB: We treat Infiniband like a NIC.
3910Sstevel@tonic-gate  */
3920Sstevel@tonic-gate pci_class_val_t pci_default_intr_weight [] = {
3930Sstevel@tonic-gate 	{0x020000, 0xff0000, 35},	/* Network Controller */
3940Sstevel@tonic-gate 	{0x010000, 0xff0000, 10},	/* Mass Storage Controller */
3950Sstevel@tonic-gate 	{0x0c0400, 0xffff00, 10},	/* Serial Bus, Fibre Channel */
3960Sstevel@tonic-gate 	{0x0c0600, 0xffff00, 50}	/* Serial Bus, Infiniband */
3970Sstevel@tonic-gate };
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate static uint32_t
4000Sstevel@tonic-gate pci_match_class_val(uint32_t key, pci_class_val_t *rec_p, int nrec,
4010Sstevel@tonic-gate     uint32_t default_val)
4020Sstevel@tonic-gate {
4030Sstevel@tonic-gate 	int i;
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	for (i = 0; i < nrec; rec_p++, i++) {
4060Sstevel@tonic-gate 		if ((rec_p->class_code & rec_p->class_mask) ==
4070Sstevel@tonic-gate 		    (key & rec_p->class_mask))
4080Sstevel@tonic-gate 			return (rec_p->class_val);
4090Sstevel@tonic-gate 	}
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	return (default_val);
4120Sstevel@tonic-gate }
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate /*
4150Sstevel@tonic-gate  * Return the configuration value, based on class code and sub class code,
4160Sstevel@tonic-gate  * from the specified property based or default pci_class_val_t table.
4170Sstevel@tonic-gate  */
4180Sstevel@tonic-gate uint32_t
4190Sstevel@tonic-gate pci_class_to_val(dev_info_t *rdip, char *property_name, pci_class_val_t *rec_p,
4200Sstevel@tonic-gate     int nrec, uint32_t default_val)
4210Sstevel@tonic-gate {
4220Sstevel@tonic-gate 	int property_len;
4230Sstevel@tonic-gate 	uint32_t class_code;
4240Sstevel@tonic-gate 	pci_class_val_t *conf;
4250Sstevel@tonic-gate 	uint32_t val = default_val;
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	/*
4280Sstevel@tonic-gate 	 * Use the "class-code" property to get the base and sub class
4290Sstevel@tonic-gate 	 * codes for the requesting device.
4300Sstevel@tonic-gate 	 */
4310Sstevel@tonic-gate 	class_code = (uint32_t)ddi_prop_get_int(DDI_DEV_T_ANY, rdip,
4320Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "class-code", -1);
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	if (class_code == -1)
4350Sstevel@tonic-gate 		return (val);
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	/* look up the val from the default table */
4380Sstevel@tonic-gate 	val = pci_match_class_val(class_code, rec_p, nrec, val);
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	/* see if there is a more specific property specified value */
4420Sstevel@tonic-gate 	if (ddi_getlongprop(DDI_DEV_T_ANY, rdip, DDI_PROP_NOTPROM,
4430Sstevel@tonic-gate 	    property_name, (caddr_t)&conf, &property_len))
4440Sstevel@tonic-gate 			return (val);
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	if ((property_len % sizeof (pci_class_val_t)) == 0)
4470Sstevel@tonic-gate 		val = pci_match_class_val(class_code, conf,
4480Sstevel@tonic-gate 		    property_len / sizeof (pci_class_val_t), val);
4490Sstevel@tonic-gate 	kmem_free(conf, property_len);
4500Sstevel@tonic-gate 	return (val);
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate /* pci_class_to_pil: return the pil for a given PCI device. */
4540Sstevel@tonic-gate uint32_t
4550Sstevel@tonic-gate pci_class_to_pil(dev_info_t *rdip)
4560Sstevel@tonic-gate {
4570Sstevel@tonic-gate 	uint32_t pil;
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	/* default pil is 0 (uninitialized) */
4600Sstevel@tonic-gate 	pil = pci_class_to_val(rdip,
4610Sstevel@tonic-gate 	    "pci-class-priorities", pci_default_pil,
4620Sstevel@tonic-gate 	    sizeof (pci_default_pil) / sizeof (pci_class_val_t), 0);
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	/* range check the result */
4650Sstevel@tonic-gate 	if (pil >= 0xf)
4660Sstevel@tonic-gate 		pil = 0;
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	return (pil);
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate /* pci_class_to_intr_weight: return the intr_weight for a given PCI device. */
4720Sstevel@tonic-gate int32_t
4730Sstevel@tonic-gate pci_class_to_intr_weight(dev_info_t *rdip)
4740Sstevel@tonic-gate {
4750Sstevel@tonic-gate 	int32_t intr_weight;
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 	/* default weight is 0% */
4780Sstevel@tonic-gate 	intr_weight = pci_class_to_val(rdip,
4790Sstevel@tonic-gate 	    "pci-class-intr-weights", pci_default_intr_weight,
4800Sstevel@tonic-gate 	    sizeof (pci_default_intr_weight) / sizeof (pci_class_val_t), 0);
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	/* range check the result */
4830Sstevel@tonic-gate 	if (intr_weight < 0)
4840Sstevel@tonic-gate 		intr_weight = 0;
4850Sstevel@tonic-gate 	if (intr_weight > 1000)
4860Sstevel@tonic-gate 		intr_weight = 1000;
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	return (intr_weight);
4890Sstevel@tonic-gate }
4900Sstevel@tonic-gate 
49166Sesolom static struct {
49266Sesolom 	kstat_named_t pciintr_ks_name;
49366Sesolom 	kstat_named_t pciintr_ks_type;
49466Sesolom 	kstat_named_t pciintr_ks_cpu;
49566Sesolom 	kstat_named_t pciintr_ks_pil;
49666Sesolom 	kstat_named_t pciintr_ks_time;
49766Sesolom 	kstat_named_t pciintr_ks_ino;
49866Sesolom 	kstat_named_t pciintr_ks_cookie;
49966Sesolom 	kstat_named_t pciintr_ks_devpath;
50066Sesolom 	kstat_named_t pciintr_ks_buspath;
50166Sesolom } pciintr_ks_template = {
50266Sesolom 	{ "name",	KSTAT_DATA_CHAR },
50366Sesolom 	{ "type",	KSTAT_DATA_CHAR },
50466Sesolom 	{ "cpu",	KSTAT_DATA_UINT64 },
50566Sesolom 	{ "pil",	KSTAT_DATA_UINT64 },
50666Sesolom 	{ "time",	KSTAT_DATA_UINT64 },
50766Sesolom 	{ "ino",	KSTAT_DATA_UINT64 },
50866Sesolom 	{ "cookie",	KSTAT_DATA_UINT64 },
50966Sesolom 	{ "devpath",	KSTAT_DATA_STRING },
51066Sesolom 	{ "buspath",	KSTAT_DATA_STRING },
51166Sesolom };
51266Sesolom static uint32_t pciintr_ks_instance;
513*1811Sesolom static char ih_devpath[MAXPATHLEN];
514*1811Sesolom static char ih_buspath[MAXPATHLEN];
51566Sesolom 
51666Sesolom kmutex_t pciintr_ks_template_lock;
51766Sesolom 
51866Sesolom int
51966Sesolom pci_ks_update(kstat_t *ksp, int rw)
52066Sesolom {
52166Sesolom 	ih_t *ih_p = ksp->ks_private;
52266Sesolom 	int maxlen = sizeof (pciintr_ks_template.pciintr_ks_name.value.c);
52366Sesolom 	ib_t *ib_p = ih_p->ih_ino_p->ino_ib_p;
52466Sesolom 	pci_t *pci_p = ib_p->ib_pci_p;
52566Sesolom 	ib_ino_t ino;
52666Sesolom 
52766Sesolom 	ino = ih_p->ih_ino_p->ino_ino;
52866Sesolom 
52966Sesolom 	(void) snprintf(pciintr_ks_template.pciintr_ks_name.value.c, maxlen,
53066Sesolom 	    "%s%d", ddi_driver_name(ih_p->ih_dip),
53166Sesolom 	    ddi_get_instance(ih_p->ih_dip));
53266Sesolom 
53366Sesolom 	(void) ddi_pathname(ih_p->ih_dip, ih_devpath);
53466Sesolom 	(void) ddi_pathname(pci_p->pci_dip, ih_buspath);
53566Sesolom 	kstat_named_setstr(&pciintr_ks_template.pciintr_ks_devpath, ih_devpath);
53666Sesolom 	kstat_named_setstr(&pciintr_ks_template.pciintr_ks_buspath, ih_buspath);
53766Sesolom 
5381087Sschwartz 	if (ih_p->ih_intr_state == PCI_INTR_STATE_ENABLE) {
5391087Sschwartz 		(void) strcpy(pciintr_ks_template.pciintr_ks_type.value.c,
5401087Sschwartz 		    "fixed");
5411087Sschwartz 		pciintr_ks_template.pciintr_ks_cpu.value.ui64 =
5421087Sschwartz 		    ih_p->ih_ino_p->ino_cpuid;
5431087Sschwartz 		pciintr_ks_template.pciintr_ks_pil.value.ui64 =
5441087Sschwartz 		    ih_p->ih_ino_p->ino_pil;
5451087Sschwartz 		pciintr_ks_template.pciintr_ks_time.value.ui64 = ih_p->ih_nsec +
5461087Sschwartz 		    (uint64_t)tick2ns((hrtime_t)ih_p->ih_ticks,
5471087Sschwartz 			ih_p->ih_ino_p->ino_cpuid);
5481087Sschwartz 		pciintr_ks_template.pciintr_ks_ino.value.ui64 = ino;
5491087Sschwartz 		pciintr_ks_template.pciintr_ks_cookie.value.ui64 =
5501087Sschwartz 			IB_INO_TO_MONDO(ib_p, ino);
5511087Sschwartz 	} else {
5521087Sschwartz 		(void) strcpy(pciintr_ks_template.pciintr_ks_type.value.c,
5531087Sschwartz 		    "disabled");
5541087Sschwartz 		pciintr_ks_template.pciintr_ks_cpu.value.ui64 = 0;
5551087Sschwartz 		pciintr_ks_template.pciintr_ks_pil.value.ui64 = 0;
5561087Sschwartz 		pciintr_ks_template.pciintr_ks_time.value.ui64 = 0;
5571087Sschwartz 		pciintr_ks_template.pciintr_ks_ino.value.ui64 = 0;
5581087Sschwartz 		pciintr_ks_template.pciintr_ks_cookie.value.ui64 = 0;
5591087Sschwartz 	}
5601087Sschwartz 
56166Sesolom 	return (0);
56266Sesolom }
56366Sesolom 
5640Sstevel@tonic-gate int
5650Sstevel@tonic-gate pci_add_intr(dev_info_t *dip, dev_info_t *rdip, ddi_intr_handle_impl_t *hdlp)
5660Sstevel@tonic-gate {
5670Sstevel@tonic-gate 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
5680Sstevel@tonic-gate 	ib_t *ib_p = pci_p->pci_ib_p;
5690Sstevel@tonic-gate 	cb_t *cb_p = pci_p->pci_cb_p;
5700Sstevel@tonic-gate 	ih_t *ih_p;
5710Sstevel@tonic-gate 	ib_ino_t ino;
5720Sstevel@tonic-gate 	ib_ino_info_t *ino_p;		/* pulse interrupts have no ino */
5730Sstevel@tonic-gate 	ib_mondo_t mondo;
5740Sstevel@tonic-gate 	uint32_t cpu_id;
5750Sstevel@tonic-gate 	int ret;
5760Sstevel@tonic-gate 	int32_t weight;
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 	ino = IB_MONDO_TO_INO(hdlp->ih_vector);
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 	DEBUG3(DBG_A_INTX, dip, "pci_add_intr: rdip=%s%d ino=%x\n",
5810Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip), ino);
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	if (ino > ib_p->ib_max_ino) {
5840Sstevel@tonic-gate 		DEBUG1(DBG_A_INTX, dip, "ino %x is invalid\n", ino);
5850Sstevel@tonic-gate 		return (DDI_INTR_NOTFOUND);
5860Sstevel@tonic-gate 	}
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	if (hdlp->ih_vector & PCI_PULSE_INO) {
5890Sstevel@tonic-gate 		volatile uint64_t *map_reg_addr;
5900Sstevel@tonic-gate 		map_reg_addr = ib_intr_map_reg_addr(ib_p, ino);
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 		mondo = pci_xlate_intr(dip, rdip, ib_p, ino);
5930Sstevel@tonic-gate 		if (mondo == 0)
5940Sstevel@tonic-gate 			goto fail1;
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 		hdlp->ih_vector = CB_MONDO_TO_XMONDO(cb_p, mondo);
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 		if (i_ddi_add_ivintr(hdlp) != DDI_SUCCESS)
5990Sstevel@tonic-gate 			goto fail1;
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 		/*
6020Sstevel@tonic-gate 		 * Select cpu and program.
6030Sstevel@tonic-gate 		 *
6040Sstevel@tonic-gate 		 * Since there is no good way to always derive cpuid in
6050Sstevel@tonic-gate 		 * pci_remove_intr for PCI_PULSE_INO (esp. for STARFIRE), we
6060Sstevel@tonic-gate 		 * don't add (or remove) device weight for pulsed interrupt
6070Sstevel@tonic-gate 		 * sources.
6080Sstevel@tonic-gate 		 */
6090Sstevel@tonic-gate 		mutex_enter(&ib_p->ib_intr_lock);
6100Sstevel@tonic-gate 		cpu_id = intr_dist_cpuid();
6110Sstevel@tonic-gate 		*map_reg_addr = ib_get_map_reg(mondo, cpu_id);
6120Sstevel@tonic-gate 		mutex_exit(&ib_p->ib_intr_lock);
6130Sstevel@tonic-gate 		*map_reg_addr;	/* flush previous write */
6140Sstevel@tonic-gate 		goto done;
6150Sstevel@tonic-gate 	}
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	if ((mondo = pci_xlate_intr(dip, rdip, pci_p->pci_ib_p, ino)) == 0)
6180Sstevel@tonic-gate 		goto fail1;
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	ino = IB_MONDO_TO_INO(mondo);
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
6230Sstevel@tonic-gate 	ih_p = ib_alloc_ih(rdip, hdlp->ih_inum,
6240Sstevel@tonic-gate 	    hdlp->ih_cb_func, hdlp->ih_cb_arg1, hdlp->ih_cb_arg2);
6250Sstevel@tonic-gate 	if (map_pcidev_cfg_reg(dip, rdip, &ih_p->ih_config_handle))
6260Sstevel@tonic-gate 		goto fail2;
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	if (ino_p = ib_locate_ino(ib_p, ino)) {		/* sharing ino */
6290Sstevel@tonic-gate 		uint32_t intr_index = hdlp->ih_inum;
6300Sstevel@tonic-gate 		if (ib_ino_locate_intr(ino_p, rdip, intr_index)) {
6310Sstevel@tonic-gate 			DEBUG1(DBG_A_INTX, dip, "dup intr #%d\n", intr_index);
6320Sstevel@tonic-gate 			goto fail3;
6330Sstevel@tonic-gate 		}
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 		/* add weight to the cpu that we are already targeting */
6360Sstevel@tonic-gate 		cpu_id = ino_p->ino_cpuid;
6370Sstevel@tonic-gate 		weight = pci_class_to_intr_weight(rdip);
6380Sstevel@tonic-gate 		intr_dist_cpuid_add_device_weight(cpu_id, rdip, weight);
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 		ib_ino_add_intr(pci_p, ino_p, ih_p);
6410Sstevel@tonic-gate 		goto ino_done;
6420Sstevel@tonic-gate 	}
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	ino_p = ib_new_ino(ib_p, ino, ih_p);
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 	if (hdlp->ih_pri == 0)
6470Sstevel@tonic-gate 		hdlp->ih_pri = pci_class_to_pil(rdip);
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	hdlp->ih_vector = CB_MONDO_TO_XMONDO(cb_p, mondo);
6500Sstevel@tonic-gate 
651909Segillett 	/* Store this global mondo */
652909Segillett 	ino_p->ino_mondo = hdlp->ih_vector;
653909Segillett 
6540Sstevel@tonic-gate 	DEBUG2(DBG_A_INTX, dip, "pci_add_intr:  pil=0x%x mondo=0x%x\n",
6550Sstevel@tonic-gate 	    hdlp->ih_pri, hdlp->ih_vector);
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp,
6580Sstevel@tonic-gate 	    (ddi_intr_handler_t *)pci_intr_wrapper, (caddr_t)ino_p, NULL);
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	ret = i_ddi_add_ivintr(hdlp);
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 	/*
6630Sstevel@tonic-gate 	 * Restore original interrupt handler
6640Sstevel@tonic-gate 	 * and arguments in interrupt handle.
6650Sstevel@tonic-gate 	 */
6660Sstevel@tonic-gate 	DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp, ih_p->ih_handler,
6670Sstevel@tonic-gate 	    ih_p->ih_handler_arg1, ih_p->ih_handler_arg2);
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
6700Sstevel@tonic-gate 		goto fail4;
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	/* Save the pil for this ino */
6730Sstevel@tonic-gate 	ino_p->ino_pil = hdlp->ih_pri;
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 	/* clear and enable interrupt */
6760Sstevel@tonic-gate 	IB_INO_INTR_CLEAR(ino_p->ino_clr_reg);
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	/* select cpu and compute weight, saving both for sharing and removal */
6790Sstevel@tonic-gate 	cpu_id = pci_intr_dist_cpuid(ib_p, ino_p);
6800Sstevel@tonic-gate 	ino_p->ino_cpuid = cpu_id;
6810Sstevel@tonic-gate 	ino_p->ino_established = 1;
6820Sstevel@tonic-gate 	weight = pci_class_to_intr_weight(rdip);
6830Sstevel@tonic-gate 	intr_dist_cpuid_add_device_weight(cpu_id, rdip, weight);
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate #ifdef _STARFIRE
6860Sstevel@tonic-gate 	cpu_id = pc_translate_tgtid(cb_p->cb_ittrans_cookie, cpu_id,
6870Sstevel@tonic-gate 		IB_GET_MAPREG_INO(ino));
6880Sstevel@tonic-gate #endif /* _STARFIRE */
6890Sstevel@tonic-gate 	*ino_p->ino_map_reg = ib_get_map_reg(mondo, cpu_id);
6900Sstevel@tonic-gate 	*ino_p->ino_map_reg;
6910Sstevel@tonic-gate ino_done:
6920Sstevel@tonic-gate 	ih_p->ih_ino_p = ino_p;
69366Sesolom 	ih_p->ih_ksp = kstat_create("pci_intrs",
69466Sesolom 	    atomic_inc_32_nv(&pciintr_ks_instance), "config", "interrupts",
69566Sesolom 	    KSTAT_TYPE_NAMED,
69666Sesolom 	    sizeof (pciintr_ks_template) / sizeof (kstat_named_t),
69766Sesolom 	    KSTAT_FLAG_VIRTUAL);
69866Sesolom 	if (ih_p->ih_ksp != NULL) {
69966Sesolom 		ih_p->ih_ksp->ks_data_size += MAXPATHLEN * 2;
70066Sesolom 		ih_p->ih_ksp->ks_lock = &pciintr_ks_template_lock;
70166Sesolom 		ih_p->ih_ksp->ks_data = &pciintr_ks_template;
70266Sesolom 		ih_p->ih_ksp->ks_private = ih_p;
70366Sesolom 		ih_p->ih_ksp->ks_update = pci_ks_update;
7040Sstevel@tonic-gate 		kstat_install(ih_p->ih_ksp);
70566Sesolom 	}
7060Sstevel@tonic-gate 	ib_ino_map_reg_share(ib_p, ino, ino_p);
7070Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
7080Sstevel@tonic-gate done:
7090Sstevel@tonic-gate 	DEBUG2(DBG_A_INTX, dip, "done! Interrupt 0x%x pil=%x\n",
7100Sstevel@tonic-gate 		hdlp->ih_vector, hdlp->ih_pri);
7110Sstevel@tonic-gate 	return (DDI_SUCCESS);
7120Sstevel@tonic-gate fail4:
7130Sstevel@tonic-gate 	ib_delete_ino(ib_p, ino_p);
7140Sstevel@tonic-gate fail3:
7150Sstevel@tonic-gate 	if (ih_p->ih_config_handle)
7160Sstevel@tonic-gate 		pci_config_teardown(&ih_p->ih_config_handle);
7170Sstevel@tonic-gate fail2:
7180Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
7190Sstevel@tonic-gate 	kmem_free(ih_p, sizeof (ih_t));
7200Sstevel@tonic-gate fail1:
7210Sstevel@tonic-gate 	DEBUG2(DBG_A_INTX, dip, "Failed! Interrupt 0x%x pil=%x\n",
7220Sstevel@tonic-gate 		hdlp->ih_vector, hdlp->ih_pri);
7230Sstevel@tonic-gate 	return (DDI_FAILURE);
7240Sstevel@tonic-gate }
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate int
7270Sstevel@tonic-gate pci_remove_intr(dev_info_t *dip, dev_info_t *rdip, ddi_intr_handle_impl_t *hdlp)
7280Sstevel@tonic-gate {
7290Sstevel@tonic-gate 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
7300Sstevel@tonic-gate 	ib_t *ib_p = pci_p->pci_ib_p;
7310Sstevel@tonic-gate 	cb_t *cb_p = pci_p->pci_cb_p;
7320Sstevel@tonic-gate 	ib_ino_t ino;
7330Sstevel@tonic-gate 	ib_mondo_t mondo;
7340Sstevel@tonic-gate 	ib_ino_info_t *ino_p;	/* non-pulse only */
7350Sstevel@tonic-gate 	ih_t *ih_p;		/* non-pulse only */
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 	ino = IB_MONDO_TO_INO(hdlp->ih_vector);
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	DEBUG3(DBG_R_INTX, dip, "pci_rem_intr: rdip=%s%d ino=%x\n",
7400Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip), ino);
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	if (hdlp->ih_vector & PCI_PULSE_INO) { /* pulse interrupt */
7430Sstevel@tonic-gate 		volatile uint64_t *map_reg_addr;
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 		/*
7460Sstevel@tonic-gate 		 * No weight was added by pci_add_intr for PCI_PULSE_INO
7470Sstevel@tonic-gate 		 * because it is difficult to determine cpuid here.
7480Sstevel@tonic-gate 		 */
7490Sstevel@tonic-gate 		map_reg_addr = ib_intr_map_reg_addr(ib_p, ino);
7500Sstevel@tonic-gate 		IB_INO_INTR_RESET(map_reg_addr);	/* disable intr */
7510Sstevel@tonic-gate 		*map_reg_addr;
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 		mondo = pci_xlate_intr(dip, rdip, ib_p, ino);
7540Sstevel@tonic-gate 		if (mondo == 0) {
7550Sstevel@tonic-gate 			DEBUG1(DBG_R_INTX, dip,
7560Sstevel@tonic-gate 				"can't get mondo for ino %x\n", ino);
7570Sstevel@tonic-gate 			return (DDI_FAILURE);
7580Sstevel@tonic-gate 		}
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 		if (hdlp->ih_pri == 0)
7610Sstevel@tonic-gate 			hdlp->ih_pri = pci_class_to_pil(rdip);
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 		hdlp->ih_vector = CB_MONDO_TO_XMONDO(cb_p, mondo);
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 		DEBUG2(DBG_R_INTX, dip, "pci_rem_intr: pil=0x%x mondo=0x%x\n",
7660Sstevel@tonic-gate 		    hdlp->ih_pri, hdlp->ih_vector);
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 		i_ddi_rem_ivintr(hdlp);
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 		DEBUG2(DBG_R_INTX, dip, "pulse success mondo=%x reg=%p\n",
7710Sstevel@tonic-gate 			mondo, map_reg_addr);
7720Sstevel@tonic-gate 		return (DDI_SUCCESS);
7730Sstevel@tonic-gate 	}
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	/* Translate the interrupt property */
7760Sstevel@tonic-gate 	mondo = pci_xlate_intr(dip, rdip, pci_p->pci_ib_p, ino);
7770Sstevel@tonic-gate 	if (mondo == 0) {
7780Sstevel@tonic-gate 		DEBUG1(DBG_R_INTX, dip, "can't get mondo for ino %x\n", ino);
7790Sstevel@tonic-gate 		return (DDI_FAILURE);
7800Sstevel@tonic-gate 	}
7810Sstevel@tonic-gate 	ino = IB_MONDO_TO_INO(mondo);
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
7840Sstevel@tonic-gate 	ino_p = ib_locate_ino(ib_p, ino);
7850Sstevel@tonic-gate 	if (!ino_p) {
7860Sstevel@tonic-gate 		int r = cb_remove_xintr(pci_p, dip, rdip, ino, mondo);
7870Sstevel@tonic-gate 		if (r != DDI_SUCCESS)
7880Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d-xintr: ino %x is invalid",
7890Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip), ino);
7900Sstevel@tonic-gate 		mutex_exit(&ib_p->ib_ino_lst_mutex);
7910Sstevel@tonic-gate 		return (r);
7920Sstevel@tonic-gate 	}
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	ih_p = ib_ino_locate_intr(ino_p, rdip, hdlp->ih_inum);
7950Sstevel@tonic-gate 	ib_ino_rem_intr(pci_p, ino_p, ih_p);
7960Sstevel@tonic-gate 	intr_dist_cpuid_rem_device_weight(ino_p->ino_cpuid, rdip);
7970Sstevel@tonic-gate 	if (ino_p->ino_ih_size == 0) {
7980Sstevel@tonic-gate 		IB_INO_INTR_PEND(ib_clear_intr_reg_addr(ib_p, ino));
7990Sstevel@tonic-gate 		hdlp->ih_vector = CB_MONDO_TO_XMONDO(cb_p, mondo);
8000Sstevel@tonic-gate 		if (hdlp->ih_pri == 0)
8010Sstevel@tonic-gate 			hdlp->ih_pri = pci_class_to_pil(rdip);
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 		i_ddi_rem_ivintr(hdlp);
8040Sstevel@tonic-gate 		ib_delete_ino(ib_p, ino_p);
8050Sstevel@tonic-gate 	}
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 	/* re-enable interrupt only if mapping register still shared */
8080Sstevel@tonic-gate 	if (ib_ino_map_reg_unshare(ib_p, ino, ino_p)) {
8090Sstevel@tonic-gate 		IB_INO_INTR_ON(ino_p->ino_map_reg);
8100Sstevel@tonic-gate 		*ino_p->ino_map_reg;
8110Sstevel@tonic-gate 	}
8120Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 	if (ino_p->ino_ih_size == 0)
8150Sstevel@tonic-gate 		kmem_free(ino_p, sizeof (ib_ino_info_t));
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	DEBUG1(DBG_R_INTX, dip, "success! mondo=%x\n", mondo);
8180Sstevel@tonic-gate 	return (DDI_SUCCESS);
8190Sstevel@tonic-gate }
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate /*
8220Sstevel@tonic-gate  * free the pci_inos array allocated during pci_intr_setup. the actual
8230Sstevel@tonic-gate  * interrupts are torn down by their respective block destroy routines:
8240Sstevel@tonic-gate  * cb_destroy, pbm_destroy, and ib_destroy.
8250Sstevel@tonic-gate  */
8260Sstevel@tonic-gate void
8270Sstevel@tonic-gate pci_intr_teardown(pci_t *pci_p)
8280Sstevel@tonic-gate {
8290Sstevel@tonic-gate 	kmem_free(pci_p->pci_inos, pci_p->pci_inos_len);
8300Sstevel@tonic-gate 	pci_p->pci_inos = NULL;
8310Sstevel@tonic-gate 	pci_p->pci_inos_len = 0;
8320Sstevel@tonic-gate }
833