xref: /onnv-gate/usr/src/uts/sun4u/io/pci/pci_cb.c (revision 946:5fb89afc576e)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * PCI Control Block object
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <sys/kmem.h>
340Sstevel@tonic-gate #include <sys/systm.h>		/* timeout() */
350Sstevel@tonic-gate #include <sys/async.h>
360Sstevel@tonic-gate #include <sys/sunddi.h>
370Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
380Sstevel@tonic-gate #include <sys/pci/pci_obj.h>
390Sstevel@tonic-gate #include <sys/machsystm.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #ifdef _STARFIRE
420Sstevel@tonic-gate #include <sys/starfire.h>
430Sstevel@tonic-gate #endif /* _STARFIRE */
440Sstevel@tonic-gate 
450Sstevel@tonic-gate /*LINTLIBRARY*/
460Sstevel@tonic-gate 
470Sstevel@tonic-gate void
cb_create(pci_t * pci_p)480Sstevel@tonic-gate cb_create(pci_t *pci_p)
490Sstevel@tonic-gate {
500Sstevel@tonic-gate 	cb_t *cb_p = (cb_t *)kmem_zalloc(sizeof (cb_t), KM_SLEEP);
510Sstevel@tonic-gate 
520Sstevel@tonic-gate 	mutex_init(&cb_p->cb_intr_lock, NULL, MUTEX_DRIVER, NULL);
530Sstevel@tonic-gate 	pci_p->pci_cb_p = cb_p;
540Sstevel@tonic-gate 	cb_p->cb_pci_cmn_p = pci_p->pci_common_p;
550Sstevel@tonic-gate 
560Sstevel@tonic-gate 	pci_cb_setup(pci_p);
570Sstevel@tonic-gate }
580Sstevel@tonic-gate 
590Sstevel@tonic-gate void
cb_destroy(pci_t * pci_p)600Sstevel@tonic-gate cb_destroy(pci_t *pci_p)
610Sstevel@tonic-gate {
620Sstevel@tonic-gate 	cb_t *cb_p = pci_p->pci_cb_p;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 	intr_dist_rem(cb_intr_dist, cb_p);
650Sstevel@tonic-gate 	pci_cb_teardown(pci_p);
660Sstevel@tonic-gate 	pci_p->pci_cb_p = NULL;
670Sstevel@tonic-gate 	mutex_destroy(&cb_p->cb_intr_lock);
680Sstevel@tonic-gate 	kmem_free(cb_p, sizeof (cb_t));
690Sstevel@tonic-gate }
700Sstevel@tonic-gate 
710Sstevel@tonic-gate static void
cb_set_nintr_reg(cb_t * cb_p,ib_ino_t ino,uint64_t value)720Sstevel@tonic-gate cb_set_nintr_reg(cb_t *cb_p, ib_ino_t ino, uint64_t value)
730Sstevel@tonic-gate {
740Sstevel@tonic-gate 	uint64_t pa = cb_ino_to_clr_pa(cb_p, ino);
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	DEBUG3(DBG_CB|DBG_CONT, NULL,
770Sstevel@tonic-gate 		"pci-%x cb_set_nintr_reg: ino=%x PA=%016llx\n",
780Sstevel@tonic-gate 		cb_p->cb_pci_cmn_p->pci_common_id, ino, pa);
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	stdphysio(pa, value);
810Sstevel@tonic-gate 	(void) lddphysio(pa);	/* flush the previous write */
820Sstevel@tonic-gate }
830Sstevel@tonic-gate 
840Sstevel@tonic-gate /*
850Sstevel@tonic-gate  * enable an internal interrupt source:
860Sstevel@tonic-gate  * if an interrupt is shared by both sides, record it in cb_inos[] and
870Sstevel@tonic-gate  * cb will own its distribution.
880Sstevel@tonic-gate  */
890Sstevel@tonic-gate void
cb_enable_nintr(pci_t * pci_p,enum cb_nintr_index idx)900Sstevel@tonic-gate cb_enable_nintr(pci_t *pci_p, enum cb_nintr_index idx)
910Sstevel@tonic-gate {
920Sstevel@tonic-gate 	cb_t *cb_p = pci_p->pci_cb_p;
930Sstevel@tonic-gate 	ib_ino_t ino = IB_MONDO_TO_INO(pci_p->pci_inos[idx]);
940Sstevel@tonic-gate 	ib_mondo_t mondo = CB_INO_TO_MONDO(cb_p, ino);
950Sstevel@tonic-gate 	uint32_t cpu_id;
960Sstevel@tonic-gate 	uint64_t reg, pa;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	ASSERT(idx < CBNINTR_MAX);
990Sstevel@tonic-gate 	pa = cb_ino_to_map_pa(cb_p, ino);
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	mutex_enter(&cb_p->cb_intr_lock);
1020Sstevel@tonic-gate 	cpu_id = intr_dist_cpuid();
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate #ifdef _STARFIRE
1050Sstevel@tonic-gate 	cpu_id = pc_translate_tgtid(cb_p->cb_ittrans_cookie, cpu_id,
1060Sstevel@tonic-gate 		IB_GET_MAPREG_INO(ino));
1070Sstevel@tonic-gate #endif /* _STARFIRE */
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	reg = ib_get_map_reg(mondo, cpu_id);
1100Sstevel@tonic-gate 	stdphysio(pa, reg);
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	ASSERT(cb_p->cb_inos[idx] == 0);
1130Sstevel@tonic-gate 	cb_p->cb_inos[idx] = ino;
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	cb_set_nintr_reg(cb_p, ino, COMMON_CLEAR_INTR_REG_IDLE);
1160Sstevel@tonic-gate 	mutex_exit(&cb_p->cb_intr_lock);
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	DEBUG3(DBG_CB|DBG_CONT, NULL,
1190Sstevel@tonic-gate 		"pci-%x cb_enable_nintr: ino=%x cpu_id=%x\n",
1200Sstevel@tonic-gate 		pci_p->pci_id, ino, cpu_id);
1210Sstevel@tonic-gate 	DEBUG2(DBG_CB|DBG_CONT, NULL, "\tPA=%016llx data=%016llx\n", pa, reg);
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate static void
cb_disable_nintr_reg(cb_t * cb_p,ib_ino_t ino,int wait)1250Sstevel@tonic-gate cb_disable_nintr_reg(cb_t *cb_p, ib_ino_t ino, int wait)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate 	uint64_t tmp, map_reg_pa = cb_ino_to_map_pa(cb_p, ino);
1280Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cb_p->cb_intr_lock));
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	/* mark interrupt invalid in mapping register */
1310Sstevel@tonic-gate 	tmp = lddphysio(map_reg_pa) & ~COMMON_INTR_MAP_REG_VALID;
1320Sstevel@tonic-gate 	stdphysio(map_reg_pa, tmp);
1330Sstevel@tonic-gate 	(void) lddphysio(map_reg_pa);   /* flush previous write */
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	if (wait) {
1360Sstevel@tonic-gate 		hrtime_t start_time;
1370Sstevel@tonic-gate 		uint64_t state_reg_pa = cb_p->cb_obsta_pa;
1380Sstevel@tonic-gate 		uint_t shift = (ino & 0x1f) << 1;
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 		/* busy wait if there is interrupt being processed */
1410Sstevel@tonic-gate 		/* unless panic or timeout for interrupt pending is reached */
1420Sstevel@tonic-gate 		start_time = gethrtime();
1430Sstevel@tonic-gate 		while ((((lddphysio(state_reg_pa) >> shift) &
1440Sstevel@tonic-gate 			COMMON_CLEAR_INTR_REG_MASK) ==
1450Sstevel@tonic-gate 			COMMON_CLEAR_INTR_REG_PENDING) && !panicstr) {
1460Sstevel@tonic-gate 			if (gethrtime() - start_time > pci_intrpend_timeout) {
1470Sstevel@tonic-gate 				cmn_err(CE_WARN,
148*946Smathue 				"pci@%x cb_disable_nintr_reg(%lx,%x) timeout",
1490Sstevel@tonic-gate 					cb_p->cb_pci_cmn_p->pci_common_id,
1500Sstevel@tonic-gate 					map_reg_pa,
1510Sstevel@tonic-gate 					CB_INO_TO_MONDO(cb_p, ino));
1520Sstevel@tonic-gate 				break;
1530Sstevel@tonic-gate 			}
1540Sstevel@tonic-gate 		}
1550Sstevel@tonic-gate 	}
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate void
cb_disable_nintr(cb_t * cb_p,enum cb_nintr_index idx,int wait)1590Sstevel@tonic-gate cb_disable_nintr(cb_t *cb_p, enum cb_nintr_index idx, int wait)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate 	ib_ino_t ino = cb_p->cb_inos[idx];
1620Sstevel@tonic-gate 	ASSERT(idx < CBNINTR_MAX);
1630Sstevel@tonic-gate 	ASSERT(ino);
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	mutex_enter(&cb_p->cb_intr_lock);
1660Sstevel@tonic-gate 	cb_disable_nintr_reg(cb_p, ino, wait);
1670Sstevel@tonic-gate 	cb_set_nintr_reg(cb_p, ino, COMMON_CLEAR_INTR_REG_PENDING);
1680Sstevel@tonic-gate 	cb_p->cb_inos[idx] = 0;
1690Sstevel@tonic-gate 	mutex_exit(&cb_p->cb_intr_lock);
1700Sstevel@tonic-gate #ifdef _STARFIRE
171*946Smathue 	pc_ittrans_cleanup(cb_p->cb_ittrans_cookie,
172*946Smathue 	    (volatile uint64_t *)(uintptr_t)ino);
1730Sstevel@tonic-gate #endif /* _STARFIRE */
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate void
cb_clear_nintr(cb_t * cb_p,enum cb_nintr_index idx)1770Sstevel@tonic-gate cb_clear_nintr(cb_t *cb_p, enum cb_nintr_index idx)
1780Sstevel@tonic-gate {
1790Sstevel@tonic-gate 	ib_ino_t ino = cb_p->cb_inos[idx];
1800Sstevel@tonic-gate 	ASSERT(idx < CBNINTR_MAX);
1810Sstevel@tonic-gate 	ASSERT(ino);
1820Sstevel@tonic-gate 	cb_set_nintr_reg(cb_p, ino, COMMON_CLEAR_INTR_REG_IDLE);
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate void
cb_intr_dist(void * arg)1860Sstevel@tonic-gate cb_intr_dist(void *arg)
1870Sstevel@tonic-gate {
1880Sstevel@tonic-gate 	int i;
1890Sstevel@tonic-gate 	cb_t *cb_p = (cb_t *)arg;
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	mutex_enter(&cb_p->cb_intr_lock);
1920Sstevel@tonic-gate 	for (i = 0; i < cb_p->cb_no_of_inos; i++) {
1930Sstevel@tonic-gate 		uint64_t mr_pa;
1940Sstevel@tonic-gate 		volatile uint64_t imr;
1950Sstevel@tonic-gate 		ib_mondo_t mondo;
1960Sstevel@tonic-gate 		uint32_t cpu_id;
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 		ib_ino_t ino = cb_p->cb_inos[i];
1990Sstevel@tonic-gate 		if (!ino)	/* skip non-shared interrupts */
2000Sstevel@tonic-gate 			continue;
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 		mr_pa = cb_ino_to_map_pa(cb_p, ino);
2030Sstevel@tonic-gate 		imr = lddphysio(mr_pa);
2040Sstevel@tonic-gate 		if (!IB_INO_INTR_ISON(imr))
2050Sstevel@tonic-gate 			continue;
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 		mondo = CB_INO_TO_MONDO(cb_p, ino);
2080Sstevel@tonic-gate 		cpu_id = intr_dist_cpuid();
2090Sstevel@tonic-gate #ifdef _STARFIRE
2100Sstevel@tonic-gate 		cpu_id = pc_translate_tgtid(cb_p->cb_ittrans_cookie, cpu_id,
2110Sstevel@tonic-gate 			IB_GET_MAPREG_INO(ino));
2120Sstevel@tonic-gate #else
2130Sstevel@tonic-gate 		if (ib_map_reg_get_cpu(imr) == cpu_id)
2140Sstevel@tonic-gate 			continue;	/* same cpu target, no re-program */
2150Sstevel@tonic-gate #endif
2160Sstevel@tonic-gate 		cb_disable_nintr_reg(cb_p, ino, IB_INTR_WAIT);
2170Sstevel@tonic-gate 		stdphysio(mr_pa, ib_get_map_reg(mondo, cpu_id));
2180Sstevel@tonic-gate 		(void) lddphysio(mr_pa);	/* flush previous write */
2190Sstevel@tonic-gate 	}
2200Sstevel@tonic-gate 	mutex_exit(&cb_p->cb_intr_lock);
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate void
cb_suspend(cb_t * cb_p)2240Sstevel@tonic-gate cb_suspend(cb_t *cb_p)
2250Sstevel@tonic-gate {
2260Sstevel@tonic-gate 	int i, inos = cb_p->cb_no_of_inos;
2270Sstevel@tonic-gate 	ASSERT(!cb_p->cb_imr_save);
2280Sstevel@tonic-gate 	cb_p->cb_imr_save = kmem_alloc(inos * sizeof (uint64_t), KM_SLEEP);
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	/*
2310Sstevel@tonic-gate 	 * save the internal interrupts' mapping registers content
2320Sstevel@tonic-gate 	 *
2330Sstevel@tonic-gate 	 * The PBM IMR really doesn't need to be saved, as it is
2340Sstevel@tonic-gate 	 * different per side and is handled by pbm_suspend/resume.
2350Sstevel@tonic-gate 	 * But it complicates the logic.
2360Sstevel@tonic-gate 	 */
2370Sstevel@tonic-gate 	for (i = 0; i < inos; i++) {
2380Sstevel@tonic-gate 		uint64_t pa;
2390Sstevel@tonic-gate 		ib_ino_t ino = cb_p->cb_inos[i];
2400Sstevel@tonic-gate 		if (!ino)
2410Sstevel@tonic-gate 			continue;
2420Sstevel@tonic-gate 		pa = cb_ino_to_map_pa(cb_p, ino);
2430Sstevel@tonic-gate 		cb_p->cb_imr_save[i] = lddphysio(pa);
2440Sstevel@tonic-gate 	}
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate void
cb_resume(cb_t * cb_p)2480Sstevel@tonic-gate cb_resume(cb_t *cb_p)
2490Sstevel@tonic-gate {
2500Sstevel@tonic-gate 	int i;
2510Sstevel@tonic-gate 	for (i = 0; i < cb_p->cb_no_of_inos; i++) {
2520Sstevel@tonic-gate 		uint64_t pa;
2530Sstevel@tonic-gate 		ib_ino_t ino = cb_p->cb_inos[i];
2540Sstevel@tonic-gate 		if (!ino)
2550Sstevel@tonic-gate 			continue;
2560Sstevel@tonic-gate 		pa = cb_ino_to_map_pa(cb_p, ino);
2570Sstevel@tonic-gate 		cb_set_nintr_reg(cb_p, ino, COMMON_CLEAR_INTR_REG_IDLE);
2580Sstevel@tonic-gate 		stdphysio(pa, cb_p->cb_imr_save[i]);	/* restore IMR */
2590Sstevel@tonic-gate 	}
2600Sstevel@tonic-gate 	kmem_free(cb_p->cb_imr_save, cb_p->cb_no_of_inos * sizeof (uint64_t));
2610Sstevel@tonic-gate 	cb_p->cb_imr_save = NULL;
2620Sstevel@tonic-gate }
263