1 /* $NetBSD: isa_irqhandler.c,v 1.8 2006/05/11 12:05:37 yamt 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.8 2006/05/11 12:05:37 yamt 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 144 set_spl_masks(); 145 146 /* Enable IRQ's and FIQ's */ 147 enable_interrupts(I32_bit | F32_bit); 148 } 149 150 151 /* 152 * int irq_claim(int irq, irqhandler_t *handler) 153 * 154 * Enable an IRQ and install a handler for it. 155 */ 156 157 int 158 irq_claim(irq, handler) 159 int irq; 160 irqhandler_t *handler; 161 { 162 163 #ifdef DIAGNOSTIC 164 /* Sanity check */ 165 if (handler == NULL) 166 panic("NULL interrupt handler"); 167 if (handler->ih_func == NULL) 168 panic("Interrupt handler does not have a function"); 169 #endif /* DIAGNOSTIC */ 170 171 /* 172 * IRQ_INSTRUCT indicates that we should get the irq number 173 * from the irq structure 174 */ 175 if (irq == IRQ_INSTRUCT) 176 irq = handler->ih_num; 177 178 /* Make sure the irq number is valid */ 179 if (irq < 0 || irq >= NIRQS) 180 return(-1); 181 182 /* Make sure the level is valid */ 183 if (handler->ih_level < 0 || handler->ih_level >= IPL_LEVELS) 184 return(-1); 185 186 /* Attach handler at top of chain */ 187 handler->ih_next = irqhandlers[irq]; 188 irqhandlers[irq] = handler; 189 190 /* 191 * Reset the flags for this handler. 192 * As the handler is now in the chain mark it as active. 193 */ 194 handler->ih_flags = 0 | IRQ_FLAG_ACTIVE; 195 196 /* 197 * Record the interrupt number for accounting. 198 * Done here as the accounting number may not be the same as the 199 * IRQ number though for the moment they are 200 */ 201 handler->ih_num = irq; 202 203 #ifdef IRQSTATS 204 /* Get the interrupt name from the head of the list */ 205 if (handler->ih_name) { 206 char *ptr = _intrnames + (irq * 14); 207 strcpy(ptr, " "); 208 strncpy(ptr, handler->ih_name, 209 min(strlen(handler->ih_name), 13)); 210 } else { 211 char *ptr = _intrnames + (irq * 14); 212 sprintf(ptr, "irq %2d ", irq); 213 } 214 #endif /* IRQSTATS */ 215 216 irq_calculatemasks(); 217 218 enable_irq(irq); 219 set_spl_masks(); 220 return(0); 221 } 222 223 224 /* 225 * int irq_release(int irq, irqhandler_t *handler) 226 * 227 * Disable an IRQ and remove a handler for it. 228 */ 229 230 int 231 irq_release(irq, handler) 232 int irq; 233 irqhandler_t *handler; 234 { 235 irqhandler_t *irqhand; 236 irqhandler_t **prehand; 237 #ifdef IRQSTATS 238 extern char *_intrnames; 239 #endif 240 241 /* 242 * IRQ_INSTRUCT indicates that we should get the irq number 243 * from the irq structure 244 */ 245 if (irq == IRQ_INSTRUCT) 246 irq = handler->ih_num; 247 248 /* Make sure the irq number is valid */ 249 if (irq < 0 || irq >= NIRQS) 250 return(-1); 251 252 /* Locate the handler */ 253 irqhand = irqhandlers[irq]; 254 prehand = &irqhandlers[irq]; 255 256 while (irqhand && handler != irqhand) { 257 prehand = &irqhand; 258 irqhand = irqhand->ih_next; 259 } 260 261 /* Remove the handler if located */ 262 if (irqhand) 263 *prehand = irqhand->ih_next; 264 else 265 return(-1); 266 267 /* Now the handler has been removed from the chain mark is as inactive */ 268 irqhand->ih_flags &= ~IRQ_FLAG_ACTIVE; 269 270 /* Make sure the head of the handler list is active */ 271 if (irqhandlers[irq]) 272 irqhandlers[irq]->ih_flags |= IRQ_FLAG_ACTIVE; 273 274 #ifdef IRQSTATS 275 /* Get the interrupt name from the head of the list */ 276 if (irqhandlers[irq] && irqhandlers[irq]->ih_name) { 277 char *ptr = _intrnames + (irq * 14); 278 strcpy(ptr, " "); 279 strncpy(ptr, irqhandlers[irq]->ih_name, 280 min(strlen(irqhandlers[irq]->ih_name), 13)); 281 } else { 282 char *ptr = _intrnames + (irq * 14); 283 sprintf(ptr, "irq %2d ", irq); 284 } 285 #endif /* IRQSTATS */ 286 287 irq_calculatemasks(); 288 289 /* 290 * Disable the appropriate mask bit if there are no handlers left for 291 * this IRQ. 292 */ 293 if (irqhandlers[irq] == NULL) 294 disable_irq(irq); 295 296 set_spl_masks(); 297 298 return(0); 299 } 300 301 /* adapted from .../i386/isa/isa_machdep.c */ 302 /* 303 * Recalculate the interrupt masks from scratch. 304 * We could code special registry and deregistry versions of this function that 305 * would be faster, but the code would be nastier, and we don't expect this to 306 * happen very much anyway. 307 */ 308 void 309 irq_calculatemasks() 310 { 311 int irq, level; 312 irqhandler_t *ptr; 313 int irqlevel[NIRQS]; 314 315 /* First, figure out which levels each IRQ uses. */ 316 for (irq = 0; irq < NIRQS; irq++) { 317 int levels = 0; 318 for (ptr = irqhandlers[irq]; ptr; ptr = ptr->ih_next) 319 levels |= 1 << ptr->ih_level; 320 irqlevel[irq] = levels; 321 } 322 323 /* Then figure out which IRQs use each level. */ 324 for (level = 0; level < IPL_LEVELS; level++) { 325 int irqs = 0; 326 for (irq = 0; irq < NIRQS; irq++) 327 if (irqlevel[irq] & (1 << level)) 328 irqs |= 1 << irq; 329 irqmasks[level] = ~irqs; 330 } 331 332 /* 333 * Enforce a hierarchy that gives slow devices a better chance at not 334 * dropping data. 335 */ 336 irqmasks[IPL_NET] &= irqmasks[IPL_BIO]; 337 irqmasks[IPL_TTY] &= irqmasks[IPL_NET]; 338 339 /* 340 * There are tty, network and disk drivers that use free() at interrupt 341 * time, so imp > (tty | net | bio). 342 */ 343 irqmasks[IPL_VM] &= irqmasks[IPL_TTY]; 344 345 irqmasks[IPL_AUDIO] &= irqmasks[IPL_VM]; 346 347 /* 348 * Since run queues may be manipulated by both the statclock and tty, 349 * network, and disk drivers, statclock > (tty | net | bio). 350 */ 351 irqmasks[IPL_CLOCK] &= irqmasks[IPL_AUDIO]; 352 353 /* 354 * IPL_HIGH must block everything that can manipulate a run queue. 355 */ 356 irqmasks[IPL_HIGH] &= irqmasks[IPL_CLOCK]; 357 358 /* 359 * We need serial drivers to run at the absolute highest priority to 360 * avoid overruns, so serial > high. 361 */ 362 irqmasks[IPL_SERIAL] &= irqmasks[IPL_HIGH]; 363 } 364 365 366 void * 367 intr_claim(irq, level, name, ih_func, ih_arg) 368 int irq; 369 int level; 370 const char *name; 371 int (*ih_func) __P((void *)); 372 void *ih_arg; 373 { 374 irqhandler_t *ih; 375 376 ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT); 377 if (!ih) 378 panic("intr_claim(): Cannot malloc handler memory"); 379 380 ih->ih_level = level; 381 ih->ih_name = name; 382 ih->ih_func = ih_func; 383 ih->ih_arg = ih_arg; 384 ih->ih_flags = 0; 385 386 if (irq_claim(irq, ih) != 0) 387 return(NULL); 388 return(ih); 389 } 390 391 int 392 intr_release(arg) 393 void *arg; 394 { 395 irqhandler_t *ih = (irqhandler_t *)arg; 396 397 if (irq_release(ih->ih_num, ih) == 0) { 398 free(ih, M_DEVBUF); 399 return(0); 400 } 401 return(1); 402 } 403 404 405 /* 406 * void disable_irq(int irq) 407 * 408 * Disables a specific irq. The irq is removed from the master irq mask 409 */ 410 411 void 412 disable_irq(irq) 413 int irq; 414 { 415 u_int oldirqstate; 416 417 oldirqstate = disable_interrupts(I32_bit); 418 current_mask &= ~(1 << irq); 419 irq_setmasks(); 420 restore_interrupts(oldirqstate); 421 } 422 423 424 /* 425 * void enable_irq(int irq) 426 * 427 * Enables a specific irq. The irq is added to the master irq mask 428 * This routine should be used with caution. A handler should already 429 * be installed. 430 */ 431 432 void 433 enable_irq(irq) 434 int irq; 435 { 436 u_int oldirqstate; 437 438 oldirqstate = disable_interrupts(I32_bit); 439 current_mask |= (1 << irq); 440 irq_setmasks(); 441 restore_interrupts(oldirqstate); 442 } 443 444 445 /* 446 * void stray_irqhandler(u_int mask) 447 * 448 * Handler for stray interrupts. This gets called if a handler cannot be 449 * found for an interrupt. 450 */ 451 452 void 453 stray_irqhandler(mask) 454 u_int mask; 455 { 456 static u_int stray_irqs = 0; 457 458 if (++stray_irqs <= 8) 459 log(LOG_ERR, "Stray interrupt %08x%s\n", mask, 460 stray_irqs >= 8 ? ": stopped logging" : ""); 461 } 462