1 /* $NetBSD: isa_irqhandler.c,v 1.25 2010/12/20 00:25:43 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.25 2010/12/20 00:25:43 matt Exp $"); 79 80 #include <sys/param.h> 81 #include <sys/systm.h> 82 #include <sys/syslog.h> 83 #include <sys/malloc.h> 84 85 #include <machine/intr.h> 86 #include <machine/irqhandler.h> 87 #include <machine/cpu.h> 88 89 irqhandler_t *irqhandlers[NIRQS]; 90 91 u_int current_mask; 92 u_int actual_mask; 93 u_int disabled_mask; 94 u_int irqmasks[NIPL]; 95 96 /* Prototypes */ 97 98 extern void set_spl_masks(void); 99 void irq_calculatemasks(void); 100 void stray_irqhandler(u_int); 101 102 #define WriteWord(a, b) *((volatile unsigned int *)(a)) = (b) 103 104 /* 105 * void irq_init(void) 106 * 107 * Initialise the IRQ/FIQ sub system 108 */ 109 110 void 111 irq_init(void) 112 { 113 int loop; 114 115 /* Clear all the IRQ handlers and the irq block masks */ 116 for (loop = 0; loop < NIRQS; ++loop) { 117 irqhandlers[loop] = NULL; 118 } 119 120 /* 121 * Setup the irqmasks for the different Interrupt Priority Levels 122 * We will start with no bits set and these will be updated as handlers 123 * are installed at different IPL's. 124 */ 125 for (loop = 0; loop < NIPL; ++loop) 126 irqmasks[loop] = 0; 127 128 current_mask = 0x00000000; 129 disabled_mask = 0x00000000; 130 actual_mask = 0x00000000; 131 132 set_spl_masks(); 133 134 /* Enable IRQ's and FIQ's */ 135 enable_interrupts(I32_bit | F32_bit); 136 } 137 138 139 /* 140 * int irq_claim(int irq, irqhandler_t *handler) 141 * 142 * Enable an IRQ and install a handler for it. 143 */ 144 145 int 146 irq_claim(int irq, irqhandler_t *handler, const char *group, const char *name) 147 { 148 149 #ifdef DIAGNOSTIC 150 /* Sanity check */ 151 if (handler == NULL) 152 panic("NULL interrupt handler"); 153 if (handler->ih_func == NULL) 154 panic("Interrupt handler does not have a function"); 155 #endif /* DIAGNOSTIC */ 156 157 /* 158 * IRQ_INSTRUCT indicates that we should get the irq number 159 * from the irq structure 160 */ 161 if (irq == IRQ_INSTRUCT) 162 irq = handler->ih_num; 163 164 /* Make sure the irq number is valid */ 165 if (irq < 0 || irq >= NIRQS) 166 return(-1); 167 168 /* Make sure the level is valid */ 169 if (handler->ih_level < 0 || handler->ih_level >= NIPL) 170 return(-1); 171 172 /* Attach evcnt */ 173 evcnt_attach_dynamic(&handler->ih_ev, EVCNT_TYPE_INTR, NULL, 174 group, name); 175 176 /* Attach handler at top of chain */ 177 handler->ih_next = irqhandlers[irq]; 178 irqhandlers[irq] = handler; 179 180 /* 181 * Reset the flags for this handler. 182 * As the handler is now in the chain mark it as active. 183 */ 184 handler->ih_flags = 0 | IRQ_FLAG_ACTIVE; 185 186 /* 187 * Record the interrupt number for accounting. 188 * Done here as the accounting number may not be the same as the 189 * IRQ number though for the moment they are 190 */ 191 handler->ih_num = irq; 192 193 irq_calculatemasks(); 194 195 enable_irq(irq); 196 set_spl_masks(); 197 return(0); 198 } 199 200 201 /* 202 * int irq_release(int irq, irqhandler_t *handler) 203 * 204 * Disable an IRQ and remove a handler for it. 205 */ 206 207 int 208 irq_release(int irq, irqhandler_t *handler) 209 { 210 irqhandler_t *irqhand; 211 irqhandler_t **prehand; 212 213 /* 214 * IRQ_INSTRUCT indicates that we should get the irq number 215 * from the irq structure 216 */ 217 if (irq == IRQ_INSTRUCT) 218 irq = handler->ih_num; 219 220 /* Make sure the irq number is valid */ 221 if (irq < 0 || irq >= NIRQS) 222 return(-1); 223 224 /* Locate the handler */ 225 prehand = &irqhandlers[irq]; 226 irqhand = *prehand; 227 228 while (irqhand && handler != irqhand) { 229 prehand = &irqhand->ih_next; 230 irqhand = *prehand; 231 } 232 233 /* Remove the handler if located */ 234 if (irqhand) 235 *prehand = irqhand->ih_next; 236 else 237 return(-1); 238 239 /* The handler has been removed from the chain so mark it as inactive */ 240 irqhand->ih_flags &= ~IRQ_FLAG_ACTIVE; 241 242 /* Make sure the head of the handler list is active */ 243 if (irqhandlers[irq]) 244 irqhandlers[irq]->ih_flags |= IRQ_FLAG_ACTIVE; 245 246 evcnt_detach(&irqhand->ih_ev); 247 248 irq_calculatemasks(); 249 250 /* 251 * Disable the appropriate mask bit if there are no handlers left for 252 * this IRQ. 253 */ 254 if (irqhandlers[irq] == NULL) 255 disable_irq(irq); 256 257 set_spl_masks(); 258 259 return(0); 260 } 261 262 /* adapted from .../i386/isa/isa_machdep.c */ 263 /* 264 * Recalculate the interrupt masks from scratch. 265 * We could code special registry and deregistry versions of this function that 266 * would be faster, but the code would be nastier, and we don't expect this to 267 * happen very much anyway. 268 */ 269 void 270 irq_calculatemasks(void) 271 { 272 int irq, level; 273 irqhandler_t *ptr; 274 int irqlevel[NIRQS]; 275 276 /* First, figure out which levels each IRQ uses. */ 277 for (irq = 0; irq < NIRQS; irq++) { 278 int levels = 0; 279 for (ptr = irqhandlers[irq]; ptr; ptr = ptr->ih_next) 280 levels |= 1 << ptr->ih_level; 281 irqlevel[irq] = levels; 282 } 283 284 /* Then figure out which IRQs use each level. */ 285 for (level = 0; level < NIPL; level++) { 286 int irqs = 0; 287 for (irq = 0; irq < NIRQS; irq++) 288 if (irqlevel[irq] & (1 << level)) 289 irqs |= 1 << irq; 290 irqmasks[level] = ~irqs; 291 } 292 293 /* 294 * Enforce a hierarchy that gives slow devices a better chance at not 295 * dropping data. 296 */ 297 KASSERT(irqmasks[IPL_NONE] == ~0); 298 irqmasks[IPL_SOFTCLOCK] &= irqmasks[IPL_NONE]; 299 irqmasks[IPL_SOFTBIO] &= irqmasks[IPL_SOFTCLOCK]; 300 irqmasks[IPL_SOFTNET] &= irqmasks[IPL_SOFTBIO]; 301 irqmasks[IPL_SOFTSERIAL] &= irqmasks[IPL_SOFTNET]; 302 irqmasks[IPL_VM] &= irqmasks[IPL_SOFTSERIAL]; 303 irqmasks[IPL_CLOCK] &= irqmasks[IPL_VM]; 304 irqmasks[IPL_HIGH] &= irqmasks[IPL_CLOCK]; 305 } 306 307 308 void * 309 intr_claim(int irq, int level, int (*ih_func)(void *), void *ih_arg, const char *group, const char *name) 310 { 311 irqhandler_t *ih; 312 313 ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT | M_ZERO); 314 if (!ih) 315 panic("intr_claim(): Cannot malloc handler memory"); 316 317 ih->ih_level = level; 318 ih->ih_func = ih_func; 319 ih->ih_arg = ih_arg; 320 ih->ih_flags = 0; 321 322 if (irq_claim(irq, ih, group, name) != 0) 323 return(NULL); 324 325 return(ih); 326 } 327 328 int 329 intr_release(void *arg) 330 { 331 irqhandler_t *ih = (irqhandler_t *)arg; 332 333 if (irq_release(ih->ih_num, ih) == 0) { 334 free(ih, M_DEVBUF); 335 return(0); 336 } 337 return(1); 338 } 339 340 341 /* 342 * void disable_irq(int irq) 343 * 344 * Disables a specific irq. The irq is removed from the master irq mask 345 */ 346 347 void 348 disable_irq(int irq) 349 { 350 u_int oldirqstate; 351 352 oldirqstate = disable_interrupts(I32_bit); 353 current_mask &= ~(1 << irq); 354 irq_setmasks(); 355 restore_interrupts(oldirqstate); 356 } 357 358 359 /* 360 * void enable_irq(int irq) 361 * 362 * Enables a specific irq. The irq is added to the master irq mask 363 * This routine should be used with caution. A handler should already 364 * be installed. 365 */ 366 367 void 368 enable_irq(int irq) 369 { 370 u_int oldirqstate; 371 372 oldirqstate = disable_interrupts(I32_bit); 373 current_mask |= (1 << irq); 374 irq_setmasks(); 375 restore_interrupts(oldirqstate); 376 } 377 378 379 /* 380 * void stray_irqhandler(u_int mask) 381 * 382 * Handler for stray interrupts. This gets called if a handler cannot be 383 * found for an interrupt. 384 */ 385 386 void 387 stray_irqhandler(u_int mask) 388 { 389 static u_int stray_irqs = 0; 390 391 if (++stray_irqs <= 8) 392 log(LOG_ERR, "Stray interrupt %08x%s\n", mask, 393 stray_irqs >= 8 ? ": stopped logging" : ""); 394 } 395