1 1.24 thorpej /* $NetBSD: iomd_irqhandler.c,v 1.24 2020/11/20 18:18:51 thorpej Exp $ */ 2 1.1 reinoud 3 1.1 reinoud /* 4 1.1 reinoud * Copyright (c) 1994-1998 Mark Brinicombe. 5 1.1 reinoud * Copyright (c) 1994 Brini. 6 1.1 reinoud * All rights reserved. 7 1.1 reinoud * 8 1.1 reinoud * This code is derived from software written for Brini by Mark Brinicombe 9 1.1 reinoud * 10 1.1 reinoud * Redistribution and use in source and binary forms, with or without 11 1.1 reinoud * modification, are permitted provided that the following conditions 12 1.1 reinoud * are met: 13 1.1 reinoud * 1. Redistributions of source code must retain the above copyright 14 1.1 reinoud * notice, this list of conditions and the following disclaimer. 15 1.1 reinoud * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 reinoud * notice, this list of conditions and the following disclaimer in the 17 1.1 reinoud * documentation and/or other materials provided with the distribution. 18 1.1 reinoud * 3. All advertising materials mentioning features or use of this software 19 1.1 reinoud * must display the following acknowledgement: 20 1.1 reinoud * This product includes software developed by Mark Brinicombe 21 1.1 reinoud * for the NetBSD Project. 22 1.1 reinoud * 4. The name of the company nor the name of the author may be used to 23 1.1 reinoud * endorse or promote products derived from this software without specific 24 1.1 reinoud * prior written permission. 25 1.1 reinoud * 26 1.1 reinoud * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 27 1.1 reinoud * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 28 1.1 reinoud * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 29 1.1 reinoud * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 30 1.1 reinoud * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 31 1.1 reinoud * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 32 1.1 reinoud * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 33 1.1 reinoud * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34 1.1 reinoud * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 35 1.1 reinoud * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 1.1 reinoud * 37 1.1 reinoud * IRQ/FIQ initialisation, claim, release and handler routines 38 1.1 reinoud * 39 1.1 reinoud * from: irqhandler.c,v 1.14 1997/04/02 21:52:19 christos Exp $ 40 1.1 reinoud */ 41 1.7 lukem 42 1.7 lukem #include <sys/cdefs.h> 43 1.24 thorpej __KERNEL_RCSID(0, "$NetBSD: iomd_irqhandler.c,v 1.24 2020/11/20 18:18:51 thorpej Exp $"); 44 1.1 reinoud 45 1.1 reinoud #include "opt_irqstats.h" 46 1.1 reinoud 47 1.1 reinoud #include <sys/param.h> 48 1.1 reinoud #include <sys/systm.h> 49 1.1 reinoud #include <sys/syslog.h> 50 1.24 thorpej #include <sys/kmem.h> 51 1.1 reinoud 52 1.22 skrll #include <arm/cpufunc.h> 53 1.1 reinoud #include <arm/iomd/iomdreg.h> 54 1.1 reinoud #include <arm/iomd/iomdvar.h> 55 1.1 reinoud 56 1.3 thorpej #include <machine/intr.h> 57 1.1 reinoud #include <machine/cpu.h> 58 1.1 reinoud 59 1.1 reinoud irqhandler_t *irqhandlers[NIRQS]; 60 1.1 reinoud 61 1.1 reinoud u_int current_mask; 62 1.1 reinoud u_int actual_mask; 63 1.1 reinoud u_int disabled_mask; 64 1.17 matt u_int irqmasks[NIPL]; 65 1.1 reinoud 66 1.1 reinoud extern char *_intrnames; 67 1.1 reinoud 68 1.1 reinoud /* Prototypes */ 69 1.1 reinoud 70 1.11 bjh21 extern void set_spl_masks(void); 71 1.1 reinoud 72 1.1 reinoud /* 73 1.1 reinoud * void irq_init(void) 74 1.1 reinoud * 75 1.1 reinoud * Initialise the IRQ/FIQ sub system 76 1.1 reinoud */ 77 1.1 reinoud 78 1.1 reinoud void 79 1.11 bjh21 irq_init(void) 80 1.1 reinoud { 81 1.1 reinoud int loop; 82 1.1 reinoud 83 1.1 reinoud /* Clear all the IRQ handlers and the irq block masks */ 84 1.11 bjh21 for (loop = 0; loop < NIRQS; ++loop) 85 1.1 reinoud irqhandlers[loop] = NULL; 86 1.1 reinoud 87 1.1 reinoud /* Clear the IRQ/FIQ masks in the IOMD */ 88 1.1 reinoud IOMD_WRITE_BYTE(IOMD_IRQMSKA, 0x00); 89 1.1 reinoud IOMD_WRITE_BYTE(IOMD_IRQMSKB, 0x00); 90 1.1 reinoud 91 1.1 reinoud switch (IOMD_ID) { 92 1.1 reinoud case RPC600_IOMD_ID: 93 1.1 reinoud break; 94 1.1 reinoud case ARM7500_IOC_ID: 95 1.1 reinoud case ARM7500FE_IOC_ID: 96 1.1 reinoud IOMD_WRITE_BYTE(IOMD_IRQMSKC, 0x00); 97 1.1 reinoud IOMD_WRITE_BYTE(IOMD_IRQMSKD, 0x00); 98 1.1 reinoud break; 99 1.1 reinoud default: 100 1.1 reinoud printf("Unknown IOMD id (%d) found in irq_init()\n", IOMD_ID); 101 1.11 bjh21 } 102 1.1 reinoud 103 1.1 reinoud IOMD_WRITE_BYTE(IOMD_FIQMSK, 0x00); 104 1.1 reinoud IOMD_WRITE_BYTE(IOMD_DMAMSK, 0x00); 105 1.1 reinoud 106 1.1 reinoud /* 107 1.1 reinoud * Setup the irqmasks for the different Interrupt Priority Levels 108 1.1 reinoud * We will start with no bits set and these will be updated as handlers 109 1.1 reinoud * are installed at different IPL's. 110 1.1 reinoud */ 111 1.17 matt for (loop = 0; loop < NIPL; ++loop) 112 1.1 reinoud irqmasks[loop] = 0; 113 1.1 reinoud 114 1.1 reinoud current_mask = 0x00000000; 115 1.1 reinoud disabled_mask = 0x00000000; 116 1.1 reinoud actual_mask = 0x00000000; 117 1.1 reinoud 118 1.1 reinoud set_spl_masks(); 119 1.1 reinoud 120 1.1 reinoud /* Enable IRQ's and FIQ's */ 121 1.1 reinoud enable_interrupts(I32_bit | F32_bit); 122 1.1 reinoud } 123 1.1 reinoud 124 1.1 reinoud 125 1.1 reinoud /* 126 1.1 reinoud * int irq_claim(int irq, irqhandler_t *handler) 127 1.1 reinoud * 128 1.1 reinoud * Enable an IRQ and install a handler for it. 129 1.1 reinoud */ 130 1.1 reinoud 131 1.1 reinoud int 132 1.11 bjh21 irq_claim(int irq, irqhandler_t *handler) 133 1.1 reinoud { 134 1.1 reinoud int level; 135 1.8 chris u_int oldirqstate; 136 1.1 reinoud 137 1.1 reinoud #ifdef DIAGNOSTIC 138 1.1 reinoud /* Sanity check */ 139 1.1 reinoud if (handler == NULL) 140 1.6 provos panic("NULL interrupt handler"); 141 1.1 reinoud if (handler->ih_func == NULL) 142 1.6 provos panic("Interrupt handler does not have a function"); 143 1.1 reinoud #endif /* DIAGNOSTIC */ 144 1.1 reinoud 145 1.1 reinoud /* 146 1.1 reinoud * IRQ_INSTRUCT indicates that we should get the irq number 147 1.1 reinoud * from the irq structure 148 1.1 reinoud */ 149 1.1 reinoud if (irq == IRQ_INSTRUCT) 150 1.1 reinoud irq = handler->ih_num; 151 1.1 reinoud 152 1.1 reinoud /* Make sure the irq number is valid */ 153 1.1 reinoud if (irq < 0 || irq >= NIRQS) 154 1.11 bjh21 return -1; 155 1.1 reinoud 156 1.1 reinoud /* Make sure the level is valid */ 157 1.17 matt if (handler->ih_level < 0 || handler->ih_level >= NIPL) 158 1.11 bjh21 return -1; 159 1.1 reinoud 160 1.8 chris oldirqstate = disable_interrupts(I32_bit); 161 1.8 chris 162 1.1 reinoud /* Attach handler at top of chain */ 163 1.1 reinoud handler->ih_next = irqhandlers[irq]; 164 1.1 reinoud irqhandlers[irq] = handler; 165 1.1 reinoud 166 1.1 reinoud /* 167 1.1 reinoud * Reset the flags for this handler. 168 1.1 reinoud * As the handler is now in the chain mark it as active. 169 1.1 reinoud */ 170 1.1 reinoud handler->ih_flags = 0 | IRQ_FLAG_ACTIVE; 171 1.1 reinoud 172 1.1 reinoud /* 173 1.1 reinoud * Record the interrupt number for accounting. 174 1.1 reinoud * Done here as the accounting number may not be the same as the 175 1.1 reinoud * IRQ number though for the moment they are 176 1.1 reinoud */ 177 1.1 reinoud handler->ih_num = irq; 178 1.1 reinoud 179 1.1 reinoud #ifdef IRQSTATS 180 1.1 reinoud /* Get the interrupt name from the head of the list */ 181 1.19 christos char *iptr = _intrnames + (irq * 14); 182 1.1 reinoud if (handler->ih_name) { 183 1.20 christos strlcpy(iptr, handler->ih_name, 14); 184 1.1 reinoud } else { 185 1.19 christos snprintf(iptr, 14, "irq %2d ", irq); 186 1.1 reinoud } 187 1.1 reinoud #endif /* IRQSTATS */ 188 1.1 reinoud 189 1.1 reinoud /* 190 1.1 reinoud * Update the irq masks. 191 1.1 reinoud * Find the lowest interrupt priority on the irq chain. 192 1.1 reinoud * Interrupt is allowable at priorities lower than this. 193 1.1 reinoud * If ih_level is out of range then don't bother to update 194 1.1 reinoud * the masks. 195 1.1 reinoud */ 196 1.17 matt if (handler->ih_level >= 0 && handler->ih_level < NIPL) { 197 1.1 reinoud irqhandler_t *ptr; 198 1.1 reinoud 199 1.1 reinoud /* 200 1.1 reinoud * Find the lowest interrupt priority on the irq chain. 201 1.1 reinoud * Interrupt is allowable at priorities lower than this. 202 1.1 reinoud */ 203 1.1 reinoud ptr = irqhandlers[irq]; 204 1.1 reinoud if (ptr) { 205 1.1 reinoud int max_level; 206 1.1 reinoud 207 1.1 reinoud level = ptr->ih_level - 1; 208 1.1 reinoud max_level = ptr->ih_level - 1; 209 1.1 reinoud while (ptr) { 210 1.1 reinoud if (ptr->ih_level - 1 < level) 211 1.1 reinoud level = ptr->ih_level - 1; 212 1.1 reinoud else if (ptr->ih_level - 1 > max_level) 213 1.1 reinoud max_level = ptr->ih_level - 1; 214 1.1 reinoud ptr = ptr->ih_next; 215 1.1 reinoud } 216 1.1 reinoud /* Clear out any levels that we cannot now allow */ 217 1.1 reinoud while (max_level >=0 && max_level > level) { 218 1.1 reinoud irqmasks[max_level] &= ~(1 << irq); 219 1.1 reinoud --max_level; 220 1.1 reinoud } 221 1.1 reinoud while (level >= 0) { 222 1.1 reinoud irqmasks[level] |= (1 << irq); 223 1.1 reinoud --level; 224 1.1 reinoud } 225 1.1 reinoud } 226 1.1 reinoud 227 1.1 reinoud #include "sl.h" 228 1.1 reinoud #include "ppp.h" 229 1.1 reinoud #if NSL > 0 || NPPP > 0 230 1.1 reinoud /* In the presence of SLIP or PPP, splimp > spltty. */ 231 1.1 reinoud irqmasks[IPL_NET] &= irqmasks[IPL_TTY]; 232 1.1 reinoud #endif 233 1.1 reinoud } 234 1.1 reinoud 235 1.1 reinoud enable_irq(irq); 236 1.1 reinoud set_spl_masks(); 237 1.8 chris restore_interrupts(oldirqstate); 238 1.1 reinoud 239 1.11 bjh21 return 0; 240 1.1 reinoud } 241 1.1 reinoud 242 1.1 reinoud 243 1.1 reinoud /* 244 1.1 reinoud * int irq_release(int irq, irqhandler_t *handler) 245 1.1 reinoud * 246 1.1 reinoud * Disable an IRQ and remove a handler for it. 247 1.1 reinoud */ 248 1.1 reinoud 249 1.1 reinoud int 250 1.11 bjh21 irq_release(int irq, irqhandler_t *handler) 251 1.1 reinoud { 252 1.1 reinoud int level; 253 1.1 reinoud irqhandler_t *irqhand; 254 1.1 reinoud irqhandler_t **prehand; 255 1.1 reinoud 256 1.1 reinoud /* 257 1.1 reinoud * IRQ_INSTRUCT indicates that we should get the irq number 258 1.1 reinoud * from the irq structure 259 1.1 reinoud */ 260 1.1 reinoud if (irq == IRQ_INSTRUCT) 261 1.1 reinoud irq = handler->ih_num; 262 1.1 reinoud 263 1.1 reinoud /* Make sure the irq number is valid */ 264 1.1 reinoud if (irq < 0 || irq >= NIRQS) 265 1.1 reinoud return(-1); 266 1.1 reinoud 267 1.1 reinoud /* Locate the handler */ 268 1.1 reinoud irqhand = irqhandlers[irq]; 269 1.1 reinoud prehand = &irqhandlers[irq]; 270 1.1 reinoud 271 1.1 reinoud while (irqhand && handler != irqhand) { 272 1.1 reinoud prehand = &irqhand->ih_next; 273 1.1 reinoud irqhand = irqhand->ih_next; 274 1.1 reinoud } 275 1.1 reinoud 276 1.1 reinoud /* Remove the handler if located */ 277 1.1 reinoud if (irqhand) 278 1.1 reinoud *prehand = irqhand->ih_next; 279 1.1 reinoud else 280 1.11 bjh21 return -1; 281 1.1 reinoud 282 1.1 reinoud /* Now the handler has been removed from the chain mark is as inactive */ 283 1.1 reinoud irqhand->ih_flags &= ~IRQ_FLAG_ACTIVE; 284 1.1 reinoud 285 1.1 reinoud /* Make sure the head of the handler list is active */ 286 1.1 reinoud if (irqhandlers[irq]) 287 1.1 reinoud irqhandlers[irq]->ih_flags |= IRQ_FLAG_ACTIVE; 288 1.1 reinoud 289 1.1 reinoud #ifdef IRQSTATS 290 1.1 reinoud /* Get the interrupt name from the head of the list */ 291 1.19 christos char *iptr = _intrnames + (irq * 14); 292 1.1 reinoud if (irqhandlers[irq] && irqhandlers[irq]->ih_name) { 293 1.19 christos strlcpy(iptr, irqhandlers[irq]->ih_name, 14); 294 1.1 reinoud } else { 295 1.19 christos snprintf(iptr, 14, "irq %2d ", irq); 296 1.1 reinoud } 297 1.1 reinoud #endif /* IRQSTATS */ 298 1.1 reinoud 299 1.1 reinoud /* 300 1.1 reinoud * Update the irq masks. 301 1.1 reinoud * If ih_level is out of range then don't bother to update 302 1.1 reinoud * the masks. 303 1.1 reinoud */ 304 1.17 matt if (handler->ih_level >= 0 && handler->ih_level < NIPL) { 305 1.1 reinoud irqhandler_t *ptr; 306 1.1 reinoud 307 1.1 reinoud /* Clean the bit from all the masks */ 308 1.17 matt for (level = 0; level < NIPL; ++level) 309 1.1 reinoud irqmasks[level] &= ~(1 << irq); 310 1.1 reinoud 311 1.1 reinoud /* 312 1.1 reinoud * Find the lowest interrupt priority on the irq chain. 313 1.1 reinoud * Interrupt is allowable at priorities lower than this. 314 1.1 reinoud */ 315 1.1 reinoud ptr = irqhandlers[irq]; 316 1.1 reinoud if (ptr) { 317 1.1 reinoud level = ptr->ih_level - 1; 318 1.1 reinoud while (ptr) { 319 1.1 reinoud if (ptr->ih_level - 1 < level) 320 1.1 reinoud level = ptr->ih_level - 1; 321 1.1 reinoud ptr = ptr->ih_next; 322 1.1 reinoud } 323 1.1 reinoud while (level >= 0) { 324 1.1 reinoud irqmasks[level] |= (1 << irq); 325 1.1 reinoud --level; 326 1.1 reinoud } 327 1.1 reinoud } 328 1.1 reinoud } 329 1.1 reinoud 330 1.1 reinoud /* 331 1.1 reinoud * Disable the appropriate mask bit if there are no handlers left for 332 1.1 reinoud * this IRQ. 333 1.1 reinoud */ 334 1.1 reinoud if (irqhandlers[irq] == NULL) 335 1.1 reinoud disable_irq(irq); 336 1.1 reinoud 337 1.1 reinoud set_spl_masks(); 338 1.1 reinoud 339 1.11 bjh21 return 0; 340 1.1 reinoud } 341 1.1 reinoud 342 1.1 reinoud 343 1.1 reinoud void * 344 1.11 bjh21 intr_claim(int irq, int level, const char *name, int (*ih_func)(void *), 345 1.11 bjh21 void *ih_arg) 346 1.1 reinoud { 347 1.1 reinoud irqhandler_t *ih; 348 1.1 reinoud 349 1.24 thorpej ih = kmem_alloc(sizeof(*ih), KM_SLEEP); 350 1.1 reinoud ih->ih_level = level; 351 1.1 reinoud ih->ih_name = name; 352 1.1 reinoud ih->ih_func = ih_func; 353 1.1 reinoud ih->ih_arg = ih_arg; 354 1.1 reinoud ih->ih_flags = 0; 355 1.1 reinoud 356 1.21 christos if (irq_claim(irq, ih) != 0) { 357 1.24 thorpej kmem_free(ih, sizeof(*ih)); 358 1.11 bjh21 return NULL; 359 1.21 christos } 360 1.11 bjh21 return ih; 361 1.1 reinoud } 362 1.1 reinoud 363 1.1 reinoud 364 1.1 reinoud int 365 1.11 bjh21 intr_release(void *arg) 366 1.1 reinoud { 367 1.1 reinoud irqhandler_t *ih = (irqhandler_t *)arg; 368 1.1 reinoud 369 1.1 reinoud if (irq_release(ih->ih_num, ih) == 0) { 370 1.24 thorpej kmem_free(ih, sizeof(*ih)); 371 1.11 bjh21 return 0 ; 372 1.1 reinoud } 373 1.11 bjh21 return 1; 374 1.1 reinoud } 375 1.1 reinoud 376 1.1 reinoud #if 0 377 1.1 reinoud u_int 378 1.11 bjh21 disable_interrupts(u_int mask) 379 1.1 reinoud { 380 1.1 reinoud u_int cpsr; 381 1.1 reinoud 382 1.1 reinoud cpsr = SetCPSR(mask, mask); 383 1.11 bjh21 return cpsr; 384 1.1 reinoud } 385 1.1 reinoud 386 1.1 reinoud 387 1.1 reinoud u_int 388 1.11 bjh21 restore_interrupts(u_int old_cpsr) 389 1.1 reinoud { 390 1.1 reinoud int mask = I32_bit | F32_bit; 391 1.11 bjh21 392 1.11 bjh21 return SetCPSR(mask, old_cpsr & mask); 393 1.1 reinoud } 394 1.1 reinoud 395 1.1 reinoud 396 1.1 reinoud u_int 397 1.11 bjh21 enable_interrupts(u_int mask) 398 1.1 reinoud { 399 1.11 bjh21 400 1.11 bjh21 return SetCPSR(mask, 0); 401 1.1 reinoud } 402 1.1 reinoud #endif 403 1.1 reinoud 404 1.1 reinoud /* 405 1.1 reinoud * void disable_irq(int irq) 406 1.1 reinoud * 407 1.1 reinoud * Disables a specific irq. The irq is removed from the master irq mask 408 1.1 reinoud */ 409 1.1 reinoud 410 1.1 reinoud void 411 1.11 bjh21 disable_irq(int irq) 412 1.1 reinoud { 413 1.1 reinoud u_int oldirqstate; 414 1.1 reinoud 415 1.1 reinoud oldirqstate = disable_interrupts(I32_bit); 416 1.1 reinoud current_mask &= ~(1 << irq); 417 1.1 reinoud irq_setmasks(); 418 1.1 reinoud restore_interrupts(oldirqstate); 419 1.1 reinoud } 420 1.1 reinoud 421 1.1 reinoud 422 1.1 reinoud /* 423 1.1 reinoud * void enable_irq(int irq) 424 1.1 reinoud * 425 1.1 reinoud * Enables a specific irq. The irq is added to the master irq mask 426 1.1 reinoud * This routine should be used with caution. A handler should already 427 1.1 reinoud * be installed. 428 1.1 reinoud */ 429 1.1 reinoud 430 1.1 reinoud void 431 1.11 bjh21 enable_irq(int irq) 432 1.1 reinoud { 433 1.1 reinoud u_int oldirqstate; 434 1.1 reinoud 435 1.1 reinoud oldirqstate = disable_interrupts(I32_bit); 436 1.1 reinoud current_mask |= (1 << irq); 437 1.1 reinoud irq_setmasks(); 438 1.1 reinoud restore_interrupts(oldirqstate); 439 1.1 reinoud } 440 1.1 reinoud 441 1.1 reinoud 442 1.1 reinoud /* 443 1.1 reinoud * void stray_irqhandler(u_int mask) 444 1.1 reinoud * 445 1.1 reinoud * Handler for stray interrupts. This gets called if a handler cannot be 446 1.1 reinoud * found for an interrupt. 447 1.1 reinoud */ 448 1.1 reinoud 449 1.1 reinoud void 450 1.11 bjh21 stray_irqhandler(u_int mask) 451 1.1 reinoud { 452 1.1 reinoud static u_int stray_irqs = 0; 453 1.1 reinoud 454 1.1 reinoud if (++stray_irqs <= 8) 455 1.1 reinoud log(LOG_ERR, "Stray interrupt %08x%s\n", mask, 456 1.1 reinoud stray_irqs >= 8 ? ": stopped logging" : ""); 457 1.1 reinoud } 458