xref: /onnv-gate/usr/src/uts/sun4u/io/pci/pci_intr.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * PCI nexus interrupt handling:
31*0Sstevel@tonic-gate  *	PCI device interrupt handler wrapper
32*0Sstevel@tonic-gate  *	pil lookup routine
33*0Sstevel@tonic-gate  *	PCI device interrupt related initchild code
34*0Sstevel@tonic-gate  */
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #include <sys/types.h>
37*0Sstevel@tonic-gate #include <sys/kmem.h>
38*0Sstevel@tonic-gate #include <sys/async.h>
39*0Sstevel@tonic-gate #include <sys/spl.h>
40*0Sstevel@tonic-gate #include <sys/sunddi.h>
41*0Sstevel@tonic-gate #include <sys/machsystm.h>	/* e_ddi_nodeid_to_dip() */
42*0Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
43*0Sstevel@tonic-gate #include <sys/pci/pci_obj.h>
44*0Sstevel@tonic-gate #include <sys/sdt.h>
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate #ifdef _STARFIRE
47*0Sstevel@tonic-gate #include <sys/starfire.h>
48*0Sstevel@tonic-gate #endif /* _STARFIRE */
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate /*
51*0Sstevel@tonic-gate  * interrupt jabber:
52*0Sstevel@tonic-gate  *
53*0Sstevel@tonic-gate  * When an interrupt line is jabbering, every time the state machine for the
54*0Sstevel@tonic-gate  * associated ino is idled, a new mondo will be sent and the ino will go into
55*0Sstevel@tonic-gate  * the pending state again. The mondo will cause a new call to
56*0Sstevel@tonic-gate  * pci_intr_wrapper() which normally idles the ino's state machine which would
57*0Sstevel@tonic-gate  * precipitate another trip round the loop.
58*0Sstevel@tonic-gate  * The loop can be broken by preventing the ino's state machine from being
59*0Sstevel@tonic-gate  * idled when an interrupt line is jabbering. See the comment at the
60*0Sstevel@tonic-gate  * beginning of pci_intr_wrapper() explaining how the 'interrupt jabber
61*0Sstevel@tonic-gate  * protection' code does this.
62*0Sstevel@tonic-gate  */
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate /*LINTLIBRARY*/
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate #ifdef NOT_DEFINED
67*0Sstevel@tonic-gate /*
68*0Sstevel@tonic-gate  * This array is used to determine the sparc PIL at the which the
69*0Sstevel@tonic-gate  * handler for a given INO will execute.  This table is for onboard
70*0Sstevel@tonic-gate  * devices only.  A different scheme will be used for plug-in cards.
71*0Sstevel@tonic-gate  */
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate uint_t ino_to_pil[] = {
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate 	/* pil */		/* ino */
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate 	0, 0, 0, 0,  		/* 0x00 - 0x03: bus A slot 0 int#A, B, C, D */
78*0Sstevel@tonic-gate 	0, 0, 0, 0,		/* 0x04 - 0x07: bus A slot 1 int#A, B, C, D */
79*0Sstevel@tonic-gate 	0, 0, 0, 0,  		/* 0x08 - 0x0B: unused */
80*0Sstevel@tonic-gate 	0, 0, 0, 0,		/* 0x0C - 0x0F: unused */
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate 	0, 0, 0, 0,  		/* 0x10 - 0x13: bus B slot 0 int#A, B, C, D */
83*0Sstevel@tonic-gate 	0, 0, 0, 0,		/* 0x14 - 0x17: bus B slot 1 int#A, B, C, D */
84*0Sstevel@tonic-gate 	0, 0, 0, 0,  		/* 0x18 - 0x1B: bus B slot 2 int#A, B, C, D */
85*0Sstevel@tonic-gate 	4, 0, 0, 0,		/* 0x1C - 0x1F: bus B slot 3 int#A, B, C, D */
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 	4,			/* 0x20: SCSI */
88*0Sstevel@tonic-gate 	6,			/* 0x21: ethernet */
89*0Sstevel@tonic-gate 	3,			/* 0x22: parallel port */
90*0Sstevel@tonic-gate 	9,			/* 0x23: audio record */
91*0Sstevel@tonic-gate 	9,			/* 0x24: audio playback */
92*0Sstevel@tonic-gate 	14,			/* 0x25: power fail */
93*0Sstevel@tonic-gate 	4,			/* 0x26: 2nd SCSI */
94*0Sstevel@tonic-gate 	8,			/* 0x27: floppy */
95*0Sstevel@tonic-gate 	14,			/* 0x28: thermal warning */
96*0Sstevel@tonic-gate 	12,			/* 0x29: keyboard */
97*0Sstevel@tonic-gate 	12,			/* 0x2A: mouse */
98*0Sstevel@tonic-gate 	12,			/* 0x2B: serial */
99*0Sstevel@tonic-gate 	0,			/* 0x2C: timer/counter 0 */
100*0Sstevel@tonic-gate 	0,			/* 0x2D: timer/counter 1 */
101*0Sstevel@tonic-gate 	14,			/* 0x2E: uncorrectable ECC errors */
102*0Sstevel@tonic-gate 	14,			/* 0x2F: correctable ECC errors */
103*0Sstevel@tonic-gate 	14,			/* 0x30: PCI bus A error */
104*0Sstevel@tonic-gate 	14,			/* 0x31: PCI bus B error */
105*0Sstevel@tonic-gate 	14,			/* 0x32: power management wakeup */
106*0Sstevel@tonic-gate 	14,			/* 0x33 */
107*0Sstevel@tonic-gate 	14,			/* 0x34 */
108*0Sstevel@tonic-gate 	14,			/* 0x35 */
109*0Sstevel@tonic-gate 	14,			/* 0x36 */
110*0Sstevel@tonic-gate 	14,			/* 0x37 */
111*0Sstevel@tonic-gate 	14,			/* 0x38 */
112*0Sstevel@tonic-gate 	14,			/* 0x39 */
113*0Sstevel@tonic-gate 	14,			/* 0x3a */
114*0Sstevel@tonic-gate 	14,			/* 0x3b */
115*0Sstevel@tonic-gate 	14,			/* 0x3c */
116*0Sstevel@tonic-gate 	14,			/* 0x3d */
117*0Sstevel@tonic-gate 	14,			/* 0x3e */
118*0Sstevel@tonic-gate 	14,			/* 0x3f */
119*0Sstevel@tonic-gate 	14			/* 0x40 */
120*0Sstevel@tonic-gate };
121*0Sstevel@tonic-gate #endif /* NOT_DEFINED */
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate #define	PCI_SIMBA_VENID		0x108e	/* vendor id for simba */
125*0Sstevel@tonic-gate #define	PCI_SIMBA_DEVID		0x5000	/* device id for simba */
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate /*
128*0Sstevel@tonic-gate  * map_pcidev_cfg_reg - create mapping to pci device configuration registers
129*0Sstevel@tonic-gate  *			if we have a simba AND a pci to pci bridge along the
130*0Sstevel@tonic-gate  *			device path.
131*0Sstevel@tonic-gate  *			Called with corresponding mutexes held!!
132*0Sstevel@tonic-gate  *
133*0Sstevel@tonic-gate  * XXX	  XXX	XXX	The purpose of this routine is to overcome a hardware
134*0Sstevel@tonic-gate  *			defect in Sabre CPU and Simba bridge configuration
135*0Sstevel@tonic-gate  *			which does not drain DMA write data stalled in
136*0Sstevel@tonic-gate  *			PCI to PCI bridges (such as the DEC bridge) beyond
137*0Sstevel@tonic-gate  *			Simba. This routine will setup the data structures
138*0Sstevel@tonic-gate  *			to allow the pci_intr_wrapper to perform a manual
139*0Sstevel@tonic-gate  *			drain data operation before passing the control to
140*0Sstevel@tonic-gate  *			interrupt handlers of device drivers.
141*0Sstevel@tonic-gate  * return value:
142*0Sstevel@tonic-gate  * DDI_SUCCESS
143*0Sstevel@tonic-gate  * DDI_FAILURE		if unable to create mapping
144*0Sstevel@tonic-gate  */
145*0Sstevel@tonic-gate static int
146*0Sstevel@tonic-gate map_pcidev_cfg_reg(dev_info_t *dip, dev_info_t *rdip, ddi_acc_handle_t *hdl_p)
147*0Sstevel@tonic-gate {
148*0Sstevel@tonic-gate 	dev_info_t *cdip;
149*0Sstevel@tonic-gate 	dev_info_t *pci_dip = NULL;
150*0Sstevel@tonic-gate 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
151*0Sstevel@tonic-gate 	int simba_found = 0, pci_bridge_found = 0;
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 	for (cdip = rdip; cdip && cdip != dip; cdip = ddi_get_parent(cdip)) {
154*0Sstevel@tonic-gate 		ddi_acc_handle_t config_handle;
155*0Sstevel@tonic-gate 		uint32_t vendor_id = ddi_getprop(DDI_DEV_T_ANY, cdip,
156*0Sstevel@tonic-gate 			DDI_PROP_DONTPASS, "vendor-id", 0xffff);
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 		DEBUG4(DBG_A_INTX, pci_p->pci_dip,
159*0Sstevel@tonic-gate 			"map dev cfg reg for %s%d: @%s%d\n",
160*0Sstevel@tonic-gate 			ddi_driver_name(rdip), ddi_get_instance(rdip),
161*0Sstevel@tonic-gate 			ddi_driver_name(cdip), ddi_get_instance(cdip));
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 		if (ddi_prop_exists(DDI_DEV_T_ANY, cdip, DDI_PROP_DONTPASS,
164*0Sstevel@tonic-gate 				"no-dma-interrupt-sync"))
165*0Sstevel@tonic-gate 			continue;
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 		/* continue to search up-stream if not a PCI device */
168*0Sstevel@tonic-gate 		if (vendor_id == 0xffff)
169*0Sstevel@tonic-gate 			continue;
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 		/* record the deepest pci device */
172*0Sstevel@tonic-gate 		if (!pci_dip)
173*0Sstevel@tonic-gate 			pci_dip = cdip;
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 		/* look for simba */
176*0Sstevel@tonic-gate 		if (vendor_id == PCI_SIMBA_VENID) {
177*0Sstevel@tonic-gate 			uint32_t device_id = ddi_getprop(DDI_DEV_T_ANY,
178*0Sstevel@tonic-gate 			    cdip, DDI_PROP_DONTPASS, "device-id", -1);
179*0Sstevel@tonic-gate 			if (device_id == PCI_SIMBA_DEVID) {
180*0Sstevel@tonic-gate 				simba_found = 1;
181*0Sstevel@tonic-gate 				DEBUG0(DBG_A_INTX, pci_p->pci_dip,
182*0Sstevel@tonic-gate 					"\tFound simba\n");
183*0Sstevel@tonic-gate 				continue; /* do not check bridge if simba */
184*0Sstevel@tonic-gate 			}
185*0Sstevel@tonic-gate 		}
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 		/* look for pci to pci bridge */
188*0Sstevel@tonic-gate 		if (pci_config_setup(cdip, &config_handle) != DDI_SUCCESS) {
189*0Sstevel@tonic-gate 			cmn_err(CE_WARN,
190*0Sstevel@tonic-gate 			    "%s%d: can't get brdg cfg space for %s%d\n",
191*0Sstevel@tonic-gate 				ddi_driver_name(dip), ddi_get_instance(dip),
192*0Sstevel@tonic-gate 				ddi_driver_name(cdip), ddi_get_instance(cdip));
193*0Sstevel@tonic-gate 			return (DDI_FAILURE);
194*0Sstevel@tonic-gate 		}
195*0Sstevel@tonic-gate 		if (pci_config_get8(config_handle, PCI_CONF_BASCLASS)
196*0Sstevel@tonic-gate 		    == PCI_CLASS_BRIDGE) {
197*0Sstevel@tonic-gate 			DEBUG0(DBG_A_INTX, pci_p->pci_dip,
198*0Sstevel@tonic-gate 				"\tFound PCI to xBus bridge\n");
199*0Sstevel@tonic-gate 			pci_bridge_found = 1;
200*0Sstevel@tonic-gate 		}
201*0Sstevel@tonic-gate 		pci_config_teardown(&config_handle);
202*0Sstevel@tonic-gate 	}
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 	if (!pci_bridge_found)
205*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
206*0Sstevel@tonic-gate 	if (!simba_found && (CHIP_TYPE(pci_p) < PCI_CHIP_SCHIZO))
207*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
208*0Sstevel@tonic-gate 	if (pci_config_setup(pci_dip, hdl_p) != DDI_SUCCESS) {
209*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: can not get config space for %s%d\n",
210*0Sstevel@tonic-gate 			ddi_driver_name(dip), ddi_get_instance(dip),
211*0Sstevel@tonic-gate 			ddi_driver_name(cdip), ddi_get_instance(cdip));
212*0Sstevel@tonic-gate 		return (DDI_FAILURE);
213*0Sstevel@tonic-gate 	}
214*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
215*0Sstevel@tonic-gate }
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate /*
218*0Sstevel@tonic-gate  * If the unclaimed interrupt count has reached the limit set by
219*0Sstevel@tonic-gate  * pci_unclaimed_intr_max within the time limit, then all interrupts
220*0Sstevel@tonic-gate  * on this ino is blocked by not idling the interrupt state machine.
221*0Sstevel@tonic-gate  */
222*0Sstevel@tonic-gate static int
223*0Sstevel@tonic-gate pci_spurintr(ib_ino_info_t *ino_p) {
224*0Sstevel@tonic-gate 	int i;
225*0Sstevel@tonic-gate 	ih_t *ih_p = ino_p->ino_ih_start;
226*0Sstevel@tonic-gate 	pci_t *pci_p = ino_p->ino_ib_p->ib_pci_p;
227*0Sstevel@tonic-gate 	char *err_fmt_str;
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate 	if (ino_p->ino_unclaimed > pci_unclaimed_intr_max)
230*0Sstevel@tonic-gate 		return (DDI_INTR_CLAIMED);
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 	if (!ino_p->ino_unclaimed)
233*0Sstevel@tonic-gate 		ino_p->ino_spurintr_begin = ddi_get_lbolt();
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 	ino_p->ino_unclaimed++;
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate 	if (ino_p->ino_unclaimed <= pci_unclaimed_intr_max)
238*0Sstevel@tonic-gate 		goto clear;
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate 	if (drv_hztousec(ddi_get_lbolt() - ino_p->ino_spurintr_begin)
241*0Sstevel@tonic-gate 	    > pci_spurintr_duration) {
242*0Sstevel@tonic-gate 		ino_p->ino_unclaimed = 0;
243*0Sstevel@tonic-gate 		goto clear;
244*0Sstevel@tonic-gate 	}
245*0Sstevel@tonic-gate 	err_fmt_str = "%s%d: ino 0x%x blocked";
246*0Sstevel@tonic-gate 	goto warn;
247*0Sstevel@tonic-gate clear:
248*0Sstevel@tonic-gate 	IB_INO_INTR_CLEAR(ino_p->ino_clr_reg);  /* clear the pending state */
249*0Sstevel@tonic-gate 	if (!pci_spurintr_msgs) /* tomatillo errata #71 spurious mondo */
250*0Sstevel@tonic-gate 		return (DDI_INTR_CLAIMED);
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate 	err_fmt_str = "!%s%d: spurious interrupt from ino 0x%x";
253*0Sstevel@tonic-gate warn:
254*0Sstevel@tonic-gate 	cmn_err(CE_WARN, err_fmt_str, NAMEINST(pci_p->pci_dip), ino_p->ino_ino);
255*0Sstevel@tonic-gate 	for (i = 0; i < ino_p->ino_ih_size; i++, ih_p = ih_p->ih_next)
256*0Sstevel@tonic-gate 		cmn_err(CE_CONT, "!%s-%d#%x ", NAMEINST(ih_p->ih_dip),
257*0Sstevel@tonic-gate 		    ih_p->ih_inum);
258*0Sstevel@tonic-gate 	cmn_err(CE_CONT, "!\n");
259*0Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
260*0Sstevel@tonic-gate }
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate /*
263*0Sstevel@tonic-gate  * pci_intr_wrapper
264*0Sstevel@tonic-gate  *
265*0Sstevel@tonic-gate  * This routine is used as wrapper around interrupt handlers installed by child
266*0Sstevel@tonic-gate  * device drivers.  This routine invokes the driver interrupt handlers and
267*0Sstevel@tonic-gate  * examines the return codes.
268*0Sstevel@tonic-gate  * There is a count of unclaimed interrupts kept on a per-ino basis. If at
269*0Sstevel@tonic-gate  * least one handler claims the interrupt then the counter is halved and the
270*0Sstevel@tonic-gate  * interrupt state machine is idled. If no handler claims the interrupt then
271*0Sstevel@tonic-gate  * the counter is incremented by one and the state machine is idled.
272*0Sstevel@tonic-gate  * If the count ever reaches the limit value set by pci_unclaimed_intr_max
273*0Sstevel@tonic-gate  * then the interrupt state machine is not idled thus preventing any further
274*0Sstevel@tonic-gate  * interrupts on that ino. The state machine will only be idled again if a
275*0Sstevel@tonic-gate  * handler is subsequently added or removed.
276*0Sstevel@tonic-gate  *
277*0Sstevel@tonic-gate  * return value: DDI_INTR_CLAIMED if any handlers claimed the interrupt,
278*0Sstevel@tonic-gate  * DDI_INTR_UNCLAIMED otherwise.
279*0Sstevel@tonic-gate  */
280*0Sstevel@tonic-gate 
281*0Sstevel@tonic-gate extern uint64_t intr_get_time(void);
282*0Sstevel@tonic-gate 
283*0Sstevel@tonic-gate uint_t
284*0Sstevel@tonic-gate pci_intr_wrapper(caddr_t arg)
285*0Sstevel@tonic-gate {
286*0Sstevel@tonic-gate 	ib_ino_info_t *ino_p = (ib_ino_info_t *)arg;
287*0Sstevel@tonic-gate 	uint_t result = 0, r;
288*0Sstevel@tonic-gate 	pci_t *pci_p = ino_p->ino_ib_p->ib_pci_p;
289*0Sstevel@tonic-gate 	pbm_t *pbm_p = pci_p->pci_pbm_p;
290*0Sstevel@tonic-gate 	ih_t *ih_p = ino_p->ino_ih_start;
291*0Sstevel@tonic-gate 	int i;
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate 	for (i = 0; i < ino_p->ino_ih_size; i++, ih_p = ih_p->ih_next) {
294*0Sstevel@tonic-gate 		dev_info_t *dip = ih_p->ih_dip;
295*0Sstevel@tonic-gate 		uint_t (*handler)() = ih_p->ih_handler;
296*0Sstevel@tonic-gate 		caddr_t arg1 = ih_p->ih_handler_arg1;
297*0Sstevel@tonic-gate 		caddr_t arg2 = ih_p->ih_handler_arg2;
298*0Sstevel@tonic-gate 		ddi_acc_handle_t cfg_hdl = ih_p->ih_config_handle;
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 		if (pci_intr_dma_sync && cfg_hdl && pbm_p->pbm_sync_reg_pa) {
301*0Sstevel@tonic-gate 			(void) pci_config_get16(cfg_hdl, PCI_CONF_VENID);
302*0Sstevel@tonic-gate 			pci_pbm_dma_sync(pbm_p, ino_p->ino_ino);
303*0Sstevel@tonic-gate 		}
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate 		if (ih_p->ih_intr_state == PCI_INTR_STATE_DISABLE) {
306*0Sstevel@tonic-gate 			DEBUG3(DBG_INTR, pci_p->pci_dip,
307*0Sstevel@tonic-gate 			    "pci_intr_wrapper: %s%d interrupt %d is disabled\n",
308*0Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip),
309*0Sstevel@tonic-gate 			    ino_p->ino_ino);
310*0Sstevel@tonic-gate 
311*0Sstevel@tonic-gate 			continue;
312*0Sstevel@tonic-gate 		}
313*0Sstevel@tonic-gate 
314*0Sstevel@tonic-gate 		DTRACE_PROBE4(interrupt__start, dev_info_t, dip,
315*0Sstevel@tonic-gate 		    void *, handler, caddr_t, arg1, caddr_t, arg2);
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate 		r = (*handler)(arg1, arg2);
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate 		/*
320*0Sstevel@tonic-gate 		 * Account for time used by this interrupt. Protect against
321*0Sstevel@tonic-gate 		 * conflicting writes to ih_ticks from ib_intr_dist_all() by
322*0Sstevel@tonic-gate 		 * using atomic ops.
323*0Sstevel@tonic-gate 		 */
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate 		if (ino_p->ino_pil <= LOCK_LEVEL)
326*0Sstevel@tonic-gate 			atomic_add_64(&ih_p->ih_ticks, intr_get_time());
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate 		DTRACE_PROBE4(interrupt__complete, dev_info_t, dip,
329*0Sstevel@tonic-gate 		    void *, handler, caddr_t, arg1, int, r);
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate 		result += r;
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate 		if (pci_check_all_handlers)
334*0Sstevel@tonic-gate 			continue;
335*0Sstevel@tonic-gate 		if (result)
336*0Sstevel@tonic-gate 			break;
337*0Sstevel@tonic-gate 	}
338*0Sstevel@tonic-gate 
339*0Sstevel@tonic-gate 	if (!result)
340*0Sstevel@tonic-gate 		return (pci_spurintr(ino_p));
341*0Sstevel@tonic-gate 
342*0Sstevel@tonic-gate 	ino_p->ino_unclaimed = 0;
343*0Sstevel@tonic-gate 	IB_INO_INTR_CLEAR(ino_p->ino_clr_reg);  /* clear the pending state */
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
346*0Sstevel@tonic-gate }
347*0Sstevel@tonic-gate 
348*0Sstevel@tonic-gate dev_info_t *
349*0Sstevel@tonic-gate get_my_childs_dip(dev_info_t *dip, dev_info_t *rdip)
350*0Sstevel@tonic-gate {
351*0Sstevel@tonic-gate 	dev_info_t *cdip = rdip;
352*0Sstevel@tonic-gate 
353*0Sstevel@tonic-gate 	for (; ddi_get_parent(cdip) != dip; cdip = ddi_get_parent(cdip))
354*0Sstevel@tonic-gate 		;
355*0Sstevel@tonic-gate 
356*0Sstevel@tonic-gate 	return (cdip);
357*0Sstevel@tonic-gate }
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate /* default class to pil value mapping */
360*0Sstevel@tonic-gate pci_class_val_t pci_default_pil [] = {
361*0Sstevel@tonic-gate 	{0x000000, 0xff0000, 0x1},	/* Class code for pre-2.0 devices */
362*0Sstevel@tonic-gate 	{0x010000, 0xff0000, 0x4},	/* Mass Storage Controller */
363*0Sstevel@tonic-gate 	{0x020000, 0xff0000, 0x6},	/* Network Controller */
364*0Sstevel@tonic-gate 	{0x030000, 0xff0000, 0x9},	/* Display Controller */
365*0Sstevel@tonic-gate 	{0x040000, 0xff0000, 0x9},	/* Multimedia Controller */
366*0Sstevel@tonic-gate 	{0x050000, 0xff0000, 0xb},	/* Memory Controller */
367*0Sstevel@tonic-gate 	{0x060000, 0xff0000, 0xb},	/* Bridge Controller */
368*0Sstevel@tonic-gate 	{0x0c0000, 0xffff00, 0x9},	/* Serial Bus, FireWire (IEEE 1394) */
369*0Sstevel@tonic-gate 	{0x0c0100, 0xffff00, 0x4},	/* Serial Bus, ACCESS.bus */
370*0Sstevel@tonic-gate 	{0x0c0200, 0xffff00, 0x4},	/* Serial Bus, SSA */
371*0Sstevel@tonic-gate 	{0x0c0300, 0xffff00, 0x9},	/* Serial Bus Universal Serial Bus */
372*0Sstevel@tonic-gate 	{0x0c0400, 0xffff00, 0x6},	/* Serial Bus, Fibre Channel */
373*0Sstevel@tonic-gate 	{0x0c0600, 0xffff00, 0x6}	/* Serial Bus, Infiniband */
374*0Sstevel@tonic-gate };
375*0Sstevel@tonic-gate 
376*0Sstevel@tonic-gate /*
377*0Sstevel@tonic-gate  * Default class to intr_weight value mapping (% of CPU).  A driver.conf
378*0Sstevel@tonic-gate  * entry on or above the pci node like
379*0Sstevel@tonic-gate  *
380*0Sstevel@tonic-gate  *	pci-class-intr-weights= 0x020000, 0xff0000, 30;
381*0Sstevel@tonic-gate  *
382*0Sstevel@tonic-gate  * can be used to augment or override entries in the default table below.
383*0Sstevel@tonic-gate  *
384*0Sstevel@tonic-gate  * NB: The values below give NICs preference on redistribution, and provide
385*0Sstevel@tonic-gate  * NICs some isolation from other interrupt sources. We need better interfaces
386*0Sstevel@tonic-gate  * that allow the NIC driver to identify a specific NIC instance as high
387*0Sstevel@tonic-gate  * bandwidth, and thus deserving of separation from other low bandwidth
388*0Sstevel@tonic-gate  * NICs additional isolation from other interrupt sources.
389*0Sstevel@tonic-gate  *
390*0Sstevel@tonic-gate  * NB: We treat Infiniband like a NIC.
391*0Sstevel@tonic-gate  */
392*0Sstevel@tonic-gate pci_class_val_t pci_default_intr_weight [] = {
393*0Sstevel@tonic-gate 	{0x020000, 0xff0000, 35},	/* Network Controller */
394*0Sstevel@tonic-gate 	{0x010000, 0xff0000, 10},	/* Mass Storage Controller */
395*0Sstevel@tonic-gate 	{0x0c0400, 0xffff00, 10},	/* Serial Bus, Fibre Channel */
396*0Sstevel@tonic-gate 	{0x0c0600, 0xffff00, 50}	/* Serial Bus, Infiniband */
397*0Sstevel@tonic-gate };
398*0Sstevel@tonic-gate 
399*0Sstevel@tonic-gate static uint32_t
400*0Sstevel@tonic-gate pci_match_class_val(uint32_t key, pci_class_val_t *rec_p, int nrec,
401*0Sstevel@tonic-gate     uint32_t default_val)
402*0Sstevel@tonic-gate {
403*0Sstevel@tonic-gate 	int i;
404*0Sstevel@tonic-gate 
405*0Sstevel@tonic-gate 	for (i = 0; i < nrec; rec_p++, i++) {
406*0Sstevel@tonic-gate 		if ((rec_p->class_code & rec_p->class_mask) ==
407*0Sstevel@tonic-gate 		    (key & rec_p->class_mask))
408*0Sstevel@tonic-gate 			return (rec_p->class_val);
409*0Sstevel@tonic-gate 	}
410*0Sstevel@tonic-gate 
411*0Sstevel@tonic-gate 	return (default_val);
412*0Sstevel@tonic-gate }
413*0Sstevel@tonic-gate 
414*0Sstevel@tonic-gate /*
415*0Sstevel@tonic-gate  * Return the configuration value, based on class code and sub class code,
416*0Sstevel@tonic-gate  * from the specified property based or default pci_class_val_t table.
417*0Sstevel@tonic-gate  */
418*0Sstevel@tonic-gate uint32_t
419*0Sstevel@tonic-gate pci_class_to_val(dev_info_t *rdip, char *property_name, pci_class_val_t *rec_p,
420*0Sstevel@tonic-gate     int nrec, uint32_t default_val)
421*0Sstevel@tonic-gate {
422*0Sstevel@tonic-gate 	int property_len;
423*0Sstevel@tonic-gate 	uint32_t class_code;
424*0Sstevel@tonic-gate 	pci_class_val_t *conf;
425*0Sstevel@tonic-gate 	uint32_t val = default_val;
426*0Sstevel@tonic-gate 
427*0Sstevel@tonic-gate 	/*
428*0Sstevel@tonic-gate 	 * Use the "class-code" property to get the base and sub class
429*0Sstevel@tonic-gate 	 * codes for the requesting device.
430*0Sstevel@tonic-gate 	 */
431*0Sstevel@tonic-gate 	class_code = (uint32_t)ddi_prop_get_int(DDI_DEV_T_ANY, rdip,
432*0Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "class-code", -1);
433*0Sstevel@tonic-gate 
434*0Sstevel@tonic-gate 	if (class_code == -1)
435*0Sstevel@tonic-gate 		return (val);
436*0Sstevel@tonic-gate 
437*0Sstevel@tonic-gate 	/* look up the val from the default table */
438*0Sstevel@tonic-gate 	val = pci_match_class_val(class_code, rec_p, nrec, val);
439*0Sstevel@tonic-gate 
440*0Sstevel@tonic-gate 
441*0Sstevel@tonic-gate 	/* see if there is a more specific property specified value */
442*0Sstevel@tonic-gate 	if (ddi_getlongprop(DDI_DEV_T_ANY, rdip, DDI_PROP_NOTPROM,
443*0Sstevel@tonic-gate 	    property_name, (caddr_t)&conf, &property_len))
444*0Sstevel@tonic-gate 			return (val);
445*0Sstevel@tonic-gate 
446*0Sstevel@tonic-gate 	if ((property_len % sizeof (pci_class_val_t)) == 0)
447*0Sstevel@tonic-gate 		val = pci_match_class_val(class_code, conf,
448*0Sstevel@tonic-gate 		    property_len / sizeof (pci_class_val_t), val);
449*0Sstevel@tonic-gate 	kmem_free(conf, property_len);
450*0Sstevel@tonic-gate 	return (val);
451*0Sstevel@tonic-gate }
452*0Sstevel@tonic-gate 
453*0Sstevel@tonic-gate /* pci_class_to_pil: return the pil for a given PCI device. */
454*0Sstevel@tonic-gate uint32_t
455*0Sstevel@tonic-gate pci_class_to_pil(dev_info_t *rdip)
456*0Sstevel@tonic-gate {
457*0Sstevel@tonic-gate 	uint32_t pil;
458*0Sstevel@tonic-gate 
459*0Sstevel@tonic-gate 	/* default pil is 0 (uninitialized) */
460*0Sstevel@tonic-gate 	pil = pci_class_to_val(rdip,
461*0Sstevel@tonic-gate 	    "pci-class-priorities", pci_default_pil,
462*0Sstevel@tonic-gate 	    sizeof (pci_default_pil) / sizeof (pci_class_val_t), 0);
463*0Sstevel@tonic-gate 
464*0Sstevel@tonic-gate 	/* range check the result */
465*0Sstevel@tonic-gate 	if (pil >= 0xf)
466*0Sstevel@tonic-gate 		pil = 0;
467*0Sstevel@tonic-gate 
468*0Sstevel@tonic-gate 	return (pil);
469*0Sstevel@tonic-gate }
470*0Sstevel@tonic-gate 
471*0Sstevel@tonic-gate /* pci_class_to_intr_weight: return the intr_weight for a given PCI device. */
472*0Sstevel@tonic-gate int32_t
473*0Sstevel@tonic-gate pci_class_to_intr_weight(dev_info_t *rdip)
474*0Sstevel@tonic-gate {
475*0Sstevel@tonic-gate 	int32_t intr_weight;
476*0Sstevel@tonic-gate 
477*0Sstevel@tonic-gate 	/* default weight is 0% */
478*0Sstevel@tonic-gate 	intr_weight = pci_class_to_val(rdip,
479*0Sstevel@tonic-gate 	    "pci-class-intr-weights", pci_default_intr_weight,
480*0Sstevel@tonic-gate 	    sizeof (pci_default_intr_weight) / sizeof (pci_class_val_t), 0);
481*0Sstevel@tonic-gate 
482*0Sstevel@tonic-gate 	/* range check the result */
483*0Sstevel@tonic-gate 	if (intr_weight < 0)
484*0Sstevel@tonic-gate 		intr_weight = 0;
485*0Sstevel@tonic-gate 	if (intr_weight > 1000)
486*0Sstevel@tonic-gate 		intr_weight = 1000;
487*0Sstevel@tonic-gate 
488*0Sstevel@tonic-gate 	return (intr_weight);
489*0Sstevel@tonic-gate }
490*0Sstevel@tonic-gate 
491*0Sstevel@tonic-gate int
492*0Sstevel@tonic-gate pci_add_intr(dev_info_t *dip, dev_info_t *rdip, ddi_intr_handle_impl_t *hdlp)
493*0Sstevel@tonic-gate {
494*0Sstevel@tonic-gate 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
495*0Sstevel@tonic-gate 	ib_t *ib_p = pci_p->pci_ib_p;
496*0Sstevel@tonic-gate 	cb_t *cb_p = pci_p->pci_cb_p;
497*0Sstevel@tonic-gate 	ih_t *ih_p;
498*0Sstevel@tonic-gate 	ib_ino_t ino;
499*0Sstevel@tonic-gate 	ib_ino_info_t *ino_p;		/* pulse interrupts have no ino */
500*0Sstevel@tonic-gate 	ib_mondo_t mondo;
501*0Sstevel@tonic-gate 	uint32_t cpu_id;
502*0Sstevel@tonic-gate 	int ret;
503*0Sstevel@tonic-gate 	int32_t weight;
504*0Sstevel@tonic-gate 
505*0Sstevel@tonic-gate 	ino = IB_MONDO_TO_INO(hdlp->ih_vector);
506*0Sstevel@tonic-gate 
507*0Sstevel@tonic-gate 	DEBUG3(DBG_A_INTX, dip, "pci_add_intr: rdip=%s%d ino=%x\n",
508*0Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip), ino);
509*0Sstevel@tonic-gate 
510*0Sstevel@tonic-gate 	if (ino > ib_p->ib_max_ino) {
511*0Sstevel@tonic-gate 		DEBUG1(DBG_A_INTX, dip, "ino %x is invalid\n", ino);
512*0Sstevel@tonic-gate 		return (DDI_INTR_NOTFOUND);
513*0Sstevel@tonic-gate 	}
514*0Sstevel@tonic-gate 
515*0Sstevel@tonic-gate 	if (hdlp->ih_vector & PCI_PULSE_INO) {
516*0Sstevel@tonic-gate 		volatile uint64_t *map_reg_addr;
517*0Sstevel@tonic-gate 		map_reg_addr = ib_intr_map_reg_addr(ib_p, ino);
518*0Sstevel@tonic-gate 
519*0Sstevel@tonic-gate 		mondo = pci_xlate_intr(dip, rdip, ib_p, ino);
520*0Sstevel@tonic-gate 		if (mondo == 0)
521*0Sstevel@tonic-gate 			goto fail1;
522*0Sstevel@tonic-gate 
523*0Sstevel@tonic-gate 		hdlp->ih_vector = CB_MONDO_TO_XMONDO(cb_p, mondo);
524*0Sstevel@tonic-gate 
525*0Sstevel@tonic-gate 		if (i_ddi_add_ivintr(hdlp) != DDI_SUCCESS)
526*0Sstevel@tonic-gate 			goto fail1;
527*0Sstevel@tonic-gate 
528*0Sstevel@tonic-gate 		/*
529*0Sstevel@tonic-gate 		 * Select cpu and program.
530*0Sstevel@tonic-gate 		 *
531*0Sstevel@tonic-gate 		 * Since there is no good way to always derive cpuid in
532*0Sstevel@tonic-gate 		 * pci_remove_intr for PCI_PULSE_INO (esp. for STARFIRE), we
533*0Sstevel@tonic-gate 		 * don't add (or remove) device weight for pulsed interrupt
534*0Sstevel@tonic-gate 		 * sources.
535*0Sstevel@tonic-gate 		 */
536*0Sstevel@tonic-gate 		mutex_enter(&ib_p->ib_intr_lock);
537*0Sstevel@tonic-gate 		cpu_id = intr_dist_cpuid();
538*0Sstevel@tonic-gate 		*map_reg_addr = ib_get_map_reg(mondo, cpu_id);
539*0Sstevel@tonic-gate 		mutex_exit(&ib_p->ib_intr_lock);
540*0Sstevel@tonic-gate 		*map_reg_addr;	/* flush previous write */
541*0Sstevel@tonic-gate 		goto done;
542*0Sstevel@tonic-gate 	}
543*0Sstevel@tonic-gate 
544*0Sstevel@tonic-gate 	if ((mondo = pci_xlate_intr(dip, rdip, pci_p->pci_ib_p, ino)) == 0)
545*0Sstevel@tonic-gate 		goto fail1;
546*0Sstevel@tonic-gate 
547*0Sstevel@tonic-gate 	ino = IB_MONDO_TO_INO(mondo);
548*0Sstevel@tonic-gate 
549*0Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
550*0Sstevel@tonic-gate 	ih_p = ib_alloc_ih(rdip, hdlp->ih_inum,
551*0Sstevel@tonic-gate 	    hdlp->ih_cb_func, hdlp->ih_cb_arg1, hdlp->ih_cb_arg2);
552*0Sstevel@tonic-gate 	if (map_pcidev_cfg_reg(dip, rdip, &ih_p->ih_config_handle))
553*0Sstevel@tonic-gate 		goto fail2;
554*0Sstevel@tonic-gate 
555*0Sstevel@tonic-gate 	if (ino_p = ib_locate_ino(ib_p, ino)) {		/* sharing ino */
556*0Sstevel@tonic-gate 		uint32_t intr_index = hdlp->ih_inum;
557*0Sstevel@tonic-gate 		if (ib_ino_locate_intr(ino_p, rdip, intr_index)) {
558*0Sstevel@tonic-gate 			DEBUG1(DBG_A_INTX, dip, "dup intr #%d\n", intr_index);
559*0Sstevel@tonic-gate 			goto fail3;
560*0Sstevel@tonic-gate 		}
561*0Sstevel@tonic-gate 
562*0Sstevel@tonic-gate 		/* add weight to the cpu that we are already targeting */
563*0Sstevel@tonic-gate 		cpu_id = ino_p->ino_cpuid;
564*0Sstevel@tonic-gate 		weight = pci_class_to_intr_weight(rdip);
565*0Sstevel@tonic-gate 		intr_dist_cpuid_add_device_weight(cpu_id, rdip, weight);
566*0Sstevel@tonic-gate 
567*0Sstevel@tonic-gate 		ib_ino_add_intr(pci_p, ino_p, ih_p);
568*0Sstevel@tonic-gate 		goto ino_done;
569*0Sstevel@tonic-gate 	}
570*0Sstevel@tonic-gate 
571*0Sstevel@tonic-gate 	ino_p = ib_new_ino(ib_p, ino, ih_p);
572*0Sstevel@tonic-gate 
573*0Sstevel@tonic-gate 	if (hdlp->ih_pri == 0)
574*0Sstevel@tonic-gate 		hdlp->ih_pri = pci_class_to_pil(rdip);
575*0Sstevel@tonic-gate 
576*0Sstevel@tonic-gate 	hdlp->ih_vector = CB_MONDO_TO_XMONDO(cb_p, mondo);
577*0Sstevel@tonic-gate 
578*0Sstevel@tonic-gate 	DEBUG2(DBG_A_INTX, dip, "pci_add_intr:  pil=0x%x mondo=0x%x\n",
579*0Sstevel@tonic-gate 	    hdlp->ih_pri, hdlp->ih_vector);
580*0Sstevel@tonic-gate 
581*0Sstevel@tonic-gate 	DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp,
582*0Sstevel@tonic-gate 	    (ddi_intr_handler_t *)pci_intr_wrapper, (caddr_t)ino_p, NULL);
583*0Sstevel@tonic-gate 
584*0Sstevel@tonic-gate 	ret = i_ddi_add_ivintr(hdlp);
585*0Sstevel@tonic-gate 
586*0Sstevel@tonic-gate 	/*
587*0Sstevel@tonic-gate 	 * Restore original interrupt handler
588*0Sstevel@tonic-gate 	 * and arguments in interrupt handle.
589*0Sstevel@tonic-gate 	 */
590*0Sstevel@tonic-gate 	DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp, ih_p->ih_handler,
591*0Sstevel@tonic-gate 	    ih_p->ih_handler_arg1, ih_p->ih_handler_arg2);
592*0Sstevel@tonic-gate 
593*0Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
594*0Sstevel@tonic-gate 		goto fail4;
595*0Sstevel@tonic-gate 
596*0Sstevel@tonic-gate 	/* Save the pil for this ino */
597*0Sstevel@tonic-gate 	ino_p->ino_pil = hdlp->ih_pri;
598*0Sstevel@tonic-gate 
599*0Sstevel@tonic-gate 	/* clear and enable interrupt */
600*0Sstevel@tonic-gate 	IB_INO_INTR_CLEAR(ino_p->ino_clr_reg);
601*0Sstevel@tonic-gate 
602*0Sstevel@tonic-gate 	/* select cpu and compute weight, saving both for sharing and removal */
603*0Sstevel@tonic-gate 	cpu_id = pci_intr_dist_cpuid(ib_p, ino_p);
604*0Sstevel@tonic-gate 	ino_p->ino_cpuid = cpu_id;
605*0Sstevel@tonic-gate 	ino_p->ino_established = 1;
606*0Sstevel@tonic-gate 	weight = pci_class_to_intr_weight(rdip);
607*0Sstevel@tonic-gate 	intr_dist_cpuid_add_device_weight(cpu_id, rdip, weight);
608*0Sstevel@tonic-gate 
609*0Sstevel@tonic-gate #ifdef _STARFIRE
610*0Sstevel@tonic-gate 	cpu_id = pc_translate_tgtid(cb_p->cb_ittrans_cookie, cpu_id,
611*0Sstevel@tonic-gate 		IB_GET_MAPREG_INO(ino));
612*0Sstevel@tonic-gate #endif /* _STARFIRE */
613*0Sstevel@tonic-gate 	*ino_p->ino_map_reg = ib_get_map_reg(mondo, cpu_id);
614*0Sstevel@tonic-gate 	*ino_p->ino_map_reg;
615*0Sstevel@tonic-gate ino_done:
616*0Sstevel@tonic-gate 	ih_p->ih_ino_p = ino_p;
617*0Sstevel@tonic-gate 	if (ih_p->ih_ksp)
618*0Sstevel@tonic-gate 		kstat_install(ih_p->ih_ksp);
619*0Sstevel@tonic-gate 	ib_ino_map_reg_share(ib_p, ino, ino_p);
620*0Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
621*0Sstevel@tonic-gate done:
622*0Sstevel@tonic-gate 	DEBUG2(DBG_A_INTX, dip, "done! Interrupt 0x%x pil=%x\n",
623*0Sstevel@tonic-gate 		hdlp->ih_vector, hdlp->ih_pri);
624*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
625*0Sstevel@tonic-gate fail4:
626*0Sstevel@tonic-gate 	ib_delete_ino(ib_p, ino_p);
627*0Sstevel@tonic-gate fail3:
628*0Sstevel@tonic-gate 	if (ih_p->ih_config_handle)
629*0Sstevel@tonic-gate 		pci_config_teardown(&ih_p->ih_config_handle);
630*0Sstevel@tonic-gate fail2:
631*0Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
632*0Sstevel@tonic-gate 	kmem_free(ih_p, sizeof (ih_t));
633*0Sstevel@tonic-gate fail1:
634*0Sstevel@tonic-gate 	DEBUG2(DBG_A_INTX, dip, "Failed! Interrupt 0x%x pil=%x\n",
635*0Sstevel@tonic-gate 		hdlp->ih_vector, hdlp->ih_pri);
636*0Sstevel@tonic-gate 	return (DDI_FAILURE);
637*0Sstevel@tonic-gate }
638*0Sstevel@tonic-gate 
639*0Sstevel@tonic-gate int
640*0Sstevel@tonic-gate pci_remove_intr(dev_info_t *dip, dev_info_t *rdip, ddi_intr_handle_impl_t *hdlp)
641*0Sstevel@tonic-gate {
642*0Sstevel@tonic-gate 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
643*0Sstevel@tonic-gate 	ib_t *ib_p = pci_p->pci_ib_p;
644*0Sstevel@tonic-gate 	cb_t *cb_p = pci_p->pci_cb_p;
645*0Sstevel@tonic-gate 	ib_ino_t ino;
646*0Sstevel@tonic-gate 	ib_mondo_t mondo;
647*0Sstevel@tonic-gate 	ib_ino_info_t *ino_p;	/* non-pulse only */
648*0Sstevel@tonic-gate 	ih_t *ih_p;		/* non-pulse only */
649*0Sstevel@tonic-gate 
650*0Sstevel@tonic-gate 	ino = IB_MONDO_TO_INO(hdlp->ih_vector);
651*0Sstevel@tonic-gate 
652*0Sstevel@tonic-gate 	DEBUG3(DBG_R_INTX, dip, "pci_rem_intr: rdip=%s%d ino=%x\n",
653*0Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip), ino);
654*0Sstevel@tonic-gate 
655*0Sstevel@tonic-gate 	if (hdlp->ih_vector & PCI_PULSE_INO) { /* pulse interrupt */
656*0Sstevel@tonic-gate 		volatile uint64_t *map_reg_addr;
657*0Sstevel@tonic-gate 
658*0Sstevel@tonic-gate 		/*
659*0Sstevel@tonic-gate 		 * No weight was added by pci_add_intr for PCI_PULSE_INO
660*0Sstevel@tonic-gate 		 * because it is difficult to determine cpuid here.
661*0Sstevel@tonic-gate 		 */
662*0Sstevel@tonic-gate 		map_reg_addr = ib_intr_map_reg_addr(ib_p, ino);
663*0Sstevel@tonic-gate 		IB_INO_INTR_RESET(map_reg_addr);	/* disable intr */
664*0Sstevel@tonic-gate 		*map_reg_addr;
665*0Sstevel@tonic-gate 
666*0Sstevel@tonic-gate 		mondo = pci_xlate_intr(dip, rdip, ib_p, ino);
667*0Sstevel@tonic-gate 		if (mondo == 0) {
668*0Sstevel@tonic-gate 			DEBUG1(DBG_R_INTX, dip,
669*0Sstevel@tonic-gate 				"can't get mondo for ino %x\n", ino);
670*0Sstevel@tonic-gate 			return (DDI_FAILURE);
671*0Sstevel@tonic-gate 		}
672*0Sstevel@tonic-gate 
673*0Sstevel@tonic-gate 		if (hdlp->ih_pri == 0)
674*0Sstevel@tonic-gate 			hdlp->ih_pri = pci_class_to_pil(rdip);
675*0Sstevel@tonic-gate 
676*0Sstevel@tonic-gate 		hdlp->ih_vector = CB_MONDO_TO_XMONDO(cb_p, mondo);
677*0Sstevel@tonic-gate 
678*0Sstevel@tonic-gate 		DEBUG2(DBG_R_INTX, dip, "pci_rem_intr: pil=0x%x mondo=0x%x\n",
679*0Sstevel@tonic-gate 		    hdlp->ih_pri, hdlp->ih_vector);
680*0Sstevel@tonic-gate 
681*0Sstevel@tonic-gate 		i_ddi_rem_ivintr(hdlp);
682*0Sstevel@tonic-gate 
683*0Sstevel@tonic-gate 		DEBUG2(DBG_R_INTX, dip, "pulse success mondo=%x reg=%p\n",
684*0Sstevel@tonic-gate 			mondo, map_reg_addr);
685*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
686*0Sstevel@tonic-gate 	}
687*0Sstevel@tonic-gate 
688*0Sstevel@tonic-gate 	/* Translate the interrupt property */
689*0Sstevel@tonic-gate 	mondo = pci_xlate_intr(dip, rdip, pci_p->pci_ib_p, ino);
690*0Sstevel@tonic-gate 	if (mondo == 0) {
691*0Sstevel@tonic-gate 		DEBUG1(DBG_R_INTX, dip, "can't get mondo for ino %x\n", ino);
692*0Sstevel@tonic-gate 		return (DDI_FAILURE);
693*0Sstevel@tonic-gate 	}
694*0Sstevel@tonic-gate 	ino = IB_MONDO_TO_INO(mondo);
695*0Sstevel@tonic-gate 
696*0Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
697*0Sstevel@tonic-gate 	ino_p = ib_locate_ino(ib_p, ino);
698*0Sstevel@tonic-gate 	if (!ino_p) {
699*0Sstevel@tonic-gate 		int r = cb_remove_xintr(pci_p, dip, rdip, ino, mondo);
700*0Sstevel@tonic-gate 		if (r != DDI_SUCCESS)
701*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d-xintr: ino %x is invalid",
702*0Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip), ino);
703*0Sstevel@tonic-gate 		mutex_exit(&ib_p->ib_ino_lst_mutex);
704*0Sstevel@tonic-gate 		return (r);
705*0Sstevel@tonic-gate 	}
706*0Sstevel@tonic-gate 
707*0Sstevel@tonic-gate 	ih_p = ib_ino_locate_intr(ino_p, rdip, hdlp->ih_inum);
708*0Sstevel@tonic-gate 	ib_ino_rem_intr(pci_p, ino_p, ih_p);
709*0Sstevel@tonic-gate 	intr_dist_cpuid_rem_device_weight(ino_p->ino_cpuid, rdip);
710*0Sstevel@tonic-gate 	if (ino_p->ino_ih_size == 0) {
711*0Sstevel@tonic-gate 		IB_INO_INTR_PEND(ib_clear_intr_reg_addr(ib_p, ino));
712*0Sstevel@tonic-gate 		hdlp->ih_vector = CB_MONDO_TO_XMONDO(cb_p, mondo);
713*0Sstevel@tonic-gate 		if (hdlp->ih_pri == 0)
714*0Sstevel@tonic-gate 			hdlp->ih_pri = pci_class_to_pil(rdip);
715*0Sstevel@tonic-gate 
716*0Sstevel@tonic-gate 		i_ddi_rem_ivintr(hdlp);
717*0Sstevel@tonic-gate 		ib_delete_ino(ib_p, ino_p);
718*0Sstevel@tonic-gate 	}
719*0Sstevel@tonic-gate 
720*0Sstevel@tonic-gate 	/* re-enable interrupt only if mapping register still shared */
721*0Sstevel@tonic-gate 	if (ib_ino_map_reg_unshare(ib_p, ino, ino_p)) {
722*0Sstevel@tonic-gate 		IB_INO_INTR_ON(ino_p->ino_map_reg);
723*0Sstevel@tonic-gate 		*ino_p->ino_map_reg;
724*0Sstevel@tonic-gate 	}
725*0Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
726*0Sstevel@tonic-gate 
727*0Sstevel@tonic-gate 	if (ino_p->ino_ih_size == 0)
728*0Sstevel@tonic-gate 		kmem_free(ino_p, sizeof (ib_ino_info_t));
729*0Sstevel@tonic-gate 
730*0Sstevel@tonic-gate 	DEBUG1(DBG_R_INTX, dip, "success! mondo=%x\n", mondo);
731*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
732*0Sstevel@tonic-gate }
733*0Sstevel@tonic-gate 
734*0Sstevel@tonic-gate /*
735*0Sstevel@tonic-gate  * free the pci_inos array allocated during pci_intr_setup. the actual
736*0Sstevel@tonic-gate  * interrupts are torn down by their respective block destroy routines:
737*0Sstevel@tonic-gate  * cb_destroy, pbm_destroy, and ib_destroy.
738*0Sstevel@tonic-gate  */
739*0Sstevel@tonic-gate void
740*0Sstevel@tonic-gate pci_intr_teardown(pci_t *pci_p)
741*0Sstevel@tonic-gate {
742*0Sstevel@tonic-gate 	kmem_free(pci_p->pci_inos, pci_p->pci_inos_len);
743*0Sstevel@tonic-gate 	pci_p->pci_inos = NULL;
744*0Sstevel@tonic-gate 	pci_p->pci_inos_len = 0;
745*0Sstevel@tonic-gate }
746