xref: /illumos-gate/usr/src/uts/sun4u/io/pci/pci_intr.c (revision 2a1fd0ff)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5d48713b8Sesolom  * Common Development and Distribution License (the "License").
6d48713b8Sesolom  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22614edcaeSEvan Yan  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
25*2a1fd0ffSPeter Tribble /*
26*2a1fd0ffSPeter Tribble  * Copyright 2019 Peter Tribble.
27*2a1fd0ffSPeter Tribble  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * PCI nexus interrupt handling:
317c478bd9Sstevel@tonic-gate  *	PCI device interrupt handler wrapper
327c478bd9Sstevel@tonic-gate  *	pil lookup routine
337c478bd9Sstevel@tonic-gate  *	PCI device interrupt related initchild code
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
387c478bd9Sstevel@tonic-gate #include <sys/async.h>
397c478bd9Sstevel@tonic-gate #include <sys/spl.h>
407c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
417c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>	/* e_ddi_nodeid_to_dip() */
427c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
437c478bd9Sstevel@tonic-gate #include <sys/pci/pci_obj.h>
447c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
456d44af1bSesolom #include <sys/clock.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate  * interrupt jabber:
497c478bd9Sstevel@tonic-gate  *
507c478bd9Sstevel@tonic-gate  * When an interrupt line is jabbering, every time the state machine for the
517c478bd9Sstevel@tonic-gate  * associated ino is idled, a new mondo will be sent and the ino will go into
527c478bd9Sstevel@tonic-gate  * the pending state again. The mondo will cause a new call to
537c478bd9Sstevel@tonic-gate  * pci_intr_wrapper() which normally idles the ino's state machine which would
547c478bd9Sstevel@tonic-gate  * precipitate another trip round the loop.
557c478bd9Sstevel@tonic-gate  * The loop can be broken by preventing the ino's state machine from being
567c478bd9Sstevel@tonic-gate  * idled when an interrupt line is jabbering. See the comment at the
577c478bd9Sstevel@tonic-gate  * beginning of pci_intr_wrapper() explaining how the 'interrupt jabber
587c478bd9Sstevel@tonic-gate  * protection' code does this.
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #ifdef NOT_DEFINED
647c478bd9Sstevel@tonic-gate /*
657c478bd9Sstevel@tonic-gate  * This array is used to determine the sparc PIL at the which the
667c478bd9Sstevel@tonic-gate  * handler for a given INO will execute.  This table is for onboard
677c478bd9Sstevel@tonic-gate  * devices only.  A different scheme will be used for plug-in cards.
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate uint_t ino_to_pil[] = {
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	/* pil */		/* ino */
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	0, 0, 0, 0,  		/* 0x00 - 0x03: bus A slot 0 int#A, B, C, D */
757c478bd9Sstevel@tonic-gate 	0, 0, 0, 0,		/* 0x04 - 0x07: bus A slot 1 int#A, B, C, D */
767c478bd9Sstevel@tonic-gate 	0, 0, 0, 0,  		/* 0x08 - 0x0B: unused */
777c478bd9Sstevel@tonic-gate 	0, 0, 0, 0,		/* 0x0C - 0x0F: unused */
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	0, 0, 0, 0,  		/* 0x10 - 0x13: bus B slot 0 int#A, B, C, D */
807c478bd9Sstevel@tonic-gate 	0, 0, 0, 0,		/* 0x14 - 0x17: bus B slot 1 int#A, B, C, D */
817c478bd9Sstevel@tonic-gate 	0, 0, 0, 0,  		/* 0x18 - 0x1B: bus B slot 2 int#A, B, C, D */
827c478bd9Sstevel@tonic-gate 	4, 0, 0, 0,		/* 0x1C - 0x1F: bus B slot 3 int#A, B, C, D */
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	4,			/* 0x20: SCSI */
857c478bd9Sstevel@tonic-gate 	6,			/* 0x21: ethernet */
867c478bd9Sstevel@tonic-gate 	3,			/* 0x22: parallel port */
877c478bd9Sstevel@tonic-gate 	9,			/* 0x23: audio record */
887c478bd9Sstevel@tonic-gate 	9,			/* 0x24: audio playback */
897c478bd9Sstevel@tonic-gate 	14,			/* 0x25: power fail */
907c478bd9Sstevel@tonic-gate 	4,			/* 0x26: 2nd SCSI */
917c478bd9Sstevel@tonic-gate 	8,			/* 0x27: floppy */
927c478bd9Sstevel@tonic-gate 	14,			/* 0x28: thermal warning */
937c478bd9Sstevel@tonic-gate 	12,			/* 0x29: keyboard */
947c478bd9Sstevel@tonic-gate 	12,			/* 0x2A: mouse */
957c478bd9Sstevel@tonic-gate 	12,			/* 0x2B: serial */
967c478bd9Sstevel@tonic-gate 	0,			/* 0x2C: timer/counter 0 */
977c478bd9Sstevel@tonic-gate 	0,			/* 0x2D: timer/counter 1 */
987c478bd9Sstevel@tonic-gate 	14,			/* 0x2E: uncorrectable ECC errors */
997c478bd9Sstevel@tonic-gate 	14,			/* 0x2F: correctable ECC errors */
1007c478bd9Sstevel@tonic-gate 	14,			/* 0x30: PCI bus A error */
1017c478bd9Sstevel@tonic-gate 	14,			/* 0x31: PCI bus B error */
1027c478bd9Sstevel@tonic-gate 	14,			/* 0x32: power management wakeup */
1037c478bd9Sstevel@tonic-gate 	14,			/* 0x33 */
1047c478bd9Sstevel@tonic-gate 	14,			/* 0x34 */
1057c478bd9Sstevel@tonic-gate 	14,			/* 0x35 */
1067c478bd9Sstevel@tonic-gate 	14,			/* 0x36 */
1077c478bd9Sstevel@tonic-gate 	14,			/* 0x37 */
1087c478bd9Sstevel@tonic-gate 	14,			/* 0x38 */
1097c478bd9Sstevel@tonic-gate 	14,			/* 0x39 */
1107c478bd9Sstevel@tonic-gate 	14,			/* 0x3a */
1117c478bd9Sstevel@tonic-gate 	14,			/* 0x3b */
1127c478bd9Sstevel@tonic-gate 	14,			/* 0x3c */
1137c478bd9Sstevel@tonic-gate 	14,			/* 0x3d */
1147c478bd9Sstevel@tonic-gate 	14,			/* 0x3e */
1157c478bd9Sstevel@tonic-gate 	14,			/* 0x3f */
1167c478bd9Sstevel@tonic-gate 	14			/* 0x40 */
1177c478bd9Sstevel@tonic-gate };
1187c478bd9Sstevel@tonic-gate #endif /* NOT_DEFINED */
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate #define	PCI_SIMBA_VENID		0x108e	/* vendor id for simba */
1227c478bd9Sstevel@tonic-gate #define	PCI_SIMBA_DEVID		0x5000	/* device id for simba */
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate /*
1257c478bd9Sstevel@tonic-gate  * map_pcidev_cfg_reg - create mapping to pci device configuration registers
1267c478bd9Sstevel@tonic-gate  *			if we have a simba AND a pci to pci bridge along the
1277c478bd9Sstevel@tonic-gate  *			device path.
1287c478bd9Sstevel@tonic-gate  *			Called with corresponding mutexes held!!
1297c478bd9Sstevel@tonic-gate  *
1307c478bd9Sstevel@tonic-gate  * XXX	  XXX	XXX	The purpose of this routine is to overcome a hardware
1317c478bd9Sstevel@tonic-gate  *			defect in Sabre CPU and Simba bridge configuration
1327c478bd9Sstevel@tonic-gate  *			which does not drain DMA write data stalled in
1337c478bd9Sstevel@tonic-gate  *			PCI to PCI bridges (such as the DEC bridge) beyond
1347c478bd9Sstevel@tonic-gate  *			Simba. This routine will setup the data structures
1357c478bd9Sstevel@tonic-gate  *			to allow the pci_intr_wrapper to perform a manual
1367c478bd9Sstevel@tonic-gate  *			drain data operation before passing the control to
1377c478bd9Sstevel@tonic-gate  *			interrupt handlers of device drivers.
1387c478bd9Sstevel@tonic-gate  * return value:
1397c478bd9Sstevel@tonic-gate  * DDI_SUCCESS
1407c478bd9Sstevel@tonic-gate  * DDI_FAILURE		if unable to create mapping
1417c478bd9Sstevel@tonic-gate  */
1427c478bd9Sstevel@tonic-gate static int
map_pcidev_cfg_reg(dev_info_t * dip,dev_info_t * rdip,ddi_acc_handle_t * hdl_p)1437c478bd9Sstevel@tonic-gate map_pcidev_cfg_reg(dev_info_t *dip, dev_info_t *rdip, ddi_acc_handle_t *hdl_p)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate 	dev_info_t *cdip;
1467c478bd9Sstevel@tonic-gate 	dev_info_t *pci_dip = NULL;
1477c478bd9Sstevel@tonic-gate 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
1487c478bd9Sstevel@tonic-gate 	int simba_found = 0, pci_bridge_found = 0;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	for (cdip = rdip; cdip && cdip != dip; cdip = ddi_get_parent(cdip)) {
1517c478bd9Sstevel@tonic-gate 		ddi_acc_handle_t config_handle;
1527c478bd9Sstevel@tonic-gate 		uint32_t vendor_id = ddi_getprop(DDI_DEV_T_ANY, cdip,
15318061c65Sgd 		    DDI_PROP_DONTPASS, "vendor-id", 0xffff);
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 		DEBUG4(DBG_A_INTX, pci_p->pci_dip,
15618061c65Sgd 		    "map dev cfg reg for %s%d: @%s%d\n",
15718061c65Sgd 		    ddi_driver_name(rdip), ddi_get_instance(rdip),
15818061c65Sgd 		    ddi_driver_name(cdip), ddi_get_instance(cdip));
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 		if (ddi_prop_exists(DDI_DEV_T_ANY, cdip, DDI_PROP_DONTPASS,
16118061c65Sgd 		    "no-dma-interrupt-sync"))
1627c478bd9Sstevel@tonic-gate 			continue;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 		/* continue to search up-stream if not a PCI device */
1657c478bd9Sstevel@tonic-gate 		if (vendor_id == 0xffff)
1667c478bd9Sstevel@tonic-gate 			continue;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 		/* record the deepest pci device */
1697c478bd9Sstevel@tonic-gate 		if (!pci_dip)
1707c478bd9Sstevel@tonic-gate 			pci_dip = cdip;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 		/* look for simba */
1737c478bd9Sstevel@tonic-gate 		if (vendor_id == PCI_SIMBA_VENID) {
1747c478bd9Sstevel@tonic-gate 			uint32_t device_id = ddi_getprop(DDI_DEV_T_ANY,
1757c478bd9Sstevel@tonic-gate 			    cdip, DDI_PROP_DONTPASS, "device-id", -1);
1767c478bd9Sstevel@tonic-gate 			if (device_id == PCI_SIMBA_DEVID) {
1777c478bd9Sstevel@tonic-gate 				simba_found = 1;
1787c478bd9Sstevel@tonic-gate 				DEBUG0(DBG_A_INTX, pci_p->pci_dip,
17918061c65Sgd 				    "\tFound simba\n");
1807c478bd9Sstevel@tonic-gate 				continue; /* do not check bridge if simba */
1817c478bd9Sstevel@tonic-gate 			}
1827c478bd9Sstevel@tonic-gate 		}
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 		/* look for pci to pci bridge */
1857c478bd9Sstevel@tonic-gate 		if (pci_config_setup(cdip, &config_handle) != DDI_SUCCESS) {
1867c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
1877c478bd9Sstevel@tonic-gate 			    "%s%d: can't get brdg cfg space for %s%d\n",
18818061c65Sgd 			    ddi_driver_name(dip), ddi_get_instance(dip),
18918061c65Sgd 			    ddi_driver_name(cdip), ddi_get_instance(cdip));
1907c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
1917c478bd9Sstevel@tonic-gate 		}
1927c478bd9Sstevel@tonic-gate 		if (pci_config_get8(config_handle, PCI_CONF_BASCLASS)
1937c478bd9Sstevel@tonic-gate 		    == PCI_CLASS_BRIDGE) {
1947c478bd9Sstevel@tonic-gate 			DEBUG0(DBG_A_INTX, pci_p->pci_dip,
19518061c65Sgd 			    "\tFound PCI to xBus bridge\n");
1967c478bd9Sstevel@tonic-gate 			pci_bridge_found = 1;
1977c478bd9Sstevel@tonic-gate 		}
1987c478bd9Sstevel@tonic-gate 		pci_config_teardown(&config_handle);
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if (!pci_bridge_found)
2027c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
2037c478bd9Sstevel@tonic-gate 	if (!simba_found && (CHIP_TYPE(pci_p) < PCI_CHIP_SCHIZO))
2047c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
2057c478bd9Sstevel@tonic-gate 	if (pci_config_setup(pci_dip, hdl_p) != DDI_SUCCESS) {
2067c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: can not get config space for %s%d\n",
20718061c65Sgd 		    ddi_driver_name(dip), ddi_get_instance(dip),
20818061c65Sgd 		    ddi_driver_name(cdip), ddi_get_instance(cdip));
2097c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2107c478bd9Sstevel@tonic-gate 	}
2117c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate /*
2157c478bd9Sstevel@tonic-gate  * If the unclaimed interrupt count has reached the limit set by
2167c478bd9Sstevel@tonic-gate  * pci_unclaimed_intr_max within the time limit, then all interrupts
2177c478bd9Sstevel@tonic-gate  * on this ino is blocked by not idling the interrupt state machine.
2187c478bd9Sstevel@tonic-gate  */
2197c478bd9Sstevel@tonic-gate static int
pci_spurintr(ib_ino_pil_t * ipil_p)220b0fc0e77Sgovinda pci_spurintr(ib_ino_pil_t *ipil_p) {
221b0fc0e77Sgovinda 	ib_ino_info_t	*ino_p = ipil_p->ipil_ino_p;
222b0fc0e77Sgovinda 	ih_t		*ih_p = ipil_p->ipil_ih_start;
223b0fc0e77Sgovinda 	pci_t		*pci_p = ino_p->ino_ib_p->ib_pci_p;
224b0fc0e77Sgovinda 	char		*err_fmt_str;
225b0fc0e77Sgovinda 	boolean_t	blocked = B_FALSE;
226b0fc0e77Sgovinda 	int		i;
227b0fc0e77Sgovinda 
228b0fc0e77Sgovinda 	if (ino_p->ino_unclaimed_intrs > pci_unclaimed_intr_max)
2297c478bd9Sstevel@tonic-gate 		return (DDI_INTR_CLAIMED);
2307c478bd9Sstevel@tonic-gate 
231b0fc0e77Sgovinda 	if (!ino_p->ino_unclaimed_intrs)
2327c478bd9Sstevel@tonic-gate 		ino_p->ino_spurintr_begin = ddi_get_lbolt();
2337c478bd9Sstevel@tonic-gate 
234b0fc0e77Sgovinda 	ino_p->ino_unclaimed_intrs++;
2357c478bd9Sstevel@tonic-gate 
236b0fc0e77Sgovinda 	if (ino_p->ino_unclaimed_intrs <= pci_unclaimed_intr_max)
2377c478bd9Sstevel@tonic-gate 		goto clear;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	if (drv_hztousec(ddi_get_lbolt() - ino_p->ino_spurintr_begin)
2407c478bd9Sstevel@tonic-gate 	    > pci_spurintr_duration) {
241b0fc0e77Sgovinda 		ino_p->ino_unclaimed_intrs = 0;
2427c478bd9Sstevel@tonic-gate 		goto clear;
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 	err_fmt_str = "%s%d: ino 0x%x blocked";
245aa01ff85Sdanice 	blocked = B_TRUE;
2467c478bd9Sstevel@tonic-gate 	goto warn;
2477c478bd9Sstevel@tonic-gate clear:
248aa01ff85Sdanice 	if (!pci_spurintr_msgs) { /* tomatillo errata #71 spurious mondo */
249aa01ff85Sdanice 		/* clear the pending state */
250aa01ff85Sdanice 		IB_INO_INTR_CLEAR(ino_p->ino_clr_reg);
2517c478bd9Sstevel@tonic-gate 		return (DDI_INTR_CLAIMED);
252aa01ff85Sdanice 	}
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	err_fmt_str = "!%s%d: spurious interrupt from ino 0x%x";
2557c478bd9Sstevel@tonic-gate warn:
2567c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, err_fmt_str, NAMEINST(pci_p->pci_dip), ino_p->ino_ino);
257b0fc0e77Sgovinda 	for (i = 0; i < ipil_p->ipil_ih_size; i++, ih_p = ih_p->ih_next)
2587c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "!%s-%d#%x ", NAMEINST(ih_p->ih_dip),
2597c478bd9Sstevel@tonic-gate 		    ih_p->ih_inum);
2607c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "!\n");
261aa01ff85Sdanice 	if (blocked == B_FALSE)  /* clear the pending state */
262aa01ff85Sdanice 		IB_INO_INTR_CLEAR(ino_p->ino_clr_reg);
263aa01ff85Sdanice 
2647c478bd9Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate /*
2687c478bd9Sstevel@tonic-gate  * pci_intr_wrapper
2697c478bd9Sstevel@tonic-gate  *
2707c478bd9Sstevel@tonic-gate  * This routine is used as wrapper around interrupt handlers installed by child
2717c478bd9Sstevel@tonic-gate  * device drivers.  This routine invokes the driver interrupt handlers and
2727c478bd9Sstevel@tonic-gate  * examines the return codes.
2737c478bd9Sstevel@tonic-gate  * There is a count of unclaimed interrupts kept on a per-ino basis. If at
2747c478bd9Sstevel@tonic-gate  * least one handler claims the interrupt then the counter is halved and the
2757c478bd9Sstevel@tonic-gate  * interrupt state machine is idled. If no handler claims the interrupt then
2767c478bd9Sstevel@tonic-gate  * the counter is incremented by one and the state machine is idled.
2777c478bd9Sstevel@tonic-gate  * If the count ever reaches the limit value set by pci_unclaimed_intr_max
2787c478bd9Sstevel@tonic-gate  * then the interrupt state machine is not idled thus preventing any further
2797c478bd9Sstevel@tonic-gate  * interrupts on that ino. The state machine will only be idled again if a
2807c478bd9Sstevel@tonic-gate  * handler is subsequently added or removed.
2817c478bd9Sstevel@tonic-gate  *
2827c478bd9Sstevel@tonic-gate  * return value: DDI_INTR_CLAIMED if any handlers claimed the interrupt,
2837c478bd9Sstevel@tonic-gate  * DDI_INTR_UNCLAIMED otherwise.
2847c478bd9Sstevel@tonic-gate  */
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate extern uint64_t intr_get_time(void);
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate uint_t
pci_intr_wrapper(caddr_t arg)2897c478bd9Sstevel@tonic-gate pci_intr_wrapper(caddr_t arg)
2907c478bd9Sstevel@tonic-gate {
291b0fc0e77Sgovinda 	ib_ino_pil_t	*ipil_p = (ib_ino_pil_t *)arg;
292b0fc0e77Sgovinda 	ib_ino_info_t	*ino_p = ipil_p->ipil_ino_p;
293b0fc0e77Sgovinda 	uint_t		result = 0, r = DDI_INTR_UNCLAIMED;
294b0fc0e77Sgovinda 	pci_t		*pci_p = ino_p->ino_ib_p->ib_pci_p;
295b0fc0e77Sgovinda 	pbm_t		*pbm_p = pci_p->pci_pbm_p;
296b0fc0e77Sgovinda 	ih_t		*ih_p = ipil_p->ipil_ih_start;
297b0fc0e77Sgovinda 	int		i;
298b0fc0e77Sgovinda 
299b0fc0e77Sgovinda 	for (i = 0; i < ipil_p->ipil_ih_size; i++, ih_p = ih_p->ih_next) {
3007c478bd9Sstevel@tonic-gate 		dev_info_t *dip = ih_p->ih_dip;
3017c478bd9Sstevel@tonic-gate 		uint_t (*handler)() = ih_p->ih_handler;
3027c478bd9Sstevel@tonic-gate 		caddr_t arg1 = ih_p->ih_handler_arg1;
3037c478bd9Sstevel@tonic-gate 		caddr_t arg2 = ih_p->ih_handler_arg2;
3047c478bd9Sstevel@tonic-gate 		ddi_acc_handle_t cfg_hdl = ih_p->ih_config_handle;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 		if (pci_intr_dma_sync && cfg_hdl && pbm_p->pbm_sync_reg_pa) {
3077c478bd9Sstevel@tonic-gate 			(void) pci_config_get16(cfg_hdl, PCI_CONF_VENID);
3087c478bd9Sstevel@tonic-gate 			pci_pbm_dma_sync(pbm_p, ino_p->ino_ino);
3097c478bd9Sstevel@tonic-gate 		}
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 		if (ih_p->ih_intr_state == PCI_INTR_STATE_DISABLE) {
3127c478bd9Sstevel@tonic-gate 			DEBUG3(DBG_INTR, pci_p->pci_dip,
3137c478bd9Sstevel@tonic-gate 			    "pci_intr_wrapper: %s%d interrupt %d is disabled\n",
3147c478bd9Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip),
3157c478bd9Sstevel@tonic-gate 			    ino_p->ino_ino);
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 			continue;
3187c478bd9Sstevel@tonic-gate 		}
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 		DTRACE_PROBE4(interrupt__start, dev_info_t, dip,
3217c478bd9Sstevel@tonic-gate 		    void *, handler, caddr_t, arg1, caddr_t, arg2);
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 		r = (*handler)(arg1, arg2);
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 		/*
3267c478bd9Sstevel@tonic-gate 		 * Account for time used by this interrupt. Protect against
3277c478bd9Sstevel@tonic-gate 		 * conflicting writes to ih_ticks from ib_intr_dist_all() by
3287c478bd9Sstevel@tonic-gate 		 * using atomic ops.
3297c478bd9Sstevel@tonic-gate 		 */
3307c478bd9Sstevel@tonic-gate 
331b0fc0e77Sgovinda 		if (ipil_p->ipil_pil <= LOCK_LEVEL)
3327c478bd9Sstevel@tonic-gate 			atomic_add_64(&ih_p->ih_ticks, intr_get_time());
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 		DTRACE_PROBE4(interrupt__complete, dev_info_t, dip,
3357c478bd9Sstevel@tonic-gate 		    void *, handler, caddr_t, arg1, int, r);
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 		result += r;
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 		if (pci_check_all_handlers)
3407c478bd9Sstevel@tonic-gate 			continue;
3417c478bd9Sstevel@tonic-gate 		if (result)
3427c478bd9Sstevel@tonic-gate 			break;
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate 
345b0fc0e77Sgovinda 	if (result)
346b0fc0e77Sgovinda 		ino_p->ino_claimed |= (1 << ipil_p->ipil_pil);
347b0fc0e77Sgovinda 
348b0fc0e77Sgovinda 	/* Interrupt can only be cleared after all pil levels are handled */
349b0fc0e77Sgovinda 	if (ipil_p->ipil_pil != ino_p->ino_lopil)
350b0fc0e77Sgovinda 		return (DDI_INTR_CLAIMED);
3517c478bd9Sstevel@tonic-gate 
352b0fc0e77Sgovinda 	if (!ino_p->ino_claimed)
353b0fc0e77Sgovinda 		return (pci_spurintr(ipil_p));
354b0fc0e77Sgovinda 
355b0fc0e77Sgovinda 	ino_p->ino_unclaimed_intrs = 0;
356b0fc0e77Sgovinda 	ino_p->ino_claimed = 0;
357b0fc0e77Sgovinda 
358b0fc0e77Sgovinda 	/* Clear the pending state */
359b0fc0e77Sgovinda 	IB_INO_INTR_CLEAR(ino_p->ino_clr_reg);
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate dev_info_t *
get_my_childs_dip(dev_info_t * dip,dev_info_t * rdip)3657c478bd9Sstevel@tonic-gate get_my_childs_dip(dev_info_t *dip, dev_info_t *rdip)
3667c478bd9Sstevel@tonic-gate {
3677c478bd9Sstevel@tonic-gate 	dev_info_t *cdip = rdip;
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	for (; ddi_get_parent(cdip) != dip; cdip = ddi_get_parent(cdip))
3707c478bd9Sstevel@tonic-gate 		;
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	return (cdip);
3737c478bd9Sstevel@tonic-gate }
3747c478bd9Sstevel@tonic-gate 
3756d44af1bSesolom static struct {
3766d44af1bSesolom 	kstat_named_t pciintr_ks_name;
3776d44af1bSesolom 	kstat_named_t pciintr_ks_type;
3786d44af1bSesolom 	kstat_named_t pciintr_ks_cpu;
3796d44af1bSesolom 	kstat_named_t pciintr_ks_pil;
3806d44af1bSesolom 	kstat_named_t pciintr_ks_time;
3816d44af1bSesolom 	kstat_named_t pciintr_ks_ino;
3826d44af1bSesolom 	kstat_named_t pciintr_ks_cookie;
3836d44af1bSesolom 	kstat_named_t pciintr_ks_devpath;
3846d44af1bSesolom 	kstat_named_t pciintr_ks_buspath;
3856d44af1bSesolom } pciintr_ks_template = {
3866d44af1bSesolom 	{ "name",	KSTAT_DATA_CHAR },
3876d44af1bSesolom 	{ "type",	KSTAT_DATA_CHAR },
3886d44af1bSesolom 	{ "cpu",	KSTAT_DATA_UINT64 },
3896d44af1bSesolom 	{ "pil",	KSTAT_DATA_UINT64 },
3906d44af1bSesolom 	{ "time",	KSTAT_DATA_UINT64 },
3916d44af1bSesolom 	{ "ino",	KSTAT_DATA_UINT64 },
3926d44af1bSesolom 	{ "cookie",	KSTAT_DATA_UINT64 },
3936d44af1bSesolom 	{ "devpath",	KSTAT_DATA_STRING },
3946d44af1bSesolom 	{ "buspath",	KSTAT_DATA_STRING },
3956d44af1bSesolom };
3966d44af1bSesolom static uint32_t pciintr_ks_instance;
397d48713b8Sesolom static char ih_devpath[MAXPATHLEN];
398d48713b8Sesolom static char ih_buspath[MAXPATHLEN];
3996d44af1bSesolom 
4006d44af1bSesolom kmutex_t pciintr_ks_template_lock;
4016d44af1bSesolom 
4026d44af1bSesolom int
pci_ks_update(kstat_t * ksp,int rw)4036d44af1bSesolom pci_ks_update(kstat_t *ksp, int rw)
4046d44af1bSesolom {
405b0fc0e77Sgovinda 	ih_t		*ih_p = ksp->ks_private;
406b0fc0e77Sgovinda 	int	maxlen = sizeof (pciintr_ks_template.pciintr_ks_name.value.c);
407b0fc0e77Sgovinda 	ib_ino_pil_t	*ipil_p = ih_p->ih_ipil_p;
408b0fc0e77Sgovinda 	ib_ino_info_t	*ino_p = ipil_p->ipil_ino_p;
409b0fc0e77Sgovinda 	ib_t		*ib_p = ino_p->ino_ib_p;
410b0fc0e77Sgovinda 	pci_t		*pci_p = ib_p->ib_pci_p;
411b0fc0e77Sgovinda 	ib_ino_t	ino;
4126d44af1bSesolom 
413b0fc0e77Sgovinda 	ino = ino_p->ino_ino;
4146d44af1bSesolom 
4156d44af1bSesolom 	(void) snprintf(pciintr_ks_template.pciintr_ks_name.value.c, maxlen,
4166d44af1bSesolom 	    "%s%d", ddi_driver_name(ih_p->ih_dip),
4176d44af1bSesolom 	    ddi_get_instance(ih_p->ih_dip));
4186d44af1bSesolom 
4196d44af1bSesolom 	(void) ddi_pathname(ih_p->ih_dip, ih_devpath);
4206d44af1bSesolom 	(void) ddi_pathname(pci_p->pci_dip, ih_buspath);
4216d44af1bSesolom 	kstat_named_setstr(&pciintr_ks_template.pciintr_ks_devpath, ih_devpath);
4226d44af1bSesolom 	kstat_named_setstr(&pciintr_ks_template.pciintr_ks_buspath, ih_buspath);
4236d44af1bSesolom 
424e1d9f4e6Sschwartz 	if (ih_p->ih_intr_state == PCI_INTR_STATE_ENABLE) {
425e1d9f4e6Sschwartz 		(void) strcpy(pciintr_ks_template.pciintr_ks_type.value.c,
426e1d9f4e6Sschwartz 		    "fixed");
427e1d9f4e6Sschwartz 		pciintr_ks_template.pciintr_ks_cpu.value.ui64 =
428b0fc0e77Sgovinda 		    ino_p->ino_cpuid;
429e1d9f4e6Sschwartz 		pciintr_ks_template.pciintr_ks_pil.value.ui64 =
430b0fc0e77Sgovinda 		    ipil_p->ipil_pil;
431e1d9f4e6Sschwartz 		pciintr_ks_template.pciintr_ks_time.value.ui64 = ih_p->ih_nsec +
432e1d9f4e6Sschwartz 		    (uint64_t)tick2ns((hrtime_t)ih_p->ih_ticks,
433b0fc0e77Sgovinda 		    ino_p->ino_cpuid);
434e1d9f4e6Sschwartz 		pciintr_ks_template.pciintr_ks_ino.value.ui64 = ino;
435e1d9f4e6Sschwartz 		pciintr_ks_template.pciintr_ks_cookie.value.ui64 =
43618061c65Sgd 		    IB_INO_TO_MONDO(ib_p, ino);
437e1d9f4e6Sschwartz 	} else {
438e1d9f4e6Sschwartz 		(void) strcpy(pciintr_ks_template.pciintr_ks_type.value.c,
439e1d9f4e6Sschwartz 		    "disabled");
440e1d9f4e6Sschwartz 		pciintr_ks_template.pciintr_ks_cpu.value.ui64 = 0;
441e1d9f4e6Sschwartz 		pciintr_ks_template.pciintr_ks_pil.value.ui64 = 0;
442e1d9f4e6Sschwartz 		pciintr_ks_template.pciintr_ks_time.value.ui64 = 0;
443e1d9f4e6Sschwartz 		pciintr_ks_template.pciintr_ks_ino.value.ui64 = 0;
444e1d9f4e6Sschwartz 		pciintr_ks_template.pciintr_ks_cookie.value.ui64 = 0;
445e1d9f4e6Sschwartz 	}
446e1d9f4e6Sschwartz 
4476d44af1bSesolom 	return (0);
4486d44af1bSesolom }
4496d44af1bSesolom 
4507c478bd9Sstevel@tonic-gate int
pci_add_intr(dev_info_t * dip,dev_info_t * rdip,ddi_intr_handle_impl_t * hdlp)4517c478bd9Sstevel@tonic-gate pci_add_intr(dev_info_t *dip, dev_info_t *rdip, ddi_intr_handle_impl_t *hdlp)
4527c478bd9Sstevel@tonic-gate {
453b0fc0e77Sgovinda 	pci_t		*pci_p = get_pci_soft_state(ddi_get_instance(dip));
454b0fc0e77Sgovinda 	ib_t		*ib_p = pci_p->pci_ib_p;
455b0fc0e77Sgovinda 	cb_t		*cb_p = pci_p->pci_cb_p;
456b0fc0e77Sgovinda 	ih_t		*ih_p;
457b0fc0e77Sgovinda 	ib_ino_t	ino;
458b0fc0e77Sgovinda 	ib_ino_info_t	*ino_p;	/* pulse interrupts have no ino */
459b0fc0e77Sgovinda 	ib_ino_pil_t	*ipil_p, *ipil_list;
460b0fc0e77Sgovinda 	ib_mondo_t	mondo;
461b0fc0e77Sgovinda 	uint32_t	cpu_id;
462b0fc0e77Sgovinda 	int		ret;
463b0fc0e77Sgovinda 	int32_t		weight;
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	ino = IB_MONDO_TO_INO(hdlp->ih_vector);
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	DEBUG3(DBG_A_INTX, dip, "pci_add_intr: rdip=%s%d ino=%x\n",
4687c478bd9Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip), ino);
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	if (ino > ib_p->ib_max_ino) {
4717c478bd9Sstevel@tonic-gate 		DEBUG1(DBG_A_INTX, dip, "ino %x is invalid\n", ino);
4727c478bd9Sstevel@tonic-gate 		return (DDI_INTR_NOTFOUND);
4737c478bd9Sstevel@tonic-gate 	}
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	if (hdlp->ih_vector & PCI_PULSE_INO) {
4767c478bd9Sstevel@tonic-gate 		volatile uint64_t *map_reg_addr;
4777c478bd9Sstevel@tonic-gate 		map_reg_addr = ib_intr_map_reg_addr(ib_p, ino);
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 		mondo = pci_xlate_intr(dip, rdip, ib_p, ino);
4807c478bd9Sstevel@tonic-gate 		if (mondo == 0)
4817c478bd9Sstevel@tonic-gate 			goto fail1;
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 		hdlp->ih_vector = CB_MONDO_TO_XMONDO(cb_p, mondo);
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 		if (i_ddi_add_ivintr(hdlp) != DDI_SUCCESS)
4867c478bd9Sstevel@tonic-gate 			goto fail1;
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 		/*
4897c478bd9Sstevel@tonic-gate 		 * Select cpu and program.
4907c478bd9Sstevel@tonic-gate 		 *
4917c478bd9Sstevel@tonic-gate 		 * Since there is no good way to always derive cpuid in
4927c478bd9Sstevel@tonic-gate 		 * pci_remove_intr for PCI_PULSE_INO (esp. for STARFIRE), we
4937c478bd9Sstevel@tonic-gate 		 * don't add (or remove) device weight for pulsed interrupt
4947c478bd9Sstevel@tonic-gate 		 * sources.
4957c478bd9Sstevel@tonic-gate 		 */
4967c478bd9Sstevel@tonic-gate 		mutex_enter(&ib_p->ib_intr_lock);
4977c478bd9Sstevel@tonic-gate 		cpu_id = intr_dist_cpuid();
4987c478bd9Sstevel@tonic-gate 		*map_reg_addr = ib_get_map_reg(mondo, cpu_id);
4997c478bd9Sstevel@tonic-gate 		mutex_exit(&ib_p->ib_intr_lock);
5007c478bd9Sstevel@tonic-gate 		*map_reg_addr;	/* flush previous write */
5017c478bd9Sstevel@tonic-gate 		goto done;
5027c478bd9Sstevel@tonic-gate 	}
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	if ((mondo = pci_xlate_intr(dip, rdip, pci_p->pci_ib_p, ino)) == 0)
5057c478bd9Sstevel@tonic-gate 		goto fail1;
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	ino = IB_MONDO_TO_INO(mondo);
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
5107c478bd9Sstevel@tonic-gate 	ih_p = ib_alloc_ih(rdip, hdlp->ih_inum,
5117c478bd9Sstevel@tonic-gate 	    hdlp->ih_cb_func, hdlp->ih_cb_arg1, hdlp->ih_cb_arg2);
5127c478bd9Sstevel@tonic-gate 	if (map_pcidev_cfg_reg(dip, rdip, &ih_p->ih_config_handle))
5137c478bd9Sstevel@tonic-gate 		goto fail2;
5147c478bd9Sstevel@tonic-gate 
515b0fc0e77Sgovinda 	ino_p = ib_locate_ino(ib_p, ino);
516b0fc0e77Sgovinda 	ipil_list = ino_p ? ino_p->ino_ipil_p:NULL;
517b0fc0e77Sgovinda 
518b0fc0e77Sgovinda 	/* Sharing ino */
519b0fc0e77Sgovinda 	if (ino_p && (ipil_p = ib_ino_locate_ipil(ino_p, hdlp->ih_pri))) {
520b0fc0e77Sgovinda 		if (ib_intr_locate_ih(ipil_p, rdip, hdlp->ih_inum)) {
521b0fc0e77Sgovinda 			DEBUG1(DBG_A_INTX, dip, "dup intr #%d\n",
522b0fc0e77Sgovinda 			    hdlp->ih_inum);
5237c478bd9Sstevel@tonic-gate 			goto fail3;
5247c478bd9Sstevel@tonic-gate 		}
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 		/* add weight to the cpu that we are already targeting */
5277c478bd9Sstevel@tonic-gate 		cpu_id = ino_p->ino_cpuid;
5287c478bd9Sstevel@tonic-gate 		weight = pci_class_to_intr_weight(rdip);
5297c478bd9Sstevel@tonic-gate 		intr_dist_cpuid_add_device_weight(cpu_id, rdip, weight);
5307c478bd9Sstevel@tonic-gate 
531b0fc0e77Sgovinda 		ib_ino_add_intr(pci_p, ipil_p, ih_p);
5327c478bd9Sstevel@tonic-gate 		goto ino_done;
5337c478bd9Sstevel@tonic-gate 	}
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	if (hdlp->ih_pri == 0)
5367c478bd9Sstevel@tonic-gate 		hdlp->ih_pri = pci_class_to_pil(rdip);
5377c478bd9Sstevel@tonic-gate 
538b0fc0e77Sgovinda 	ipil_p = ib_new_ino_pil(ib_p, ino, hdlp->ih_pri, ih_p);
539b0fc0e77Sgovinda 	ino_p = ipil_p->ipil_ino_p;
540b0fc0e77Sgovinda 
5417c478bd9Sstevel@tonic-gate 	hdlp->ih_vector = CB_MONDO_TO_XMONDO(cb_p, mondo);
5427c478bd9Sstevel@tonic-gate 
54336fe4a92Segillett 	/* Store this global mondo */
54436fe4a92Segillett 	ino_p->ino_mondo = hdlp->ih_vector;
54536fe4a92Segillett 
5467c478bd9Sstevel@tonic-gate 	DEBUG2(DBG_A_INTX, dip, "pci_add_intr:  pil=0x%x mondo=0x%x\n",
5477c478bd9Sstevel@tonic-gate 	    hdlp->ih_pri, hdlp->ih_vector);
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp,
550b0fc0e77Sgovinda 	    (ddi_intr_handler_t *)pci_intr_wrapper, (caddr_t)ipil_p, NULL);
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	ret = i_ddi_add_ivintr(hdlp);
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	/*
5557c478bd9Sstevel@tonic-gate 	 * Restore original interrupt handler
5567c478bd9Sstevel@tonic-gate 	 * and arguments in interrupt handle.
5577c478bd9Sstevel@tonic-gate 	 */
5587c478bd9Sstevel@tonic-gate 	DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp, ih_p->ih_handler,
5597c478bd9Sstevel@tonic-gate 	    ih_p->ih_handler_arg1, ih_p->ih_handler_arg2);
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
5627c478bd9Sstevel@tonic-gate 		goto fail4;
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	/* Save the pil for this ino */
565b0fc0e77Sgovinda 	ipil_p->ipil_pil = hdlp->ih_pri;
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	/* clear and enable interrupt */
5687c478bd9Sstevel@tonic-gate 	IB_INO_INTR_CLEAR(ino_p->ino_clr_reg);
5697c478bd9Sstevel@tonic-gate 
570b0fc0e77Sgovinda 	/*
571b0fc0e77Sgovinda 	 * Select cpu and compute weight, saving both for sharing and removal.
572b0fc0e77Sgovinda 	 */
573b0fc0e77Sgovinda 	if (ipil_list == NULL)
574b0fc0e77Sgovinda 		ino_p->ino_cpuid = pci_intr_dist_cpuid(ib_p, ino_p);
575b0fc0e77Sgovinda 
576b0fc0e77Sgovinda 	cpu_id = ino_p->ino_cpuid;
5777c478bd9Sstevel@tonic-gate 	ino_p->ino_established = 1;
5787c478bd9Sstevel@tonic-gate 	weight = pci_class_to_intr_weight(rdip);
5797c478bd9Sstevel@tonic-gate 	intr_dist_cpuid_add_device_weight(cpu_id, rdip, weight);
5807c478bd9Sstevel@tonic-gate 
581b0fc0e77Sgovinda 	if (!ipil_list) {
582b0fc0e77Sgovinda 		*ino_p->ino_map_reg = ib_get_map_reg(mondo, cpu_id);
583b0fc0e77Sgovinda 		*ino_p->ino_map_reg;
584b0fc0e77Sgovinda 	}
5857c478bd9Sstevel@tonic-gate ino_done:
58609b1eac2SEvan Yan 	hdlp->ih_target = ino_p->ino_cpuid;
587b0fc0e77Sgovinda 	ih_p->ih_ipil_p = ipil_p;
5886d44af1bSesolom 	ih_p->ih_ksp = kstat_create("pci_intrs",
5896d44af1bSesolom 	    atomic_inc_32_nv(&pciintr_ks_instance), "config", "interrupts",
5906d44af1bSesolom 	    KSTAT_TYPE_NAMED,
5916d44af1bSesolom 	    sizeof (pciintr_ks_template) / sizeof (kstat_named_t),
5926d44af1bSesolom 	    KSTAT_FLAG_VIRTUAL);
5936d44af1bSesolom 	if (ih_p->ih_ksp != NULL) {
5946d44af1bSesolom 		ih_p->ih_ksp->ks_data_size += MAXPATHLEN * 2;
5956d44af1bSesolom 		ih_p->ih_ksp->ks_lock = &pciintr_ks_template_lock;
5966d44af1bSesolom 		ih_p->ih_ksp->ks_data = &pciintr_ks_template;
5976d44af1bSesolom 		ih_p->ih_ksp->ks_private = ih_p;
5986d44af1bSesolom 		ih_p->ih_ksp->ks_update = pci_ks_update;
5997c478bd9Sstevel@tonic-gate 		kstat_install(ih_p->ih_ksp);
6006d44af1bSesolom 	}
6017c478bd9Sstevel@tonic-gate 	ib_ino_map_reg_share(ib_p, ino, ino_p);
6027c478bd9Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
6037c478bd9Sstevel@tonic-gate done:
6047c478bd9Sstevel@tonic-gate 	DEBUG2(DBG_A_INTX, dip, "done! Interrupt 0x%x pil=%x\n",
60518061c65Sgd 	    hdlp->ih_vector, hdlp->ih_pri);
6067c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
6077c478bd9Sstevel@tonic-gate fail4:
608b0fc0e77Sgovinda 	ib_delete_ino_pil(ib_p, ipil_p);
6097c478bd9Sstevel@tonic-gate fail3:
6107c478bd9Sstevel@tonic-gate 	if (ih_p->ih_config_handle)
6117c478bd9Sstevel@tonic-gate 		pci_config_teardown(&ih_p->ih_config_handle);
6127c478bd9Sstevel@tonic-gate fail2:
6137c478bd9Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
6147c478bd9Sstevel@tonic-gate 	kmem_free(ih_p, sizeof (ih_t));
6157c478bd9Sstevel@tonic-gate fail1:
6167c478bd9Sstevel@tonic-gate 	DEBUG2(DBG_A_INTX, dip, "Failed! Interrupt 0x%x pil=%x\n",
61718061c65Sgd 	    hdlp->ih_vector, hdlp->ih_pri);
6187c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
6197c478bd9Sstevel@tonic-gate }
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate int
pci_remove_intr(dev_info_t * dip,dev_info_t * rdip,ddi_intr_handle_impl_t * hdlp)6227c478bd9Sstevel@tonic-gate pci_remove_intr(dev_info_t *dip, dev_info_t *rdip, ddi_intr_handle_impl_t *hdlp)
6237c478bd9Sstevel@tonic-gate {
624b0fc0e77Sgovinda 	pci_t		*pci_p = get_pci_soft_state(ddi_get_instance(dip));
625b0fc0e77Sgovinda 	ib_t		*ib_p = pci_p->pci_ib_p;
626b0fc0e77Sgovinda 	cb_t		*cb_p = pci_p->pci_cb_p;
627b0fc0e77Sgovinda 	ib_ino_t	ino;
628b0fc0e77Sgovinda 	ib_mondo_t	mondo;
629b0fc0e77Sgovinda 	ib_ino_info_t	*ino_p;	/* non-pulse only */
630b0fc0e77Sgovinda 	ib_ino_pil_t	*ipil_p; /* non-pulse only */
631b0fc0e77Sgovinda 	ih_t		*ih_p;	/* non-pulse only */
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	ino = IB_MONDO_TO_INO(hdlp->ih_vector);
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	DEBUG3(DBG_R_INTX, dip, "pci_rem_intr: rdip=%s%d ino=%x\n",
6367c478bd9Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip), ino);
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	if (hdlp->ih_vector & PCI_PULSE_INO) { /* pulse interrupt */
6397c478bd9Sstevel@tonic-gate 		volatile uint64_t *map_reg_addr;
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 		/*
6427c478bd9Sstevel@tonic-gate 		 * No weight was added by pci_add_intr for PCI_PULSE_INO
6437c478bd9Sstevel@tonic-gate 		 * because it is difficult to determine cpuid here.
6447c478bd9Sstevel@tonic-gate 		 */
6457c478bd9Sstevel@tonic-gate 		map_reg_addr = ib_intr_map_reg_addr(ib_p, ino);
6467c478bd9Sstevel@tonic-gate 		IB_INO_INTR_RESET(map_reg_addr);	/* disable intr */
6477c478bd9Sstevel@tonic-gate 		*map_reg_addr;
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 		mondo = pci_xlate_intr(dip, rdip, ib_p, ino);
6507c478bd9Sstevel@tonic-gate 		if (mondo == 0) {
6517c478bd9Sstevel@tonic-gate 			DEBUG1(DBG_R_INTX, dip,
65218061c65Sgd 			    "can't get mondo for ino %x\n", ino);
6537c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
6547c478bd9Sstevel@tonic-gate 		}
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 		if (hdlp->ih_pri == 0)
6577c478bd9Sstevel@tonic-gate 			hdlp->ih_pri = pci_class_to_pil(rdip);
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 		hdlp->ih_vector = CB_MONDO_TO_XMONDO(cb_p, mondo);
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 		DEBUG2(DBG_R_INTX, dip, "pci_rem_intr: pil=0x%x mondo=0x%x\n",
6627c478bd9Sstevel@tonic-gate 		    hdlp->ih_pri, hdlp->ih_vector);
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 		i_ddi_rem_ivintr(hdlp);
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 		DEBUG2(DBG_R_INTX, dip, "pulse success mondo=%x reg=%p\n",
66718061c65Sgd 		    mondo, map_reg_addr);
6687c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
6697c478bd9Sstevel@tonic-gate 	}
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	/* Translate the interrupt property */
6727c478bd9Sstevel@tonic-gate 	mondo = pci_xlate_intr(dip, rdip, pci_p->pci_ib_p, ino);
6737c478bd9Sstevel@tonic-gate 	if (mondo == 0) {
6747c478bd9Sstevel@tonic-gate 		DEBUG1(DBG_R_INTX, dip, "can't get mondo for ino %x\n", ino);
6757c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6767c478bd9Sstevel@tonic-gate 	}
6777c478bd9Sstevel@tonic-gate 	ino = IB_MONDO_TO_INO(mondo);
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
6807c478bd9Sstevel@tonic-gate 	ino_p = ib_locate_ino(ib_p, ino);
6817c478bd9Sstevel@tonic-gate 	if (!ino_p) {
6827c478bd9Sstevel@tonic-gate 		int r = cb_remove_xintr(pci_p, dip, rdip, ino, mondo);
6837c478bd9Sstevel@tonic-gate 		if (r != DDI_SUCCESS)
6847c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d-xintr: ino %x is invalid",
6857c478bd9Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip), ino);
6867c478bd9Sstevel@tonic-gate 		mutex_exit(&ib_p->ib_ino_lst_mutex);
6877c478bd9Sstevel@tonic-gate 		return (r);
6887c478bd9Sstevel@tonic-gate 	}
6897c478bd9Sstevel@tonic-gate 
690b0fc0e77Sgovinda 	ipil_p = ib_ino_locate_ipil(ino_p, hdlp->ih_pri);
691b0fc0e77Sgovinda 	ih_p = ib_intr_locate_ih(ipil_p, rdip, hdlp->ih_inum);
692b0fc0e77Sgovinda 	ib_ino_rem_intr(pci_p, ipil_p, ih_p);
6937c478bd9Sstevel@tonic-gate 	intr_dist_cpuid_rem_device_weight(ino_p->ino_cpuid, rdip);
694b0fc0e77Sgovinda 	if (ipil_p->ipil_ih_size == 0) {
6957c478bd9Sstevel@tonic-gate 		IB_INO_INTR_PEND(ib_clear_intr_reg_addr(ib_p, ino));
6967c478bd9Sstevel@tonic-gate 		hdlp->ih_vector = CB_MONDO_TO_XMONDO(cb_p, mondo);
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 		i_ddi_rem_ivintr(hdlp);
699b0fc0e77Sgovinda 		ib_delete_ino_pil(ib_p, ipil_p);
7007c478bd9Sstevel@tonic-gate 	}
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	/* re-enable interrupt only if mapping register still shared */
703b0fc0e77Sgovinda 	if (ib_ino_map_reg_unshare(ib_p, ino, ino_p) || ino_p->ino_ipil_size) {
7047c478bd9Sstevel@tonic-gate 		IB_INO_INTR_ON(ino_p->ino_map_reg);
7057c478bd9Sstevel@tonic-gate 		*ino_p->ino_map_reg;
7067c478bd9Sstevel@tonic-gate 	}
7077c478bd9Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
7087c478bd9Sstevel@tonic-gate 
709b0fc0e77Sgovinda 	if (ino_p->ino_ipil_size == 0)
7107c478bd9Sstevel@tonic-gate 		kmem_free(ino_p, sizeof (ib_ino_info_t));
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	DEBUG1(DBG_R_INTX, dip, "success! mondo=%x\n", mondo);
7137c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
7147c478bd9Sstevel@tonic-gate }
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate /*
7177c478bd9Sstevel@tonic-gate  * free the pci_inos array allocated during pci_intr_setup. the actual
7187c478bd9Sstevel@tonic-gate  * interrupts are torn down by their respective block destroy routines:
7197c478bd9Sstevel@tonic-gate  * cb_destroy, pbm_destroy, and ib_destroy.
7207c478bd9Sstevel@tonic-gate  */
7217c478bd9Sstevel@tonic-gate void
pci_intr_teardown(pci_t * pci_p)7227c478bd9Sstevel@tonic-gate pci_intr_teardown(pci_t *pci_p)
7237c478bd9Sstevel@tonic-gate {
7247c478bd9Sstevel@tonic-gate 	kmem_free(pci_p->pci_inos, pci_p->pci_inos_len);
7257c478bd9Sstevel@tonic-gate 	pci_p->pci_inos = NULL;
7267c478bd9Sstevel@tonic-gate 	pci_p->pci_inos_len = 0;
7277c478bd9Sstevel@tonic-gate }
728