Home | History | Annotate | Line # | Download | only in ofw
ofw_irqhandler.c revision 1.4.12.2
      1  1.4.12.2     yamt /*	$NetBSD: ofw_irqhandler.c,v 1.4.12.2 2007/02/26 09:06:01 yamt Exp $	*/
      2       1.1  thorpej 
      3       1.1  thorpej /*
      4       1.1  thorpej  * Copyright (c) 1994-1998 Mark Brinicombe.
      5       1.1  thorpej  * Copyright (c) 1994 Brini.
      6       1.1  thorpej  * All rights reserved.
      7       1.1  thorpej  *
      8       1.1  thorpej  * This code is derived from software written for Brini by Mark Brinicombe
      9       1.1  thorpej  *
     10       1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     11       1.1  thorpej  * modification, are permitted provided that the following conditions
     12       1.1  thorpej  * are met:
     13       1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     14       1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     15       1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     17       1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     18       1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     19       1.1  thorpej  *    must display the following acknowledgement:
     20       1.1  thorpej  *	This product includes software developed by Mark Brinicombe
     21       1.1  thorpej  *	for the NetBSD Project.
     22       1.1  thorpej  * 4. The name of the company nor the name of the author may be used to
     23       1.1  thorpej  *    endorse or promote products derived from this software without specific
     24       1.1  thorpej  *    prior written permission.
     25       1.1  thorpej  *
     26       1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     27       1.1  thorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     28       1.1  thorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     29       1.1  thorpej  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     30       1.1  thorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     31       1.1  thorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     32       1.1  thorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     33       1.1  thorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     34       1.1  thorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     35       1.1  thorpej  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     36       1.1  thorpej  *
     37       1.1  thorpej  *	from: irqhandler.c
     38       1.1  thorpej  *
     39       1.1  thorpej  * IRQ/FIQ initialisation, claim, release and handler routines
     40       1.1  thorpej  *
     41       1.1  thorpej  * Created      : 30/09/94
     42       1.1  thorpej  */
     43       1.3    lukem 
     44       1.3    lukem #include <sys/cdefs.h>
     45  1.4.12.2     yamt __KERNEL_RCSID(0, "$NetBSD: ofw_irqhandler.c,v 1.4.12.2 2007/02/26 09:06:01 yamt Exp $");
     46       1.1  thorpej 
     47       1.1  thorpej #include "opt_irqstats.h"
     48       1.1  thorpej 
     49       1.1  thorpej #include <sys/param.h>
     50       1.1  thorpej #include <sys/systm.h>
     51       1.1  thorpej #include <sys/syslog.h>
     52       1.1  thorpej #include <sys/malloc.h>
     53       1.1  thorpej 
     54       1.1  thorpej #include <uvm/uvm_extern.h>
     55       1.1  thorpej 
     56       1.1  thorpej #include <machine/intr.h>
     57       1.1  thorpej #include <machine/cpu.h>
     58       1.1  thorpej 
     59       1.1  thorpej irqhandler_t *irqhandlers[NIRQS];
     60       1.1  thorpej 
     61       1.1  thorpej int current_intr_depth;
     62       1.1  thorpej u_int current_mask;
     63       1.1  thorpej u_int actual_mask;
     64       1.1  thorpej u_int disabled_mask;
     65       1.1  thorpej u_int spl_mask;
     66       1.1  thorpej u_int irqmasks[IPL_LEVELS];
     67       1.1  thorpej extern u_int intrcnt[];
     68       1.1  thorpej 
     69       1.1  thorpej extern char *_intrnames;
     70       1.1  thorpej 
     71       1.1  thorpej /* Prototypes */
     72       1.1  thorpej 
     73       1.1  thorpej int podule_irqhandler		__P((void));
     74       1.1  thorpej extern void set_spl_masks	__P((void));
     75       1.1  thorpej 
     76       1.1  thorpej /*
     77       1.1  thorpej  * void irq_init(void)
     78       1.1  thorpej  *
     79       1.1  thorpej  * Initialise the IRQ/FIQ sub system
     80       1.1  thorpej  */
     81       1.1  thorpej 
     82       1.1  thorpej void
     83       1.1  thorpej irq_init()
     84       1.1  thorpej {
     85       1.1  thorpej 	int loop;
     86       1.1  thorpej 
     87       1.1  thorpej 	/* Clear all the IRQ handlers and the irq block masks */
     88       1.1  thorpej 	for (loop = 0; loop < NIRQS; ++loop) {
     89       1.1  thorpej 		irqhandlers[loop] = NULL;
     90       1.1  thorpej 	}
     91       1.1  thorpej 
     92       1.1  thorpej 	/*
     93       1.1  thorpej 	 * Setup the irqmasks for the different Interrupt Priority Levels
     94       1.1  thorpej 	 * We will start with no bits set and these will be updated as handlers
     95       1.1  thorpej 	 * are installed at different IPL's.
     96       1.1  thorpej 	 */
     97       1.1  thorpej 	for (loop = 0; loop < IPL_LEVELS; ++loop)
     98       1.1  thorpej 		irqmasks[loop] = 0;
     99       1.1  thorpej 
    100       1.1  thorpej 	current_intr_depth = 0;
    101       1.1  thorpej 	current_mask = 0x00000000;
    102       1.1  thorpej 	disabled_mask = 0x00000000;
    103       1.1  thorpej 	actual_mask = 0x00000000;
    104       1.1  thorpej 	spl_mask = 0x00000000;
    105       1.1  thorpej 
    106       1.1  thorpej 	set_spl_masks();
    107       1.1  thorpej 
    108       1.1  thorpej 	/* Enable IRQ's and FIQ's */
    109       1.1  thorpej 	enable_interrupts(I32_bit | F32_bit);
    110       1.1  thorpej }
    111       1.1  thorpej 
    112       1.1  thorpej 
    113       1.1  thorpej /*
    114       1.1  thorpej  * int irq_claim(int irq, irqhandler_t *handler)
    115       1.1  thorpej  *
    116       1.1  thorpej  * Enable an IRQ and install a handler for it.
    117       1.1  thorpej  */
    118       1.1  thorpej 
    119       1.1  thorpej int
    120       1.1  thorpej irq_claim(irq, handler)
    121       1.1  thorpej 	int irq;
    122       1.1  thorpej 	irqhandler_t *handler;
    123       1.1  thorpej {
    124       1.1  thorpej 	int level;
    125       1.1  thorpej 	int loop;
    126       1.1  thorpej 
    127       1.1  thorpej #ifdef DIAGNOSTIC
    128       1.1  thorpej 	/* Sanity check */
    129       1.1  thorpej 	if (handler == NULL)
    130       1.2   provos 		panic("NULL interrupt handler");
    131       1.1  thorpej 	if (handler->ih_func == NULL)
    132       1.2   provos 		panic("Interrupt handler does not have a function");
    133       1.1  thorpej #endif	/* DIAGNOSTIC */
    134       1.1  thorpej 
    135       1.1  thorpej 	/*
    136       1.1  thorpej 	 * IRQ_INSTRUCT indicates that we should get the irq number
    137       1.1  thorpej 	 * from the irq structure
    138       1.1  thorpej 	 */
    139       1.1  thorpej 	if (irq == IRQ_INSTRUCT)
    140       1.1  thorpej 		irq = handler->ih_num;
    141       1.1  thorpej 
    142       1.1  thorpej 	/* Make sure the irq number is valid */
    143       1.1  thorpej 	if (irq < 0 || irq >= NIRQS)
    144       1.1  thorpej 		return(-1);
    145       1.1  thorpej 
    146       1.1  thorpej 	/* Make sure the level is valid */
    147       1.1  thorpej 	if (handler->ih_level < 0 || handler->ih_level >= IPL_LEVELS)
    148       1.1  thorpej     	        return(-1);
    149       1.1  thorpej 
    150       1.1  thorpej 	/* Attach handler at top of chain */
    151       1.1  thorpej 	handler->ih_next = irqhandlers[irq];
    152       1.1  thorpej 	irqhandlers[irq] = handler;
    153       1.1  thorpej 
    154       1.1  thorpej 	/*
    155       1.1  thorpej 	 * Reset the flags for this handler.
    156       1.1  thorpej 	 * As the handler is now in the chain mark it as active.
    157       1.1  thorpej 	 */
    158       1.1  thorpej 	handler->ih_flags = 0 | IRQ_FLAG_ACTIVE;
    159       1.1  thorpej 
    160       1.1  thorpej 	/*
    161       1.1  thorpej 	 * Record the interrupt number for accounting.
    162       1.1  thorpej 	 * Done here as the accounting number may not be the same as the
    163       1.1  thorpej 	 * IRQ number though for the moment they are
    164       1.1  thorpej 	 */
    165       1.1  thorpej 	handler->ih_num = irq;
    166       1.1  thorpej 
    167       1.1  thorpej #ifdef IRQSTATS
    168       1.1  thorpej 	/* Get the interrupt name from the head of the list */
    169       1.1  thorpej 	if (handler->ih_name) {
    170       1.1  thorpej 		char *ptr = _intrnames + (irq * 14);
    171       1.1  thorpej 		strcpy(ptr, "             ");
    172       1.1  thorpej 		strncpy(ptr, handler->ih_name,
    173       1.1  thorpej 		    min(strlen(handler->ih_name), 13));
    174       1.1  thorpej 	} else {
    175       1.1  thorpej 		char *ptr = _intrnames + (irq * 14);
    176       1.1  thorpej 		sprintf(ptr, "irq %2d     ", irq);
    177       1.1  thorpej 	}
    178       1.1  thorpej #endif	/* IRQSTATS */
    179       1.1  thorpej 
    180       1.1  thorpej 	/*
    181       1.1  thorpej 	 * Update the irq masks.
    182       1.1  thorpej 	 * Find the lowest interrupt priority on the irq chain.
    183       1.1  thorpej 	 * Interrupt is allowable at priorities lower than this.
    184       1.1  thorpej 	 * If ih_level is out of range then don't bother to update
    185       1.1  thorpej 	 * the masks.
    186       1.1  thorpej 	 */
    187       1.1  thorpej 	if (handler->ih_level >= 0 && handler->ih_level < IPL_LEVELS) {
    188       1.1  thorpej 		irqhandler_t *ptr;
    189       1.1  thorpej 
    190       1.1  thorpej 		/*
    191       1.1  thorpej 		 * Find the lowest interrupt priority on the irq chain.
    192       1.1  thorpej 		 * Interrupt is allowable at priorities lower than this.
    193       1.1  thorpej 		 */
    194       1.1  thorpej 		ptr = irqhandlers[irq];
    195       1.1  thorpej 		if (ptr) {
    196       1.1  thorpej 			level = ptr->ih_level - 1;
    197       1.1  thorpej 			while (ptr) {
    198       1.1  thorpej 				if (ptr->ih_level - 1 < level)
    199       1.1  thorpej 					level = ptr->ih_level - 1;
    200       1.1  thorpej 				ptr = ptr->ih_next;
    201       1.1  thorpej 			}
    202       1.1  thorpej 			while (level >= 0) {
    203       1.1  thorpej 				irqmasks[level] |= (1 << irq);
    204       1.1  thorpej 				--level;
    205       1.1  thorpej 			}
    206       1.1  thorpej 		}
    207       1.1  thorpej 
    208       1.1  thorpej #include "sl.h"
    209       1.1  thorpej #include "ppp.h"
    210       1.1  thorpej #if NSL > 0 || NPPP > 0
    211       1.1  thorpej 		/* In the presence of SLIP or PPP, splimp > spltty. */
    212       1.1  thorpej 		irqmasks[IPL_NET] &= irqmasks[IPL_TTY];
    213       1.1  thorpej #endif
    214       1.1  thorpej 	}
    215       1.1  thorpej 
    216       1.1  thorpej 	enable_irq(irq);
    217       1.1  thorpej 	set_spl_masks();
    218       1.1  thorpej 
    219       1.1  thorpej 	return(0);
    220       1.1  thorpej }
    221       1.1  thorpej 
    222       1.1  thorpej 
    223       1.1  thorpej /*
    224       1.1  thorpej  * int irq_release(int irq, irqhandler_t *handler)
    225       1.1  thorpej  *
    226       1.1  thorpej  * Disable an IRQ and remove a handler for it.
    227       1.1  thorpej  */
    228       1.1  thorpej 
    229       1.1  thorpej int
    230       1.1  thorpej irq_release(irq, handler)
    231       1.1  thorpej 	int irq;
    232       1.1  thorpej 	irqhandler_t *handler;
    233       1.1  thorpej {
    234       1.1  thorpej 	int level;
    235       1.1  thorpej 	int loop;
    236       1.1  thorpej 	irqhandler_t *irqhand;
    237       1.1  thorpej 	irqhandler_t **prehand;
    238       1.1  thorpej 	extern char *_intrnames;
    239       1.1  thorpej 
    240       1.1  thorpej 	/*
    241       1.1  thorpej 	 * IRQ_INSTRUCT indicates that we should get the irq number
    242       1.1  thorpej 	 * from the irq structure
    243       1.1  thorpej 	 */
    244       1.1  thorpej 	if (irq == IRQ_INSTRUCT)
    245       1.1  thorpej 		irq = handler->ih_num;
    246       1.1  thorpej 
    247       1.1  thorpej 	/* Make sure the irq number is valid */
    248       1.1  thorpej 	if (irq < 0 || irq >= NIRQS)
    249       1.1  thorpej 		return(-1);
    250       1.1  thorpej 
    251       1.1  thorpej 	/* Locate the handler */
    252       1.1  thorpej 	irqhand = irqhandlers[irq];
    253       1.1  thorpej 	prehand = &irqhandlers[irq];
    254       1.1  thorpej 
    255       1.1  thorpej 	while (irqhand && handler != irqhand) {
    256       1.1  thorpej 		prehand = &irqhand;
    257       1.1  thorpej 		irqhand = irqhand->ih_next;
    258       1.1  thorpej 	}
    259       1.1  thorpej 
    260       1.1  thorpej 	/* Remove the handler if located */
    261       1.1  thorpej 	if (irqhand)
    262       1.1  thorpej 		*prehand = irqhand->ih_next;
    263       1.1  thorpej 	else
    264       1.1  thorpej 		return(-1);
    265       1.1  thorpej 
    266       1.1  thorpej 	/* Now the handler has been removed from the chain mark is as inactive */
    267       1.1  thorpej 	irqhand->ih_flags &= ~IRQ_FLAG_ACTIVE;
    268       1.1  thorpej 
    269       1.1  thorpej 	/* Make sure the head of the handler list is active */
    270       1.1  thorpej 	if (irqhandlers[irq])
    271       1.1  thorpej 		irqhandlers[irq]->ih_flags |= IRQ_FLAG_ACTIVE;
    272       1.1  thorpej 
    273       1.1  thorpej #ifdef IRQSTATS
    274       1.1  thorpej 	/* Get the interrupt name from the head of the list */
    275       1.1  thorpej 	if (irqhandlers[irq] && irqhandlers[irq]->ih_name) {
    276       1.1  thorpej 		char *ptr = _intrnames + (irq * 14);
    277       1.1  thorpej 		strcpy(ptr, "             ");
    278       1.1  thorpej 		strncpy(ptr, irqhandlers[irq]->ih_name,
    279       1.1  thorpej 		    min(strlen(irqhandlers[irq]->ih_name), 13));
    280       1.1  thorpej 	} else {
    281       1.1  thorpej 		char *ptr = _intrnames + (irq * 14);
    282       1.1  thorpej 		sprintf(ptr, "irq %2d     ", irq);
    283       1.1  thorpej 	}
    284       1.1  thorpej #endif	/* IRQSTATS */
    285       1.1  thorpej 
    286       1.1  thorpej 	/*
    287       1.1  thorpej 	 * Update the irq masks.
    288       1.1  thorpej 	 * If ih_level is out of range then don't bother to update
    289       1.1  thorpej 	 * the masks.
    290       1.1  thorpej 	 */
    291       1.1  thorpej 	if (handler->ih_level >= 0 && handler->ih_level < IPL_LEVELS) {
    292       1.1  thorpej 		irqhandler_t *ptr;
    293       1.1  thorpej 
    294       1.1  thorpej 		/* Clean the bit from all the masks */
    295       1.1  thorpej 		for (level = 0; level < IPL_LEVELS; ++level)
    296       1.1  thorpej 			irqmasks[level] &= ~(1 << irq);
    297       1.1  thorpej 
    298       1.1  thorpej 		/*
    299       1.1  thorpej 		 * Find the lowest interrupt priority on the irq chain.
    300       1.1  thorpej 		 * Interrupt is allowable at priorities lower than this.
    301       1.1  thorpej 		 */
    302       1.1  thorpej 		ptr = irqhandlers[irq];
    303       1.1  thorpej 		if (ptr) {
    304       1.1  thorpej 			level = ptr->ih_level - 1;
    305       1.1  thorpej 			while (ptr) {
    306       1.1  thorpej 				if (ptr->ih_level - 1 < level)
    307       1.1  thorpej 					level = ptr->ih_level - 1;
    308       1.1  thorpej 				ptr = ptr->ih_next;
    309       1.1  thorpej 			}
    310       1.1  thorpej 			while (level >= 0) {
    311       1.1  thorpej 				irqmasks[level] |= (1 << irq);
    312       1.1  thorpej 				--level;
    313       1.1  thorpej 			}
    314       1.1  thorpej 		}
    315       1.1  thorpej 	}
    316       1.1  thorpej 
    317       1.1  thorpej 	/*
    318       1.1  thorpej 	 * Disable the appropriate mask bit if there are no handlers left for
    319       1.1  thorpej 	 * this IRQ.
    320       1.1  thorpej 	 */
    321       1.1  thorpej 	if (irqhandlers[irq] == NULL)
    322       1.1  thorpej 		disable_irq(irq);
    323       1.1  thorpej 
    324       1.1  thorpej 	set_spl_masks();
    325       1.1  thorpej 
    326       1.1  thorpej 	return(0);
    327       1.1  thorpej }
    328       1.1  thorpej 
    329       1.1  thorpej 
    330       1.1  thorpej void *
    331       1.1  thorpej intr_claim(irq, level, name, ih_func, ih_arg)
    332       1.1  thorpej 	int irq;
    333       1.1  thorpej 	int level;
    334       1.1  thorpej 	const char *name;
    335       1.1  thorpej 	int (*ih_func) __P((void *));
    336       1.1  thorpej 	void *ih_arg;
    337       1.1  thorpej {
    338       1.1  thorpej 	irqhandler_t *ih;
    339       1.1  thorpej 
    340       1.1  thorpej 	ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
    341       1.1  thorpej 	if (!ih)
    342       1.2   provos 		panic("intr_claim(): Cannot malloc handler memory");
    343       1.1  thorpej 
    344       1.1  thorpej 	ih->ih_level = level;
    345       1.1  thorpej 	ih->ih_name = name;
    346       1.1  thorpej 	ih->ih_func = ih_func;
    347       1.1  thorpej 	ih->ih_arg = ih_arg;
    348       1.1  thorpej 	ih->ih_flags = 0;
    349       1.1  thorpej 
    350       1.1  thorpej 	if (irq_claim(irq, ih) != 0)
    351       1.1  thorpej 		return(NULL);
    352       1.1  thorpej 	return(ih);
    353       1.1  thorpej }
    354       1.1  thorpej 
    355       1.1  thorpej 
    356       1.1  thorpej int
    357       1.1  thorpej intr_release(arg)
    358       1.1  thorpej 	void *arg;
    359       1.1  thorpej {
    360       1.1  thorpej 	irqhandler_t *ih = (irqhandler_t *)arg;
    361       1.1  thorpej 
    362       1.1  thorpej 	if (irq_release(ih->ih_num, ih) == 0) {
    363       1.1  thorpej 		free(ih, M_DEVBUF);
    364       1.1  thorpej 		return(0);
    365       1.1  thorpej 	}
    366       1.1  thorpej 	return(1);
    367       1.1  thorpej }
    368       1.1  thorpej 
    369       1.1  thorpej 
    370       1.1  thorpej /*
    371       1.1  thorpej  * void disable_irq(int irq)
    372       1.1  thorpej  *
    373       1.1  thorpej  * Disables a specific irq. The irq is removed from the master irq mask
    374       1.1  thorpej  */
    375       1.1  thorpej 
    376       1.1  thorpej void
    377       1.1  thorpej disable_irq(irq)
    378       1.1  thorpej 	int irq;
    379       1.1  thorpej {
    380       1.1  thorpej 	register int oldirqstate;
    381       1.1  thorpej 
    382       1.1  thorpej 	oldirqstate = disable_interrupts(I32_bit);
    383       1.1  thorpej 	current_mask &= ~(1 << irq);
    384       1.1  thorpej 	irq_setmasks();
    385       1.1  thorpej 	restore_interrupts(oldirqstate);
    386       1.1  thorpej }
    387       1.1  thorpej 
    388       1.1  thorpej 
    389       1.1  thorpej /*
    390       1.1  thorpej  * void enable_irq(int irq)
    391       1.1  thorpej  *
    392       1.1  thorpej  * Enables a specific irq. The irq is added to the master irq mask
    393       1.1  thorpej  * This routine should be used with caution. A handler should already
    394       1.1  thorpej  * be installed.
    395       1.1  thorpej  */
    396       1.1  thorpej 
    397       1.1  thorpej void
    398       1.1  thorpej enable_irq(irq)
    399       1.1  thorpej 	int irq;
    400       1.1  thorpej {
    401       1.1  thorpej 	register u_int oldirqstate;
    402       1.1  thorpej 
    403       1.1  thorpej 	oldirqstate = disable_interrupts(I32_bit);
    404       1.1  thorpej 	current_mask |= (1 << irq);
    405       1.1  thorpej 	irq_setmasks();
    406       1.1  thorpej 	restore_interrupts(oldirqstate);
    407       1.1  thorpej }
    408       1.1  thorpej 
    409       1.1  thorpej 
    410       1.1  thorpej /*
    411       1.1  thorpej  * void stray_irqhandler(u_int mask)
    412       1.1  thorpej  *
    413       1.1  thorpej  * Handler for stray interrupts. This gets called if a handler cannot be
    414       1.1  thorpej  * found for an interrupt.
    415       1.1  thorpej  */
    416       1.1  thorpej 
    417       1.4  thorpej void	stray_irqhandler(u_int);	/* called from assembly */
    418       1.4  thorpej 
    419       1.1  thorpej void
    420       1.1  thorpej stray_irqhandler(mask)
    421       1.1  thorpej 	u_int mask;
    422       1.1  thorpej {
    423       1.1  thorpej 	static u_int stray_irqs = 0;
    424       1.1  thorpej 
    425       1.1  thorpej 	if (++stray_irqs <= 8)
    426       1.1  thorpej 		log(LOG_ERR, "Stray interrupt %08x%s\n", mask,
    427       1.1  thorpej 		    stray_irqs >= 8 ? ": stopped logging" : "");
    428       1.1  thorpej }
    429