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 53446Smrj * Common Development and Distribution License (the "License"). 63446Smrj * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*12683SJimmy.Vetayases@oracle.com * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 230Sstevel@tonic-gate */ 240Sstevel@tonic-gate 250Sstevel@tonic-gate /* 260Sstevel@tonic-gate * Autovectored Interrupt Configuration and Deconfiguration 270Sstevel@tonic-gate */ 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <sys/param.h> 300Sstevel@tonic-gate #include <sys/cmn_err.h> 310Sstevel@tonic-gate #include <sys/trap.h> 320Sstevel@tonic-gate #include <sys/t_lock.h> 330Sstevel@tonic-gate #include <sys/avintr.h> 340Sstevel@tonic-gate #include <sys/kmem.h> 350Sstevel@tonic-gate #include <sys/machlock.h> 360Sstevel@tonic-gate #include <sys/systm.h> 370Sstevel@tonic-gate #include <sys/machsystm.h> 380Sstevel@tonic-gate #include <sys/sunddi.h> 390Sstevel@tonic-gate #include <sys/x_call.h> 400Sstevel@tonic-gate #include <sys/cpuvar.h> 410Sstevel@tonic-gate #include <sys/atomic.h> 420Sstevel@tonic-gate #include <sys/smp_impldefs.h> 430Sstevel@tonic-gate #include <sys/sdt.h> 440Sstevel@tonic-gate #include <sys/stack.h> 450Sstevel@tonic-gate #include <sys/ddi_impldefs.h> 465084Sjohnlev #ifdef __xpv 475084Sjohnlev #include <sys/evtchn_impl.h> 485084Sjohnlev #endif 490Sstevel@tonic-gate 50999Slq150181 typedef struct av_softinfo { 51999Slq150181 cpuset_t av_pending; /* pending bitmasks */ 52999Slq150181 } av_softinfo_t; 53999Slq150181 54999Slq150181 static void insert_av(void *intr_id, struct av_head *vectp, avfunc f, 55916Sschwartz caddr_t arg1, caddr_t arg2, uint64_t *ticksp, int pri_level, 56916Sschwartz dev_info_t *dip); 570Sstevel@tonic-gate static void remove_av(void *intr_id, struct av_head *vectp, avfunc f, 580Sstevel@tonic-gate int pri_level, int vect); 590Sstevel@tonic-gate 600Sstevel@tonic-gate /* 610Sstevel@tonic-gate * Arrange for a driver to be called when a particular 620Sstevel@tonic-gate * auto-vectored interrupt occurs. 630Sstevel@tonic-gate * NOTE: if a device can generate interrupts on more than 640Sstevel@tonic-gate * one level, or if a driver services devices that interrupt 650Sstevel@tonic-gate * on more than one level, then the driver should install 660Sstevel@tonic-gate * itself on each of those levels. 670Sstevel@tonic-gate */ 680Sstevel@tonic-gate static char badsoft[] = 690Sstevel@tonic-gate "add_avintr: bad soft interrupt level %d for driver '%s'\n"; 700Sstevel@tonic-gate static char multilevel[] = 710Sstevel@tonic-gate "!IRQ%d is being shared by drivers with different interrupt levels.\n" 720Sstevel@tonic-gate "This may result in reduced system performance."; 730Sstevel@tonic-gate static char multilevel2[] = 740Sstevel@tonic-gate "Cannot register interrupt for '%s' device at IPL %d because it\n" 750Sstevel@tonic-gate "conflicts with another device using the same vector %d with an IPL\n" 760Sstevel@tonic-gate "of %d. Reconfigure the conflicting devices to use different vectors."; 770Sstevel@tonic-gate 785084Sjohnlev #ifdef __xpv 795084Sjohnlev #define MAX_VECT NR_IRQS 805084Sjohnlev #else 810Sstevel@tonic-gate #define MAX_VECT 256 825084Sjohnlev #endif 835084Sjohnlev 840Sstevel@tonic-gate struct autovec *nmivect = NULL; 850Sstevel@tonic-gate struct av_head autovect[MAX_VECT]; 860Sstevel@tonic-gate struct av_head softvect[LOCK_LEVEL + 1]; 870Sstevel@tonic-gate kmutex_t av_lock; 885107Seota /* 895107Seota * These are software interrupt handlers dedicated to ddi timer. 905107Seota * The interrupt levels up to 10 are supported, but high interrupts 915107Seota * must not be used there. 925107Seota */ 935107Seota ddi_softint_hdl_impl_t softlevel_hdl[DDI_IPL_10] = { 945107Seota {0, NULL, NULL, NULL, 0, NULL, NULL, NULL}, /* level 1 */ 955107Seota {0, NULL, NULL, NULL, 0, NULL, NULL, NULL}, /* level 2 */ 965107Seota {0, NULL, NULL, NULL, 0, NULL, NULL, NULL}, /* level 3 */ 975107Seota {0, NULL, NULL, NULL, 0, NULL, NULL, NULL}, /* level 4 */ 985107Seota {0, NULL, NULL, NULL, 0, NULL, NULL, NULL}, /* level 5 */ 995107Seota {0, NULL, NULL, NULL, 0, NULL, NULL, NULL}, /* level 6 */ 1005107Seota {0, NULL, NULL, NULL, 0, NULL, NULL, NULL}, /* level 7 */ 1015107Seota {0, NULL, NULL, NULL, 0, NULL, NULL, NULL}, /* level 8 */ 1025107Seota {0, NULL, NULL, NULL, 0, NULL, NULL, NULL}, /* level 9 */ 1035107Seota {0, NULL, NULL, NULL, 0, NULL, NULL, NULL}, /* level 10 */ 1045107Seota }; 1050Sstevel@tonic-gate ddi_softint_hdl_impl_t softlevel1_hdl = 106999Slq150181 {0, NULL, NULL, NULL, 0, NULL, NULL, NULL}; 107999Slq150181 108999Slq150181 /* 109999Slq150181 * clear/check softint pending flag corresponding for 110999Slq150181 * the current CPU 111999Slq150181 */ 1120Sstevel@tonic-gate void 113999Slq150181 av_clear_softint_pending(av_softinfo_t *infop) 114999Slq150181 { 115999Slq150181 CPUSET_ATOMIC_DEL(infop->av_pending, CPU->cpu_seqid); 116999Slq150181 } 117999Slq150181 118999Slq150181 boolean_t 119999Slq150181 av_check_softint_pending(av_softinfo_t *infop, boolean_t check_all) 1200Sstevel@tonic-gate { 121999Slq150181 if (check_all) 122999Slq150181 return (!CPUSET_ISNULL(infop->av_pending)); 123999Slq150181 else 124999Slq150181 return (CPU_IN_SET(infop->av_pending, CPU->cpu_seqid) != 0); 125999Slq150181 } 126999Slq150181 127999Slq150181 /* 1284652Scwb * This is the wrapper function which is generally used to set a softint 1294652Scwb * pending 1304652Scwb */ 1314652Scwb void 1324652Scwb av_set_softint_pending(int pri, av_softinfo_t *infop) 1334652Scwb { 1344652Scwb kdi_av_set_softint_pending(pri, infop); 1354652Scwb } 1364652Scwb 1374652Scwb /* 1384652Scwb * This is kmdb's private entry point to setsoftint called from kdi_siron 139999Slq150181 * It first sets our av softint pending bit for the current CPU, 140999Slq150181 * then it sets the CPU softint pending bit for pri. 141999Slq150181 */ 142999Slq150181 void 1434652Scwb kdi_av_set_softint_pending(int pri, av_softinfo_t *infop) 144999Slq150181 { 145999Slq150181 CPUSET_ATOMIC_ADD(infop->av_pending, CPU->cpu_seqid); 146999Slq150181 1470Sstevel@tonic-gate atomic_or_32((uint32_t *)&CPU->cpu_softinfo.st_pending, 1 << pri); 1480Sstevel@tonic-gate } 1490Sstevel@tonic-gate 1500Sstevel@tonic-gate /* 1510Sstevel@tonic-gate * register nmi interrupt routine. The first arg is used only to order 1520Sstevel@tonic-gate * various nmi interrupt service routines in the chain. Higher lvls will 1530Sstevel@tonic-gate * be called first 1540Sstevel@tonic-gate */ 1550Sstevel@tonic-gate int 1560Sstevel@tonic-gate add_nmintr(int lvl, avfunc nmintr, char *name, caddr_t arg) 1570Sstevel@tonic-gate { 1580Sstevel@tonic-gate struct autovec *mem; 1590Sstevel@tonic-gate struct autovec *p, *prev = NULL; 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate if (nmintr == NULL) { 1620Sstevel@tonic-gate printf("Attempt to add null vect for %s on nmi\n", name); 1630Sstevel@tonic-gate return (0); 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate } 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate mem = kmem_zalloc(sizeof (struct autovec), KM_SLEEP); 1680Sstevel@tonic-gate mem->av_vector = nmintr; 1690Sstevel@tonic-gate mem->av_intarg1 = arg; 1700Sstevel@tonic-gate mem->av_intarg2 = NULL; 1710Sstevel@tonic-gate mem->av_intr_id = NULL; 1720Sstevel@tonic-gate mem->av_prilevel = lvl; 173191Sahl mem->av_dip = NULL; 1740Sstevel@tonic-gate mem->av_link = NULL; 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate mutex_enter(&av_lock); 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate if (!nmivect) { 1790Sstevel@tonic-gate nmivect = mem; 1800Sstevel@tonic-gate mutex_exit(&av_lock); 1810Sstevel@tonic-gate return (1); 1820Sstevel@tonic-gate } 1830Sstevel@tonic-gate /* find where it goes in list */ 1840Sstevel@tonic-gate for (p = nmivect; p != NULL; p = p->av_link) { 1850Sstevel@tonic-gate if (p->av_vector == nmintr && p->av_intarg1 == arg) { 1860Sstevel@tonic-gate /* 1870Sstevel@tonic-gate * already in list 1880Sstevel@tonic-gate * So? Somebody added the same interrupt twice. 1890Sstevel@tonic-gate */ 1900Sstevel@tonic-gate cmn_err(CE_WARN, "Driver already registered '%s'", 1910Sstevel@tonic-gate name); 1920Sstevel@tonic-gate kmem_free(mem, sizeof (struct autovec)); 1930Sstevel@tonic-gate mutex_exit(&av_lock); 1940Sstevel@tonic-gate return (0); 1950Sstevel@tonic-gate } 1960Sstevel@tonic-gate if (p->av_prilevel < lvl) { 1970Sstevel@tonic-gate if (p == nmivect) { /* it's at head of list */ 1980Sstevel@tonic-gate mem->av_link = p; 1990Sstevel@tonic-gate nmivect = mem; 2000Sstevel@tonic-gate } else { 2010Sstevel@tonic-gate mem->av_link = p; 2020Sstevel@tonic-gate prev->av_link = mem; 2030Sstevel@tonic-gate } 2040Sstevel@tonic-gate mutex_exit(&av_lock); 2050Sstevel@tonic-gate return (1); 2060Sstevel@tonic-gate } 2070Sstevel@tonic-gate prev = p; 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate } 2100Sstevel@tonic-gate /* didn't find it, add it to the end */ 2110Sstevel@tonic-gate prev->av_link = mem; 2120Sstevel@tonic-gate mutex_exit(&av_lock); 2130Sstevel@tonic-gate return (1); 2140Sstevel@tonic-gate 2150Sstevel@tonic-gate } 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate /* 2180Sstevel@tonic-gate * register a hardware interrupt handler. 219*12683SJimmy.Vetayases@oracle.com * 220*12683SJimmy.Vetayases@oracle.com * The autovect data structure only supports globally 256 interrupts. 221*12683SJimmy.Vetayases@oracle.com * In order to support 256 * #LocalAPIC interrupts, a new PSM module 222*12683SJimmy.Vetayases@oracle.com * apix is introduced. It defines PSM private data structures for the 223*12683SJimmy.Vetayases@oracle.com * interrupt handlers. The PSM module initializes addintr to a PSM 224*12683SJimmy.Vetayases@oracle.com * private function so that it could override add_avintr() to operate 225*12683SJimmy.Vetayases@oracle.com * on its private data structures. 2260Sstevel@tonic-gate */ 2270Sstevel@tonic-gate int 2280Sstevel@tonic-gate add_avintr(void *intr_id, int lvl, avfunc xxintr, char *name, int vect, 229916Sschwartz caddr_t arg1, caddr_t arg2, uint64_t *ticksp, dev_info_t *dip) 2300Sstevel@tonic-gate { 2310Sstevel@tonic-gate struct av_head *vecp = (struct av_head *)0; 2320Sstevel@tonic-gate avfunc f; 2330Sstevel@tonic-gate int s, vectindex; /* save old spl value */ 2340Sstevel@tonic-gate ushort_t hi_pri; 2350Sstevel@tonic-gate 236*12683SJimmy.Vetayases@oracle.com if (addintr) { 237*12683SJimmy.Vetayases@oracle.com return ((*addintr)(intr_id, lvl, xxintr, name, vect, 238*12683SJimmy.Vetayases@oracle.com arg1, arg2, ticksp, dip)); 239*12683SJimmy.Vetayases@oracle.com } 240*12683SJimmy.Vetayases@oracle.com 2410Sstevel@tonic-gate if ((f = xxintr) == NULL) { 2420Sstevel@tonic-gate printf("Attempt to add null vect for %s on vector %d\n", 2434652Scwb name, vect); 2440Sstevel@tonic-gate return (0); 2450Sstevel@tonic-gate 2460Sstevel@tonic-gate } 2470Sstevel@tonic-gate vectindex = vect % MAX_VECT; 2480Sstevel@tonic-gate 2490Sstevel@tonic-gate vecp = &autovect[vectindex]; 2500Sstevel@tonic-gate 2510Sstevel@tonic-gate /* 2520Sstevel@tonic-gate * "hi_pri == 0" implies all entries on list are "unused", 2530Sstevel@tonic-gate * which means that it's OK to just insert this one. 2540Sstevel@tonic-gate */ 2550Sstevel@tonic-gate hi_pri = vecp->avh_hi_pri; 2560Sstevel@tonic-gate if (vecp->avh_link && (hi_pri != 0)) { 2570Sstevel@tonic-gate if (((hi_pri > LOCK_LEVEL) && (lvl < LOCK_LEVEL)) || 2580Sstevel@tonic-gate ((hi_pri < LOCK_LEVEL) && (lvl > LOCK_LEVEL))) { 2590Sstevel@tonic-gate cmn_err(CE_WARN, multilevel2, name, lvl, vect, 2604652Scwb hi_pri); 2610Sstevel@tonic-gate return (0); 2620Sstevel@tonic-gate } 2630Sstevel@tonic-gate if ((vecp->avh_lo_pri != lvl) || (hi_pri != lvl)) 2640Sstevel@tonic-gate cmn_err(CE_NOTE, multilevel, vect); 2650Sstevel@tonic-gate } 2660Sstevel@tonic-gate 267999Slq150181 insert_av(intr_id, vecp, f, arg1, arg2, ticksp, lvl, dip); 2680Sstevel@tonic-gate s = splhi(); 2690Sstevel@tonic-gate /* 2700Sstevel@tonic-gate * do what ever machine specific things are necessary 2710Sstevel@tonic-gate * to set priority level (e.g. set picmasks) 2720Sstevel@tonic-gate */ 2730Sstevel@tonic-gate mutex_enter(&av_lock); 2740Sstevel@tonic-gate (*addspl)(vect, lvl, vecp->avh_lo_pri, vecp->avh_hi_pri); 2750Sstevel@tonic-gate mutex_exit(&av_lock); 2760Sstevel@tonic-gate splx(s); 2770Sstevel@tonic-gate return (1); 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate } 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate void 2820Sstevel@tonic-gate update_avsoftintr_args(void *intr_id, int lvl, caddr_t arg2) 2830Sstevel@tonic-gate { 2840Sstevel@tonic-gate struct autovec *p; 2850Sstevel@tonic-gate struct autovec *target = NULL; 2860Sstevel@tonic-gate struct av_head *vectp = (struct av_head *)&softvect[lvl]; 2870Sstevel@tonic-gate 2880Sstevel@tonic-gate for (p = vectp->avh_link; p && p->av_vector; p = p->av_link) { 2890Sstevel@tonic-gate if (p->av_intr_id == intr_id) { 2900Sstevel@tonic-gate target = p; 2910Sstevel@tonic-gate break; 2920Sstevel@tonic-gate } 2930Sstevel@tonic-gate } 2940Sstevel@tonic-gate 2950Sstevel@tonic-gate if (target == NULL) 2960Sstevel@tonic-gate return; 2970Sstevel@tonic-gate target->av_intarg2 = arg2; 2980Sstevel@tonic-gate } 2990Sstevel@tonic-gate 3000Sstevel@tonic-gate /* 3010Sstevel@tonic-gate * Register a software interrupt handler 3020Sstevel@tonic-gate */ 3030Sstevel@tonic-gate int 3040Sstevel@tonic-gate add_avsoftintr(void *intr_id, int lvl, avfunc xxintr, char *name, 3050Sstevel@tonic-gate caddr_t arg1, caddr_t arg2) 3060Sstevel@tonic-gate { 3070Sstevel@tonic-gate int slvl; 308999Slq150181 ddi_softint_hdl_impl_t *hdlp = (ddi_softint_hdl_impl_t *)intr_id; 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate if ((slvl = slvltovect(lvl)) != -1) 3110Sstevel@tonic-gate return (add_avintr(intr_id, lvl, xxintr, 312916Sschwartz name, slvl, arg1, arg2, NULL, NULL)); 3130Sstevel@tonic-gate 3140Sstevel@tonic-gate if (intr_id == NULL) { 3150Sstevel@tonic-gate printf("Attempt to add null intr_id for %s on level %d\n", 3160Sstevel@tonic-gate name, lvl); 3170Sstevel@tonic-gate return (0); 3180Sstevel@tonic-gate } 3190Sstevel@tonic-gate 3200Sstevel@tonic-gate if (xxintr == NULL) { 3210Sstevel@tonic-gate printf("Attempt to add null handler for %s on level %d\n", 3220Sstevel@tonic-gate name, lvl); 3230Sstevel@tonic-gate return (0); 3240Sstevel@tonic-gate } 3250Sstevel@tonic-gate 3260Sstevel@tonic-gate if (lvl <= 0 || lvl > LOCK_LEVEL) { 3270Sstevel@tonic-gate printf(badsoft, lvl, name); 3280Sstevel@tonic-gate return (0); 3290Sstevel@tonic-gate } 330999Slq150181 331999Slq150181 if (hdlp->ih_pending == NULL) { 332999Slq150181 hdlp->ih_pending = 3334652Scwb kmem_zalloc(sizeof (av_softinfo_t), KM_SLEEP); 3340Sstevel@tonic-gate } 335999Slq150181 336999Slq150181 insert_av(intr_id, &softvect[lvl], xxintr, arg1, arg2, NULL, lvl, NULL); 337999Slq150181 3380Sstevel@tonic-gate return (1); 3390Sstevel@tonic-gate } 3400Sstevel@tonic-gate 341*12683SJimmy.Vetayases@oracle.com /* 342*12683SJimmy.Vetayases@oracle.com * insert an interrupt vector into chain by its priority from high 343*12683SJimmy.Vetayases@oracle.com * to low 344*12683SJimmy.Vetayases@oracle.com */ 345999Slq150181 static void 3460Sstevel@tonic-gate insert_av(void *intr_id, struct av_head *vectp, avfunc f, caddr_t arg1, 347916Sschwartz caddr_t arg2, uint64_t *ticksp, int pri_level, dev_info_t *dip) 3480Sstevel@tonic-gate { 3490Sstevel@tonic-gate /* 3500Sstevel@tonic-gate * Protect rewrites of the list 3510Sstevel@tonic-gate */ 352*12683SJimmy.Vetayases@oracle.com struct autovec *p, *prep, *mem; 3530Sstevel@tonic-gate 3540Sstevel@tonic-gate mem = kmem_zalloc(sizeof (struct autovec), KM_SLEEP); 3550Sstevel@tonic-gate mem->av_vector = f; 3560Sstevel@tonic-gate mem->av_intarg1 = arg1; 3570Sstevel@tonic-gate mem->av_intarg2 = arg2; 358916Sschwartz mem->av_ticksp = ticksp; 3590Sstevel@tonic-gate mem->av_intr_id = intr_id; 3600Sstevel@tonic-gate mem->av_prilevel = pri_level; 3610Sstevel@tonic-gate mem->av_dip = dip; 3620Sstevel@tonic-gate mem->av_link = NULL; 3630Sstevel@tonic-gate 3640Sstevel@tonic-gate mutex_enter(&av_lock); 3650Sstevel@tonic-gate 3660Sstevel@tonic-gate if (vectp->avh_link == NULL) { /* Nothing on list - put it at head */ 3670Sstevel@tonic-gate vectp->avh_link = mem; 3680Sstevel@tonic-gate vectp->avh_hi_pri = vectp->avh_lo_pri = (ushort_t)pri_level; 3690Sstevel@tonic-gate 3700Sstevel@tonic-gate mutex_exit(&av_lock); 371999Slq150181 return; 3720Sstevel@tonic-gate } 3730Sstevel@tonic-gate 3740Sstevel@tonic-gate /* find where it goes in list */ 375*12683SJimmy.Vetayases@oracle.com prep = NULL; 3760Sstevel@tonic-gate for (p = vectp->avh_link; p != NULL; p = p->av_link) { 377*12683SJimmy.Vetayases@oracle.com if (p->av_vector && p->av_prilevel <= pri_level) 378*12683SJimmy.Vetayases@oracle.com break; 379*12683SJimmy.Vetayases@oracle.com prep = p; 380*12683SJimmy.Vetayases@oracle.com } 381*12683SJimmy.Vetayases@oracle.com if (prep != NULL) { 382*12683SJimmy.Vetayases@oracle.com if (prep->av_vector == NULL) { /* freed struct available */ 383*12683SJimmy.Vetayases@oracle.com p = prep; 3840Sstevel@tonic-gate p->av_intarg1 = arg1; 3850Sstevel@tonic-gate p->av_intarg2 = arg2; 386916Sschwartz p->av_ticksp = ticksp; 3870Sstevel@tonic-gate p->av_intr_id = intr_id; 3880Sstevel@tonic-gate p->av_prilevel = pri_level; 389191Sahl p->av_dip = dip; 3900Sstevel@tonic-gate if (pri_level > (int)vectp->avh_hi_pri) { 3910Sstevel@tonic-gate vectp->avh_hi_pri = (ushort_t)pri_level; 3920Sstevel@tonic-gate } 3930Sstevel@tonic-gate if (pri_level < (int)vectp->avh_lo_pri) { 3940Sstevel@tonic-gate vectp->avh_lo_pri = (ushort_t)pri_level; 3950Sstevel@tonic-gate } 3965084Sjohnlev /* 3975084Sjohnlev * To prevent calling service routine before args 3985084Sjohnlev * and ticksp are ready fill in vector last. 3995084Sjohnlev */ 4000Sstevel@tonic-gate p->av_vector = f; 4010Sstevel@tonic-gate mutex_exit(&av_lock); 4025084Sjohnlev kmem_free(mem, sizeof (struct autovec)); 403999Slq150181 return; 4040Sstevel@tonic-gate } 405*12683SJimmy.Vetayases@oracle.com 406*12683SJimmy.Vetayases@oracle.com mem->av_link = prep->av_link; 407*12683SJimmy.Vetayases@oracle.com prep->av_link = mem; 408*12683SJimmy.Vetayases@oracle.com } else { 409*12683SJimmy.Vetayases@oracle.com /* insert new intpt at beginning of chain */ 410*12683SJimmy.Vetayases@oracle.com mem->av_link = vectp->avh_link; 411*12683SJimmy.Vetayases@oracle.com vectp->avh_link = mem; 4120Sstevel@tonic-gate } 4130Sstevel@tonic-gate if (pri_level > (int)vectp->avh_hi_pri) { 4140Sstevel@tonic-gate vectp->avh_hi_pri = (ushort_t)pri_level; 4150Sstevel@tonic-gate } 4160Sstevel@tonic-gate if (pri_level < (int)vectp->avh_lo_pri) { 4170Sstevel@tonic-gate vectp->avh_lo_pri = (ushort_t)pri_level; 4180Sstevel@tonic-gate } 4190Sstevel@tonic-gate mutex_exit(&av_lock); 4200Sstevel@tonic-gate } 4210Sstevel@tonic-gate 422999Slq150181 static int 423999Slq150181 av_rem_softintr(void *intr_id, int lvl, avfunc xxintr, boolean_t rem_softinfo) 4240Sstevel@tonic-gate { 4250Sstevel@tonic-gate struct av_head *vecp = (struct av_head *)0; 4260Sstevel@tonic-gate int slvl; 427999Slq150181 ddi_softint_hdl_impl_t *hdlp = (ddi_softint_hdl_impl_t *)intr_id; 428999Slq150181 av_softinfo_t *infop = (av_softinfo_t *)hdlp->ih_pending; 4290Sstevel@tonic-gate 4300Sstevel@tonic-gate if (xxintr == NULL) 4310Sstevel@tonic-gate return (0); 4320Sstevel@tonic-gate 4330Sstevel@tonic-gate if ((slvl = slvltovect(lvl)) != -1) { 4340Sstevel@tonic-gate rem_avintr(intr_id, lvl, xxintr, slvl); 4350Sstevel@tonic-gate return (1); 4360Sstevel@tonic-gate } 4370Sstevel@tonic-gate 4380Sstevel@tonic-gate if (lvl <= 0 && lvl >= LOCK_LEVEL) { 4390Sstevel@tonic-gate return (0); 4400Sstevel@tonic-gate } 4410Sstevel@tonic-gate vecp = &softvect[lvl]; 4420Sstevel@tonic-gate remove_av(intr_id, vecp, xxintr, lvl, 0); 4430Sstevel@tonic-gate 444999Slq150181 if (rem_softinfo) { 445999Slq150181 kmem_free(infop, sizeof (av_softinfo_t)); 446999Slq150181 hdlp->ih_pending = NULL; 447999Slq150181 } 448999Slq150181 4490Sstevel@tonic-gate return (1); 4500Sstevel@tonic-gate } 4510Sstevel@tonic-gate 452999Slq150181 int 453999Slq150181 av_softint_movepri(void *intr_id, int old_lvl) 454999Slq150181 { 455999Slq150181 int ret; 456999Slq150181 ddi_softint_hdl_impl_t *hdlp = (ddi_softint_hdl_impl_t *)intr_id; 457999Slq150181 458999Slq150181 ret = add_avsoftintr(intr_id, hdlp->ih_pri, hdlp->ih_cb_func, 459999Slq150181 DEVI(hdlp->ih_dip)->devi_name, hdlp->ih_cb_arg1, hdlp->ih_cb_arg2); 460999Slq150181 461999Slq150181 if (ret) { 462999Slq150181 (void) av_rem_softintr(intr_id, old_lvl, hdlp->ih_cb_func, 463999Slq150181 B_FALSE); 464999Slq150181 } 465999Slq150181 466999Slq150181 return (ret); 467999Slq150181 } 468999Slq150181 469999Slq150181 /* 470999Slq150181 * Remove a driver from the autovector list. 471999Slq150181 */ 472999Slq150181 int 473999Slq150181 rem_avsoftintr(void *intr_id, int lvl, avfunc xxintr) 474999Slq150181 { 475999Slq150181 return (av_rem_softintr(intr_id, lvl, xxintr, B_TRUE)); 476999Slq150181 } 477999Slq150181 478*12683SJimmy.Vetayases@oracle.com /* 479*12683SJimmy.Vetayases@oracle.com * Remove specified interrupt handler. 480*12683SJimmy.Vetayases@oracle.com * 481*12683SJimmy.Vetayases@oracle.com * PSM module could initialize remintr to some PSM private function 482*12683SJimmy.Vetayases@oracle.com * so that it could override rem_avintr() to operate on its private 483*12683SJimmy.Vetayases@oracle.com * data structures. 484*12683SJimmy.Vetayases@oracle.com */ 4850Sstevel@tonic-gate void 4860Sstevel@tonic-gate rem_avintr(void *intr_id, int lvl, avfunc xxintr, int vect) 4870Sstevel@tonic-gate { 4880Sstevel@tonic-gate struct av_head *vecp = (struct av_head *)0; 4890Sstevel@tonic-gate avfunc f; 4900Sstevel@tonic-gate int s, vectindex; /* save old spl value */ 4910Sstevel@tonic-gate 492*12683SJimmy.Vetayases@oracle.com if (remintr) { 493*12683SJimmy.Vetayases@oracle.com (*remintr)(intr_id, lvl, xxintr, vect); 494*12683SJimmy.Vetayases@oracle.com return; 495*12683SJimmy.Vetayases@oracle.com } 496*12683SJimmy.Vetayases@oracle.com 4970Sstevel@tonic-gate if ((f = xxintr) == NULL) 4980Sstevel@tonic-gate return; 4990Sstevel@tonic-gate 5000Sstevel@tonic-gate vectindex = vect % MAX_VECT; 5010Sstevel@tonic-gate vecp = &autovect[vectindex]; 5020Sstevel@tonic-gate remove_av(intr_id, vecp, f, lvl, vect); 5030Sstevel@tonic-gate s = splhi(); 5040Sstevel@tonic-gate mutex_enter(&av_lock); 5050Sstevel@tonic-gate (*delspl)(vect, lvl, vecp->avh_lo_pri, vecp->avh_hi_pri); 5060Sstevel@tonic-gate mutex_exit(&av_lock); 5070Sstevel@tonic-gate splx(s); 5080Sstevel@tonic-gate } 5090Sstevel@tonic-gate 5100Sstevel@tonic-gate 5110Sstevel@tonic-gate /* 5120Sstevel@tonic-gate * After having made a change to an autovector list, wait until we have 5130Sstevel@tonic-gate * seen each cpu not executing an interrupt at that level--so we know our 5140Sstevel@tonic-gate * change has taken effect completely (no old state in registers, etc). 5150Sstevel@tonic-gate */ 516*12683SJimmy.Vetayases@oracle.com void 5170Sstevel@tonic-gate wait_till_seen(int ipl) 5180Sstevel@tonic-gate { 5190Sstevel@tonic-gate int cpu_in_chain, cix; 5200Sstevel@tonic-gate struct cpu *cpup; 5210Sstevel@tonic-gate cpuset_t cpus_to_check; 5220Sstevel@tonic-gate 5230Sstevel@tonic-gate CPUSET_ALL(cpus_to_check); 5240Sstevel@tonic-gate do { 5250Sstevel@tonic-gate cpu_in_chain = 0; 5260Sstevel@tonic-gate for (cix = 0; cix < NCPU; cix++) { 5270Sstevel@tonic-gate cpup = cpu[cix]; 5280Sstevel@tonic-gate if (cpup != NULL && CPU_IN_SET(cpus_to_check, cix)) { 5299717SColin.Yi@Sun.COM if (INTR_ACTIVE(cpup, ipl)) { 5300Sstevel@tonic-gate cpu_in_chain = 1; 5310Sstevel@tonic-gate } else { 5320Sstevel@tonic-gate CPUSET_DEL(cpus_to_check, cix); 5330Sstevel@tonic-gate } 5340Sstevel@tonic-gate } 5350Sstevel@tonic-gate } 5360Sstevel@tonic-gate } while (cpu_in_chain); 5370Sstevel@tonic-gate } 5380Sstevel@tonic-gate 5395084Sjohnlev static uint64_t dummy_tick; 5405084Sjohnlev 5410Sstevel@tonic-gate /* remove an interrupt vector from the chain */ 5420Sstevel@tonic-gate static void 5430Sstevel@tonic-gate remove_av(void *intr_id, struct av_head *vectp, avfunc f, int pri_level, 5440Sstevel@tonic-gate int vect) 5450Sstevel@tonic-gate { 5465084Sjohnlev struct autovec *p, *target; 5470Sstevel@tonic-gate int lo_pri, hi_pri; 5480Sstevel@tonic-gate int ipl; 5490Sstevel@tonic-gate /* 5500Sstevel@tonic-gate * Protect rewrites of the list 5510Sstevel@tonic-gate */ 5520Sstevel@tonic-gate target = NULL; 5530Sstevel@tonic-gate 5540Sstevel@tonic-gate mutex_enter(&av_lock); 5550Sstevel@tonic-gate ipl = pri_level; 5560Sstevel@tonic-gate lo_pri = MAXIPL; 5570Sstevel@tonic-gate hi_pri = 0; 5585084Sjohnlev for (p = vectp->avh_link; p; p = p->av_link) { 5590Sstevel@tonic-gate if ((p->av_vector == f) && (p->av_intr_id == intr_id)) { 5600Sstevel@tonic-gate /* found the handler */ 5610Sstevel@tonic-gate target = p; 5620Sstevel@tonic-gate continue; 5630Sstevel@tonic-gate } 5645084Sjohnlev if (p->av_vector != NULL) { 5655084Sjohnlev if (p->av_prilevel > hi_pri) 5665084Sjohnlev hi_pri = p->av_prilevel; 5675084Sjohnlev if (p->av_prilevel < lo_pri) 5685084Sjohnlev lo_pri = p->av_prilevel; 5695084Sjohnlev } 5700Sstevel@tonic-gate } 5710Sstevel@tonic-gate if (ipl < hi_pri) 5720Sstevel@tonic-gate ipl = hi_pri; 5730Sstevel@tonic-gate if (target == NULL) { /* not found */ 5740Sstevel@tonic-gate printf("Couldn't remove function %p at %d, %d\n", 5754652Scwb (void *)f, vect, pri_level); 5760Sstevel@tonic-gate mutex_exit(&av_lock); 5770Sstevel@tonic-gate return; 5780Sstevel@tonic-gate } 5790Sstevel@tonic-gate 5805084Sjohnlev /* 5815084Sjohnlev * This drops the handler from the chain, it can no longer be called. 5825084Sjohnlev * However, there is no guarantee that the handler is not currently 5835084Sjohnlev * still executing. 5845084Sjohnlev */ 5850Sstevel@tonic-gate target->av_vector = NULL; 5865084Sjohnlev /* 5875084Sjohnlev * There is a race where we could be just about to pick up the ticksp 5885084Sjohnlev * pointer to increment it after returning from the service routine 5895084Sjohnlev * in av_dispatch_autovect. Rather than NULL it out let's just point 5905084Sjohnlev * it off to something safe so that any final tick update attempt 5915084Sjohnlev * won't fault. 5925084Sjohnlev */ 5935084Sjohnlev target->av_ticksp = &dummy_tick; 5940Sstevel@tonic-gate wait_till_seen(ipl); 5950Sstevel@tonic-gate 5960Sstevel@tonic-gate if (lo_pri > hi_pri) { /* the chain is now empty */ 5970Sstevel@tonic-gate /* Leave the unused entries here for probable future use */ 5980Sstevel@tonic-gate vectp->avh_lo_pri = MAXIPL; 5990Sstevel@tonic-gate vectp->avh_hi_pri = 0; 6000Sstevel@tonic-gate } else { 6010Sstevel@tonic-gate if ((int)vectp->avh_lo_pri < lo_pri) 6020Sstevel@tonic-gate vectp->avh_lo_pri = (ushort_t)lo_pri; 6030Sstevel@tonic-gate if ((int)vectp->avh_hi_pri > hi_pri) 6040Sstevel@tonic-gate vectp->avh_hi_pri = (ushort_t)hi_pri; 6050Sstevel@tonic-gate } 6060Sstevel@tonic-gate mutex_exit(&av_lock); 6070Sstevel@tonic-gate wait_till_seen(ipl); 6080Sstevel@tonic-gate } 6090Sstevel@tonic-gate 6100Sstevel@tonic-gate /* 6114652Scwb * kmdb uses siron (and thus setsoftint) while the world is stopped in order to 6124652Scwb * inform its driver component that there's work to be done. We need to keep 6134652Scwb * DTrace from instrumenting kmdb's siron and setsoftint. We duplicate siron, 6144652Scwb * giving kmdb's version a kdi prefix to keep DTrace at bay. We also 6154652Scwb * provide a version of the various setsoftint functions available for kmdb to 6164652Scwb * use using a kdi_ prefix while the main *setsoftint() functionality is 6174652Scwb * implemented as a wrapper. This allows tracing, while still providing a 6184652Scwb * way for kmdb to sneak in unmolested. 6194652Scwb */ 6204652Scwb void 6214652Scwb kdi_siron(void) 6224652Scwb { 6234652Scwb (*kdisetsoftint)(1, softlevel1_hdl.ih_pending); 6244652Scwb } 6254652Scwb 6264652Scwb /* 6270Sstevel@tonic-gate * Trigger a soft interrupt. 6280Sstevel@tonic-gate */ 6290Sstevel@tonic-gate void 6300Sstevel@tonic-gate siron(void) 6310Sstevel@tonic-gate { 6325107Seota /* Level 1 software interrupt */ 633999Slq150181 (*setsoftint)(1, softlevel1_hdl.ih_pending); 6340Sstevel@tonic-gate } 6350Sstevel@tonic-gate 6360Sstevel@tonic-gate /* 6375107Seota * Trigger software interrupts dedicated to ddi timer. 6385107Seota */ 6395107Seota void 6405107Seota sir_on(int level) 6415107Seota { 6425107Seota ASSERT(level >= DDI_IPL_1 && level <= DDI_IPL_10); 6435107Seota (*setsoftint)(level, softlevel_hdl[level-1].ih_pending); 6445107Seota } 6455107Seota 6465107Seota /* 6475076Smishra * The handler which is executed on the target CPU. 6485076Smishra */ 6495076Smishra /*ARGSUSED*/ 6505076Smishra static int 6515076Smishra siron_poke_intr(xc_arg_t a1, xc_arg_t a2, xc_arg_t a3) 6525076Smishra { 6535076Smishra siron(); 6545076Smishra return (0); 6555076Smishra } 6565076Smishra 6575076Smishra /* 6585076Smishra * May get called from softcall to poke CPUs. 6595076Smishra */ 6605076Smishra void 6615076Smishra siron_poke_cpu(cpuset_t poke) 6625076Smishra { 6635076Smishra int cpuid = CPU->cpu_id; 6645076Smishra 6655076Smishra /* 6665076Smishra * If we are poking to ourself then we can simply 6675076Smishra * generate level1 using siron() 6685076Smishra */ 6695076Smishra if (CPU_IN_SET(poke, cpuid)) { 6705076Smishra siron(); 6715076Smishra CPUSET_DEL(poke, cpuid); 6725076Smishra if (CPUSET_ISNULL(poke)) 6735076Smishra return; 6745076Smishra } 6755076Smishra 6769489SJoe.Bonasera@sun.com xc_call(0, 0, 0, CPUSET2BV(poke), (xc_func_t)siron_poke_intr); 6775076Smishra } 6785076Smishra 6795076Smishra /* 6800Sstevel@tonic-gate * Walk the autovector table for this vector, invoking each 6810Sstevel@tonic-gate * interrupt handler as we go. 6820Sstevel@tonic-gate */ 683916Sschwartz 684916Sschwartz extern uint64_t intr_get_time(void); 685916Sschwartz 6860Sstevel@tonic-gate void 6870Sstevel@tonic-gate av_dispatch_autovect(uint_t vec) 6880Sstevel@tonic-gate { 6890Sstevel@tonic-gate struct autovec *av; 6900Sstevel@tonic-gate 6910Sstevel@tonic-gate ASSERT_STACK_ALIGNED(); 6920Sstevel@tonic-gate 6930Sstevel@tonic-gate while ((av = autovect[vec].avh_link) != NULL) { 6940Sstevel@tonic-gate uint_t numcalled = 0; 6950Sstevel@tonic-gate uint_t claimed = 0; 6960Sstevel@tonic-gate 6970Sstevel@tonic-gate for (; av; av = av->av_link) { 6980Sstevel@tonic-gate uint_t r; 6990Sstevel@tonic-gate uint_t (*intr)() = av->av_vector; 7000Sstevel@tonic-gate caddr_t arg1 = av->av_intarg1; 7010Sstevel@tonic-gate caddr_t arg2 = av->av_intarg2; 7020Sstevel@tonic-gate dev_info_t *dip = av->av_dip; 7030Sstevel@tonic-gate 7045084Sjohnlev /* 7055084Sjohnlev * We must walk the entire chain. Removed handlers 7065084Sjohnlev * may be anywhere in the chain. 7075084Sjohnlev */ 7080Sstevel@tonic-gate if (intr == NULL) 7095084Sjohnlev continue; 7100Sstevel@tonic-gate 7110Sstevel@tonic-gate DTRACE_PROBE4(interrupt__start, dev_info_t *, dip, 7120Sstevel@tonic-gate void *, intr, caddr_t, arg1, caddr_t, arg2); 7130Sstevel@tonic-gate r = (*intr)(arg1, arg2); 7140Sstevel@tonic-gate DTRACE_PROBE4(interrupt__complete, dev_info_t *, dip, 7150Sstevel@tonic-gate void *, intr, caddr_t, arg1, uint_t, r); 7163446Smrj numcalled++; 7170Sstevel@tonic-gate claimed |= r; 718916Sschwartz if (av->av_ticksp && av->av_prilevel <= LOCK_LEVEL) 719916Sschwartz atomic_add_64(av->av_ticksp, intr_get_time()); 7200Sstevel@tonic-gate } 7210Sstevel@tonic-gate 7220Sstevel@tonic-gate /* 7230Sstevel@tonic-gate * If there's only one interrupt handler in the chain, 7240Sstevel@tonic-gate * or if no-one claimed the interrupt at all give up now. 7250Sstevel@tonic-gate */ 7260Sstevel@tonic-gate if (numcalled == 1 || claimed == 0) 7270Sstevel@tonic-gate break; 7280Sstevel@tonic-gate } 7290Sstevel@tonic-gate } 7300Sstevel@tonic-gate 7310Sstevel@tonic-gate /* 7320Sstevel@tonic-gate * Call every soft interrupt handler we can find at this level once. 7330Sstevel@tonic-gate */ 7340Sstevel@tonic-gate void 7350Sstevel@tonic-gate av_dispatch_softvect(uint_t pil) 7360Sstevel@tonic-gate { 7370Sstevel@tonic-gate struct autovec *av; 7380Sstevel@tonic-gate ddi_softint_hdl_impl_t *hdlp; 7390Sstevel@tonic-gate uint_t (*intr)(); 7400Sstevel@tonic-gate caddr_t arg1; 7410Sstevel@tonic-gate caddr_t arg2; 7420Sstevel@tonic-gate 7430Sstevel@tonic-gate ASSERT_STACK_ALIGNED(); 7440Sstevel@tonic-gate ASSERT(pil >= 0 && pil <= PIL_MAX); 7450Sstevel@tonic-gate 7460Sstevel@tonic-gate for (av = softvect[pil].avh_link; av; av = av->av_link) { 7475084Sjohnlev /* 7485084Sjohnlev * We must walk the entire chain. Removed handlers 7495084Sjohnlev * may be anywhere in the chain. 7505084Sjohnlev */ 7510Sstevel@tonic-gate if ((intr = av->av_vector) == NULL) 7525084Sjohnlev continue; 7530Sstevel@tonic-gate arg1 = av->av_intarg1; 7540Sstevel@tonic-gate arg2 = av->av_intarg2; 7550Sstevel@tonic-gate 7560Sstevel@tonic-gate hdlp = (ddi_softint_hdl_impl_t *)av->av_intr_id; 7570Sstevel@tonic-gate ASSERT(hdlp); 7580Sstevel@tonic-gate 759999Slq150181 /* 760999Slq150181 * Each cpu has its own pending bit in hdlp->ih_pending, 761999Slq150181 * here av_check/clear_softint_pending is just checking 762999Slq150181 * and clearing the pending bit for the current cpu, who 763999Slq150181 * has just triggered a softint. 764999Slq150181 */ 765999Slq150181 if (av_check_softint_pending(hdlp->ih_pending, B_FALSE)) { 766999Slq150181 av_clear_softint_pending(hdlp->ih_pending); 7670Sstevel@tonic-gate (void) (*intr)(arg1, arg2); 7680Sstevel@tonic-gate } 7690Sstevel@tonic-gate } 7700Sstevel@tonic-gate } 7710Sstevel@tonic-gate 7720Sstevel@tonic-gate struct regs; 7730Sstevel@tonic-gate 7740Sstevel@tonic-gate /* 7750Sstevel@tonic-gate * Call every NMI handler we know of once. 7760Sstevel@tonic-gate */ 7770Sstevel@tonic-gate void 7780Sstevel@tonic-gate av_dispatch_nmivect(struct regs *rp) 7790Sstevel@tonic-gate { 7800Sstevel@tonic-gate struct autovec *av; 7810Sstevel@tonic-gate 7820Sstevel@tonic-gate ASSERT_STACK_ALIGNED(); 7830Sstevel@tonic-gate 7840Sstevel@tonic-gate for (av = nmivect; av; av = av->av_link) 7850Sstevel@tonic-gate (void) (av->av_vector)(av->av_intarg1, rp); 7860Sstevel@tonic-gate } 787