1 /* $NetBSD: intr.c,v 1.36 2026/11/13 21:14:17 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 1996 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Adam Glass, Gordon W. Ross, Jason R. Thorpe, and Leo Weppelman. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.36 2026/11/13 21:14:17 thorpej Exp $"); 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/kernel.h> 38 #include <sys/kmem.h> 39 #include <sys/vmmeter.h> 40 #include <sys/queue.h> 41 #include <sys/device.h> 42 #include <sys/cpu.h> 43 44 #include <machine/intr.h> 45 46 #define AVEC_MIN 1 47 #define AVEC_MAX 7 48 #define AVEC_LOC 25 49 #define UVEC_MIN 0 50 #define UVEC_MAX 191 51 #define UVEC_LOC 64 52 53 typedef LIST_HEAD(, intrhand) ih_list_t; 54 static ih_list_t autovec_list[AVEC_MAX - AVEC_MIN + 1]; 55 static ih_list_t uservec_list[UVEC_MAX - UVEC_MIN + 1]; 56 57 void 58 intr_init(void) 59 { 60 int i; 61 62 for (i = 0; i < (AVEC_MAX - AVEC_MIN + 1); ++i) { 63 LIST_INIT(&autovec_list[i]); 64 } 65 for (i = 0; i < (UVEC_MAX - UVEC_MIN + 1); ++i) { 66 LIST_INIT(&uservec_list[i]); 67 } 68 } 69 70 /* 71 * Establish an interrupt vector. 72 * - vector 73 * The vector number the interrupt should be hooked on. It can either 74 * be an auto-vector or a user-vector. 75 * - type 76 * A bit-wise of: 77 * - AUTO_VEC (mutually exclusive with USER_VEC) 78 * Attach to one of the 7 auto vectors 79 * - USER_VEC (mutually exclusive with AUTO_VEC) 80 * Attach to one of the 192 user vectors 81 * - FAST_VEC 82 * The interrupt function 'ih_fun' will be 83 * put into the 'real' interrupt table. This 84 * means: 85 * - This vector can't be shared 86 * - 'ih_fun' must save registers 87 * - 'ih_fun' must do its own interrupt accounting 88 * - The argument to 'ih_fun' is a standard 89 * interrupt frame. 90 * - ARG_CLOCKRAME 91 * The 'ih_fun' function will be called with 92 * a standard clock-frame instead of 'ih_arg'. 93 * 94 * - pri 95 * When multiple interrupts are established on the same vector, 96 * interrupts with the highest priority will be called first. The 97 * basic ordering is the order of establishment. 98 * - ih_fun 99 * The interrupt function to be called 100 * - ih_arg 101 * The argument given to 'ih_fun' when ARG_CLOCKFRAME is not 102 * specified. 103 */ 104 105 struct intrhand * 106 intr_establish(int vector, int type, int pri, hw_ifun_t ih_fun, void *ih_arg) 107 { 108 struct intrhand *ih, *cur_vec; 109 ih_list_t *vec_list; 110 u_long *hard_vec; 111 int s; 112 113 ih = kmem_alloc(sizeof *ih, KM_SLEEP); 114 ih->ih_fun = ih_fun; 115 ih->ih_arg = ih_arg; 116 ih->ih_type = type; 117 ih->ih_pri = pri; 118 ih->ih_vector = vector; 119 120 /* 121 * Do some validity checking on the 'vector' argument and determine 122 * vector list this interrupt should be on. 123 */ 124 switch (type & (AUTO_VEC|USER_VEC)) { 125 case AUTO_VEC: 126 if (vector < AVEC_MIN || vector > AVEC_MAX) { 127 kmem_free(ih, sizeof(*ih)); 128 return NULL; 129 } 130 vec_list = &autovec_list[vector - 1]; 131 hard_vec = &autovects[vector - 1]; 132 ih->ih_intrcnt = &intrcnt_auto[vector - 1]; 133 break; 134 case USER_VEC: 135 if (vector < UVEC_MIN || vector > UVEC_MAX) { 136 kmem_free(ih, sizeof(*ih)); 137 return NULL; 138 } 139 vec_list = &uservec_list[vector]; 140 hard_vec = &uservects[vector]; 141 ih->ih_intrcnt = &intrcnt_user[vector]; 142 break; 143 default: 144 printf("%s: bogus vector type\n", __func__); 145 kmem_free(ih, sizeof(*ih)); 146 return NULL; 147 } 148 149 /* 150 * If the vec_list is empty, we insert ourselves at the head of the 151 * list and we re-route the 'hard-vector' to the appropriate handler. 152 */ 153 if (LIST_EMPTY(vec_list)) { 154 155 s = splhigh(); 156 LIST_INSERT_HEAD(vec_list, ih, ih_link); 157 if ((type & FAST_VEC) != 0) 158 *hard_vec = (u_long)ih->ih_fun; 159 else if (*hard_vec != (u_long)intr_glue) { 160 /* 161 * Normally, all settable vectors are already 162 * re-routed to the intr_glue() function. The 163 * marvelous exception to these are the HBL/VBL 164 * interrupts. They happen *very* often and 165 * can't be turned off on the Falcon. So they 166 * are normally vectored to an 'rte' instruction. 167 */ 168 *hard_vec = (u_long)intr_glue; 169 } 170 171 splx(s); 172 173 return ih; 174 } 175 176 /* 177 * Check for FAST_VEC botches 178 */ 179 cur_vec = LIST_FIRST(vec_list); 180 if (cur_vec->ih_type & FAST_VEC) { 181 kmem_free(ih, sizeof(*ih)); 182 printf("%s: vector cannot be shared\n", __func__); 183 return NULL; 184 } 185 186 /* 187 * We traverse the list and place ourselves after any handlers with 188 * our current (or higher) priority level. 189 */ 190 for (cur_vec = LIST_FIRST(vec_list); 191 LIST_NEXT(cur_vec, ih_link) != NULL; 192 cur_vec = LIST_NEXT(cur_vec, ih_link)) { 193 if (ih->ih_pri > cur_vec->ih_pri) { 194 195 s = splhigh(); 196 LIST_INSERT_BEFORE(cur_vec, ih, ih_link); 197 splx(s); 198 199 return ih; 200 } 201 } 202 203 /* 204 * We're the least important entry, it seems. We just go 205 * on the end. 206 */ 207 s = splhigh(); 208 LIST_INSERT_AFTER(cur_vec, ih, ih_link); 209 splx(s); 210 211 return ih; 212 } 213 214 int 215 intr_disestablish(struct intrhand *ih) 216 { 217 ih_list_t *vec_list; 218 u_long *hard_vec; 219 int vector, s; 220 struct intrhand *cur_vec; 221 222 vector = ih->ih_vector; 223 switch (ih->ih_type & (AUTO_VEC|USER_VEC)) { 224 case AUTO_VEC: 225 if (vector < AVEC_MIN || vector > AVEC_MAX) 226 return 0; 227 vec_list = &autovec_list[vector - 1]; 228 hard_vec = &autovects[vector - 1]; 229 break; 230 case USER_VEC: 231 if (vector < UVEC_MIN || vector > UVEC_MAX) 232 return 0; 233 vec_list = &uservec_list[vector]; 234 hard_vec = &uservects[vector]; 235 break; 236 default: 237 printf("%s: bogus vector type\n", __func__); 238 return 0; 239 } 240 241 /* 242 * Check if the vector is really in the list we think it's in.... 243 */ 244 for (cur_vec = LIST_FIRST(vec_list); 245 LIST_NEXT(cur_vec, ih_link) != NULL; 246 cur_vec = LIST_NEXT(cur_vec, ih_link)) { 247 if (ih == cur_vec) 248 break; 249 } 250 if (ih != cur_vec) { 251 printf("%s: 'ih' has inconsistent data\n", __func__); 252 return 0; 253 } 254 255 s = splhigh(); 256 LIST_REMOVE(ih, ih_link); 257 if (LIST_EMPTY(vec_list) && (ih->ih_type & FAST_VEC) != 0) 258 *hard_vec = (u_long)intr_glue; 259 splx(s); 260 261 kmem_free(ih, sizeof(*ih)); 262 return 1; 263 } 264 265 /* 266 * This is the dispatcher called by the low-level 267 * assembly language interrupt-glue routine. 268 */ 269 void 270 intr_dispatch(struct clockframe frame) 271 { 272 static int unexpected, straycount; 273 int vector; 274 int handled = 0; 275 ih_list_t *vec_list; 276 struct intrhand *ih; 277 278 curcpu()->ci_data.cpu_nintr++; 279 vector = (frame.cf_vo & 0xfff) >> 2; 280 if (vector < (AVEC_LOC + AVEC_MAX) && vector >= AVEC_LOC) 281 vec_list = &autovec_list[vector - AVEC_LOC]; 282 else if (vector <= (UVEC_LOC + UVEC_MAX) && vector >= UVEC_LOC) 283 vec_list = &uservec_list[vector - UVEC_LOC]; 284 else 285 panic("%s: Bogus vector %d", __func__, vector); 286 287 if ((ih = LIST_FIRST(vec_list)) == NULL) { 288 printf("%s: vector %d unexpected\n", __func__, vector); 289 if (++unexpected > 10) 290 panic("%s: too many unexpected interrupts", __func__); 291 return; 292 } 293 ih->ih_intrcnt[0]++; 294 295 /* Give all the handlers a chance. */ 296 for (; ih != NULL; ih = LIST_NEXT(ih, ih_link)) 297 handled |= (*ih->ih_fun)((ih->ih_type & ARG_CLOCKFRAME) ? 298 &frame : ih->ih_arg, frame.cf_sr); 299 300 if (handled) 301 straycount = 0; 302 else if (++straycount > 50) 303 panic("%s: too many stray interrupts", __func__); 304 else 305 printf("%s: stray level %d interrupt\n", __func__, vector); 306 } 307 308 const uint16_t ipl2psl_table[NIPL] = { 309 [IPL_NONE] = PSL_S | PSL_IPL0, 310 [IPL_SOFTCLOCK] = PSL_S | PSL_IPL1, 311 [IPL_SOFTBIO] = PSL_S | PSL_IPL1, 312 [IPL_SOFTNET] = PSL_S | PSL_IPL1, 313 [IPL_SOFTSERIAL] = PSL_S | PSL_IPL1, 314 [IPL_VM] = PSL_S | PSL_IPL4, 315 [IPL_SCHED] = PSL_S | PSL_IPL6, 316 [IPL_HIGH] = PSL_S | PSL_IPL7, 317 }; 318