Home | History | Annotate | Line # | Download | only in sa11x0
sa11x0_irqhandler.c revision 1.6.6.2
      1 /*	$NetBSD: sa11x0_irqhandler.c,v 1.6.6.2 2006/06/01 22:34:16 kardel Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996, 1997, 1998, 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to the NetBSD Foundation
      8  * by IWAMOTO Toshihiro.
      9  *
     10  * This code is derived from software contributed to The NetBSD Foundation
     11  * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
     12  * Simulation Facility, NASA Ames Research Center.
     13  *
     14  * Redistribution and use in source and binary forms, with or without
     15  * modification, are permitted provided that the following conditions
     16  * are met:
     17  * 1. Redistributions of source code must retain the above copyright
     18  *    notice, this list of conditions and the following disclaimer.
     19  * 2. Redistributions in binary form must reproduce the above copyright
     20  *    notice, this list of conditions and the following disclaimer in the
     21  *    documentation and/or other materials provided with the distribution.
     22  * 3. All advertising materials mentioning features or use of this software
     23  *    must display the following acknowledgement:
     24  *	This product includes software developed by the NetBSD
     25  *	Foundation, Inc. and its contributors.
     26  * 4. Neither the name of The NetBSD Foundation nor the names of its
     27  *    contributors may be used to endorse or promote products derived
     28  *    from this software without specific prior written permission.
     29  *
     30  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     31  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     32  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     33  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     34  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     35  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     36  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     37  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     38  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     39  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     40  * POSSIBILITY OF SUCH DAMAGE.
     41  */
     42 
     43 /*-
     44  * Copyright (c) 1991 The Regents of the University of California.
     45  * All rights reserved.
     46  *
     47  * This code is derived from software contributed to Berkeley by
     48  * William Jolitz.
     49  *
     50  * Redistribution and use in source and binary forms, with or without
     51  * modification, are permitted provided that the following conditions
     52  * are met:
     53  * 1. Redistributions of source code must retain the above copyright
     54  *    notice, this list of conditions and the following disclaimer.
     55  * 2. Redistributions in binary form must reproduce the above copyright
     56  *    notice, this list of conditions and the following disclaimer in the
     57  *    documentation and/or other materials provided with the distribution.
     58  * 3. Neither the name of the University nor the names of its contributors
     59  *    may be used to endorse or promote products derived from this software
     60  *    without specific prior written permission.
     61  *
     62  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     63  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     64  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     65  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     66  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     67  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     68  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     69  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     70  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     71  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     72  * SUCH DAMAGE.
     73  *
     74  *	@(#)isa.c	7.2 (Berkeley) 5/13/91
     75  */
     76 
     77 
     78 #include <sys/cdefs.h>
     79 __KERNEL_RCSID(0, "$NetBSD: sa11x0_irqhandler.c,v 1.6.6.2 2006/06/01 22:34:16 kardel Exp $");
     80 
     81 #include "opt_irqstats.h"
     82 
     83 #include <sys/param.h>
     84 #include <sys/kernel.h>
     85 #include <sys/systm.h>
     86 #include <sys/syslog.h>
     87 #include <sys/malloc.h>
     88 #include <uvm/uvm_extern.h>
     89 
     90 #include <arm/sa11x0/sa11x0_reg.h>
     91 #include <arm/sa11x0/sa11x0_var.h>
     92 
     93 #include <machine/intr.h>
     94 #include <machine/cpu.h>
     95 
     96 irqhandler_t *irqhandlers[NIRQS];
     97 
     98 int current_intr_depth;
     99 u_int actual_mask;
    100 #ifdef hpcarm
    101 #define IPL_LEVELS (NIPL+1)
    102 u_int imask[NIPL];
    103 #else
    104 u_int spl_mask;
    105 u_int irqmasks[IPL_LEVELS];
    106 #endif
    107 
    108 
    109 extern void set_spl_masks(void);
    110 static int fakeintr(void *);
    111 #ifdef INTR_DEBUG
    112 static int dumpirqhandlers(void);
    113 #endif
    114 void intr_calculatemasks(void);
    115 
    116 const struct evcnt *sa11x0_intr_evcnt(sa11x0_chipset_tag_t, int);
    117 void stray_irqhandler(void *);
    118 
    119 /*
    120  * Recalculate the interrupt masks from scratch.
    121  * We could code special registry and deregistry versions of this function that
    122  * would be faster, but the code would be nastier, and we don't expect this to
    123  * happen very much anyway.
    124  */
    125 void
    126 intr_calculatemasks(void)
    127 {
    128 	int irq, level;
    129 	struct irqhandler *q;
    130 	int intrlevel[ICU_LEN];
    131 
    132 	/* First, figure out which levels each IRQ uses. */
    133 	for (irq = 0; irq < ICU_LEN; irq++) {
    134 		int levels = 0;
    135 		for (q = irqhandlers[irq]; q; q = q->ih_next)
    136 			levels |= 1 << q->ih_level;
    137 		intrlevel[irq] = levels;
    138 	}
    139 
    140 	/* Then figure out which IRQs use each level. */
    141 #ifdef hpcarm
    142 	for (level = 0; level < NIPL; level++) {
    143 #else
    144 	for (level = 0; level <= IPL_LEVELS; level++) {
    145 #endif
    146 		int irqs = 0;
    147 		for (irq = 0; irq < ICU_LEN; irq++)
    148 			if (intrlevel[irq] & (1 << level))
    149 				irqs |= 1 << irq;
    150 #ifdef hpcarm
    151 		imask[level] = irqs;
    152 #else
    153 		irqmasks[level] = irqs;
    154 #endif
    155 	}
    156 
    157 	/*
    158 	 * Enforce a hierarchy that gives slow devices a better chance at not
    159 	 * dropping data.
    160 	 */
    161 #ifdef hpcarm
    162 	for (level = NIPL - 1; level > 0; level--)
    163 		imask[level - 1] |= imask[level];
    164 #else
    165 	for (level = IPL_LEVELS; level > 0; level--)
    166 		irqmasks[level - 1] |= irqmasks[level];
    167 #endif
    168 }
    169 
    170 
    171 const struct evcnt *
    172 sa11x0_intr_evcnt(sa11x0_chipset_tag_t ic, int irq)
    173 {
    174 
    175 	/* XXX for now, no evcnt parent reported */
    176 	return NULL;
    177 }
    178 
    179 void *
    180 sa11x0_intr_establish(sa11x0_chipset_tag_t ic, int irq, int type, int level,
    181     int (*ih_fun)(void *), void *ih_arg)
    182 {
    183 	int saved_cpsr;
    184 	struct irqhandler **p, *q, *ih;
    185 	static struct irqhandler fakehand = {fakeintr};
    186 
    187 	/* no point in sleeping unless someone can free memory. */
    188 	ih = malloc(sizeof *ih, M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
    189 	if (ih == NULL)
    190 		panic("sa11x0_intr_establish: can't malloc handler info");
    191 
    192 	if (irq < 0 || irq >= ICU_LEN || type == IST_NONE)
    193 		panic("intr_establish: bogus irq or type");
    194 
    195 	/* All interrupts are level intrs. */
    196 
    197 	/*
    198 	 * Figure out where to put the handler.
    199 	 * This is O(N^2), but we want to preserve the order, and N is
    200 	 * generally small.
    201 	 */
    202 	for (p = &irqhandlers[irq]; (q = *p) != NULL; p = &q->ih_next)
    203 		;
    204 
    205 	/*
    206 	 * Actually install a fake handler momentarily, since we might be doing
    207 	 * this with interrupts enabled and don't want the real routine called
    208 	 * until masking is set up.
    209 	 */
    210 	fakehand.ih_level = level;
    211 	*p = &fakehand;
    212 
    213 	intr_calculatemasks();
    214 
    215 	/*
    216 	 * Poke the real handler in now.
    217 	 */
    218 	ih->ih_func = ih_fun;
    219 	ih->ih_arg = ih_arg;
    220 #ifdef hpcarm
    221 	ih->ih_count = 0;
    222 #else
    223 	ih->ih_num = 0;
    224 #endif
    225 	ih->ih_next = NULL;
    226 	ih->ih_level = level;
    227 #ifdef hpcarm
    228 	ih->ih_irq = irq;
    229 #endif
    230 	ih->ih_name = NULL; /* XXX */
    231 	*p = ih;
    232 
    233 	saved_cpsr = SetCPSR(I32_bit, I32_bit);
    234 	set_spl_masks();
    235 
    236 	irq_setmasks();
    237 
    238 	SetCPSR(I32_bit, saved_cpsr & I32_bit);
    239 #ifdef INTR_DEBUG
    240 	dumpirqhandlers();
    241 #endif
    242 	return (ih);
    243 }
    244 
    245 #ifdef hpcarm
    246 /*
    247  * Deregister an interrupt handler.
    248  */
    249 void
    250 sa11x0_intr_disestablish(sa11x0_chipset_tag_t ic, void *arg)
    251 {
    252 	struct irqhandler *ih = arg;
    253 	int irq = ih->ih_irq;
    254 	int saved_cpsr;
    255 	struct irqhandler **p, *q;
    256 
    257 #if DIAGNOSTIC
    258 	if (irq < 0 || irq >= ICU_LEN)
    259 		panic("intr_disestablish: bogus irq");
    260 #endif
    261 
    262 	/*
    263 	 * Remove the handler from the chain.
    264 	 * This is O(n^2), too.
    265 	 */
    266 	for (p = &irqhandlers[irq]; (q = *p) != NULL && q != ih;
    267 	     p = &q->ih_next)
    268 		;
    269 	if (q)
    270 		*p = q->ih_next;
    271 	else
    272 		panic("intr_disestablish: handler not registered");
    273 	free(ih, M_DEVBUF);
    274 
    275 	intr_calculatemasks();
    276 	saved_cpsr = SetCPSR(I32_bit, I32_bit);
    277 	set_spl_masks();
    278 
    279 	irq_setmasks();
    280 	SetCPSR(I32_bit, saved_cpsr & I32_bit);
    281 
    282 }
    283 #endif
    284 
    285 void
    286 stray_irqhandler(void *p)
    287 {
    288 
    289 	printf("stray interrupt\n");
    290 }
    291 
    292 int
    293 fakeintr(void *p)
    294 {
    295 
    296 	return 0;
    297 }
    298 
    299 #ifdef INTR_DEBUG
    300 int
    301 dumpirqhandlers(void)
    302 {
    303 	int irq;
    304 	struct irqhandler *p;
    305 
    306 	for (irq = 0; irq < ICU_LEN; irq++) {
    307 		printf("irq %d:", irq);
    308 		p = irqhandlers[irq];
    309 		for (; p; p = p->ih_next)
    310 			printf("ih_func: 0x%lx, ", (unsigned long)p->ih_func);
    311 		printf("\n");
    312 	}
    313 	return 0;
    314 }
    315 #endif
    316 /* End of irqhandler.c */
    317