1 /* $NetBSD: isa_irqhandler.c,v 1.9 2007/02/18 07:20:40 matt Exp $ */ 2 3 /* 4 * Copyright 1997 5 * Digital Equipment Corporation. All rights reserved. 6 * 7 * This software is furnished under license and may be used and 8 * copied only in accordance with the following terms and conditions. 9 * Subject to these conditions, you may download, copy, install, 10 * use, modify and distribute this software in source and/or binary 11 * form. No title or ownership is transferred hereby. 12 * 13 * 1) Any source code used, modified or distributed must reproduce 14 * and retain this copyright notice and list of conditions as 15 * they appear in the source file. 16 * 17 * 2) No right is granted to use any trade name, trademark, or logo of 18 * Digital Equipment Corporation. Neither the "Digital Equipment 19 * Corporation" name nor any trademark or logo of Digital Equipment 20 * Corporation may be used to endorse or promote products derived 21 * from this software without the prior written permission of 22 * Digital Equipment Corporation. 23 * 24 * 3) This software is provided "AS-IS" and any express or implied 25 * warranties, including but not limited to, any implied warranties 26 * of merchantability, fitness for a particular purpose, or 27 * non-infringement are disclaimed. In no event shall DIGITAL be 28 * liable for any damages whatsoever, and in particular, DIGITAL 29 * shall not be liable for special, indirect, consequential, or 30 * incidental damages or damages for lost profits, loss of 31 * revenue or loss of use, whether such damages arise in contract, 32 * negligence, tort, under statute, in equity, at law or otherwise, 33 * even if advised of the possibility of such damage. 34 */ 35 36 /* 37 * Copyright (c) 1994-1998 Mark Brinicombe. 38 * Copyright (c) 1994 Brini. 39 * All rights reserved. 40 * 41 * This code is derived from software written for Brini by Mark Brinicombe 42 * 43 * Redistribution and use in source and binary forms, with or without 44 * modification, are permitted provided that the following conditions 45 * are met: 46 * 1. Redistributions of source code must retain the above copyright 47 * notice, this list of conditions and the following disclaimer. 48 * 2. Redistributions in binary form must reproduce the above copyright 49 * notice, this list of conditions and the following disclaimer in the 50 * documentation and/or other materials provided with the distribution. 51 * 3. All advertising materials mentioning features or use of this software 52 * must display the following acknowledgement: 53 * This product includes software developed by Mark Brinicombe 54 * for the NetBSD Project. 55 * 4. The name of the company nor the name of the author may be used to 56 * endorse or promote products derived from this software without specific 57 * prior written permission. 58 * 59 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 60 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 61 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 62 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 63 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 64 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 65 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 66 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 67 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 68 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 69 * 70 * IRQ/FIQ initialisation, claim, release and handler routines 71 * 72 * from: irqhandler.c 73 * 74 * Created : 30/09/94 75 */ 76 77 #include <sys/cdefs.h> 78 __KERNEL_RCSID(0, "$NetBSD: isa_irqhandler.c,v 1.9 2007/02/18 07:20:40 matt Exp $"); 79 80 #include "opt_irqstats.h" 81 82 #include <sys/param.h> 83 #include <sys/systm.h> 84 #include <sys/syslog.h> 85 #include <sys/malloc.h> 86 87 #include <uvm/uvm_extern.h> 88 89 #include <machine/intr.h> 90 #include <machine/cpu.h> 91 92 irqhandler_t *irqhandlers[NIRQS]; 93 94 int current_intr_depth; 95 u_int current_mask; 96 u_int actual_mask; 97 u_int disabled_mask; 98 u_int spl_mask; 99 u_int irqmasks[IPL_LEVELS]; 100 101 extern u_int soft_interrupts; /* Only so we can initialise it */ 102 103 extern char *_intrnames; 104 105 /* Prototypes */ 106 107 extern void set_spl_masks(void); 108 void irq_calculatemasks(void); 109 void stray_irqhandler(u_int); 110 111 #define WriteWord(a, b) *((volatile unsigned int *)(a)) = (b) 112 113 /* 114 * void irq_init(void) 115 * 116 * Initialise the IRQ/FIQ sub system 117 */ 118 119 void 120 irq_init() 121 { 122 int loop; 123 124 /* Clear all the IRQ handlers and the irq block masks */ 125 for (loop = 0; loop < NIRQS; ++loop) { 126 irqhandlers[loop] = NULL; 127 } 128 129 /* 130 * Setup the irqmasks for the different Interrupt Priority Levels 131 * We will start with no bits set and these will be updated as handlers 132 * are installed at different IPL's. 133 */ 134 for (loop = 0; loop < IPL_LEVELS; ++loop) 135 irqmasks[loop] = 0; 136 137 current_intr_depth = 0; 138 current_mask = 0x00000000; 139 disabled_mask = 0x00000000; 140 actual_mask = 0x00000000; 141 spl_mask = 0x00000000; 142 soft_interrupts = 0x00000000; 143 softintr_init(); 144 145 set_spl_masks(); 146 147 /* Enable IRQ's and FIQ's */ 148 enable_interrupts(I32_bit | F32_bit); 149 } 150 151 152 /* 153 * int irq_claim(int irq, irqhandler_t *handler) 154 * 155 * Enable an IRQ and install a handler for it. 156 */ 157 158 int 159 irq_claim(irq, handler) 160 int irq; 161 irqhandler_t *handler; 162 { 163 164 #ifdef DIAGNOSTIC 165 /* Sanity check */ 166 if (handler == NULL) 167 panic("NULL interrupt handler"); 168 if (handler->ih_func == NULL) 169 panic("Interrupt handler does not have a function"); 170 #endif /* DIAGNOSTIC */ 171 172 /* 173 * IRQ_INSTRUCT indicates that we should get the irq number 174 * from the irq structure 175 */ 176 if (irq == IRQ_INSTRUCT) 177 irq = handler->ih_num; 178 179 /* Make sure the irq number is valid */ 180 if (irq < 0 || irq >= NIRQS) 181 return(-1); 182 183 /* Make sure the level is valid */ 184 if (handler->ih_level < 0 || handler->ih_level >= IPL_LEVELS) 185 return(-1); 186 187 /* Attach handler at top of chain */ 188 handler->ih_next = irqhandlers[irq]; 189 irqhandlers[irq] = handler; 190 191 /* 192 * Reset the flags for this handler. 193 * As the handler is now in the chain mark it as active. 194 */ 195 handler->ih_flags = 0 | IRQ_FLAG_ACTIVE; 196 197 /* 198 * Record the interrupt number for accounting. 199 * Done here as the accounting number may not be the same as the 200 * IRQ number though for the moment they are 201 */ 202 handler->ih_num = irq; 203 204 #ifdef IRQSTATS 205 /* Get the interrupt name from the head of the list */ 206 if (handler->ih_name) { 207 char *ptr = _intrnames + (irq * 14); 208 strcpy(ptr, " "); 209 strncpy(ptr, handler->ih_name, 210 min(strlen(handler->ih_name), 13)); 211 } else { 212 char *ptr = _intrnames + (irq * 14); 213 sprintf(ptr, "irq %2d ", irq); 214 } 215 #endif /* IRQSTATS */ 216 217 irq_calculatemasks(); 218 219 enable_irq(irq); 220 set_spl_masks(); 221 return(0); 222 } 223 224 225 /* 226 * int irq_release(int irq, irqhandler_t *handler) 227 * 228 * Disable an IRQ and remove a handler for it. 229 */ 230 231 int 232 irq_release(irq, handler) 233 int irq; 234 irqhandler_t *handler; 235 { 236 irqhandler_t *irqhand; 237 irqhandler_t **prehand; 238 #ifdef IRQSTATS 239 extern char *_intrnames; 240 #endif 241 242 /* 243 * IRQ_INSTRUCT indicates that we should get the irq number 244 * from the irq structure 245 */ 246 if (irq == IRQ_INSTRUCT) 247 irq = handler->ih_num; 248 249 /* Make sure the irq number is valid */ 250 if (irq < 0 || irq >= NIRQS) 251 return(-1); 252 253 /* Locate the handler */ 254 irqhand = irqhandlers[irq]; 255 prehand = &irqhandlers[irq]; 256 257 while (irqhand && handler != irqhand) { 258 prehand = &irqhand; 259 irqhand = irqhand->ih_next; 260 } 261 262 /* Remove the handler if located */ 263 if (irqhand) 264 *prehand = irqhand->ih_next; 265 else 266 return(-1); 267 268 /* Now the handler has been removed from the chain mark is as inactive */ 269 irqhand->ih_flags &= ~IRQ_FLAG_ACTIVE; 270 271 /* Make sure the head of the handler list is active */ 272 if (irqhandlers[irq]) 273 irqhandlers[irq]->ih_flags |= IRQ_FLAG_ACTIVE; 274 275 #ifdef IRQSTATS 276 /* Get the interrupt name from the head of the list */ 277 if (irqhandlers[irq] && irqhandlers[irq]->ih_name) { 278 char *ptr = _intrnames + (irq * 14); 279 strcpy(ptr, " "); 280 strncpy(ptr, irqhandlers[irq]->ih_name, 281 min(strlen(irqhandlers[irq]->ih_name), 13)); 282 } else { 283 char *ptr = _intrnames + (irq * 14); 284 sprintf(ptr, "irq %2d ", irq); 285 } 286 #endif /* IRQSTATS */ 287 288 irq_calculatemasks(); 289 290 /* 291 * Disable the appropriate mask bit if there are no handlers left for 292 * this IRQ. 293 */ 294 if (irqhandlers[irq] == NULL) 295 disable_irq(irq); 296 297 set_spl_masks(); 298 299 return(0); 300 } 301 302 /* adapted from .../i386/isa/isa_machdep.c */ 303 /* 304 * Recalculate the interrupt masks from scratch. 305 * We could code special registry and deregistry versions of this function that 306 * would be faster, but the code would be nastier, and we don't expect this to 307 * happen very much anyway. 308 */ 309 void 310 irq_calculatemasks() 311 { 312 int irq, level; 313 irqhandler_t *ptr; 314 int irqlevel[NIRQS]; 315 316 /* First, figure out which levels each IRQ uses. */ 317 for (irq = 0; irq < NIRQS; irq++) { 318 int levels = 0; 319 for (ptr = irqhandlers[irq]; ptr; ptr = ptr->ih_next) 320 levels |= 1 << ptr->ih_level; 321 irqlevel[irq] = levels; 322 } 323 324 /* Then figure out which IRQs use each level. */ 325 for (level = 0; level < IPL_LEVELS; level++) { 326 int irqs = 0; 327 for (irq = 0; irq < NIRQS; irq++) 328 if (irqlevel[irq] & (1 << level)) 329 irqs |= 1 << irq; 330 irqmasks[level] = ~irqs; 331 } 332 333 /* 334 * Enforce a hierarchy that gives slow devices a better chance at not 335 * dropping data. 336 */ 337 irqmasks[IPL_NET] &= irqmasks[IPL_BIO]; 338 irqmasks[IPL_TTY] &= irqmasks[IPL_NET]; 339 340 /* 341 * There are tty, network and disk drivers that use free() at interrupt 342 * time, so imp > (tty | net | bio). 343 */ 344 irqmasks[IPL_VM] &= irqmasks[IPL_TTY]; 345 346 irqmasks[IPL_AUDIO] &= irqmasks[IPL_VM]; 347 348 /* 349 * Since run queues may be manipulated by both the statclock and tty, 350 * network, and disk drivers, statclock > (tty | net | bio). 351 */ 352 irqmasks[IPL_CLOCK] &= irqmasks[IPL_AUDIO]; 353 354 /* 355 * IPL_HIGH must block everything that can manipulate a run queue. 356 */ 357 irqmasks[IPL_HIGH] &= irqmasks[IPL_CLOCK]; 358 359 /* 360 * We need serial drivers to run at the absolute highest priority to 361 * avoid overruns, so serial > high. 362 */ 363 irqmasks[IPL_SERIAL] &= irqmasks[IPL_HIGH]; 364 } 365 366 367 void * 368 intr_claim(irq, level, name, ih_func, ih_arg) 369 int irq; 370 int level; 371 const char *name; 372 int (*ih_func) __P((void *)); 373 void *ih_arg; 374 { 375 irqhandler_t *ih; 376 377 ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT); 378 if (!ih) 379 panic("intr_claim(): Cannot malloc handler memory"); 380 381 ih->ih_level = level; 382 ih->ih_name = name; 383 ih->ih_func = ih_func; 384 ih->ih_arg = ih_arg; 385 ih->ih_flags = 0; 386 387 if (irq_claim(irq, ih) != 0) 388 return(NULL); 389 return(ih); 390 } 391 392 int 393 intr_release(arg) 394 void *arg; 395 { 396 irqhandler_t *ih = (irqhandler_t *)arg; 397 398 if (irq_release(ih->ih_num, ih) == 0) { 399 free(ih, M_DEVBUF); 400 return(0); 401 } 402 return(1); 403 } 404 405 406 /* 407 * void disable_irq(int irq) 408 * 409 * Disables a specific irq. The irq is removed from the master irq mask 410 */ 411 412 void 413 disable_irq(irq) 414 int irq; 415 { 416 u_int oldirqstate; 417 418 oldirqstate = disable_interrupts(I32_bit); 419 current_mask &= ~(1 << irq); 420 irq_setmasks(); 421 restore_interrupts(oldirqstate); 422 } 423 424 425 /* 426 * void enable_irq(int irq) 427 * 428 * Enables a specific irq. The irq is added to the master irq mask 429 * This routine should be used with caution. A handler should already 430 * be installed. 431 */ 432 433 void 434 enable_irq(irq) 435 int irq; 436 { 437 u_int oldirqstate; 438 439 oldirqstate = disable_interrupts(I32_bit); 440 current_mask |= (1 << irq); 441 irq_setmasks(); 442 restore_interrupts(oldirqstate); 443 } 444 445 446 /* 447 * void stray_irqhandler(u_int mask) 448 * 449 * Handler for stray interrupts. This gets called if a handler cannot be 450 * found for an interrupt. 451 */ 452 453 void 454 stray_irqhandler(mask) 455 u_int mask; 456 { 457 static u_int stray_irqs = 0; 458 459 if (++stray_irqs <= 8) 460 log(LOG_ERR, "Stray interrupt %08x%s\n", mask, 461 stray_irqs >= 8 ? ": stopped logging" : ""); 462 } 463