Home | History | Annotate | Line # | Download | only in iq80310
iq80310_intr.c revision 1.25
      1 /*	$NetBSD: iq80310_intr.c,v 1.25 2008/01/06 01:37:58 matt Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001, 2002 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed for the NetBSD Project by
     20  *	Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: iq80310_intr.c,v 1.25 2008/01/06 01:37:58 matt Exp $");
     40 
     41 #ifndef EVBARM_SPL_NOINLINE
     42 #define	EVBARM_SPL_NOINLINE
     43 #endif
     44 
     45 /*
     46  * Interrupt support for the Intel IQ80310.
     47  */
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/malloc.h>
     52 
     53 #include <uvm/uvm_extern.h>
     54 
     55 #include <machine/bus.h>
     56 #include <machine/intr.h>
     57 
     58 #include <arm/cpufunc.h>
     59 
     60 #include <arm/xscale/i80200reg.h>
     61 #include <arm/xscale/i80200var.h>
     62 
     63 #include <evbarm/iq80310/iq80310reg.h>
     64 #include <evbarm/iq80310/iq80310var.h>
     65 #include <evbarm/iq80310/obiovar.h>
     66 
     67 /* Interrupt handler queues. */
     68 struct intrq intrq[NIRQ];
     69 
     70 /* Interrupts to mask at each level. */
     71 int iq80310_imask[NIPL];
     72 
     73 /* Current interrupt priority level. */
     74 volatile int current_spl_level;
     75 
     76 /* Interrupts pending. */
     77 volatile int iq80310_ipending;
     78 
     79 /* Software copy of the IRQs we have enabled. */
     80 uint32_t intr_enabled;
     81 
     82 #ifdef __HAVE_FAST_SOFTINTRS
     83 /*
     84  * Map a software interrupt queue index (at the top of the word, and
     85  * highest priority softintr is encountered first in an ffs()).
     86  */
     87 #define	SI_TO_IRQBIT(si)	(1U << (31 - (si)))
     88 
     89 /*
     90  * Map a software interrupt queue to an interrupt priority level.
     91  */
     92 static const int si_to_ipl[SI_NQUEUES] = {
     93 	IPL_SOFT,		/* SI_SOFT */
     94 	IPL_SOFTCLOCK,		/* SI_SOFTCLOCK */
     95 	IPL_SOFTNET,		/* SI_SOFTNET */
     96 	IPL_SOFTSERIAL,		/* SI_SOFTSERIAL */
     97 };
     98 #endif
     99 
    100 void	iq80310_intr_dispatch(struct irqframe *frame);
    101 
    102 static inline uint32_t
    103 iq80310_intstat_read(void)
    104 {
    105 	uint32_t intstat;
    106 
    107 	intstat = CPLD_READ(IQ80310_XINT3_STATUS) & 0x1f;
    108 #if defined(IRQ_READ_XINT0)
    109 	if (IRQ_READ_XINT0)
    110 		intstat |= (CPLD_READ(IQ80310_XINT0_STATUS) & 0x7) << 5;
    111 #endif
    112 
    113 	/* XXX Why do we have to mask off? */
    114 	return (intstat & intr_enabled);
    115 }
    116 
    117 static inline void
    118 iq80310_set_intrmask(void)
    119 {
    120 	uint32_t disabled;
    121 
    122 	intr_enabled |= IRQ_BITS_ALWAYS_ON;
    123 
    124 	/* The XINT_MASK register sets a bit to *disable*. */
    125 	disabled = (~intr_enabled) & IRQ_BITS;
    126 
    127 	CPLD_WRITE(IQ80310_XINT_MASK, disabled & 0x1f);
    128 }
    129 
    130 static inline void
    131 iq80310_enable_irq(int irq)
    132 {
    133 
    134 	intr_enabled |= (1U << irq);
    135 	iq80310_set_intrmask();
    136 }
    137 
    138 static inline void
    139 iq80310_disable_irq(int irq)
    140 {
    141 
    142 	intr_enabled &= ~(1U << irq);
    143 	iq80310_set_intrmask();
    144 }
    145 
    146 /*
    147  * NOTE: This routine must be called with interrupts disabled in the CPSR.
    148  */
    149 static void
    150 iq80310_intr_calculate_masks(void)
    151 {
    152 	struct intrq *iq;
    153 	struct intrhand *ih;
    154 	int irq, ipl;
    155 
    156 	/* First, figure out which IPLs each IRQ has. */
    157 	for (irq = 0; irq < NIRQ; irq++) {
    158 		int levels = 0;
    159 		iq = &intrq[irq];
    160 		iq80310_disable_irq(irq);
    161 		for (ih = TAILQ_FIRST(&iq->iq_list); ih != NULL;
    162 		     ih = TAILQ_NEXT(ih, ih_list))
    163 			levels |= (1U << ih->ih_ipl);
    164 		iq->iq_levels = levels;
    165 	}
    166 
    167 	/* Next, figure out which IRQs are used by each IPL. */
    168 	for (ipl = 0; ipl < NIPL; ipl++) {
    169 		int irqs = 0;
    170 		for (irq = 0; irq < NIRQ; irq++) {
    171 			if (intrq[irq].iq_levels & (1U << ipl))
    172 				irqs |= (1U << irq);
    173 		}
    174 		iq80310_imask[ipl] = irqs;
    175 	}
    176 
    177 	iq80310_imask[IPL_NONE] = 0;
    178 	iq80310_imask[IPL_SOFTCLOCK] = 0;
    179 	iq80310_imask[IPL_SOFTNET] = 0;
    180 	iq80310_imask[IPL_SOFTSERIAL] = 0;
    181 
    182 	/*
    183 	 * splsoftnet() must also block splsoftclock(), since we don't
    184 	 * want timer-driven network events to occur while we're
    185 	 * processing incoming packets.
    186 	 */
    187 	iq80310_imask[IPL_SOFTNET] |= iq80310_imask[IPL_SOFTCLOCK];
    188 
    189 	/*
    190 	 * Enforce a hierarchy that gives "slow" device (or devices with
    191 	 * limited input buffer space/"real-time" requirements) a better
    192 	 * chance at not dropping data.
    193 	 */
    194 	iq80310_imask[IPL_BIO] |= iq80310_imask[IPL_SOFTNET];
    195 	iq80310_imask[IPL_NET] |= iq80310_imask[IPL_BIO];
    196 	iq80310_imask[IPL_SOFTSERIAL] |= iq80310_imask[IPL_NET];
    197 	iq80310_imask[IPL_TTY] |= iq80310_imask[IPL_SOFTSERIAL];
    198 
    199 	/*
    200 	 * splvm() blocks all interrupts that use the kernel memory
    201 	 * allocation facilities.
    202 	 */
    203 	iq80310_imask[IPL_VM] |= iq80310_imask[IPL_TTY];
    204 
    205 	/*
    206 	 * Audio devices are not allowed to perform memory allocation
    207 	 * in their interrupt routines, and they have fairly "real-time"
    208 	 * requirements, so give them a high interrupt priority.
    209 	 */
    210 	iq80310_imask[IPL_AUDIO] |= iq80310_imask[IPL_VM];
    211 
    212 	/*
    213 	 * splclock() must block anything that uses the scheduler.
    214 	 */
    215 	iq80310_imask[IPL_CLOCK] |= iq80310_imask[IPL_AUDIO];
    216 
    217 	/*
    218 	 * No separate statclock on the IQ80310.
    219 	 */
    220 #ifdef IPL_STATCLOCK
    221 	iq80310_imask[IPL_STATCLOCK] |= iq80310_imask[IPL_CLOCK];
    222 #endif
    223 
    224 	/*
    225 	 * splhigh() must block "everything".
    226 	 */
    227 #ifdef IPL_STATCLOCK
    228 	iq80310_imask[IPL_HIGH] |= iq80310_imask[IPL_STATCLOCK];
    229 #else
    230 	iq80310_imask[IPL_HIGH] |= iq80310_imask[IPL_CLOCK];
    231 #endif
    232 
    233 	/*
    234 	 * XXX We need serial drivers to run at the absolute highest priority
    235 	 * in order to avoid overruns, so serial > high.
    236 	 */
    237 	iq80310_imask[IPL_SERIAL] |= iq80310_imask[IPL_HIGH];
    238 
    239 	/*
    240 	 * Now compute which IRQs must be blocked when servicing any
    241 	 * given IRQ.
    242 	 */
    243 	for (irq = 0; irq < NIRQ; irq++) {
    244 		int irqs = (1U << irq);
    245 		iq = &intrq[irq];
    246 		if (TAILQ_FIRST(&iq->iq_list) != NULL)
    247 			iq80310_enable_irq(irq);
    248 		for (ih = TAILQ_FIRST(&iq->iq_list); ih != NULL;
    249 		     ih = TAILQ_NEXT(ih, ih_list))
    250 			irqs |= iq80310_imask[ih->ih_ipl];
    251 		iq->iq_mask = irqs;
    252 	}
    253 }
    254 
    255 #ifdef __HAVE_FAST_SOFTINTRS
    256 void
    257 iq80310_do_soft(void)
    258 {
    259 	static __cpu_simple_lock_t processing = __SIMPLELOCK_UNLOCKED;
    260 	int new, oldirqstate;
    261 
    262 	if (__cpu_simple_lock_try(&processing) == 0)
    263 		return;
    264 
    265 	new = current_spl_level;
    266 
    267 	oldirqstate = disable_interrupts(I32_bit);
    268 
    269 #define	DO_SOFTINT(si)							\
    270 	if ((iq80310_ipending & ~new) & SI_TO_IRQBIT(si)) {		\
    271 		iq80310_ipending &= ~SI_TO_IRQBIT(si);			\
    272 		current_spl_level |= iq80310_imask[si_to_ipl[(si)]];	\
    273 		restore_interrupts(oldirqstate);			\
    274 		softintr_dispatch(si);					\
    275 		oldirqstate = disable_interrupts(I32_bit);		\
    276 		current_spl_level = new;				\
    277 	}
    278 
    279 	DO_SOFTINT(SI_SOFTSERIAL);
    280 	DO_SOFTINT(SI_SOFTNET);
    281 	DO_SOFTINT(SI_SOFTCLOCK);
    282 	DO_SOFTINT(SI_SOFT);
    283 
    284 	__cpu_simple_unlock(&processing);
    285 
    286 	restore_interrupts(oldirqstate);
    287 }
    288 #endif	/* __HAVE_SOFT_FASTINTRS */
    289 
    290 int
    291 _splraise(int ipl)
    292 {
    293 
    294 	return (iq80310_splraise(ipl));
    295 }
    296 
    297 inline void
    298 splx(int new)
    299 {
    300 
    301 	return (iq80310_splx(new));
    302 }
    303 
    304 int
    305 _spllower(int ipl)
    306 {
    307 
    308 	return (iq80310_spllower(ipl));
    309 }
    310 
    311 #ifdef __HAVE_FAST_SOFTINTRS
    312 void
    313 _setsoftintr(int si)
    314 {
    315 	int oldirqstate;
    316 
    317 	oldirqstate = disable_interrupts(I32_bit);
    318 	iq80310_ipending |= SI_TO_IRQBIT(si);
    319 	restore_interrupts(oldirqstate);
    320 
    321 	/* Process unmasked pending soft interrupts. */
    322 	if ((iq80310_ipending & ~IRQ_BITS) & ~current_spl_level)
    323 		iq80310_do_soft();
    324 }
    325 #endif
    326 
    327 void
    328 iq80310_intr_init(void)
    329 {
    330 	struct intrq *iq;
    331 	int i;
    332 
    333 	/*
    334 	 * The Secondary PCI interrupts INTA, INTB, and INTC
    335 	 * area always enabled, since they cannot be masked
    336 	 * in the CPLD.
    337 	 */
    338 	intr_enabled |= IRQ_BITS_ALWAYS_ON;
    339 
    340 	for (i = 0; i < NIRQ; i++) {
    341 		iq = &intrq[i];
    342 		TAILQ_INIT(&iq->iq_list);
    343 
    344 		sprintf(iq->iq_name, "irq %d", i);
    345 		evcnt_attach_dynamic(&iq->iq_ev, EVCNT_TYPE_INTR,
    346 		    NULL, "iq80310", iq->iq_name);
    347 	}
    348 
    349 	iq80310_intr_calculate_masks();
    350 
    351 	/* Enable external interrupts on the i80200. */
    352 	i80200_extirq_dispatch = iq80310_intr_dispatch;
    353 	i80200_intr_enable(INTCTL_IM | INTCTL_PM);
    354 
    355 	/* Enable IRQs (don't yet use FIQs). */
    356 	enable_interrupts(I32_bit);
    357 }
    358 
    359 void *
    360 iq80310_intr_establish(int irq, int ipl, int (*func)(void *), void *arg)
    361 {
    362 	struct intrq *iq;
    363 	struct intrhand *ih;
    364 	u_int oldirqstate;
    365 
    366 	if (irq < 0 || irq > NIRQ)
    367 		panic("iq80310_intr_establish: IRQ %d out of range", irq);
    368 
    369 	ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
    370 	if (ih == NULL)
    371 		return (NULL);
    372 
    373 	ih->ih_func = func;
    374 	ih->ih_arg = arg;
    375 	ih->ih_ipl = ipl;
    376 	ih->ih_irq = irq;
    377 
    378 	iq = &intrq[irq];
    379 
    380 	/* All IQ80310 interrupts are level-triggered. */
    381 	iq->iq_ist = IST_LEVEL;
    382 
    383 	oldirqstate = disable_interrupts(I32_bit);
    384 
    385 	TAILQ_INSERT_TAIL(&iq->iq_list, ih, ih_list);
    386 
    387 	iq80310_intr_calculate_masks();
    388 
    389 	restore_interrupts(oldirqstate);
    390 
    391 	return (ih);
    392 }
    393 
    394 void
    395 iq80310_intr_disestablish(void *cookie)
    396 {
    397 	struct intrhand *ih = cookie;
    398 	struct intrq *iq = &intrq[ih->ih_irq];
    399 	int oldirqstate;
    400 
    401 	oldirqstate = disable_interrupts(I32_bit);
    402 
    403 	TAILQ_REMOVE(&iq->iq_list, ih, ih_list);
    404 
    405 	iq80310_intr_calculate_masks();
    406 
    407 	restore_interrupts(oldirqstate);
    408 }
    409 
    410 void
    411 iq80310_intr_dispatch(struct irqframe *frame)
    412 {
    413 	struct intrq *iq;
    414 	struct intrhand *ih;
    415 	int oldirqstate, pcpl, irq, ibit, hwpend, rv, stray;
    416 
    417 	stray = 1;
    418 
    419 	/* First, disable external IRQs. */
    420 	i80200_intr_disable(INTCTL_IM | INTCTL_PM);
    421 
    422 	pcpl = current_spl_level;
    423 
    424 	for (hwpend = iq80310_intstat_read(); hwpend != 0;) {
    425 		irq = ffs(hwpend) - 1;
    426 		ibit = (1U << irq);
    427 
    428 		stray = 0;
    429 
    430 		hwpend &= ~ibit;
    431 
    432 		if (pcpl & ibit) {
    433 			/*
    434 			 * IRQ is masked; mark it as pending and check
    435 			 * the next one.  Note: external IRQs are already
    436 			 * disabled.
    437 			 */
    438 			iq80310_ipending |= ibit;
    439 			continue;
    440 		}
    441 
    442 		iq80310_ipending &= ~ibit;
    443 		rv = 0;
    444 
    445 		iq = &intrq[irq];
    446 		iq->iq_ev.ev_count++;
    447 		uvmexp.intrs++;
    448 		current_spl_level |= iq->iq_mask;
    449 		oldirqstate = enable_interrupts(I32_bit);
    450 		for (ih = TAILQ_FIRST(&iq->iq_list); ih != NULL;
    451 		     ih = TAILQ_NEXT(ih, ih_list)) {
    452 			rv |= (*ih->ih_func)(ih->ih_arg ? ih->ih_arg : frame);
    453 		}
    454 		restore_interrupts(oldirqstate);
    455 
    456 		current_spl_level = pcpl;
    457 
    458 #if 0 /* XXX */
    459 		if (rv == 0)
    460 			printf("Stray interrupt: IRQ %d\n", irq);
    461 #endif
    462 	}
    463 
    464 #if 0 /* XXX */
    465 	if (stray)
    466 		printf("Stray external interrupt\n");
    467 #endif
    468 
    469 #if 0
    470 	/* Check for pendings soft intrs. */
    471 	if ((iq80310_ipending & ~IRQ_BITS) & ~current_spl_level) {
    472 		oldirqstate = enable_interrupts(I32_bit);
    473 		iq80310_do_soft();
    474 		restore_interrupts(oldirqstate);
    475 	}
    476 #endif
    477 
    478 	/*
    479 	 * If no hardware interrupts are masked, re-enable external
    480 	 * interrupts.
    481 	 */
    482 	if ((iq80310_ipending & IRQ_BITS) == 0)
    483 		i80200_intr_enable(INTCTL_IM | INTCTL_PM);
    484 }
    485