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
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate * PCI Control Block object
317c478bd9Sstevel@tonic-gate */
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
347c478bd9Sstevel@tonic-gate #include <sys/systm.h> /* timeout() */
357c478bd9Sstevel@tonic-gate #include <sys/async.h>
367c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
377c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
387c478bd9Sstevel@tonic-gate #include <sys/pci/pci_obj.h>
397c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate #ifdef _STARFIRE
427c478bd9Sstevel@tonic-gate #include <sys/starfire.h>
437c478bd9Sstevel@tonic-gate #endif /* _STARFIRE */
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate void
cb_create(pci_t * pci_p)487c478bd9Sstevel@tonic-gate cb_create(pci_t *pci_p)
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate cb_t *cb_p = (cb_t *)kmem_zalloc(sizeof (cb_t), KM_SLEEP);
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate mutex_init(&cb_p->cb_intr_lock, NULL, MUTEX_DRIVER, NULL);
537c478bd9Sstevel@tonic-gate pci_p->pci_cb_p = cb_p;
547c478bd9Sstevel@tonic-gate cb_p->cb_pci_cmn_p = pci_p->pci_common_p;
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate pci_cb_setup(pci_p);
577c478bd9Sstevel@tonic-gate }
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate void
cb_destroy(pci_t * pci_p)607c478bd9Sstevel@tonic-gate cb_destroy(pci_t *pci_p)
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate cb_t *cb_p = pci_p->pci_cb_p;
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate intr_dist_rem(cb_intr_dist, cb_p);
657c478bd9Sstevel@tonic-gate pci_cb_teardown(pci_p);
667c478bd9Sstevel@tonic-gate pci_p->pci_cb_p = NULL;
677c478bd9Sstevel@tonic-gate mutex_destroy(&cb_p->cb_intr_lock);
687c478bd9Sstevel@tonic-gate kmem_free(cb_p, sizeof (cb_t));
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate static void
cb_set_nintr_reg(cb_t * cb_p,ib_ino_t ino,uint64_t value)727c478bd9Sstevel@tonic-gate cb_set_nintr_reg(cb_t *cb_p, ib_ino_t ino, uint64_t value)
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate uint64_t pa = cb_ino_to_clr_pa(cb_p, ino);
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate DEBUG3(DBG_CB|DBG_CONT, NULL,
777c478bd9Sstevel@tonic-gate "pci-%x cb_set_nintr_reg: ino=%x PA=%016llx\n",
787c478bd9Sstevel@tonic-gate cb_p->cb_pci_cmn_p->pci_common_id, ino, pa);
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate stdphysio(pa, value);
817c478bd9Sstevel@tonic-gate (void) lddphysio(pa); /* flush the previous write */
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate * enable an internal interrupt source:
867c478bd9Sstevel@tonic-gate * if an interrupt is shared by both sides, record it in cb_inos[] and
877c478bd9Sstevel@tonic-gate * cb will own its distribution.
887c478bd9Sstevel@tonic-gate */
897c478bd9Sstevel@tonic-gate void
cb_enable_nintr(pci_t * pci_p,enum cb_nintr_index idx)907c478bd9Sstevel@tonic-gate cb_enable_nintr(pci_t *pci_p, enum cb_nintr_index idx)
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate cb_t *cb_p = pci_p->pci_cb_p;
937c478bd9Sstevel@tonic-gate ib_ino_t ino = IB_MONDO_TO_INO(pci_p->pci_inos[idx]);
947c478bd9Sstevel@tonic-gate ib_mondo_t mondo = CB_INO_TO_MONDO(cb_p, ino);
957c478bd9Sstevel@tonic-gate uint32_t cpu_id;
967c478bd9Sstevel@tonic-gate uint64_t reg, pa;
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate ASSERT(idx < CBNINTR_MAX);
997c478bd9Sstevel@tonic-gate pa = cb_ino_to_map_pa(cb_p, ino);
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate mutex_enter(&cb_p->cb_intr_lock);
1027c478bd9Sstevel@tonic-gate cpu_id = intr_dist_cpuid();
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate #ifdef _STARFIRE
1057c478bd9Sstevel@tonic-gate cpu_id = pc_translate_tgtid(cb_p->cb_ittrans_cookie, cpu_id,
1067c478bd9Sstevel@tonic-gate IB_GET_MAPREG_INO(ino));
1077c478bd9Sstevel@tonic-gate #endif /* _STARFIRE */
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate reg = ib_get_map_reg(mondo, cpu_id);
1107c478bd9Sstevel@tonic-gate stdphysio(pa, reg);
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate ASSERT(cb_p->cb_inos[idx] == 0);
1137c478bd9Sstevel@tonic-gate cb_p->cb_inos[idx] = ino;
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate cb_set_nintr_reg(cb_p, ino, COMMON_CLEAR_INTR_REG_IDLE);
1167c478bd9Sstevel@tonic-gate mutex_exit(&cb_p->cb_intr_lock);
1177c478bd9Sstevel@tonic-gate
1187c478bd9Sstevel@tonic-gate DEBUG3(DBG_CB|DBG_CONT, NULL,
1197c478bd9Sstevel@tonic-gate "pci-%x cb_enable_nintr: ino=%x cpu_id=%x\n",
1207c478bd9Sstevel@tonic-gate pci_p->pci_id, ino, cpu_id);
1217c478bd9Sstevel@tonic-gate DEBUG2(DBG_CB|DBG_CONT, NULL, "\tPA=%016llx data=%016llx\n", pa, reg);
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate static void
cb_disable_nintr_reg(cb_t * cb_p,ib_ino_t ino,int wait)1257c478bd9Sstevel@tonic-gate cb_disable_nintr_reg(cb_t *cb_p, ib_ino_t ino, int wait)
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate uint64_t tmp, map_reg_pa = cb_ino_to_map_pa(cb_p, ino);
1287c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cb_p->cb_intr_lock));
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate /* mark interrupt invalid in mapping register */
1317c478bd9Sstevel@tonic-gate tmp = lddphysio(map_reg_pa) & ~COMMON_INTR_MAP_REG_VALID;
1327c478bd9Sstevel@tonic-gate stdphysio(map_reg_pa, tmp);
1337c478bd9Sstevel@tonic-gate (void) lddphysio(map_reg_pa); /* flush previous write */
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate if (wait) {
1367c478bd9Sstevel@tonic-gate hrtime_t start_time;
1377c478bd9Sstevel@tonic-gate uint64_t state_reg_pa = cb_p->cb_obsta_pa;
1387c478bd9Sstevel@tonic-gate uint_t shift = (ino & 0x1f) << 1;
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate /* busy wait if there is interrupt being processed */
1417c478bd9Sstevel@tonic-gate /* unless panic or timeout for interrupt pending is reached */
1427c478bd9Sstevel@tonic-gate start_time = gethrtime();
1437c478bd9Sstevel@tonic-gate while ((((lddphysio(state_reg_pa) >> shift) &
1447c478bd9Sstevel@tonic-gate COMMON_CLEAR_INTR_REG_MASK) ==
1457c478bd9Sstevel@tonic-gate COMMON_CLEAR_INTR_REG_PENDING) && !panicstr) {
1467c478bd9Sstevel@tonic-gate if (gethrtime() - start_time > pci_intrpend_timeout) {
1477c478bd9Sstevel@tonic-gate cmn_err(CE_WARN,
148*f47a9c50Smathue "pci@%x cb_disable_nintr_reg(%lx,%x) timeout",
1497c478bd9Sstevel@tonic-gate cb_p->cb_pci_cmn_p->pci_common_id,
1507c478bd9Sstevel@tonic-gate map_reg_pa,
1517c478bd9Sstevel@tonic-gate CB_INO_TO_MONDO(cb_p, ino));
1527c478bd9Sstevel@tonic-gate break;
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate
1587c478bd9Sstevel@tonic-gate void
cb_disable_nintr(cb_t * cb_p,enum cb_nintr_index idx,int wait)1597c478bd9Sstevel@tonic-gate cb_disable_nintr(cb_t *cb_p, enum cb_nintr_index idx, int wait)
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate ib_ino_t ino = cb_p->cb_inos[idx];
1627c478bd9Sstevel@tonic-gate ASSERT(idx < CBNINTR_MAX);
1637c478bd9Sstevel@tonic-gate ASSERT(ino);
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate mutex_enter(&cb_p->cb_intr_lock);
1667c478bd9Sstevel@tonic-gate cb_disable_nintr_reg(cb_p, ino, wait);
1677c478bd9Sstevel@tonic-gate cb_set_nintr_reg(cb_p, ino, COMMON_CLEAR_INTR_REG_PENDING);
1687c478bd9Sstevel@tonic-gate cb_p->cb_inos[idx] = 0;
1697c478bd9Sstevel@tonic-gate mutex_exit(&cb_p->cb_intr_lock);
1707c478bd9Sstevel@tonic-gate #ifdef _STARFIRE
171*f47a9c50Smathue pc_ittrans_cleanup(cb_p->cb_ittrans_cookie,
172*f47a9c50Smathue (volatile uint64_t *)(uintptr_t)ino);
1737c478bd9Sstevel@tonic-gate #endif /* _STARFIRE */
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate
1767c478bd9Sstevel@tonic-gate void
cb_clear_nintr(cb_t * cb_p,enum cb_nintr_index idx)1777c478bd9Sstevel@tonic-gate cb_clear_nintr(cb_t *cb_p, enum cb_nintr_index idx)
1787c478bd9Sstevel@tonic-gate {
1797c478bd9Sstevel@tonic-gate ib_ino_t ino = cb_p->cb_inos[idx];
1807c478bd9Sstevel@tonic-gate ASSERT(idx < CBNINTR_MAX);
1817c478bd9Sstevel@tonic-gate ASSERT(ino);
1827c478bd9Sstevel@tonic-gate cb_set_nintr_reg(cb_p, ino, COMMON_CLEAR_INTR_REG_IDLE);
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate
1857c478bd9Sstevel@tonic-gate void
cb_intr_dist(void * arg)1867c478bd9Sstevel@tonic-gate cb_intr_dist(void *arg)
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate int i;
1897c478bd9Sstevel@tonic-gate cb_t *cb_p = (cb_t *)arg;
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate mutex_enter(&cb_p->cb_intr_lock);
1927c478bd9Sstevel@tonic-gate for (i = 0; i < cb_p->cb_no_of_inos; i++) {
1937c478bd9Sstevel@tonic-gate uint64_t mr_pa;
1947c478bd9Sstevel@tonic-gate volatile uint64_t imr;
1957c478bd9Sstevel@tonic-gate ib_mondo_t mondo;
1967c478bd9Sstevel@tonic-gate uint32_t cpu_id;
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate ib_ino_t ino = cb_p->cb_inos[i];
1997c478bd9Sstevel@tonic-gate if (!ino) /* skip non-shared interrupts */
2007c478bd9Sstevel@tonic-gate continue;
2017c478bd9Sstevel@tonic-gate
2027c478bd9Sstevel@tonic-gate mr_pa = cb_ino_to_map_pa(cb_p, ino);
2037c478bd9Sstevel@tonic-gate imr = lddphysio(mr_pa);
2047c478bd9Sstevel@tonic-gate if (!IB_INO_INTR_ISON(imr))
2057c478bd9Sstevel@tonic-gate continue;
2067c478bd9Sstevel@tonic-gate
2077c478bd9Sstevel@tonic-gate mondo = CB_INO_TO_MONDO(cb_p, ino);
2087c478bd9Sstevel@tonic-gate cpu_id = intr_dist_cpuid();
2097c478bd9Sstevel@tonic-gate #ifdef _STARFIRE
2107c478bd9Sstevel@tonic-gate cpu_id = pc_translate_tgtid(cb_p->cb_ittrans_cookie, cpu_id,
2117c478bd9Sstevel@tonic-gate IB_GET_MAPREG_INO(ino));
2127c478bd9Sstevel@tonic-gate #else
2137c478bd9Sstevel@tonic-gate if (ib_map_reg_get_cpu(imr) == cpu_id)
2147c478bd9Sstevel@tonic-gate continue; /* same cpu target, no re-program */
2157c478bd9Sstevel@tonic-gate #endif
2167c478bd9Sstevel@tonic-gate cb_disable_nintr_reg(cb_p, ino, IB_INTR_WAIT);
2177c478bd9Sstevel@tonic-gate stdphysio(mr_pa, ib_get_map_reg(mondo, cpu_id));
2187c478bd9Sstevel@tonic-gate (void) lddphysio(mr_pa); /* flush previous write */
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate mutex_exit(&cb_p->cb_intr_lock);
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate
2237c478bd9Sstevel@tonic-gate void
cb_suspend(cb_t * cb_p)2247c478bd9Sstevel@tonic-gate cb_suspend(cb_t *cb_p)
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate int i, inos = cb_p->cb_no_of_inos;
2277c478bd9Sstevel@tonic-gate ASSERT(!cb_p->cb_imr_save);
2287c478bd9Sstevel@tonic-gate cb_p->cb_imr_save = kmem_alloc(inos * sizeof (uint64_t), KM_SLEEP);
2297c478bd9Sstevel@tonic-gate
2307c478bd9Sstevel@tonic-gate /*
2317c478bd9Sstevel@tonic-gate * save the internal interrupts' mapping registers content
2327c478bd9Sstevel@tonic-gate *
2337c478bd9Sstevel@tonic-gate * The PBM IMR really doesn't need to be saved, as it is
2347c478bd9Sstevel@tonic-gate * different per side and is handled by pbm_suspend/resume.
2357c478bd9Sstevel@tonic-gate * But it complicates the logic.
2367c478bd9Sstevel@tonic-gate */
2377c478bd9Sstevel@tonic-gate for (i = 0; i < inos; i++) {
2387c478bd9Sstevel@tonic-gate uint64_t pa;
2397c478bd9Sstevel@tonic-gate ib_ino_t ino = cb_p->cb_inos[i];
2407c478bd9Sstevel@tonic-gate if (!ino)
2417c478bd9Sstevel@tonic-gate continue;
2427c478bd9Sstevel@tonic-gate pa = cb_ino_to_map_pa(cb_p, ino);
2437c478bd9Sstevel@tonic-gate cb_p->cb_imr_save[i] = lddphysio(pa);
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate
2477c478bd9Sstevel@tonic-gate void
cb_resume(cb_t * cb_p)2487c478bd9Sstevel@tonic-gate cb_resume(cb_t *cb_p)
2497c478bd9Sstevel@tonic-gate {
2507c478bd9Sstevel@tonic-gate int i;
2517c478bd9Sstevel@tonic-gate for (i = 0; i < cb_p->cb_no_of_inos; i++) {
2527c478bd9Sstevel@tonic-gate uint64_t pa;
2537c478bd9Sstevel@tonic-gate ib_ino_t ino = cb_p->cb_inos[i];
2547c478bd9Sstevel@tonic-gate if (!ino)
2557c478bd9Sstevel@tonic-gate continue;
2567c478bd9Sstevel@tonic-gate pa = cb_ino_to_map_pa(cb_p, ino);
2577c478bd9Sstevel@tonic-gate cb_set_nintr_reg(cb_p, ino, COMMON_CLEAR_INTR_REG_IDLE);
2587c478bd9Sstevel@tonic-gate stdphysio(pa, cb_p->cb_imr_save[i]); /* restore IMR */
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate kmem_free(cb_p->cb_imr_save, cb_p->cb_no_of_inos * sizeof (uint64_t));
2617c478bd9Sstevel@tonic-gate cb_p->cb_imr_save = NULL;
2627c478bd9Sstevel@tonic-gate }
263