Home | History | Annotate | Line # | Download | only in isa
isa_irqhandler.c revision 1.1
      1 /*	$NetBSD: isa_irqhandler.c,v 1.1 2002/02/10 01:57:54 thorpej 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 "opt_irqstats.h"
     78 
     79 #include <sys/param.h>
     80 #include <sys/systm.h>
     81 #include <sys/syslog.h>
     82 #include <sys/malloc.h>
     83 
     84 #include <uvm/uvm_extern.h>
     85 
     86 #include <machine/intr.h>
     87 #include <machine/cpu.h>
     88 
     89 irqhandler_t *irqhandlers[NIRQS];
     90 
     91 int current_intr_depth;
     92 u_int current_mask;
     93 u_int actual_mask;
     94 u_int disabled_mask;
     95 u_int spl_mask;
     96 u_int irqmasks[IPL_LEVELS];
     97 u_int irqblock[NIRQS];
     98 
     99 extern u_int soft_interrupts;	/* Only so we can initialise it */
    100 
    101 extern char *_intrnames;
    102 
    103 /* Prototypes */
    104 
    105 extern void zero_page_readonly	__P((void));
    106 extern void zero_page_readwrite	__P((void));
    107 extern void set_spl_masks	__P((void));
    108 
    109 void irq_calculatemasks __P((void));
    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 		irqblock[loop] = 0;
    128 	}
    129 
    130 	/*
    131 	 * Setup the irqmasks for the different Interrupt Priority Levels
    132 	 * We will start with no bits set and these will be updated as handlers
    133 	 * are installed at different IPL's.
    134 	 */
    135 	for (loop = 0; loop < IPL_LEVELS; ++loop)
    136 		irqmasks[loop] = 0;
    137 
    138 	current_intr_depth = 0;
    139 	current_mask = 0x00000000;
    140 	disabled_mask = 0x00000000;
    141 	actual_mask = 0x00000000;
    142 	spl_mask = 0x00000000;
    143 	soft_interrupts = 0x00000000;
    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\n");
    168 	if (handler->ih_func == NULL)
    169 		panic("Interrupt handler does not have a function\n");
    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_IMP] &= irqmasks[IPL_TTY];
    345 
    346 	irqmasks[IPL_AUDIO] &= irqmasks[IPL_IMP];
    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 	 * We now need to update the irqblock array. This array indicates
    367 	 * what other interrupts should be blocked when interrupt is asserted
    368 	 * This basically emulates hardware interrupt priorities e.g. by
    369 	 * blocking all other IPL_BIO interrupts with an IPL_BIO interrupt
    370 	 * is asserted. For each interrupt we find the highest IPL and set
    371 	 * the block mask to the interrupt mask for that level.
    372 	 */
    373 
    374 	/* And eventually calculate the complete masks. */
    375 	for (irq = 0; irq < NIRQS; irq++) {
    376 		int irqs = 1 << irq;
    377 		for (ptr = irqhandlers[irq]; ptr; ptr = ptr->ih_next)
    378 			irqs |= ~(irqmasks[ptr->ih_level]);
    379 		irqblock[irq] = irqs;
    380 	}
    381 }
    382 
    383 
    384 void *
    385 intr_claim(irq, level, name, ih_func, ih_arg)
    386 	int irq;
    387 	int level;
    388 	const char *name;
    389 	int (*ih_func) __P((void *));
    390 	void *ih_arg;
    391 {
    392 	irqhandler_t *ih;
    393 
    394 	ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
    395 	if (!ih)
    396 		panic("intr_claim(): Cannot malloc handler memory\n");
    397 
    398 	ih->ih_level = level;
    399 	ih->ih_name = name;
    400 	ih->ih_func = ih_func;
    401 	ih->ih_arg = ih_arg;
    402 	ih->ih_flags = 0;
    403 
    404 	if (irq_claim(irq, ih) != 0)
    405 		return(NULL);
    406 	return(ih);
    407 }
    408 
    409 int
    410 intr_release(arg)
    411 	void *arg;
    412 {
    413 	irqhandler_t *ih = (irqhandler_t *)arg;
    414 
    415 	if (irq_release(ih->ih_num, ih) == 0) {
    416 		free(ih, M_DEVBUF);
    417 		return(0);
    418 	}
    419 	return(1);
    420 }
    421 
    422 
    423 /*
    424  * void disable_irq(int irq)
    425  *
    426  * Disables a specific irq. The irq is removed from the master irq mask
    427  */
    428 
    429 void
    430 disable_irq(irq)
    431 	int irq;
    432 {
    433 	u_int oldirqstate;
    434 
    435 	oldirqstate = disable_interrupts(I32_bit);
    436 	current_mask &= ~(1 << irq);
    437 	irq_setmasks();
    438 	restore_interrupts(oldirqstate);
    439 }
    440 
    441 
    442 /*
    443  * void enable_irq(int irq)
    444  *
    445  * Enables a specific irq. The irq is added to the master irq mask
    446  * This routine should be used with caution. A handler should already
    447  * be installed.
    448  */
    449 
    450 void
    451 enable_irq(irq)
    452 	int irq;
    453 {
    454 	u_int oldirqstate;
    455 
    456 	oldirqstate = disable_interrupts(I32_bit);
    457 	current_mask |= (1 << irq);
    458 	irq_setmasks();
    459 	restore_interrupts(oldirqstate);
    460 }
    461 
    462 
    463 /*
    464  * void stray_irqhandler(u_int mask)
    465  *
    466  * Handler for stray interrupts. This gets called if a handler cannot be
    467  * found for an interrupt.
    468  */
    469 
    470 void
    471 stray_irqhandler(mask)
    472 	u_int mask;
    473 {
    474 	static u_int stray_irqs = 0;
    475 
    476 	if (++stray_irqs <= 8)
    477 		log(LOG_ERR, "Stray interrupt %08x%s\n", mask,
    478 		    stray_irqs >= 8 ? ": stopped logging" : "");
    479 }
    480