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