xref: /illumos-gate/usr/src/uts/sun4u/io/pci/pci_ib.c (revision 09b1eac2)
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
5b0fc0e77Sgovinda  * Common Development and Distribution License (the "License").
6b0fc0e77Sgovinda  * 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 /*
22*09b1eac2SEvan Yan  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * PCI Interrupt Block (RISCx) implementation
287c478bd9Sstevel@tonic-gate  *	initialization
297c478bd9Sstevel@tonic-gate  *	interrupt enable/disable/clear and mapping register manipulation
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
347c478bd9Sstevel@tonic-gate #include <sys/async.h>
357c478bd9Sstevel@tonic-gate #include <sys/systm.h>		/* panicstr */
367c478bd9Sstevel@tonic-gate #include <sys/spl.h>
377c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
387c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>	/* intr_dist_add */
397c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
407c478bd9Sstevel@tonic-gate #include <sys/clock.h>
417c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
427c478bd9Sstevel@tonic-gate #include <sys/pci/pci_obj.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #ifdef _STARFIRE
457c478bd9Sstevel@tonic-gate #include <sys/starfire.h>
467c478bd9Sstevel@tonic-gate #endif /* _STARFIRE */
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
497c478bd9Sstevel@tonic-gate static uint_t ib_intr_reset(void *arg);
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate void
527c478bd9Sstevel@tonic-gate ib_create(pci_t *pci_p)
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate 	dev_info_t *dip = pci_p->pci_dip;
557c478bd9Sstevel@tonic-gate 	ib_t *ib_p;
567c478bd9Sstevel@tonic-gate 	uintptr_t a;
577c478bd9Sstevel@tonic-gate 	int i;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	/*
607c478bd9Sstevel@tonic-gate 	 * Allocate interrupt block state structure and link it to
617c478bd9Sstevel@tonic-gate 	 * the pci state structure.
627c478bd9Sstevel@tonic-gate 	 */
637c478bd9Sstevel@tonic-gate 	ib_p = kmem_zalloc(sizeof (ib_t), KM_SLEEP);
647c478bd9Sstevel@tonic-gate 	pci_p->pci_ib_p = ib_p;
657c478bd9Sstevel@tonic-gate 	ib_p->ib_pci_p = pci_p;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	a = pci_ib_setup(ib_p);
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	/*
707c478bd9Sstevel@tonic-gate 	 * Determine virtual addresses of interrupt mapping, clear and diag
717c478bd9Sstevel@tonic-gate 	 * registers that have common offsets.
727c478bd9Sstevel@tonic-gate 	 */
737c478bd9Sstevel@tonic-gate 	ib_p->ib_slot_clear_intr_regs =
74f0d69850Srameshc 	    a + COMMON_IB_SLOT_CLEAR_INTR_REG_OFFSET;
757c478bd9Sstevel@tonic-gate 	ib_p->ib_intr_retry_timer_reg =
76f0d69850Srameshc 	    (uint64_t *)(a + COMMON_IB_INTR_RETRY_TIMER_OFFSET);
777c478bd9Sstevel@tonic-gate 	ib_p->ib_slot_intr_state_diag_reg =
78f0d69850Srameshc 	    (uint64_t *)(a + COMMON_IB_SLOT_INTR_STATE_DIAG_REG);
797c478bd9Sstevel@tonic-gate 	ib_p->ib_obio_intr_state_diag_reg =
80f0d69850Srameshc 	    (uint64_t *)(a + COMMON_IB_OBIO_INTR_STATE_DIAG_REG);
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	if (CHIP_TYPE(pci_p) != PCI_CHIP_XMITS) {
837c478bd9Sstevel@tonic-gate 		ib_p->ib_upa_imr[0] = (volatile uint64_t *)
84f0d69850Srameshc 		    (a + COMMON_IB_UPA0_INTR_MAP_REG_OFFSET);
857c478bd9Sstevel@tonic-gate 		ib_p->ib_upa_imr[1] = (volatile uint64_t *)
86f0d69850Srameshc 		    (a + COMMON_IB_UPA1_INTR_MAP_REG_OFFSET);
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	DEBUG2(DBG_ATTACH, dip, "ib_create: slot_imr=%x, slot_cir=%x\n",
90f0d69850Srameshc 	    ib_p->ib_slot_intr_map_regs, ib_p->ib_obio_intr_map_regs);
917c478bd9Sstevel@tonic-gate 	DEBUG2(DBG_ATTACH, dip, "ib_create: obio_imr=%x, obio_cir=%x\n",
92f0d69850Srameshc 	    ib_p->ib_slot_clear_intr_regs, ib_p->ib_obio_clear_intr_regs);
937c478bd9Sstevel@tonic-gate 	DEBUG2(DBG_ATTACH, dip, "ib_create: upa0_imr=%x, upa1_imr=%x\n",
94f0d69850Srameshc 	    ib_p->ib_upa_imr[0], ib_p->ib_upa_imr[1]);
957c478bd9Sstevel@tonic-gate 	DEBUG3(DBG_ATTACH, dip,
96f0d69850Srameshc 	    "ib_create: retry_timer=%x, obio_diag=%x slot_diag=%x\n",
97f0d69850Srameshc 	    ib_p->ib_intr_retry_timer_reg,
98f0d69850Srameshc 	    ib_p->ib_obio_intr_state_diag_reg,
99f0d69850Srameshc 	    ib_p->ib_slot_intr_state_diag_reg);
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	ib_p->ib_ino_lst = (ib_ino_info_t *)NULL;
1027c478bd9Sstevel@tonic-gate 	mutex_init(&ib_p->ib_intr_lock, NULL, MUTEX_DRIVER, NULL);
1037c478bd9Sstevel@tonic-gate 	mutex_init(&ib_p->ib_ino_lst_mutex, NULL, MUTEX_DRIVER, NULL);
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	DEBUG1(DBG_ATTACH, dip, "ib_create: numproxy=%x\n",
106f0d69850Srameshc 	    pci_p->pci_numproxy);
1077c478bd9Sstevel@tonic-gate 	for (i = 1; i <= pci_p->pci_numproxy; i++) {
1087c478bd9Sstevel@tonic-gate 		set_intr_mapping_reg(pci_p->pci_id,
109f0d69850Srameshc 		    (uint64_t *)ib_p->ib_upa_imr[i - 1], i);
1107c478bd9Sstevel@tonic-gate 	}
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	ib_configure(ib_p);
1137c478bd9Sstevel@tonic-gate 	bus_func_register(BF_TYPE_RESINTR, ib_intr_reset, ib_p);
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate void
1177c478bd9Sstevel@tonic-gate ib_destroy(pci_t *pci_p)
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate 	ib_t *ib_p = pci_p->pci_ib_p;
1207c478bd9Sstevel@tonic-gate 	dev_info_t *dip = pci_p->pci_dip;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	DEBUG0(DBG_IB, dip, "ib_destroy\n");
1237c478bd9Sstevel@tonic-gate 	bus_func_unregister(BF_TYPE_RESINTR, ib_intr_reset, ib_p);
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	intr_dist_rem_weighted(ib_intr_dist_all, ib_p);
1267c478bd9Sstevel@tonic-gate 	mutex_destroy(&ib_p->ib_ino_lst_mutex);
1277c478bd9Sstevel@tonic-gate 	mutex_destroy(&ib_p->ib_intr_lock);
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	ib_free_ino_all(ib_p);
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	kmem_free(ib_p, sizeof (ib_t));
1327c478bd9Sstevel@tonic-gate 	pci_p->pci_ib_p = NULL;
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate void
1367c478bd9Sstevel@tonic-gate ib_configure(ib_t *ib_p)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate 	/* XXX could be different between psycho and schizo */
1397c478bd9Sstevel@tonic-gate 	*ib_p->ib_intr_retry_timer_reg = pci_intr_retry_intv;
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate /*
1437c478bd9Sstevel@tonic-gate  * can only used for psycho internal interrupts thermal, power,
1447c478bd9Sstevel@tonic-gate  * ue, ce, pbm
1457c478bd9Sstevel@tonic-gate  */
1467c478bd9Sstevel@tonic-gate void
1477c478bd9Sstevel@tonic-gate ib_intr_enable(pci_t *pci_p, ib_ino_t ino)
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate 	ib_t *ib_p = pci_p->pci_ib_p;
1507c478bd9Sstevel@tonic-gate 	ib_mondo_t mondo = IB_INO_TO_MONDO(ib_p, ino);
1517c478bd9Sstevel@tonic-gate 	volatile uint64_t *imr_p = ib_intr_map_reg_addr(ib_p, ino);
1527c478bd9Sstevel@tonic-gate 	uint_t cpu_id;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	/*
1557c478bd9Sstevel@tonic-gate 	 * Determine the cpu for the interrupt.
1567c478bd9Sstevel@tonic-gate 	 */
1577c478bd9Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_intr_lock);
1587c478bd9Sstevel@tonic-gate 	cpu_id = intr_dist_cpuid();
1597c478bd9Sstevel@tonic-gate #ifdef _STARFIRE
1607c478bd9Sstevel@tonic-gate 	cpu_id = pc_translate_tgtid(IB2CB(ib_p)->cb_ittrans_cookie, cpu_id,
161f0d69850Srameshc 	    IB_GET_MAPREG_INO(ino));
1627c478bd9Sstevel@tonic-gate #endif /* _STARFIRE */
1637c478bd9Sstevel@tonic-gate 	DEBUG2(DBG_IB, pci_p->pci_dip,
164f0d69850Srameshc 	    "ib_intr_enable: ino=%x cpu_id=%x\n", ino, cpu_id);
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	*imr_p = ib_get_map_reg(mondo, cpu_id);
1677c478bd9Sstevel@tonic-gate 	IB_INO_INTR_CLEAR(ib_clear_intr_reg_addr(ib_p, ino));
1687c478bd9Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_intr_lock);
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate /*
1727c478bd9Sstevel@tonic-gate  * Disable the interrupt via its interrupt mapping register.
1737c478bd9Sstevel@tonic-gate  * Can only be used for internal interrupts: thermal, power, ue, ce, pbm.
1747c478bd9Sstevel@tonic-gate  * If called under interrupt context, wait should be set to 0
1757c478bd9Sstevel@tonic-gate  */
1767c478bd9Sstevel@tonic-gate void
1777c478bd9Sstevel@tonic-gate ib_intr_disable(ib_t *ib_p, ib_ino_t ino, int wait)
1787c478bd9Sstevel@tonic-gate {
1797c478bd9Sstevel@tonic-gate 	volatile uint64_t *imr_p = ib_intr_map_reg_addr(ib_p, ino);
1807c478bd9Sstevel@tonic-gate 	volatile uint64_t *state_reg_p = IB_INO_INTR_STATE_REG(ib_p, ino);
1817c478bd9Sstevel@tonic-gate 	hrtime_t start_time;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	/* disable the interrupt */
1847c478bd9Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_intr_lock);
1857c478bd9Sstevel@tonic-gate 	IB_INO_INTR_OFF(imr_p);
1867c478bd9Sstevel@tonic-gate 	*imr_p;	/* flush previous write */
1877c478bd9Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_intr_lock);
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	if (!wait)
1907c478bd9Sstevel@tonic-gate 		goto wait_done;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	start_time = gethrtime();
1937c478bd9Sstevel@tonic-gate 	/* busy wait if there is interrupt being processed */
1947c478bd9Sstevel@tonic-gate 	while (IB_INO_INTR_PENDING(state_reg_p, ino) && !panicstr) {
1957c478bd9Sstevel@tonic-gate 		if (gethrtime() - start_time > pci_intrpend_timeout) {
1967c478bd9Sstevel@tonic-gate 			pbm_t *pbm_p = ib_p->ib_pci_p->pci_pbm_p;
1977c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s:%s: ib_intr_disable timeout %x",
198f0d69850Srameshc 			    pbm_p->pbm_nameinst_str,
199f0d69850Srameshc 			    pbm_p->pbm_nameaddr_str, ino);
2007c478bd9Sstevel@tonic-gate 				break;
2017c478bd9Sstevel@tonic-gate 		}
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate wait_done:
2047c478bd9Sstevel@tonic-gate 	IB_INO_INTR_PEND(ib_clear_intr_reg_addr(ib_p, ino));
2057c478bd9Sstevel@tonic-gate #ifdef _STARFIRE
2067c478bd9Sstevel@tonic-gate 	pc_ittrans_cleanup(IB2CB(ib_p)->cb_ittrans_cookie,
207f47a9c50Smathue 	    (volatile uint64_t *)(uintptr_t)ino);
2087c478bd9Sstevel@tonic-gate #endif /* _STARFIRE */
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate /* can only used for psycho internal interrupts thermal, power, ue, ce, pbm */
2127c478bd9Sstevel@tonic-gate void
2137c478bd9Sstevel@tonic-gate ib_nintr_clear(ib_t *ib_p, ib_ino_t ino)
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate 	uint64_t *clr_reg = ib_clear_intr_reg_addr(ib_p, ino);
2167c478bd9Sstevel@tonic-gate 	IB_INO_INTR_CLEAR(clr_reg);
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate /*
2207c478bd9Sstevel@tonic-gate  * distribute PBM and UPA interrupts. ino is set to 0 by caller if we
2217c478bd9Sstevel@tonic-gate  * are dealing with UPA interrupts (without inos).
2227c478bd9Sstevel@tonic-gate  */
2237c478bd9Sstevel@tonic-gate void
2247c478bd9Sstevel@tonic-gate ib_intr_dist_nintr(ib_t *ib_p, ib_ino_t ino, volatile uint64_t *imr_p)
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate 	volatile uint64_t imr = *imr_p;
2277c478bd9Sstevel@tonic-gate 	uint32_t cpu_id;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	if (!IB_INO_INTR_ISON(imr))
2307c478bd9Sstevel@tonic-gate 		return;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	cpu_id = intr_dist_cpuid();
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate #ifdef _STARFIRE
2357c478bd9Sstevel@tonic-gate 	if (ino) {
2367c478bd9Sstevel@tonic-gate 		cpu_id = pc_translate_tgtid(IB2CB(ib_p)->cb_ittrans_cookie,
237f0d69850Srameshc 		    cpu_id, IB_GET_MAPREG_INO(ino));
2387c478bd9Sstevel@tonic-gate 	}
2397c478bd9Sstevel@tonic-gate #else /* _STARFIRE */
2407c478bd9Sstevel@tonic-gate 	if (ib_map_reg_get_cpu(*imr_p) == cpu_id)
2417c478bd9Sstevel@tonic-gate 		return;
2427c478bd9Sstevel@tonic-gate #endif /* _STARFIRE */
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	*imr_p = ib_get_map_reg(IB_IMR2MONDO(imr), cpu_id);
2457c478bd9Sstevel@tonic-gate 	imr = *imr_p;	/* flush previous write */
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate 
2487851eb82Sschwartz /*
2497851eb82Sschwartz  * Converts into nsec, ticks logged with a given CPU.  Adds nsec to ih.
2507851eb82Sschwartz  */
2517851eb82Sschwartz /*ARGSUSED*/
2527851eb82Sschwartz void
2537851eb82Sschwartz ib_cpu_ticks_to_ih_nsec(ib_t *ib_p, ih_t *ih_p, uint32_t cpu_id)
2547851eb82Sschwartz {
2557851eb82Sschwartz 	extern kmutex_t pciintr_ks_template_lock;
2567851eb82Sschwartz 	hrtime_t ticks;
2577851eb82Sschwartz 
2587851eb82Sschwartz 	/*
2597851eb82Sschwartz 	 * Because we are updating two fields in ih_t we must lock
2607851eb82Sschwartz 	 * pciintr_ks_template_lock to prevent someone from reading the
2617851eb82Sschwartz 	 * kstats after we set ih_ticks to 0 and before we increment
2627851eb82Sschwartz 	 * ih_nsec to compensate.
2637851eb82Sschwartz 	 *
2647851eb82Sschwartz 	 * We must also protect against the interrupt arriving and incrementing
2657851eb82Sschwartz 	 * ih_ticks between the time we read it and when we reset it to 0.
2667851eb82Sschwartz 	 * To do this we use atomic_swap.
2677851eb82Sschwartz 	 */
2687851eb82Sschwartz 
2697851eb82Sschwartz 	ASSERT(MUTEX_HELD(&ib_p->ib_ino_lst_mutex));
2707851eb82Sschwartz 
2717851eb82Sschwartz 	mutex_enter(&pciintr_ks_template_lock);
2727851eb82Sschwartz 	ticks = atomic_swap_64(&ih_p->ih_ticks, 0);
2737851eb82Sschwartz 	ih_p->ih_nsec += (uint64_t)tick2ns(ticks, cpu_id);
2747851eb82Sschwartz 	mutex_exit(&pciintr_ks_template_lock);
2757851eb82Sschwartz }
2767851eb82Sschwartz 
2777c478bd9Sstevel@tonic-gate static void
2787c478bd9Sstevel@tonic-gate ib_intr_dist(ib_t *ib_p, ib_ino_info_t *ino_p)
2797c478bd9Sstevel@tonic-gate {
2807c478bd9Sstevel@tonic-gate 	uint32_t cpu_id = ino_p->ino_cpuid;
2817c478bd9Sstevel@tonic-gate 	ib_ino_t ino = ino_p->ino_ino;
2827c478bd9Sstevel@tonic-gate 	volatile uint64_t imr, *imr_p, *state_reg;
2837c478bd9Sstevel@tonic-gate 	hrtime_t start_time;
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ib_p->ib_ino_lst_mutex));
2867c478bd9Sstevel@tonic-gate 	imr_p = ib_intr_map_reg_addr(ib_p, ino);
2877c478bd9Sstevel@tonic-gate 	state_reg = IB_INO_INTR_STATE_REG(ib_p, ino);
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate #ifdef _STARFIRE
2907c478bd9Sstevel@tonic-gate 	/*
2917c478bd9Sstevel@tonic-gate 	 * For Starfire it is a pain to check the current target for
2927c478bd9Sstevel@tonic-gate 	 * the mondo since we have to read the PC asics ITTR slot
2937c478bd9Sstevel@tonic-gate 	 * assigned to this mondo. It will be much easier to assume
2947c478bd9Sstevel@tonic-gate 	 * the current target is always different and do the target
2957c478bd9Sstevel@tonic-gate 	 * reprogram all the time.
2967c478bd9Sstevel@tonic-gate 	 */
2977c478bd9Sstevel@tonic-gate 	cpu_id = pc_translate_tgtid(IB2CB(ib_p)->cb_ittrans_cookie, cpu_id,
298f0d69850Srameshc 	    IB_GET_MAPREG_INO(ino));
2997c478bd9Sstevel@tonic-gate #else
3007c478bd9Sstevel@tonic-gate 	if (ib_map_reg_get_cpu(*imr_p) == cpu_id) /* same cpu, no reprog */
3017c478bd9Sstevel@tonic-gate 		return;
3027c478bd9Sstevel@tonic-gate #endif /* _STARFIRE */
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	/* disable interrupt, this could disrupt devices sharing our slot */
3057c478bd9Sstevel@tonic-gate 	IB_INO_INTR_OFF(imr_p);
3067c478bd9Sstevel@tonic-gate 	imr = *imr_p;	/* flush previous write */
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	/* busy wait if there is interrupt being processed */
3097c478bd9Sstevel@tonic-gate 	start_time = gethrtime();
3107c478bd9Sstevel@tonic-gate 	while (IB_INO_INTR_PENDING(state_reg, ino) && !panicstr) {
3117c478bd9Sstevel@tonic-gate 		if (gethrtime() - start_time > pci_intrpend_timeout) {
3127c478bd9Sstevel@tonic-gate 			pbm_t *pbm_p = ib_p->ib_pci_p->pci_pbm_p;
3137c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s:%s: ib_intr_dist(%p,%x) timeout",
314f0d69850Srameshc 			    pbm_p->pbm_nameinst_str,
315f0d69850Srameshc 			    pbm_p->pbm_nameaddr_str,
316f0d69850Srameshc 			    imr_p, IB_INO_TO_MONDO(ib_p, ino));
3177c478bd9Sstevel@tonic-gate 			break;
3187c478bd9Sstevel@tonic-gate 		}
3197c478bd9Sstevel@tonic-gate 	}
3207c478bd9Sstevel@tonic-gate 	*imr_p = ib_get_map_reg(IB_IMR2MONDO(imr), cpu_id);
3217c478bd9Sstevel@tonic-gate 	imr = *imr_p;	/* flush previous write */
3227c478bd9Sstevel@tonic-gate }
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate /*
3257c478bd9Sstevel@tonic-gate  * Redistribute interrupts of the specified weight. The first call has a weight
3267c478bd9Sstevel@tonic-gate  * of weight_max, which can be used to trigger initialization for
3277c478bd9Sstevel@tonic-gate  * redistribution. The inos with weight [weight_max, inf.) should be processed
3287c478bd9Sstevel@tonic-gate  * on the "weight == weight_max" call.  This first call is followed by calls
3297c478bd9Sstevel@tonic-gate  * of decreasing weights, inos of that weight should be processed.  The final
3307c478bd9Sstevel@tonic-gate  * call specifies a weight of zero, this can be used to trigger processing of
3317c478bd9Sstevel@tonic-gate  * stragglers.
3327c478bd9Sstevel@tonic-gate  */
3337c478bd9Sstevel@tonic-gate void
3347c478bd9Sstevel@tonic-gate ib_intr_dist_all(void *arg, int32_t weight_max, int32_t weight)
3357c478bd9Sstevel@tonic-gate {
3367c478bd9Sstevel@tonic-gate 	ib_t *ib_p = (ib_t *)arg;
3377c478bd9Sstevel@tonic-gate 	pci_t *pci_p = ib_p->ib_pci_p;
3387c478bd9Sstevel@tonic-gate 	ib_ino_info_t *ino_p;
339b0fc0e77Sgovinda 	ib_ino_pil_t *ipil_p;
3407c478bd9Sstevel@tonic-gate 	ih_t *ih_lst;
3417c478bd9Sstevel@tonic-gate 	int32_t dweight;
3427c478bd9Sstevel@tonic-gate 	int i;
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	if (weight == 0) {
3457c478bd9Sstevel@tonic-gate 		mutex_enter(&ib_p->ib_intr_lock);
3467c478bd9Sstevel@tonic-gate 		if (CHIP_TYPE(pci_p) != PCI_CHIP_XMITS) {
3477c478bd9Sstevel@tonic-gate 			for (i = 0; i < 2; i++)
3487c478bd9Sstevel@tonic-gate 				ib_intr_dist_nintr(ib_p, 0,
3497c478bd9Sstevel@tonic-gate 				    ib_p->ib_upa_imr[i]);
3507c478bd9Sstevel@tonic-gate 		}
3517c478bd9Sstevel@tonic-gate 		mutex_exit(&ib_p->ib_intr_lock);
3527c478bd9Sstevel@tonic-gate 	}
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	/* Perform special processing for first call of a redistribution. */
3577c478bd9Sstevel@tonic-gate 	if (weight == weight_max) {
358b0fc0e77Sgovinda 		for (ino_p = ib_p->ib_ino_lst; ino_p;
359b0fc0e77Sgovinda 		    ino_p = ino_p->ino_next_p) {
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 			/*
3627c478bd9Sstevel@tonic-gate 			 * Clear ino_established of each ino on first call.
3637c478bd9Sstevel@tonic-gate 			 * The ino_established field may be used by a pci
3647c478bd9Sstevel@tonic-gate 			 * nexus driver's pci_intr_dist_cpuid implementation
3657c478bd9Sstevel@tonic-gate 			 * when detection of established pci slot-cpu binding
3667c478bd9Sstevel@tonic-gate 			 * for multi function pci cards.
3677c478bd9Sstevel@tonic-gate 			 */
3687c478bd9Sstevel@tonic-gate 			ino_p->ino_established = 0;
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 			/*
3717c478bd9Sstevel@tonic-gate 			 * recompute the ino_intr_weight based on the device
3727c478bd9Sstevel@tonic-gate 			 * weight of all devinfo nodes sharing the ino (this
3737c478bd9Sstevel@tonic-gate 			 * will allow us to pick up new weights established by
3747c478bd9Sstevel@tonic-gate 			 * i_ddi_set_intr_weight()).
3757c478bd9Sstevel@tonic-gate 			 */
3767c478bd9Sstevel@tonic-gate 			ino_p->ino_intr_weight = 0;
377b0fc0e77Sgovinda 
378b0fc0e77Sgovinda 			for (ipil_p = ino_p->ino_ipil_p; ipil_p;
379b0fc0e77Sgovinda 			    ipil_p = ipil_p->ipil_next_p) {
380b0fc0e77Sgovinda 				for (i = 0, ih_lst = ipil_p->ipil_ih_head;
381b0fc0e77Sgovinda 				    i < ipil_p->ipil_ih_size; i++,
382b0fc0e77Sgovinda 				    ih_lst = ih_lst->ih_next) {
383b0fc0e77Sgovinda 					dweight = i_ddi_get_intr_weight
384b0fc0e77Sgovinda 					    (ih_lst->ih_dip);
385b0fc0e77Sgovinda 					if (dweight > 0)
386b0fc0e77Sgovinda 						ino_p->ino_intr_weight +=
387b0fc0e77Sgovinda 						    dweight;
388b0fc0e77Sgovinda 				}
3897c478bd9Sstevel@tonic-gate 			}
3907c478bd9Sstevel@tonic-gate 		}
3917c478bd9Sstevel@tonic-gate 	}
3927c478bd9Sstevel@tonic-gate 
393b0fc0e77Sgovinda 	for (ino_p = ib_p->ib_ino_lst; ino_p; ino_p = ino_p->ino_next_p) {
3947c478bd9Sstevel@tonic-gate 		uint32_t orig_cpuid;
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 		/*
3977c478bd9Sstevel@tonic-gate 		 * Get the weight of the ino and determine if we are going to
3987c478bd9Sstevel@tonic-gate 		 * process call.  We wait until an ib_intr_dist_all call of
3997c478bd9Sstevel@tonic-gate 		 * the proper weight occurs to support redistribution of all
4007c478bd9Sstevel@tonic-gate 		 * heavy weighted interrupts first (across all nexus driver
4017c478bd9Sstevel@tonic-gate 		 * instances).  This is done to ensure optimal
4027c478bd9Sstevel@tonic-gate 		 * INTR_WEIGHTED_DIST behavior.
4037c478bd9Sstevel@tonic-gate 		 */
4047c478bd9Sstevel@tonic-gate 		if ((weight == ino_p->ino_intr_weight) ||
4057c478bd9Sstevel@tonic-gate 		    ((weight >= weight_max) &&
4067c478bd9Sstevel@tonic-gate 		    (ino_p->ino_intr_weight >= weight_max))) {
4077c478bd9Sstevel@tonic-gate 			/* select cpuid to target and mark ino established */
4087c478bd9Sstevel@tonic-gate 			orig_cpuid = ino_p->ino_cpuid;
4097c478bd9Sstevel@tonic-gate 			if (cpu[orig_cpuid] == NULL)
4107c478bd9Sstevel@tonic-gate 				orig_cpuid = CPU->cpu_id;
4117c478bd9Sstevel@tonic-gate 			ino_p->ino_cpuid = pci_intr_dist_cpuid(ib_p, ino_p);
4127c478bd9Sstevel@tonic-gate 			ino_p->ino_established = 1;
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 			/* Add device weight of ino devinfos to targeted cpu. */
415b0fc0e77Sgovinda 			for (ipil_p = ino_p->ino_ipil_p; ipil_p;
416b0fc0e77Sgovinda 			    ipil_p = ipil_p->ipil_next_p) {
417b0fc0e77Sgovinda 				for (i = 0, ih_lst = ipil_p->ipil_ih_head;
418b0fc0e77Sgovinda 				    i < ipil_p->ipil_ih_size; i++,
419b0fc0e77Sgovinda 				    ih_lst = ih_lst->ih_next) {
420b0fc0e77Sgovinda 
421b0fc0e77Sgovinda 					dweight = i_ddi_get_intr_weight(
422b0fc0e77Sgovinda 					    ih_lst->ih_dip);
423b0fc0e77Sgovinda 					intr_dist_cpuid_add_device_weight(
424b0fc0e77Sgovinda 					    ino_p->ino_cpuid, ih_lst->ih_dip,
425b0fc0e77Sgovinda 					    dweight);
426b0fc0e77Sgovinda 
427b0fc0e77Sgovinda 					/*
428b0fc0e77Sgovinda 					 * Different cpus may have different
429b0fc0e77Sgovinda 					 * clock speeds. to account for this,
430b0fc0e77Sgovinda 					 * whenever an interrupt is moved to a
431b0fc0e77Sgovinda 					 * new CPU, we convert the accumulated
432b0fc0e77Sgovinda 					 * ticks into nsec, based upon the clock
433b0fc0e77Sgovinda 					 * rate of the prior CPU.
434b0fc0e77Sgovinda 					 *
435b0fc0e77Sgovinda 					 * It is possible that the prior CPU no
436b0fc0e77Sgovinda 					 * longer exists. In this case, fall
437b0fc0e77Sgovinda 					 * back to using this CPU's clock rate.
438b0fc0e77Sgovinda 					 *
439b0fc0e77Sgovinda 					 * Note that the value in ih_ticks has
440b0fc0e77Sgovinda 					 * already been corrected for any power
441b0fc0e77Sgovinda 					 * savings mode which might have been
442b0fc0e77Sgovinda 					 * in effect.
443b0fc0e77Sgovinda 					 */
444b0fc0e77Sgovinda 					ib_cpu_ticks_to_ih_nsec(ib_p, ih_lst,
445b0fc0e77Sgovinda 					    orig_cpuid);
446b0fc0e77Sgovinda 				}
4477c478bd9Sstevel@tonic-gate 			}
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 			/* program the hardware */
4507c478bd9Sstevel@tonic-gate 			ib_intr_dist(ib_p, ino_p);
4517c478bd9Sstevel@tonic-gate 		}
4527c478bd9Sstevel@tonic-gate 	}
4537c478bd9Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
4547c478bd9Sstevel@tonic-gate }
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate /*
4577c478bd9Sstevel@tonic-gate  * Reset interrupts to IDLE.  This function is called during
4587c478bd9Sstevel@tonic-gate  * panic handling after redistributing interrupts; it's needed to
4597c478bd9Sstevel@tonic-gate  * support dumping to network devices after 'sync' from OBP.
4607c478bd9Sstevel@tonic-gate  *
4617c478bd9Sstevel@tonic-gate  * N.B.  This routine runs in a context where all other threads
4627c478bd9Sstevel@tonic-gate  * are permanently suspended.
4637c478bd9Sstevel@tonic-gate  */
4647c478bd9Sstevel@tonic-gate static uint_t
4657c478bd9Sstevel@tonic-gate ib_intr_reset(void *arg)
4667c478bd9Sstevel@tonic-gate {
4677c478bd9Sstevel@tonic-gate 	ib_t *ib_p = (ib_t *)arg;
4687c478bd9Sstevel@tonic-gate 	ib_ino_t ino;
4697c478bd9Sstevel@tonic-gate 	uint64_t *clr_reg;
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	/*
4727c478bd9Sstevel@tonic-gate 	 * Note that we only actually care about interrupts that are
4737c478bd9Sstevel@tonic-gate 	 * potentially from network devices.
4747c478bd9Sstevel@tonic-gate 	 */
4757c478bd9Sstevel@tonic-gate 	for (ino = 0; ino <= ib_p->ib_max_ino; ino++) {
4767c478bd9Sstevel@tonic-gate 		clr_reg = ib_clear_intr_reg_addr(ib_p, ino);
4777c478bd9Sstevel@tonic-gate 		IB_INO_INTR_CLEAR(clr_reg);
4787c478bd9Sstevel@tonic-gate 	}
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	return (BF_NONE);
4817c478bd9Sstevel@tonic-gate }
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate void
4847c478bd9Sstevel@tonic-gate ib_suspend(ib_t *ib_p)
4857c478bd9Sstevel@tonic-gate {
4867c478bd9Sstevel@tonic-gate 	ib_ino_info_t *ip;
4877c478bd9Sstevel@tonic-gate 	pci_t *pci_p = ib_p->ib_pci_p;
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	/* save ino_lst interrupts' mapping registers content */
4907c478bd9Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
491b0fc0e77Sgovinda 	for (ip = ib_p->ib_ino_lst; ip; ip = ip->ino_next_p)
4927c478bd9Sstevel@tonic-gate 		ip->ino_map_reg_save = *ip->ino_map_reg;
4937c478bd9Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	if (CHIP_TYPE(pci_p) != PCI_CHIP_XMITS) {
4967c478bd9Sstevel@tonic-gate 		ib_p->ib_upa_imr_state[0] = *ib_p->ib_upa_imr[0];
4977c478bd9Sstevel@tonic-gate 		ib_p->ib_upa_imr_state[1] = *ib_p->ib_upa_imr[1];
4987c478bd9Sstevel@tonic-gate 	}
4997c478bd9Sstevel@tonic-gate }
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate void
5027c478bd9Sstevel@tonic-gate ib_resume(ib_t *ib_p)
5037c478bd9Sstevel@tonic-gate {
5047c478bd9Sstevel@tonic-gate 	ib_ino_info_t *ip;
5057c478bd9Sstevel@tonic-gate 	pci_t *pci_p = ib_p->ib_pci_p;
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	/* restore ino_lst interrupts' mapping registers content */
5087c478bd9Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
509b0fc0e77Sgovinda 	for (ip = ib_p->ib_ino_lst; ip; ip = ip->ino_next_p) {
5107c478bd9Sstevel@tonic-gate 		IB_INO_INTR_CLEAR(ip->ino_clr_reg);	 /* set intr to idle */
5117c478bd9Sstevel@tonic-gate 		*ip->ino_map_reg = ip->ino_map_reg_save; /* restore IMR */
5127c478bd9Sstevel@tonic-gate 	}
5137c478bd9Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	if (CHIP_TYPE(pci_p) != PCI_CHIP_XMITS) {
5167c478bd9Sstevel@tonic-gate 		*ib_p->ib_upa_imr[0] = ib_p->ib_upa_imr_state[0];
5177c478bd9Sstevel@tonic-gate 		*ib_p->ib_upa_imr[1] = ib_p->ib_upa_imr_state[1];
5187c478bd9Sstevel@tonic-gate 	}
5197c478bd9Sstevel@tonic-gate }
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate /*
5227c478bd9Sstevel@tonic-gate  * locate ino_info structure on ib_p->ib_ino_lst according to ino#
5237c478bd9Sstevel@tonic-gate  * returns NULL if not found.
5247c478bd9Sstevel@tonic-gate  */
5257c478bd9Sstevel@tonic-gate ib_ino_info_t *
5267c478bd9Sstevel@tonic-gate ib_locate_ino(ib_t *ib_p, ib_ino_t ino_num)
5277c478bd9Sstevel@tonic-gate {
5287c478bd9Sstevel@tonic-gate 	ib_ino_info_t *ino_p = ib_p->ib_ino_lst;
5297c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ib_p->ib_ino_lst_mutex));
5307c478bd9Sstevel@tonic-gate 
531f0d69850Srameshc 	for (; ino_p && ino_p->ino_ino != ino_num; ino_p = ino_p->ino_next_p)
532f0d69850Srameshc 		;
5337c478bd9Sstevel@tonic-gate 	return (ino_p);
5347c478bd9Sstevel@tonic-gate }
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate #define	IB_INO_TO_SLOT(ino) (IB_IS_OBIO_INO(ino) ? 0xff : ((ino) & 0x1f) >> 2)
5377c478bd9Sstevel@tonic-gate 
538b0fc0e77Sgovinda ib_ino_pil_t *
539b0fc0e77Sgovinda ib_new_ino_pil(ib_t *ib_p, ib_ino_t ino_num, uint_t pil, ih_t *ih_p)
5407c478bd9Sstevel@tonic-gate {
541b0fc0e77Sgovinda 	ib_ino_pil_t	*ipil_p = kmem_zalloc(sizeof (ib_ino_pil_t), KM_SLEEP);
542b0fc0e77Sgovinda 	ib_ino_info_t	*ino_p;
5437c478bd9Sstevel@tonic-gate 
544b0fc0e77Sgovinda 	if ((ino_p = ib_locate_ino(ib_p, ino_num)) == NULL) {
545b0fc0e77Sgovinda 		ino_p = kmem_zalloc(sizeof (ib_ino_info_t), KM_SLEEP);
546b0fc0e77Sgovinda 
547b0fc0e77Sgovinda 		ino_p->ino_next_p = ib_p->ib_ino_lst;
548b0fc0e77Sgovinda 		ib_p->ib_ino_lst = ino_p;
549b0fc0e77Sgovinda 
550b0fc0e77Sgovinda 		ino_p->ino_ino = ino_num;
551b0fc0e77Sgovinda 		ino_p->ino_slot_no = IB_INO_TO_SLOT(ino_num);
552b0fc0e77Sgovinda 		ino_p->ino_ib_p = ib_p;
553b0fc0e77Sgovinda 		ino_p->ino_clr_reg = ib_clear_intr_reg_addr(ib_p, ino_num);
554b0fc0e77Sgovinda 		ino_p->ino_map_reg = ib_intr_map_reg_addr(ib_p, ino_num);
555b0fc0e77Sgovinda 		ino_p->ino_unclaimed_intrs = 0;
556b0fc0e77Sgovinda 		ino_p->ino_lopil = pil;
557b0fc0e77Sgovinda 	}
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	ih_p->ih_next = ih_p;
560b0fc0e77Sgovinda 	ipil_p->ipil_pil = pil;
561b0fc0e77Sgovinda 	ipil_p->ipil_ih_head = ih_p;
562b0fc0e77Sgovinda 	ipil_p->ipil_ih_tail = ih_p;
563b0fc0e77Sgovinda 	ipil_p->ipil_ih_start = ih_p;
564b0fc0e77Sgovinda 	ipil_p->ipil_ih_size = 1;
565b0fc0e77Sgovinda 	ipil_p->ipil_ino_p = ino_p;
5667c478bd9Sstevel@tonic-gate 
567b0fc0e77Sgovinda 	ipil_p->ipil_next_p = ino_p->ino_ipil_p;
568b0fc0e77Sgovinda 	ino_p->ino_ipil_p = ipil_p;
569b0fc0e77Sgovinda 	ino_p->ino_ipil_size++;
570b0fc0e77Sgovinda 
571b0fc0e77Sgovinda 	if (ino_p->ino_lopil > pil)
572b0fc0e77Sgovinda 		ino_p->ino_lopil = pil;
573b0fc0e77Sgovinda 
574b0fc0e77Sgovinda 	return (ipil_p);
5757c478bd9Sstevel@tonic-gate }
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate void
578b0fc0e77Sgovinda ib_delete_ino_pil(ib_t *ib_p, ib_ino_pil_t *ipil_p)
5797c478bd9Sstevel@tonic-gate {
580b0fc0e77Sgovinda 	ib_ino_info_t	*ino_p = ipil_p->ipil_ino_p;
581b0fc0e77Sgovinda 	ib_ino_pil_t	*prev, *next;
582b0fc0e77Sgovinda 	ushort_t	pil = ipil_p->ipil_pil;
583b0fc0e77Sgovinda 
5847c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ib_p->ib_ino_lst_mutex));
585b0fc0e77Sgovinda 
586b0fc0e77Sgovinda 	if (ino_p->ino_ipil_p == ipil_p)
587b0fc0e77Sgovinda 		ino_p->ino_ipil_p = ipil_p->ipil_next_p;
5887c478bd9Sstevel@tonic-gate 	else {
589b0fc0e77Sgovinda 		for (prev = next = ino_p->ino_ipil_p; next != ipil_p;
590f0d69850Srameshc 		    prev = next, next = next->ipil_next_p)
591f0d69850Srameshc 			;
592b0fc0e77Sgovinda 
593b0fc0e77Sgovinda 		if (prev)
594b0fc0e77Sgovinda 			prev->ipil_next_p = ipil_p->ipil_next_p;
595b0fc0e77Sgovinda 	}
596b0fc0e77Sgovinda 
597b0fc0e77Sgovinda 	kmem_free(ipil_p, sizeof (ib_ino_pil_t));
598b0fc0e77Sgovinda 
599e4517573Srameshc 	if ((--ino_p->ino_ipil_size) && (ino_p->ino_lopil == pil)) {
600e4517573Srameshc 		for (next = ino_p->ino_ipil_p, pil = next->ipil_pil;
601e4517573Srameshc 		    next; next = next->ipil_next_p) {
602e4517573Srameshc 
603b0fc0e77Sgovinda 			if (pil > next->ipil_pil)
604b0fc0e77Sgovinda 				pil = next->ipil_pil;
605b0fc0e77Sgovinda 		}
606e4517573Srameshc 		/*
607e4517573Srameshc 		 * Value stored in pil should be the lowest pil.
608e4517573Srameshc 		 */
609b0fc0e77Sgovinda 		ino_p->ino_lopil = pil;
610b0fc0e77Sgovinda 	}
611b0fc0e77Sgovinda 
612e4517573Srameshc 	if (ino_p->ino_ipil_size)
613b0fc0e77Sgovinda 		return;
614b0fc0e77Sgovinda 
615b0fc0e77Sgovinda 	if (ib_p->ib_ino_lst == ino_p)
616b0fc0e77Sgovinda 		ib_p->ib_ino_lst = ino_p->ino_next_p;
617b0fc0e77Sgovinda 	else {
618b0fc0e77Sgovinda 		ib_ino_info_t	*list = ib_p->ib_ino_lst;
619b0fc0e77Sgovinda 
620f0d69850Srameshc 		for (; list->ino_next_p != ino_p; list = list->ino_next_p)
621f0d69850Srameshc 			;
622b0fc0e77Sgovinda 		list->ino_next_p = ino_p->ino_next_p;
6237c478bd9Sstevel@tonic-gate 	}
6247c478bd9Sstevel@tonic-gate }
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate /* free all ino when we are detaching */
6277c478bd9Sstevel@tonic-gate void
6287c478bd9Sstevel@tonic-gate ib_free_ino_all(ib_t *ib_p)
6297c478bd9Sstevel@tonic-gate {
630b0fc0e77Sgovinda 	ib_ino_info_t *ino_p = ib_p->ib_ino_lst;
6317c478bd9Sstevel@tonic-gate 	ib_ino_info_t *next = NULL;
632b0fc0e77Sgovinda 
633b0fc0e77Sgovinda 	while (ino_p) {
634b0fc0e77Sgovinda 		next = ino_p->ino_next_p;
635b0fc0e77Sgovinda 		kmem_free(ino_p, sizeof (ib_ino_info_t));
636b0fc0e77Sgovinda 		ino_p = next;
6377c478bd9Sstevel@tonic-gate 	}
6387c478bd9Sstevel@tonic-gate }
6397c478bd9Sstevel@tonic-gate 
640b0fc0e77Sgovinda /*
641b0fc0e77Sgovinda  * Locate ib_ino_pil_t structure on ino_p->ino_ipil_p according to ino#
642b0fc0e77Sgovinda  * returns NULL if not found.
643b0fc0e77Sgovinda  */
644b0fc0e77Sgovinda ib_ino_pil_t *
645b0fc0e77Sgovinda ib_ino_locate_ipil(ib_ino_info_t *ino_p, uint_t pil)
646b0fc0e77Sgovinda {
647b0fc0e77Sgovinda 	ib_ino_pil_t	*ipil_p = ino_p->ino_ipil_p;
648b0fc0e77Sgovinda 
649f0d69850Srameshc 	for (; ipil_p && ipil_p->ipil_pil != pil; ipil_p = ipil_p->ipil_next_p)
650f0d69850Srameshc 		;
651b0fc0e77Sgovinda 
652b0fc0e77Sgovinda 	return (ipil_p);
653b0fc0e77Sgovinda }
654b0fc0e77Sgovinda 
6557c478bd9Sstevel@tonic-gate void
656b0fc0e77Sgovinda ib_ino_add_intr(pci_t *pci_p, ib_ino_pil_t *ipil_p, ih_t *ih_p)
6577c478bd9Sstevel@tonic-gate {
658b0fc0e77Sgovinda 	ib_ino_info_t *ino_p = ipil_p->ipil_ino_p;
6597c478bd9Sstevel@tonic-gate 	ib_ino_t ino = ino_p->ino_ino;
6607c478bd9Sstevel@tonic-gate 	ib_t *ib_p = ino_p->ino_ib_p;
6617c478bd9Sstevel@tonic-gate 	volatile uint64_t *state_reg = IB_INO_INTR_STATE_REG(ib_p, ino);
6627c478bd9Sstevel@tonic-gate 	hrtime_t start_time;
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	ASSERT(ib_p == pci_p->pci_ib_p);
6657c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ib_p->ib_ino_lst_mutex));
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	/* disable interrupt, this could disrupt devices sharing our slot */
6687c478bd9Sstevel@tonic-gate 	IB_INO_INTR_OFF(ino_p->ino_map_reg);
6697c478bd9Sstevel@tonic-gate 	*ino_p->ino_map_reg;
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	/* do NOT modify the link list until after the busy wait */
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	/*
6747c478bd9Sstevel@tonic-gate 	 * busy wait if there is interrupt being processed.
6757c478bd9Sstevel@tonic-gate 	 * either the pending state will be cleared by the interrupt wrapper
6767c478bd9Sstevel@tonic-gate 	 * or the interrupt will be marked as blocked indicating that it was
6777c478bd9Sstevel@tonic-gate 	 * jabbering.
6787c478bd9Sstevel@tonic-gate 	 */
6797c478bd9Sstevel@tonic-gate 	start_time = gethrtime();
680b0fc0e77Sgovinda 	while ((ino_p->ino_unclaimed_intrs <= pci_unclaimed_intr_max) &&
681f0d69850Srameshc 	    IB_INO_INTR_PENDING(state_reg, ino) && !panicstr) {
6827c478bd9Sstevel@tonic-gate 		if (gethrtime() - start_time > pci_intrpend_timeout) {
6837c478bd9Sstevel@tonic-gate 			pbm_t *pbm_p = pci_p->pci_pbm_p;
6847c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s:%s: ib_ino_add_intr %x timeout",
685f0d69850Srameshc 			    pbm_p->pbm_nameinst_str,
686f0d69850Srameshc 			    pbm_p->pbm_nameaddr_str, ino);
6877c478bd9Sstevel@tonic-gate 			break;
6887c478bd9Sstevel@tonic-gate 		}
6897c478bd9Sstevel@tonic-gate 	}
6907c478bd9Sstevel@tonic-gate 
691b0fc0e77Sgovinda 	/* link up ih_t */
692b0fc0e77Sgovinda 	ih_p->ih_next = ipil_p->ipil_ih_head;
693b0fc0e77Sgovinda 	ipil_p->ipil_ih_tail->ih_next = ih_p;
694b0fc0e77Sgovinda 	ipil_p->ipil_ih_tail = ih_p;
6957c478bd9Sstevel@tonic-gate 
696b0fc0e77Sgovinda 	ipil_p->ipil_ih_start = ipil_p->ipil_ih_head;
697b0fc0e77Sgovinda 	ipil_p->ipil_ih_size++;
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	/*
7007c478bd9Sstevel@tonic-gate 	 * if the interrupt was previously blocked (left in pending state)
7017c478bd9Sstevel@tonic-gate 	 * because of jabber we need to clear the pending state in case the
7027c478bd9Sstevel@tonic-gate 	 * jabber has gone away.
7037c478bd9Sstevel@tonic-gate 	 */
704b0fc0e77Sgovinda 	if (ino_p->ino_unclaimed_intrs > pci_unclaimed_intr_max) {
7057c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
7067c478bd9Sstevel@tonic-gate 		    "%s%d: ib_ino_add_intr: ino 0x%x has been unblocked",
7077c478bd9Sstevel@tonic-gate 		    ddi_driver_name(pci_p->pci_dip),
7087c478bd9Sstevel@tonic-gate 		    ddi_get_instance(pci_p->pci_dip),
7097c478bd9Sstevel@tonic-gate 		    ino_p->ino_ino);
710b0fc0e77Sgovinda 		ino_p->ino_unclaimed_intrs = 0;
7117c478bd9Sstevel@tonic-gate 		IB_INO_INTR_CLEAR(ino_p->ino_clr_reg);
7127c478bd9Sstevel@tonic-gate 	}
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	/* re-enable interrupt */
7157c478bd9Sstevel@tonic-gate 	IB_INO_INTR_ON(ino_p->ino_map_reg);
7167c478bd9Sstevel@tonic-gate 	*ino_p->ino_map_reg;
7177c478bd9Sstevel@tonic-gate }
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate /*
7207c478bd9Sstevel@tonic-gate  * removes pci_ispec_t from the ino's link list.
7217c478bd9Sstevel@tonic-gate  * uses hardware mutex to lock out interrupt threads.
7227c478bd9Sstevel@tonic-gate  * Side effects: interrupt belongs to that ino is turned off on return.
7237c478bd9Sstevel@tonic-gate  * if we are sharing PCI slot with other inos, the caller needs
7247c478bd9Sstevel@tonic-gate  * to turn it back on.
7257c478bd9Sstevel@tonic-gate  */
7267c478bd9Sstevel@tonic-gate void
727b0fc0e77Sgovinda ib_ino_rem_intr(pci_t *pci_p, ib_ino_pil_t *ipil_p, ih_t *ih_p)
7287c478bd9Sstevel@tonic-gate {
729b0fc0e77Sgovinda 	ib_ino_info_t *ino_p = ipil_p->ipil_ino_p;
7307c478bd9Sstevel@tonic-gate 	int i;
7317c478bd9Sstevel@tonic-gate 	ib_ino_t ino = ino_p->ino_ino;
732b0fc0e77Sgovinda 	ih_t *ih_lst = ipil_p->ipil_ih_head;
7337c478bd9Sstevel@tonic-gate 	volatile uint64_t *state_reg =
734f0d69850Srameshc 	    IB_INO_INTR_STATE_REG(ino_p->ino_ib_p, ino);
7357c478bd9Sstevel@tonic-gate 	hrtime_t start_time;
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ino_p->ino_ib_p->ib_ino_lst_mutex));
7387c478bd9Sstevel@tonic-gate 	/* disable interrupt, this could disrupt devices sharing our slot */
7397c478bd9Sstevel@tonic-gate 	IB_INO_INTR_OFF(ino_p->ino_map_reg);
7407c478bd9Sstevel@tonic-gate 	*ino_p->ino_map_reg;
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	/* do NOT modify the link list until after the busy wait */
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	/*
7457c478bd9Sstevel@tonic-gate 	 * busy wait if there is interrupt being processed.
7467c478bd9Sstevel@tonic-gate 	 * either the pending state will be cleared by the interrupt wrapper
7477c478bd9Sstevel@tonic-gate 	 * or the interrupt will be marked as blocked indicating that it was
7487c478bd9Sstevel@tonic-gate 	 * jabbering.
7497c478bd9Sstevel@tonic-gate 	 */
7507c478bd9Sstevel@tonic-gate 	start_time = gethrtime();
751b0fc0e77Sgovinda 	while ((ino_p->ino_unclaimed_intrs <= pci_unclaimed_intr_max) &&
752f0d69850Srameshc 	    IB_INO_INTR_PENDING(state_reg, ino) && !panicstr) {
7537c478bd9Sstevel@tonic-gate 		if (gethrtime() - start_time > pci_intrpend_timeout) {
7547c478bd9Sstevel@tonic-gate 			pbm_t *pbm_p = pci_p->pci_pbm_p;
7557c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s:%s: ib_ino_rem_intr %x timeout",
756f0d69850Srameshc 			    pbm_p->pbm_nameinst_str,
757f0d69850Srameshc 			    pbm_p->pbm_nameaddr_str, ino);
7587c478bd9Sstevel@tonic-gate 			break;
7597c478bd9Sstevel@tonic-gate 		}
7607c478bd9Sstevel@tonic-gate 	}
7617c478bd9Sstevel@tonic-gate 
762b0fc0e77Sgovinda 	if (ipil_p->ipil_ih_size == 1) {
7637c478bd9Sstevel@tonic-gate 		if (ih_lst != ih_p)
7647c478bd9Sstevel@tonic-gate 			goto not_found;
7657c478bd9Sstevel@tonic-gate 		/* no need to set head/tail as ino_p will be freed */
7667c478bd9Sstevel@tonic-gate 		goto reset;
7677c478bd9Sstevel@tonic-gate 	}
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	/*
7707c478bd9Sstevel@tonic-gate 	 * if the interrupt was previously blocked (left in pending state)
7717c478bd9Sstevel@tonic-gate 	 * because of jabber we need to clear the pending state in case the
7727c478bd9Sstevel@tonic-gate 	 * jabber has gone away.
7737c478bd9Sstevel@tonic-gate 	 */
774b0fc0e77Sgovinda 	if (ino_p->ino_unclaimed_intrs > pci_unclaimed_intr_max) {
7757c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
7767c478bd9Sstevel@tonic-gate 		    "%s%d: ib_ino_rem_intr: ino 0x%x has been unblocked",
7777c478bd9Sstevel@tonic-gate 		    ddi_driver_name(pci_p->pci_dip),
7787c478bd9Sstevel@tonic-gate 		    ddi_get_instance(pci_p->pci_dip),
7797c478bd9Sstevel@tonic-gate 		    ino_p->ino_ino);
780b0fc0e77Sgovinda 		ino_p->ino_unclaimed_intrs = 0;
7817c478bd9Sstevel@tonic-gate 		IB_INO_INTR_CLEAR(ino_p->ino_clr_reg);
7827c478bd9Sstevel@tonic-gate 	}
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	/* search the link list for ih_p */
7857c478bd9Sstevel@tonic-gate 	for (i = 0;
786f0d69850Srameshc 	    (i < ipil_p->ipil_ih_size) && (ih_lst->ih_next != ih_p);
787f0d69850Srameshc 	    i++, ih_lst = ih_lst->ih_next)
788f0d69850Srameshc 		;
7897c478bd9Sstevel@tonic-gate 	if (ih_lst->ih_next != ih_p)
7907c478bd9Sstevel@tonic-gate 		goto not_found;
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	/* remove ih_p from the link list and maintain the head/tail */
7937c478bd9Sstevel@tonic-gate 	ih_lst->ih_next = ih_p->ih_next;
794b0fc0e77Sgovinda 	if (ipil_p->ipil_ih_head == ih_p)
795b0fc0e77Sgovinda 		ipil_p->ipil_ih_head = ih_p->ih_next;
796b0fc0e77Sgovinda 	if (ipil_p->ipil_ih_tail == ih_p)
797b0fc0e77Sgovinda 		ipil_p->ipil_ih_tail = ih_lst;
798b0fc0e77Sgovinda 	ipil_p->ipil_ih_start = ipil_p->ipil_ih_head;
7997c478bd9Sstevel@tonic-gate reset:
8007c478bd9Sstevel@tonic-gate 	if (ih_p->ih_config_handle)
8017c478bd9Sstevel@tonic-gate 		pci_config_teardown(&ih_p->ih_config_handle);
8027c478bd9Sstevel@tonic-gate 	if (ih_p->ih_ksp != NULL)
8037c478bd9Sstevel@tonic-gate 		kstat_delete(ih_p->ih_ksp);
8047c478bd9Sstevel@tonic-gate 	kmem_free(ih_p, sizeof (ih_t));
805b0fc0e77Sgovinda 	ipil_p->ipil_ih_size--;
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	return;
8087c478bd9Sstevel@tonic-gate not_found:
8097c478bd9Sstevel@tonic-gate 	DEBUG2(DBG_R_INTX, ino_p->ino_ib_p->ib_pci_p->pci_dip,
810f0d69850Srameshc 	    "ino_p=%x does not have ih_p=%x\n", ino_p, ih_p);
8117c478bd9Sstevel@tonic-gate }
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate ih_t *
814b0fc0e77Sgovinda ib_intr_locate_ih(ib_ino_pil_t *ipil_p, dev_info_t *rdip, uint32_t inum)
8157c478bd9Sstevel@tonic-gate {
816b0fc0e77Sgovinda 	ih_t *ih_p = ipil_p->ipil_ih_head;
8177c478bd9Sstevel@tonic-gate 	int i;
818b0fc0e77Sgovinda 
819b0fc0e77Sgovinda 	for (i = 0; i < ipil_p->ipil_ih_size; i++, ih_p = ih_p->ih_next) {
820b0fc0e77Sgovinda 		if (ih_p->ih_dip == rdip && ih_p->ih_inum == inum)
821b0fc0e77Sgovinda 			return (ih_p);
8227c478bd9Sstevel@tonic-gate 	}
823b0fc0e77Sgovinda 
8247c478bd9Sstevel@tonic-gate 	return ((ih_t *)NULL);
8257c478bd9Sstevel@tonic-gate }
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate ih_t *
8287c478bd9Sstevel@tonic-gate ib_alloc_ih(dev_info_t *rdip, uint32_t inum,
8297851eb82Sschwartz 	uint_t (*int_handler)(caddr_t int_handler_arg1,
8307851eb82Sschwartz 	caddr_t int_handler_arg2),
8317851eb82Sschwartz 	caddr_t int_handler_arg1,
8327851eb82Sschwartz 	caddr_t int_handler_arg2)
8337c478bd9Sstevel@tonic-gate {
8347c478bd9Sstevel@tonic-gate 	ih_t *ih_p;
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 	ih_p = kmem_alloc(sizeof (ih_t), KM_SLEEP);
8377c478bd9Sstevel@tonic-gate 	ih_p->ih_dip = rdip;
8387c478bd9Sstevel@tonic-gate 	ih_p->ih_inum = inum;
8397c478bd9Sstevel@tonic-gate 	ih_p->ih_intr_state = PCI_INTR_STATE_DISABLE;
8407c478bd9Sstevel@tonic-gate 	ih_p->ih_handler = int_handler;
8417c478bd9Sstevel@tonic-gate 	ih_p->ih_handler_arg1 = int_handler_arg1;
8427c478bd9Sstevel@tonic-gate 	ih_p->ih_handler_arg2 = int_handler_arg2;
8437c478bd9Sstevel@tonic-gate 	ih_p->ih_config_handle = NULL;
8447c478bd9Sstevel@tonic-gate 	ih_p->ih_nsec = 0;
8457c478bd9Sstevel@tonic-gate 	ih_p->ih_ticks = 0;
8466d44af1bSesolom 	ih_p->ih_ksp = NULL;
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 	return (ih_p);
8497c478bd9Sstevel@tonic-gate }
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate int
8527c478bd9Sstevel@tonic-gate ib_update_intr_state(pci_t *pci_p, dev_info_t *rdip,
8537851eb82Sschwartz 	ddi_intr_handle_impl_t *hdlp, uint_t new_intr_state)
8547c478bd9Sstevel@tonic-gate {
8557c478bd9Sstevel@tonic-gate 	ib_t		*ib_p = pci_p->pci_ib_p;
8567c478bd9Sstevel@tonic-gate 	ib_ino_info_t	*ino_p;
857b0fc0e77Sgovinda 	ib_ino_pil_t	*ipil_p;
8587c478bd9Sstevel@tonic-gate 	ib_mondo_t	mondo;
8597c478bd9Sstevel@tonic-gate 	ih_t		*ih_p;
8607c478bd9Sstevel@tonic-gate 	int		ret = DDI_FAILURE;
8617c478bd9Sstevel@tonic-gate 
862f910463cSgovinda 	/*
863f910463cSgovinda 	 * For PULSE interrupts, pci driver don't allocate
864f910463cSgovinda 	 * ib_ino_info_t and ih_t data structures and also,
865f910463cSgovinda 	 * not maintains any interrupt state information.
866f910463cSgovinda 	 * So, just return success from here.
867f910463cSgovinda 	 */
868f910463cSgovinda 	if (hdlp->ih_vector & PCI_PULSE_INO) {
869f910463cSgovinda 		DEBUG0(DBG_IB, ib_p->ib_pci_p->pci_dip,
870f910463cSgovinda 		    "ib_update_intr_state: PULSE interrupt, return success\n");
871f910463cSgovinda 
872f910463cSgovinda 		return (DDI_SUCCESS);
873f910463cSgovinda 	}
874f910463cSgovinda 
8757c478bd9Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 	if ((mondo = pci_xlate_intr(pci_p->pci_dip, rdip, pci_p->pci_ib_p,
878a195726fSgovinda 	    IB_MONDO_TO_INO(hdlp->ih_vector))) == 0) {
8797c478bd9Sstevel@tonic-gate 		mutex_exit(&ib_p->ib_ino_lst_mutex);
8807c478bd9Sstevel@tonic-gate 		return (ret);
8817c478bd9Sstevel@tonic-gate 	}
8827c478bd9Sstevel@tonic-gate 
883b0fc0e77Sgovinda 	ino_p = ib_locate_ino(ib_p, IB_MONDO_TO_INO(mondo));
884b0fc0e77Sgovinda 	if (ino_p && (ipil_p = ib_ino_locate_ipil(ino_p, hdlp->ih_pri))) {
885b0fc0e77Sgovinda 		if (ih_p = ib_intr_locate_ih(ipil_p, rdip, hdlp->ih_inum)) {
8867c478bd9Sstevel@tonic-gate 			ih_p->ih_intr_state = new_intr_state;
8877c478bd9Sstevel@tonic-gate 			ret = DDI_SUCCESS;
8887c478bd9Sstevel@tonic-gate 		}
8897c478bd9Sstevel@tonic-gate 	}
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
8927c478bd9Sstevel@tonic-gate 	return (ret);
8937c478bd9Sstevel@tonic-gate }
894*09b1eac2SEvan Yan 
895*09b1eac2SEvan Yan /*
896*09b1eac2SEvan Yan  * Get interrupt CPU for a given ino.
897*09b1eac2SEvan Yan  * Return info only for inos which are already mapped to devices.
898*09b1eac2SEvan Yan  */
899*09b1eac2SEvan Yan /*ARGSUSED*/
900*09b1eac2SEvan Yan int
901*09b1eac2SEvan Yan ib_get_intr_target(pci_t *pci_p, ib_ino_t ino, int *cpu_id_p)
902*09b1eac2SEvan Yan {
903*09b1eac2SEvan Yan 	dev_info_t		*dip = pci_p->pci_dip;
904*09b1eac2SEvan Yan 	ib_t			*ib_p = pci_p->pci_ib_p;
905*09b1eac2SEvan Yan 	volatile uint64_t	*imregp;
906*09b1eac2SEvan Yan 	uint64_t		imregval;
907*09b1eac2SEvan Yan 
908*09b1eac2SEvan Yan 	DEBUG1(DBG_IB, dip, "ib_get_intr_target: ino %x\n", ino);
909*09b1eac2SEvan Yan 
910*09b1eac2SEvan Yan 	imregp = ib_intr_map_reg_addr(ib_p, ino);
911*09b1eac2SEvan Yan 	imregval = *imregp;
912*09b1eac2SEvan Yan 
913*09b1eac2SEvan Yan 	*cpu_id_p = ib_map_reg_get_cpu(imregval);
914*09b1eac2SEvan Yan 
915*09b1eac2SEvan Yan 	DEBUG1(DBG_IB, dip, "ib_get_intr_target: cpu_id %x\n", *cpu_id_p);
916*09b1eac2SEvan Yan 
917*09b1eac2SEvan Yan 	return (DDI_SUCCESS);
918*09b1eac2SEvan Yan }
919*09b1eac2SEvan Yan 
920*09b1eac2SEvan Yan /*
921*09b1eac2SEvan Yan  * Associate a new CPU with a given ino.
922*09b1eac2SEvan Yan  * Operate only on inos which are already mapped to devices.
923*09b1eac2SEvan Yan  */
924*09b1eac2SEvan Yan int
925*09b1eac2SEvan Yan ib_set_intr_target(pci_t *pci_p, ib_ino_t ino, int cpu_id)
926*09b1eac2SEvan Yan {
927*09b1eac2SEvan Yan 	dev_info_t		*dip = pci_p->pci_dip;
928*09b1eac2SEvan Yan 	ib_t			*ib_p = pci_p->pci_ib_p;
929*09b1eac2SEvan Yan 	int			ret = DDI_SUCCESS;
930*09b1eac2SEvan Yan 	uint32_t		old_cpu_id;
931*09b1eac2SEvan Yan 	hrtime_t		start_time;
932*09b1eac2SEvan Yan 	uint64_t		imregval;
933*09b1eac2SEvan Yan 	uint64_t		new_imregval;
934*09b1eac2SEvan Yan 	volatile uint64_t	*imregp;
935*09b1eac2SEvan Yan 	volatile uint64_t	*idregp;
936*09b1eac2SEvan Yan 	extern const int	_ncpu;
937*09b1eac2SEvan Yan 	extern cpu_t		*cpu[];
938*09b1eac2SEvan Yan 
939*09b1eac2SEvan Yan 	DEBUG2(DBG_IB, dip, "ib_set_intr_target: ino %x cpu_id %x\n",
940*09b1eac2SEvan Yan 	    ino, cpu_id);
941*09b1eac2SEvan Yan 
942*09b1eac2SEvan Yan 	imregp = (uint64_t *)ib_intr_map_reg_addr(ib_p, ino);
943*09b1eac2SEvan Yan 	idregp = IB_INO_INTR_STATE_REG(ib_p, ino);
944*09b1eac2SEvan Yan 
945*09b1eac2SEvan Yan 	/* Save original mapreg value. */
946*09b1eac2SEvan Yan 	imregval = *imregp;
947*09b1eac2SEvan Yan 	DEBUG1(DBG_IB, dip, "ib_set_intr_target: orig mapreg value: 0x%llx\n",
948*09b1eac2SEvan Yan 	    imregval);
949*09b1eac2SEvan Yan 
950*09b1eac2SEvan Yan 	/* Operate only on inos which are already enabled. */
951*09b1eac2SEvan Yan 	if (!(imregval & COMMON_INTR_MAP_REG_VALID))
952*09b1eac2SEvan Yan 		return (DDI_FAILURE);
953*09b1eac2SEvan Yan 
954*09b1eac2SEvan Yan 	/* Is this request a noop? */
955*09b1eac2SEvan Yan 	if ((old_cpu_id = ib_map_reg_get_cpu(imregval)) == cpu_id)
956*09b1eac2SEvan Yan 		return (DDI_SUCCESS);
957*09b1eac2SEvan Yan 
958*09b1eac2SEvan Yan 	/* Clear the interrupt valid/enable bit for particular ino. */
959*09b1eac2SEvan Yan 	DEBUG0(DBG_IB, dip, "Clearing intr_enabled...\n");
960*09b1eac2SEvan Yan 	*imregp = imregval & ~COMMON_INTR_MAP_REG_VALID;
961*09b1eac2SEvan Yan 
962*09b1eac2SEvan Yan 	/* Wait until there are no more pending interrupts. */
963*09b1eac2SEvan Yan 	start_time = gethrtime();
964*09b1eac2SEvan Yan 
965*09b1eac2SEvan Yan 	DEBUG0(DBG_IB, dip, "About to check for pending interrupts...\n");
966*09b1eac2SEvan Yan 
967*09b1eac2SEvan Yan 	while (IB_INO_INTR_PENDING(idregp, ino)) {
968*09b1eac2SEvan Yan 		DEBUG0(DBG_IB, dip, "Waiting for pending ints to clear\n");
969*09b1eac2SEvan Yan 		if ((gethrtime() - start_time) < pci_intrpend_timeout) {
970*09b1eac2SEvan Yan 			continue;
971*09b1eac2SEvan Yan 		} else { /* Timed out waiting. */
972*09b1eac2SEvan Yan 			DEBUG0(DBG_IB, dip, "Timed out waiting \n");
973*09b1eac2SEvan Yan 			return (DDI_EPENDING);
974*09b1eac2SEvan Yan 		}
975*09b1eac2SEvan Yan 	}
976*09b1eac2SEvan Yan 
977*09b1eac2SEvan Yan 	new_imregval = *imregp;
978*09b1eac2SEvan Yan 
979*09b1eac2SEvan Yan 	DEBUG1(DBG_IB, dip,
980*09b1eac2SEvan Yan 	    "after disabling intr, mapreg value: 0x%llx\n", new_imregval);
981*09b1eac2SEvan Yan 
982*09b1eac2SEvan Yan 	/*
983*09b1eac2SEvan Yan 	 * Get lock, validate cpu and write new mapreg value.
984*09b1eac2SEvan Yan 	 */
985*09b1eac2SEvan Yan 	mutex_enter(&cpu_lock);
986*09b1eac2SEvan Yan 	if ((cpu_id < _ncpu) && (cpu[cpu_id] && cpu_is_online(cpu[cpu_id]))) {
987*09b1eac2SEvan Yan 		/* Prepare new mapreg value with intr enabled and new cpu_id. */
988*09b1eac2SEvan Yan 		new_imregval &=
989*09b1eac2SEvan Yan 		    COMMON_INTR_MAP_REG_IGN | COMMON_INTR_MAP_REG_INO;
990*09b1eac2SEvan Yan 		new_imregval = ib_get_map_reg(new_imregval, cpu_id);
991*09b1eac2SEvan Yan 
992*09b1eac2SEvan Yan 		DEBUG1(DBG_IB, dip, "Writing new mapreg value:0x%llx\n",
993*09b1eac2SEvan Yan 		    new_imregval);
994*09b1eac2SEvan Yan 
995*09b1eac2SEvan Yan 		*imregp = new_imregval;
996*09b1eac2SEvan Yan 
997*09b1eac2SEvan Yan 		ib_log_new_cpu(ib_p, old_cpu_id, cpu_id, ino);
998*09b1eac2SEvan Yan 	} else {	/* Invalid cpu.  Restore original register image. */
999*09b1eac2SEvan Yan 		DEBUG0(DBG_IB, dip,
1000*09b1eac2SEvan Yan 		    "Invalid cpuid: writing orig mapreg value\n");
1001*09b1eac2SEvan Yan 
1002*09b1eac2SEvan Yan 		*imregp = imregval;
1003*09b1eac2SEvan Yan 		ret = DDI_EINVAL;
1004*09b1eac2SEvan Yan 	}
1005*09b1eac2SEvan Yan 	mutex_exit(&cpu_lock);
1006*09b1eac2SEvan Yan 
1007*09b1eac2SEvan Yan 	return (ret);
1008*09b1eac2SEvan Yan }
1009*09b1eac2SEvan Yan 
10107851eb82Sschwartz 
10117851eb82Sschwartz /*
10127851eb82Sschwartz  * Return the dips or number of dips associated with a given interrupt block.
10137851eb82Sschwartz  * Size of dips array arg is passed in as dips_ret arg.
10147851eb82Sschwartz  * Number of dips returned is returned in dips_ret arg.
10157851eb82Sschwartz  * Array of dips gets returned in the dips argument.
10167851eb82Sschwartz  * Function returns number of dips existing for the given interrupt block.
10177851eb82Sschwartz  *
10187851eb82Sschwartz  */
10197851eb82Sschwartz uint8_t
10207851eb82Sschwartz ib_get_ino_devs(
10217851eb82Sschwartz 	ib_t *ib_p, uint32_t ino, uint8_t *devs_ret, pcitool_intr_dev_t *devs)
10227851eb82Sschwartz {
1023b0fc0e77Sgovinda 	ib_ino_info_t	*ino_p;
1024b0fc0e77Sgovinda 	ib_ino_pil_t	*ipil_p;
1025b0fc0e77Sgovinda 	ih_t		*ih_p;
1026b0fc0e77Sgovinda 	uint32_t	num_devs = 0;
1027b0fc0e77Sgovinda 	int		i, j;
10287851eb82Sschwartz 
10297851eb82Sschwartz 	mutex_enter(&ib_p->ib_ino_lst_mutex);
10307851eb82Sschwartz 	ino_p = ib_locate_ino(ib_p, ino);
10317851eb82Sschwartz 	if (ino_p != NULL) {
1032b0fc0e77Sgovinda 		for (j = 0, ipil_p = ino_p->ino_ipil_p; ipil_p;
1033b0fc0e77Sgovinda 		    ipil_p = ipil_p->ipil_next_p) {
1034b0fc0e77Sgovinda 			num_devs += ipil_p->ipil_ih_size;
1035b0fc0e77Sgovinda 
1036b0fc0e77Sgovinda 			for (i = 0, ih_p = ipil_p->ipil_ih_head;
1037b0fc0e77Sgovinda 			    ((i < ipil_p->ipil_ih_size) && (i < *devs_ret));
1038b0fc0e77Sgovinda 			    i++, j++, ih_p = ih_p->ih_next) {
1039b0fc0e77Sgovinda 				(void) strncpy(devs[i].driver_name,
1040b0fc0e77Sgovinda 				    ddi_driver_name(ih_p->ih_dip),
1041b0fc0e77Sgovinda 				    MAXMODCONFNAME-1);
1042b0fc0e77Sgovinda 				devs[i].driver_name[MAXMODCONFNAME] = '\0';
1043b0fc0e77Sgovinda 				(void) ddi_pathname(ih_p->ih_dip, devs[i].path);
1044b0fc0e77Sgovinda 				devs[i].dev_inst =
1045b0fc0e77Sgovinda 				    ddi_get_instance(ih_p->ih_dip);
1046b0fc0e77Sgovinda 			}
10477851eb82Sschwartz 		}
1048b0fc0e77Sgovinda 		*devs_ret = j;
10497851eb82Sschwartz 	}
10507851eb82Sschwartz 
10517851eb82Sschwartz 	mutex_exit(&ib_p->ib_ino_lst_mutex);
10527851eb82Sschwartz 
10537851eb82Sschwartz 	return (num_devs);
10547851eb82Sschwartz }
10557851eb82Sschwartz 
10567851eb82Sschwartz void ib_log_new_cpu(ib_t *ib_p, uint32_t old_cpu_id, uint32_t new_cpu_id,
10577851eb82Sschwartz 	uint32_t ino)
10587851eb82Sschwartz {
1059b0fc0e77Sgovinda 	ib_ino_info_t	*ino_p;
1060b0fc0e77Sgovinda 	ib_ino_pil_t	*ipil_p;
1061b0fc0e77Sgovinda 	ih_t		*ih_p;
1062b0fc0e77Sgovinda 	int		i;
10637851eb82Sschwartz 
10647851eb82Sschwartz 	mutex_enter(&ib_p->ib_ino_lst_mutex);
10657851eb82Sschwartz 
10667851eb82Sschwartz 	/* Log in OS data structures the new CPU. */
10677851eb82Sschwartz 	ino_p = ib_locate_ino(ib_p, ino);
10687851eb82Sschwartz 	if (ino_p != NULL) {
10697851eb82Sschwartz 
10707851eb82Sschwartz 		/* Log in OS data structures the new CPU. */
10717851eb82Sschwartz 		ino_p->ino_cpuid = new_cpu_id;
10727851eb82Sschwartz 
1073b0fc0e77Sgovinda 		for (ipil_p = ino_p->ino_ipil_p; ipil_p;
1074b0fc0e77Sgovinda 		    ipil_p = ipil_p->ipil_next_p) {
1075b0fc0e77Sgovinda 			for (i = 0, ih_p = ipil_p->ipil_ih_head;
1076b0fc0e77Sgovinda 			    (i < ipil_p->ipil_ih_size);
1077b0fc0e77Sgovinda 			    i++, ih_p = ih_p->ih_next) {
1078b0fc0e77Sgovinda 				/*
1079b0fc0e77Sgovinda 				 * Account for any residual time
1080b0fc0e77Sgovinda 				 * to be logged for old cpu.
1081b0fc0e77Sgovinda 				 */
1082b0fc0e77Sgovinda 				ib_cpu_ticks_to_ih_nsec(ib_p,
1083b0fc0e77Sgovinda 				    ipil_p->ipil_ih_head, old_cpu_id);
1084b0fc0e77Sgovinda 			}
1085b0fc0e77Sgovinda 		}
10867851eb82Sschwartz 	}
10877851eb82Sschwartz 
10887851eb82Sschwartz 	mutex_exit(&ib_p->ib_ino_lst_mutex);
10897851eb82Sschwartz }
1090