xref: /illumos-gate/usr/src/uts/sun4u/opl/io/pcicmu/pcmu_ib.c (revision 628e3cbed6489fa1db545d8524a06cd6535af456)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * CMU-CH Interrupt Block
30  */
31 
32 #include <sys/types.h>
33 #include <sys/kmem.h>
34 #include <sys/async.h>
35 #include <sys/systm.h>
36 #include <sys/spl.h>
37 #include <sys/sunddi.h>
38 #include <sys/machsystm.h>
39 #include <sys/ddi_impldefs.h>
40 #include <sys/pcicmu/pcicmu.h>
41 
42 static uint_t pcmu_ib_intr_reset(void *arg);
43 
44 extern uint64_t	xc_tick_jump_limit;
45 
46 void
47 pcmu_ib_create(pcmu_t *pcmu_p)
48 {
49 	pcmu_ib_t *pib_p;
50 	uintptr_t a;
51 	int i;
52 
53 	/*
54 	 * Allocate interrupt block state structure and link it to
55 	 * the pci state structure.
56 	 */
57 	pib_p = kmem_zalloc(sizeof (pcmu_ib_t), KM_SLEEP);
58 	pcmu_p->pcmu_ib_p = pib_p;
59 	pib_p->pib_pcmu_p = pcmu_p;
60 
61 	a = pcmu_ib_setup(pib_p);
62 
63 	/*
64 	 * Determine virtual addresses of interrupt mapping, clear and diag
65 	 * registers that have common offsets.
66 	 */
67 	pib_p->pib_intr_retry_timer_reg =
68 	    (uint64_t *)(a + PCMU_IB_INTR_RETRY_TIMER_OFFSET);
69 	pib_p->pib_obio_intr_state_diag_reg =
70 	    (uint64_t *)(a + PCMU_IB_OBIO_INTR_STATE_DIAG_REG);
71 
72 	PCMU_DBG2(PCMU_DBG_ATTACH, pcmu_p->pcmu_dip,
73 	    "pcmu_ib_create: obio_imr=%x, obio_cir=%x\n",
74 	    pib_p->pib_obio_intr_map_regs, pib_p->pib_obio_clear_intr_regs);
75 	PCMU_DBG2(PCMU_DBG_ATTACH, pcmu_p->pcmu_dip,
76 	    "pcmu_ib_create: retry_timer=%x, obio_diag=%x\n",
77 	    pib_p->pib_intr_retry_timer_reg,
78 	    pib_p->pib_obio_intr_state_diag_reg);
79 
80 	pib_p->pib_ino_lst = (pcmu_ib_ino_info_t *)NULL;
81 	mutex_init(&pib_p->pib_intr_lock, NULL, MUTEX_DRIVER, NULL);
82 	mutex_init(&pib_p->pib_ino_lst_mutex, NULL, MUTEX_DRIVER, NULL);
83 
84 	PCMU_DBG1(PCMU_DBG_ATTACH, pcmu_p->pcmu_dip,
85 	    "pcmu_ib_create: numproxy=%x\n", pcmu_p->pcmu_numproxy);
86 	for (i = 1; i <= pcmu_p->pcmu_numproxy; i++) {
87 		set_intr_mapping_reg(pcmu_p->pcmu_id,
88 		    (uint64_t *)pib_p->pib_upa_imr[i - 1], i);
89 	}
90 
91 	pcmu_ib_configure(pib_p);
92 	bus_func_register(BF_TYPE_RESINTR, pcmu_ib_intr_reset, pib_p);
93 }
94 
95 void
96 pcmu_ib_destroy(pcmu_t *pcmu_p)
97 {
98 	pcmu_ib_t *pib_p = pcmu_p->pcmu_ib_p;
99 
100 	PCMU_DBG0(PCMU_DBG_IB, pcmu_p->pcmu_dip, "pcmu_ib_destroy\n");
101 	bus_func_unregister(BF_TYPE_RESINTR, pcmu_ib_intr_reset, pib_p);
102 
103 	intr_dist_rem_weighted(pcmu_ib_intr_dist_all, pib_p);
104 	mutex_destroy(&pib_p->pib_ino_lst_mutex);
105 	mutex_destroy(&pib_p->pib_intr_lock);
106 
107 	pcmu_ib_free_ino_all(pib_p);
108 
109 	kmem_free(pib_p, sizeof (pcmu_ib_t));
110 	pcmu_p->pcmu_ib_p = NULL;
111 }
112 
113 void
114 pcmu_ib_configure(pcmu_ib_t *pib_p)
115 {
116 	*pib_p->pib_intr_retry_timer_reg = pcmu_intr_retry_intv;
117 }
118 
119 /*
120  * can only used for CMU-CH internal interrupts ue, pbm
121  */
122 void
123 pcmu_ib_intr_enable(pcmu_t *pcmu_p, pcmu_ib_ino_t ino)
124 {
125 	pcmu_ib_t *pib_p = pcmu_p->pcmu_ib_p;
126 	pcmu_ib_mondo_t mondo = PCMU_IB_INO_TO_MONDO(pib_p, ino);
127 	volatile uint64_t *imr_p = ib_intr_map_reg_addr(pib_p, ino);
128 	uint_t cpu_id;
129 
130 	/*
131 	 * Determine the cpu for the interrupt.
132 	 */
133 	mutex_enter(&pib_p->pib_intr_lock);
134 	cpu_id = intr_dist_cpuid();
135 	cpu_id = u2u_translate_tgtid(pcmu_p, cpu_id, imr_p);
136 	PCMU_DBG2(PCMU_DBG_IB, pcmu_p->pcmu_dip,
137 	    "pcmu_ib_intr_enable: ino=%x cpu_id=%x\n", ino, cpu_id);
138 
139 	*imr_p = ib_get_map_reg(mondo, cpu_id);
140 	PCMU_IB_INO_INTR_CLEAR(ib_clear_intr_reg_addr(pib_p, ino));
141 	mutex_exit(&pib_p->pib_intr_lock);
142 }
143 
144 /*
145  * Disable the interrupt via its interrupt mapping register.
146  * Can only be used for internal interrupts: ue, pbm.
147  * If called under interrupt context, wait should be set to 0
148  */
149 void
150 pcmu_ib_intr_disable(pcmu_ib_t *pib_p, pcmu_ib_ino_t ino, int wait)
151 {
152 	volatile uint64_t *imr_p = ib_intr_map_reg_addr(pib_p, ino);
153 	volatile uint64_t *state_reg_p = PCMU_IB_INO_INTR_STATE_REG(pib_p, ino);
154 	hrtime_t start_time;
155 	hrtime_t prev, curr, interval, jump;
156 	hrtime_t intr_timeout;
157 
158 	/* disable the interrupt */
159 	mutex_enter(&pib_p->pib_intr_lock);
160 	PCMU_IB_INO_INTR_OFF(imr_p);
161 	*imr_p;	/* flush previous write */
162 	mutex_exit(&pib_p->pib_intr_lock);
163 
164 	if (!wait)
165 		goto wait_done;
166 
167 	intr_timeout = pcmu_intrpend_timeout;
168 	jump = TICK_TO_NSEC(xc_tick_jump_limit);
169 	start_time = curr = gethrtime();
170 	/* busy wait if there is interrupt being processed */
171 	while (PCMU_IB_INO_INTR_PENDING(state_reg_p, ino) && !panicstr) {
172 		/*
173 		 * If we have a really large jump in hrtime, it is most
174 		 * probably because we entered the debugger (or OBP,
175 		 * in general). So, we adjust the timeout accordingly
176 		 * to prevent declaring an interrupt timeout. The
177 		 * master-interrupt mechanism in OBP should deliver
178 		 * the interrupts properly.
179 		 */
180 		prev = curr;
181 		curr = gethrtime();
182 		interval = curr - prev;
183 		if (interval > jump)
184 			intr_timeout += interval;
185 		if (curr - start_time > intr_timeout) {
186 			pcmu_pbm_t *pcbm_p = pib_p->pib_pcmu_p->pcmu_pcbm_p;
187 			cmn_err(CE_WARN,
188 			    "%s:%s: pcmu_ib_intr_disable timeout %x",
189 			    pcbm_p->pcbm_nameinst_str,
190 			    pcbm_p->pcbm_nameaddr_str, ino);
191 			break;
192 		}
193 	}
194 wait_done:
195 	PCMU_IB_INO_INTR_PEND(ib_clear_intr_reg_addr(pib_p, ino));
196 	u2u_ittrans_cleanup((u2u_ittrans_data_t *)
197 	    (PCMU_IB2CB(pib_p)->pcb_ittrans_cookie), imr_p);
198 }
199 
200 /* can only used for CMU-CH internal interrupts ue, pbm */
201 void
202 pcmu_ib_nintr_clear(pcmu_ib_t *pib_p, pcmu_ib_ino_t ino)
203 {
204 	uint64_t *clr_reg = ib_clear_intr_reg_addr(pib_p, ino);
205 	PCMU_IB_INO_INTR_CLEAR(clr_reg);
206 }
207 
208 /*
209  * distribute PBM and UPA interrupts. ino is set to 0 by caller if we
210  * are dealing with UPA interrupts (without inos).
211  */
212 void
213 pcmu_ib_intr_dist_nintr(pcmu_ib_t *pib_p, pcmu_ib_ino_t ino,
214     volatile uint64_t *imr_p)
215 {
216 	volatile uint64_t imr = *imr_p;
217 	uint32_t cpu_id;
218 
219 	if (!PCMU_IB_INO_INTR_ISON(imr))
220 		return;
221 
222 	cpu_id = intr_dist_cpuid();
223 
224 	if (ino) {
225 		cpu_id = u2u_translate_tgtid(pib_p->pib_pcmu_p, cpu_id, imr_p);
226 	}
227 
228 	if (ib_map_reg_get_cpu(*imr_p) == cpu_id) {
229 		return;
230 	}
231 	*imr_p = ib_get_map_reg(PCMU_IB_IMR2MONDO(imr), cpu_id);
232 	imr = *imr_p;	/* flush previous write */
233 }
234 
235 static void
236 pcmu_ib_intr_dist(pcmu_ib_t *pib_p, pcmu_ib_ino_info_t *ino_p)
237 {
238 	uint32_t cpu_id = ino_p->pino_cpuid;
239 	pcmu_ib_ino_t ino = ino_p->pino_ino;
240 	volatile uint64_t imr, *imr_p, *state_reg;
241 	hrtime_t start_time;
242 	hrtime_t prev, curr, interval, jump;
243 	hrtime_t intr_timeout;
244 
245 	ASSERT(MUTEX_HELD(&pib_p->pib_ino_lst_mutex));
246 	imr_p = ib_intr_map_reg_addr(pib_p, ino);
247 	state_reg = PCMU_IB_INO_INTR_STATE_REG(pib_p, ino);
248 
249 	/* disable interrupt, this could disrupt devices sharing our slot */
250 	PCMU_IB_INO_INTR_OFF(imr_p);
251 	imr = *imr_p;	/* flush previous write */
252 
253 	/* busy wait if there is interrupt being processed */
254 	intr_timeout = pcmu_intrpend_timeout;
255 	jump = TICK_TO_NSEC(xc_tick_jump_limit);
256 	start_time = curr = gethrtime();
257 	while (PCMU_IB_INO_INTR_PENDING(state_reg, ino) && !panicstr) {
258 		/*
259 		 * If we have a really large jump in hrtime, it is most
260 		 * probably because we entered the debugger (or OBP,
261 		 * in general). So, we adjust the timeout accordingly
262 		 * to prevent declaring an interrupt timeout. The
263 		 * master-interrupt mechanism in OBP should deliver
264 		 * the interrupts properly.
265 		 */
266 		prev = curr;
267 		curr = gethrtime();
268 		interval = curr - prev;
269 		if (interval > jump)
270 			intr_timeout += interval;
271 		if (curr - start_time > intr_timeout) {
272 			pcmu_pbm_t *pcbm_p = pib_p->pib_pcmu_p->pcmu_pcbm_p;
273 			cmn_err(CE_WARN,
274 			    "%s:%s: pcmu_ib_intr_dist(%p,%x) timeout",
275 			    pcbm_p->pcbm_nameinst_str,
276 			    pcbm_p->pcbm_nameaddr_str,
277 			    imr_p, PCMU_IB_INO_TO_MONDO(pib_p, ino));
278 			break;
279 		}
280 	}
281 	cpu_id = u2u_translate_tgtid(pib_p->pib_pcmu_p, cpu_id, imr_p);
282 	*imr_p = ib_get_map_reg(PCMU_IB_IMR2MONDO(imr), cpu_id);
283 	imr = *imr_p;	/* flush previous write */
284 }
285 
286 /*
287  * Redistribute interrupts of the specified weight. The first call has a weight
288  * of weight_max, which can be used to trigger initialization for
289  * redistribution. The inos with weight [weight_max, inf.) should be processed
290  * on the "weight == weight_max" call.  This first call is followed by calls
291  * of decreasing weights, inos of that weight should be processed.  The final
292  * call specifies a weight of zero, this can be used to trigger processing of
293  * stragglers.
294  */
295 void
296 pcmu_ib_intr_dist_all(void *arg, int32_t weight_max, int32_t weight)
297 {
298 	pcmu_ib_t *pib_p = (pcmu_ib_t *)arg;
299 	pcmu_ib_ino_info_t *ino_p;
300 	ih_t *ih_lst;
301 	int32_t dweight;
302 	int i;
303 
304 	mutex_enter(&pib_p->pib_ino_lst_mutex);
305 
306 	/* Perform special processing for first call of a redistribution. */
307 	if (weight == weight_max) {
308 		for (ino_p = pib_p->pib_ino_lst; ino_p;
309 		    ino_p = ino_p->pino_next) {
310 
311 			/*
312 			 * Clear pino_established of each ino on first call.
313 			 * The pino_established field may be used by a pci
314 			 * nexus driver's pcmu_intr_dist_cpuid implementation
315 			 * when detection of established pci slot-cpu binding
316 			 * for multi function pci cards.
317 			 */
318 			ino_p->pino_established = 0;
319 
320 			/*
321 			 * recompute the pino_intr_weight based on the device
322 			 * weight of all devinfo nodes sharing the ino (this
323 			 * will allow us to pick up new weights established by
324 			 * i_ddi_set_intr_weight()).
325 			 */
326 			ino_p->pino_intr_weight = 0;
327 			for (i = 0, ih_lst = ino_p->pino_ih_head;
328 			    i < ino_p->pino_ih_size;
329 			    i++, ih_lst = ih_lst->ih_next) {
330 				dweight = i_ddi_get_intr_weight(ih_lst->ih_dip);
331 				if (dweight > 0)
332 					ino_p->pino_intr_weight += dweight;
333 			}
334 		}
335 	}
336 
337 	for (ino_p = pib_p->pib_ino_lst; ino_p; ino_p = ino_p->pino_next) {
338 		/*
339 		 * Get the weight of the ino and determine if we are going to
340 		 * process call.  We wait until an pcmu_ib_intr_dist_all call of
341 		 * the proper weight occurs to support redistribution of all
342 		 * heavy weighted interrupts first (across all nexus driver
343 		 * instances).  This is done to ensure optimal
344 		 * INTR_WEIGHTED_DIST behavior.
345 		 */
346 		if ((weight == ino_p->pino_intr_weight) ||
347 		    ((weight >= weight_max) &&
348 		    (ino_p->pino_intr_weight >= weight_max))) {
349 			/* select cpuid to target and mark ino established */
350 			ino_p->pino_cpuid = pcmu_intr_dist_cpuid(pib_p, ino_p);
351 			ino_p->pino_established = 1;
352 
353 			/* Add device weight of ino devinfos to targeted cpu. */
354 			for (i = 0, ih_lst = ino_p->pino_ih_head;
355 			    i < ino_p->pino_ih_size;
356 			    i++, ih_lst = ih_lst->ih_next) {
357 				dweight = i_ddi_get_intr_weight(ih_lst->ih_dip);
358 				intr_dist_cpuid_add_device_weight(
359 				    ino_p->pino_cpuid, ih_lst->ih_dip, dweight);
360 			}
361 
362 			/* program the hardware */
363 			pcmu_ib_intr_dist(pib_p, ino_p);
364 		}
365 	}
366 	mutex_exit(&pib_p->pib_ino_lst_mutex);
367 }
368 
369 /*
370  * Reset interrupts to IDLE.  This function is called during
371  * panic handling after redistributing interrupts; it's needed to
372  * support dumping to network devices after 'sync' from OBP.
373  *
374  * N.B.  This routine runs in a context where all other threads
375  * are permanently suspended.
376  */
377 static uint_t
378 pcmu_ib_intr_reset(void *arg)
379 {
380 	pcmu_ib_t *pib_p = (pcmu_ib_t *)arg;
381 	pcmu_ib_ino_t ino;
382 	uint64_t *clr_reg;
383 
384 	/*
385 	 * Note that we only actually care about interrupts that are
386 	 * potentially from network devices.
387 	 */
388 	for (ino = 0; ino <= pib_p->pib_max_ino; ino++) {
389 		clr_reg = ib_clear_intr_reg_addr(pib_p, ino);
390 		PCMU_IB_INO_INTR_CLEAR(clr_reg);
391 	}
392 	return (BF_NONE);
393 }
394 
395 void
396 pcmu_ib_suspend(pcmu_ib_t *pib_p)
397 {
398 	pcmu_ib_ino_info_t *ip;
399 
400 	/* save ino_lst interrupts' mapping registers content */
401 	mutex_enter(&pib_p->pib_ino_lst_mutex);
402 	for (ip = pib_p->pib_ino_lst; ip; ip = ip->pino_next) {
403 		ip->pino_map_reg_save = *ip->pino_map_reg;
404 	}
405 	mutex_exit(&pib_p->pib_ino_lst_mutex);
406 }
407 
408 void
409 pcmu_ib_resume(pcmu_ib_t *pib_p)
410 {
411 	pcmu_ib_ino_info_t *ip;
412 
413 	/* restore ino_lst interrupts' mapping registers content */
414 	mutex_enter(&pib_p->pib_ino_lst_mutex);
415 	for (ip = pib_p->pib_ino_lst; ip; ip = ip->pino_next) {
416 		PCMU_IB_INO_INTR_CLEAR(ip->pino_clr_reg); /* set intr to idle */
417 		*ip->pino_map_reg = ip->pino_map_reg_save; /* restore IMR */
418 	}
419 	mutex_exit(&pib_p->pib_ino_lst_mutex);
420 }
421 
422 /*
423  * locate ino_info structure on pib_p->pib_ino_lst according to ino#
424  * returns NULL if not found.
425  */
426 pcmu_ib_ino_info_t *
427 pcmu_ib_locate_ino(pcmu_ib_t *pib_p, pcmu_ib_ino_t ino_num)
428 {
429 	pcmu_ib_ino_info_t *ino_p = pib_p->pib_ino_lst;
430 	ASSERT(MUTEX_HELD(&pib_p->pib_ino_lst_mutex));
431 
432 	for (; ino_p && ino_p->pino_ino != ino_num; ino_p = ino_p->pino_next)
433 		;
434 	return (ino_p);
435 }
436 
437 #define	PCMU_IB_INO_TO_SLOT(ino)		\
438 	(PCMU_IB_IS_OBIO_INO(ino) ? 0xff : ((ino) & 0x1f) >> 2)
439 
440 pcmu_ib_ino_info_t *
441 pcmu_ib_new_ino(pcmu_ib_t *pib_p, pcmu_ib_ino_t ino_num, ih_t *ih_p)
442 {
443 	pcmu_ib_ino_info_t *ino_p = kmem_alloc(sizeof (pcmu_ib_ino_info_t),
444 	    KM_SLEEP);
445 	ino_p->pino_ino = ino_num;
446 	ino_p->pino_slot_no = PCMU_IB_INO_TO_SLOT(ino_num);
447 	ino_p->pino_ib_p = pib_p;
448 	ino_p->pino_clr_reg = ib_clear_intr_reg_addr(pib_p, ino_num);
449 	ino_p->pino_map_reg = ib_intr_map_reg_addr(pib_p, ino_num);
450 	ino_p->pino_unclaimed = 0;
451 
452 	/*
453 	 * cannot disable interrupt since we might share slot
454 	 * PCMU_IB_INO_INTR_OFF(ino_p->pino_map_reg);
455 	 */
456 
457 	ih_p->ih_next = ih_p;
458 	ino_p->pino_ih_head = ih_p;
459 	ino_p->pino_ih_tail = ih_p;
460 	ino_p->pino_ih_start = ih_p;
461 	ino_p->pino_ih_size = 1;
462 
463 	ino_p->pino_next = pib_p->pib_ino_lst;
464 	pib_p->pib_ino_lst = ino_p;
465 	return (ino_p);
466 }
467 
468 /* the ino_p is retrieved by previous call to pcmu_ib_locate_ino() */
469 void
470 pcmu_ib_delete_ino(pcmu_ib_t *pib_p, pcmu_ib_ino_info_t *ino_p)
471 {
472 	pcmu_ib_ino_info_t *list = pib_p->pib_ino_lst;
473 	ASSERT(MUTEX_HELD(&pib_p->pib_ino_lst_mutex));
474 	if (list == ino_p) {
475 		pib_p->pib_ino_lst = list->pino_next;
476 	} else {
477 		for (; list->pino_next != ino_p; list = list->pino_next)
478 			;
479 		list->pino_next = ino_p->pino_next;
480 	}
481 }
482 
483 /* free all ino when we are detaching */
484 void
485 pcmu_ib_free_ino_all(pcmu_ib_t *pib_p)
486 {
487 	pcmu_ib_ino_info_t *tmp = pib_p->pib_ino_lst;
488 	pcmu_ib_ino_info_t *next = NULL;
489 	while (tmp) {
490 		next = tmp->pino_next;
491 		kmem_free(tmp, sizeof (pcmu_ib_ino_info_t));
492 		tmp = next;
493 	}
494 }
495 
496 void
497 pcmu_ib_ino_add_intr(pcmu_t *pcmu_p, pcmu_ib_ino_info_t *ino_p, ih_t *ih_p)
498 {
499 	pcmu_ib_ino_t ino = ino_p->pino_ino;
500 	pcmu_ib_t *pib_p = ino_p->pino_ib_p;
501 	volatile uint64_t *state_reg = PCMU_IB_INO_INTR_STATE_REG(pib_p, ino);
502 	hrtime_t start_time;
503 	hrtime_t prev, curr, interval, jump;
504 	hrtime_t intr_timeout;
505 
506 	ASSERT(pib_p == pcmu_p->pcmu_ib_p);
507 	ASSERT(MUTEX_HELD(&pib_p->pib_ino_lst_mutex));
508 
509 	/* disable interrupt, this could disrupt devices sharing our slot */
510 	PCMU_IB_INO_INTR_OFF(ino_p->pino_map_reg);
511 	*ino_p->pino_map_reg;
512 
513 	/* do NOT modify the link list until after the busy wait */
514 
515 	/*
516 	 * busy wait if there is interrupt being processed.
517 	 * either the pending state will be cleared by the interrupt wrapper
518 	 * or the interrupt will be marked as blocked indicating that it was
519 	 * jabbering.
520 	 */
521 	intr_timeout = pcmu_intrpend_timeout;
522 	jump = TICK_TO_NSEC(xc_tick_jump_limit);
523 	start_time = curr = gethrtime();
524 	while ((ino_p->pino_unclaimed <= pcmu_unclaimed_intr_max) &&
525 	    PCMU_IB_INO_INTR_PENDING(state_reg, ino) && !panicstr) {
526 		/*
527 		 * If we have a really large jump in hrtime, it is most
528 		 * probably because we entered the debugger (or OBP,
529 		 * in general). So, we adjust the timeout accordingly
530 		 * to prevent declaring an interrupt timeout. The
531 		 * master-interrupt mechanism in OBP should deliver
532 		 * the interrupts properly.
533 		 */
534 		prev = curr;
535 		curr = gethrtime();
536 		interval = curr - prev;
537 		if (interval > jump)
538 			intr_timeout += interval;
539 		if (curr - start_time > intr_timeout) {
540 			pcmu_pbm_t *pcbm_p = pcmu_p->pcmu_pcbm_p;
541 			cmn_err(CE_WARN,
542 			    "%s:%s: pcmu_ib_ino_add_intr %x timeout",
543 			    pcbm_p->pcbm_nameinst_str,
544 			    pcbm_p->pcbm_nameaddr_str, ino);
545 			break;
546 		}
547 	}
548 
549 	/* link up pcmu_ispec_t portion of the ppd */
550 	ih_p->ih_next = ino_p->pino_ih_head;
551 	ino_p->pino_ih_tail->ih_next = ih_p;
552 	ino_p->pino_ih_tail = ih_p;
553 
554 	ino_p->pino_ih_start = ino_p->pino_ih_head;
555 	ino_p->pino_ih_size++;
556 
557 	/*
558 	 * if the interrupt was previously blocked (left in pending state)
559 	 * because of jabber we need to clear the pending state in case the
560 	 * jabber has gone away.
561 	 */
562 	if (ino_p->pino_unclaimed > pcmu_unclaimed_intr_max) {
563 		cmn_err(CE_WARN,
564 		    "%s%d: pcmu_ib_ino_add_intr: ino 0x%x has been unblocked",
565 		    ddi_driver_name(pcmu_p->pcmu_dip),
566 		    ddi_get_instance(pcmu_p->pcmu_dip),
567 		    ino_p->pino_ino);
568 		ino_p->pino_unclaimed = 0;
569 		PCMU_IB_INO_INTR_CLEAR(ino_p->pino_clr_reg);
570 	}
571 
572 	/* re-enable interrupt */
573 	PCMU_IB_INO_INTR_ON(ino_p->pino_map_reg);
574 	*ino_p->pino_map_reg;
575 }
576 
577 /*
578  * removes pcmu_ispec_t from the ino's link list.
579  * uses hardware mutex to lock out interrupt threads.
580  * Side effects: interrupt belongs to that ino is turned off on return.
581  * if we are sharing PCI slot with other inos, the caller needs
582  * to turn it back on.
583  */
584 int
585 pcmu_ib_ino_rem_intr(pcmu_t *pcmu_p, pcmu_ib_ino_info_t *ino_p, ih_t *ih_p)
586 {
587 	int i;
588 	pcmu_ib_ino_t ino = ino_p->pino_ino;
589 	ih_t *ih_lst = ino_p->pino_ih_head;
590 	volatile uint64_t *state_reg =
591 	    PCMU_IB_INO_INTR_STATE_REG(ino_p->pino_ib_p, ino);
592 	hrtime_t start_time;
593 	hrtime_t prev, curr, interval, jump;
594 	hrtime_t intr_timeout;
595 
596 	ASSERT(MUTEX_HELD(&ino_p->pino_ib_p->pib_ino_lst_mutex));
597 	/* disable interrupt, this could disrupt devices sharing our slot */
598 	PCMU_IB_INO_INTR_OFF(ino_p->pino_map_reg);
599 	*ino_p->pino_map_reg;
600 
601 	/* do NOT modify the link list until after the busy wait */
602 
603 	/*
604 	 * busy wait if there is interrupt being processed.
605 	 * either the pending state will be cleared by the interrupt wrapper
606 	 * or the interrupt will be marked as blocked indicating that it was
607 	 * jabbering.
608 	 */
609 	intr_timeout = pcmu_intrpend_timeout;
610 	jump = TICK_TO_NSEC(xc_tick_jump_limit);
611 	start_time = curr = gethrtime();
612 	while ((ino_p->pino_unclaimed <= pcmu_unclaimed_intr_max) &&
613 	    PCMU_IB_INO_INTR_PENDING(state_reg, ino) && !panicstr) {
614 		/*
615 		 * If we have a really large jump in hrtime, it is most
616 		 * probably because we entered the debugger (or OBP,
617 		 * in general). So, we adjust the timeout accordingly
618 		 * to prevent declaring an interrupt timeout. The
619 		 * master-interrupt mechanism in OBP should deliver
620 		 * the interrupts properly.
621 		 */
622 		prev = curr;
623 		curr = gethrtime();
624 		interval = curr - prev;
625 		if (interval > jump)
626 			intr_timeout += interval;
627 		if (curr - start_time > intr_timeout) {
628 			pcmu_pbm_t *pcbm_p = pcmu_p->pcmu_pcbm_p;
629 			cmn_err(CE_WARN,
630 			    "%s:%s: pcmu_ib_ino_rem_intr %x timeout",
631 			    pcbm_p->pcbm_nameinst_str,
632 			    pcbm_p->pcbm_nameaddr_str, ino);
633 			PCMU_IB_INO_INTR_ON(ino_p->pino_map_reg);
634 			*ino_p->pino_map_reg;
635 			return (DDI_FAILURE);
636 		}
637 	}
638 
639 	if (ino_p->pino_ih_size == 1) {
640 		if (ih_lst != ih_p)
641 			goto not_found;
642 		/* no need to set head/tail as ino_p will be freed */
643 		goto reset;
644 	}
645 
646 	/*
647 	 * if the interrupt was previously blocked (left in pending state)
648 	 * because of jabber we need to clear the pending state in case the
649 	 * jabber has gone away.
650 	 */
651 	if (ino_p->pino_unclaimed > pcmu_unclaimed_intr_max) {
652 		cmn_err(CE_WARN,
653 		    "%s%d: pcmu_ib_ino_rem_intr: ino 0x%x has been unblocked",
654 		    ddi_driver_name(pcmu_p->pcmu_dip),
655 		    ddi_get_instance(pcmu_p->pcmu_dip),
656 		    ino_p->pino_ino);
657 		ino_p->pino_unclaimed = 0;
658 		PCMU_IB_INO_INTR_CLEAR(ino_p->pino_clr_reg);
659 	}
660 
661 	/* search the link list for ih_p */
662 	for (i = 0; (i < ino_p->pino_ih_size) && (ih_lst->ih_next != ih_p);
663 	    i++, ih_lst = ih_lst->ih_next)
664 		;
665 	if (ih_lst->ih_next != ih_p) {
666 		goto not_found;
667 	}
668 
669 	/* remove ih_p from the link list and maintain the head/tail */
670 	ih_lst->ih_next = ih_p->ih_next;
671 	if (ino_p->pino_ih_head == ih_p) {
672 		ino_p->pino_ih_head = ih_p->ih_next;
673 	}
674 	if (ino_p->pino_ih_tail == ih_p) {
675 		ino_p->pino_ih_tail = ih_lst;
676 	}
677 	ino_p->pino_ih_start = ino_p->pino_ih_head;
678 reset:
679 	if (ih_p->ih_config_handle) {
680 		pci_config_teardown(&ih_p->ih_config_handle);
681 	}
682 	kmem_free(ih_p, sizeof (ih_t));
683 	ino_p->pino_ih_size--;
684 
685 	return (DDI_SUCCESS);
686 not_found:
687 	PCMU_DBG2(PCMU_DBG_R_INTX, ino_p->pino_ib_p->pib_pcmu_p->pcmu_dip,
688 	    "ino_p=%x does not have ih_p=%x\n", ino_p, ih_p);
689 	return (DDI_SUCCESS);
690 }
691 
692 ih_t *
693 pcmu_ib_ino_locate_intr(pcmu_ib_ino_info_t *ino_p,
694     dev_info_t *rdip, uint32_t inum)
695 {
696 	ih_t *ih_lst = ino_p->pino_ih_head;
697 	int i;
698 	for (i = 0; i < ino_p->pino_ih_size; i++, ih_lst = ih_lst->ih_next) {
699 		if (ih_lst->ih_dip == rdip && ih_lst->ih_inum == inum) {
700 			return (ih_lst);
701 		}
702 	}
703 	return ((ih_t *)NULL);
704 }
705 
706 ih_t *
707 pcmu_ib_alloc_ih(dev_info_t *rdip, uint32_t inum,
708     uint_t (*int_handler)(caddr_t int_handler_arg1, caddr_t int_handler_arg2),
709     caddr_t int_handler_arg1,
710     caddr_t int_handler_arg2)
711 {
712 	ih_t *ih_p;
713 
714 	ih_p = kmem_alloc(sizeof (ih_t), KM_SLEEP);
715 	ih_p->ih_dip = rdip;
716 	ih_p->ih_inum = inum;
717 	ih_p->ih_intr_state = PCMU_INTR_STATE_DISABLE;
718 	ih_p->ih_handler = int_handler;
719 	ih_p->ih_handler_arg1 = int_handler_arg1;
720 	ih_p->ih_handler_arg2 = int_handler_arg2;
721 	ih_p->ih_config_handle = NULL;
722 	return (ih_p);
723 }
724 
725 int
726 pcmu_ib_update_intr_state(pcmu_t *pcmu_p, dev_info_t *rdip,
727     ddi_intr_handle_impl_t *hdlp, uint_t new_intr_state)
728 {
729 	pcmu_ib_t		*pib_p = pcmu_p->pcmu_ib_p;
730 	pcmu_ib_ino_info_t	*ino_p;
731 	pcmu_ib_mondo_t	mondo;
732 	ih_t		*ih_p;
733 	int		ret = DDI_FAILURE;
734 
735 	mutex_enter(&pib_p->pib_ino_lst_mutex);
736 
737 	if ((mondo = PCMU_IB_INO_TO_MONDO(pcmu_p->pcmu_ib_p,
738 	    PCMU_IB_MONDO_TO_INO((int32_t)hdlp->ih_vector))) == 0) {
739 		mutex_exit(&pib_p->pib_ino_lst_mutex);
740 		return (ret);
741 	}
742 
743 	if (ino_p = pcmu_ib_locate_ino(pib_p, PCMU_IB_MONDO_TO_INO(mondo))) {
744 		if (ih_p = pcmu_ib_ino_locate_intr(ino_p,
745 		    rdip, hdlp->ih_inum)) {
746 			ih_p->ih_intr_state = new_intr_state;
747 			ret = DDI_SUCCESS;
748 		}
749 	}
750 	mutex_exit(&pib_p->pib_ino_lst_mutex);
751 	return (ret);
752 }
753