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