Home | History | Annotate | Line # | Download | only in xscale
i80321_icu.c revision 1.1
      1  1.1  thorpej /*	$NetBSD: i80321_icu.c,v 1.1 2002/03/27 21:45:47 thorpej Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*
      4  1.1  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 i80321 I/O Processor.
     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.1  thorpej #include <uvm/uvm_extern.h>
     47  1.1  thorpej 
     48  1.1  thorpej #include <machine/bus.h>
     49  1.1  thorpej #include <machine/intr.h>
     50  1.1  thorpej 
     51  1.1  thorpej #include <arm/cpufunc.h>
     52  1.1  thorpej 
     53  1.1  thorpej #include <arm/xscale/i80321reg.h>
     54  1.1  thorpej #include <arm/xscale/i80321var.h>
     55  1.1  thorpej 
     56  1.1  thorpej /* Interrupt handler queues. */
     57  1.1  thorpej struct intrq intrq[NIRQ];
     58  1.1  thorpej 
     59  1.1  thorpej /* Interrupts to mask at each level. */
     60  1.1  thorpej static int imask[NIPL];
     61  1.1  thorpej 
     62  1.1  thorpej /* Current interrupt priority level. */
     63  1.1  thorpej __volatile int current_spl_level;
     64  1.1  thorpej 
     65  1.1  thorpej /* Interrupts pending. */
     66  1.1  thorpej static __volatile int ipending;
     67  1.1  thorpej 
     68  1.1  thorpej /* Software copy of the IRQs we have enabled. */
     69  1.1  thorpej __volatile uint32_t intr_enabled;
     70  1.1  thorpej 
     71  1.1  thorpej /* Mask if interrupts steered to FIQs. */
     72  1.1  thorpej uint32_t intr_steer;
     73  1.1  thorpej 
     74  1.1  thorpej /*
     75  1.1  thorpej  * Map a software interrupt queue index (to the unused bits in the
     76  1.1  thorpej  * ICU registers -- XXX will need to revisit this if those bits are
     77  1.1  thorpej  * ever used in future steppings).
     78  1.1  thorpej  */
     79  1.1  thorpej static const uint32_t si_to_irqbit[SI_NQUEUES] = {
     80  1.1  thorpej 	ICU_INT_bit26,		/* SI_SOFT */
     81  1.1  thorpej 	ICU_INT_bit22,		/* SI_SOFTCLOCK */
     82  1.1  thorpej 	ICU_INT_bit5,		/* SI_SOFTNET */
     83  1.1  thorpej 	ICU_INT_bit4,		/* SI_SOFTSERIAL */
     84  1.1  thorpej };
     85  1.1  thorpej 
     86  1.1  thorpej #define	SI_TO_IRQBIT(si)	(1U << si_to_irqbit[(si)])
     87  1.1  thorpej 
     88  1.1  thorpej /*
     89  1.1  thorpej  * Map a software interrupt queue to an interrupt priority level.
     90  1.1  thorpej  */
     91  1.1  thorpej static const int si_to_ipl[SI_NQUEUES] = {
     92  1.1  thorpej 	IPL_SOFT,		/* SI_SOFT */
     93  1.1  thorpej 	IPL_SOFTCLOCK,		/* SI_SOFTCLOCK */
     94  1.1  thorpej 	IPL_SOFTNET,		/* SI_SOFTNET */
     95  1.1  thorpej 	IPL_SOFTSERIAL,		/* SI_SOFTSERIAL */
     96  1.1  thorpej };
     97  1.1  thorpej 
     98  1.1  thorpej void	i80321_intr_dispatch(struct clockframe *frame);
     99  1.1  thorpej 
    100  1.1  thorpej static __inline uint32_t
    101  1.1  thorpej i80321_iintsrc_read(void)
    102  1.1  thorpej {
    103  1.1  thorpej 	uint32_t iintsrc;
    104  1.1  thorpej 
    105  1.1  thorpej 	__asm __volatile("mrc p6, 0, %0, c8, c0, 0"
    106  1.1  thorpej 		: "=r" (iintsrc));
    107  1.1  thorpej 
    108  1.1  thorpej 	/*
    109  1.1  thorpej 	 * The IINTSRC register shows bits that are active even
    110  1.1  thorpej 	 * if they are masked in INTCTL, so we have to mask them
    111  1.1  thorpej 	 * off with the interrupts we consider enabled.
    112  1.1  thorpej 	 */
    113  1.1  thorpej 	return (iintsrc & intr_enabled);
    114  1.1  thorpej }
    115  1.1  thorpej 
    116  1.1  thorpej static __inline void
    117  1.1  thorpej i80321_set_intrmask(void)
    118  1.1  thorpej {
    119  1.1  thorpej 
    120  1.1  thorpej 	__asm __volatile("mcr p6, 0, %0, c0, c0, 0"
    121  1.1  thorpej 		:
    122  1.1  thorpej 		: "r" (intr_enabled & ICU_INT_HWMASK));
    123  1.1  thorpej }
    124  1.1  thorpej 
    125  1.1  thorpej static __inline void
    126  1.1  thorpej i80321_set_intrsteer(void)
    127  1.1  thorpej {
    128  1.1  thorpej 
    129  1.1  thorpej 	__asm __volatile("mcr p6, 0, %0, c4, c0, 0"
    130  1.1  thorpej 		:
    131  1.1  thorpej 		: "r" (intr_steer & ICU_INT_HWMASK));
    132  1.1  thorpej }
    133  1.1  thorpej 
    134  1.1  thorpej static __inline void
    135  1.1  thorpej i80321_enable_irq(int irq)
    136  1.1  thorpej {
    137  1.1  thorpej 
    138  1.1  thorpej 	intr_enabled |= (1U << irq);
    139  1.1  thorpej 	i80321_set_intrmask();
    140  1.1  thorpej }
    141  1.1  thorpej 
    142  1.1  thorpej static __inline void
    143  1.1  thorpej i80321_disable_irq(int irq)
    144  1.1  thorpej {
    145  1.1  thorpej 
    146  1.1  thorpej 	intr_enabled &= ~(1U << irq);
    147  1.1  thorpej 	i80321_set_intrmask();
    148  1.1  thorpej }
    149  1.1  thorpej 
    150  1.1  thorpej /*
    151  1.1  thorpej  * NOTE: This routine must be called with interrupts disabled in the CPSR.
    152  1.1  thorpej  */
    153  1.1  thorpej static void
    154  1.1  thorpej i80321_intr_calculate_masks(void)
    155  1.1  thorpej {
    156  1.1  thorpej 	struct intrq *iq;
    157  1.1  thorpej 	struct intrhand *ih;
    158  1.1  thorpej 	int irq, ipl;
    159  1.1  thorpej 
    160  1.1  thorpej 	/* First, figure out which IPLs each IRQ has. */
    161  1.1  thorpej 	for (irq = 0; irq < NIRQ; irq++) {
    162  1.1  thorpej 		int levels = 0;
    163  1.1  thorpej 		iq = &intrq[irq];
    164  1.1  thorpej 		i80321_disable_irq(irq);
    165  1.1  thorpej 		for (ih = TAILQ_FIRST(&iq->iq_list); ih != NULL;
    166  1.1  thorpej 		     ih = TAILQ_NEXT(ih, ih_list))
    167  1.1  thorpej 			levels |= (1U << ih->ih_ipl);
    168  1.1  thorpej 		iq->iq_levels = levels;
    169  1.1  thorpej 	}
    170  1.1  thorpej 
    171  1.1  thorpej 	/* Next, figure out which IRQs are used by each IPL. */
    172  1.1  thorpej 	for (ipl = 0; ipl < NIPL; ipl++) {
    173  1.1  thorpej 		int irqs = 0;
    174  1.1  thorpej 		for (irq = 0; irq < NIRQ; irq++) {
    175  1.1  thorpej 			if (intrq[irq].iq_levels & (1U << ipl))
    176  1.1  thorpej 				irqs |= (1U << irq);
    177  1.1  thorpej 		}
    178  1.1  thorpej 		imask[ipl] = irqs;
    179  1.1  thorpej 	}
    180  1.1  thorpej 
    181  1.1  thorpej 	imask[IPL_NONE] = 0;
    182  1.1  thorpej 
    183  1.1  thorpej 	/*
    184  1.1  thorpej 	 * Initialize the soft interrupt masks to block themselves.
    185  1.1  thorpej 	 */
    186  1.1  thorpej 	imask[IPL_SOFT] = SI_TO_IRQBIT(SI_SOFT);
    187  1.1  thorpej 	imask[IPL_SOFTCLOCK] = SI_TO_IRQBIT(SI_SOFTCLOCK);
    188  1.1  thorpej 	imask[IPL_SOFTNET] = SI_TO_IRQBIT(SI_SOFTNET);
    189  1.1  thorpej 	imask[IPL_SOFTSERIAL] = SI_TO_IRQBIT(SI_SOFTSERIAL);
    190  1.1  thorpej 
    191  1.1  thorpej 	/*
    192  1.1  thorpej 	 * splsoftclock() is the only interface that users of the
    193  1.1  thorpej 	 * generic software interrupt facility have to block their
    194  1.1  thorpej 	 * soft intrs, so splsoftclock() must also block IPL_SOFT.
    195  1.1  thorpej 	 */
    196  1.1  thorpej 	imask[IPL_SOFTCLOCK] |= imask[IPL_SOFT];
    197  1.1  thorpej 
    198  1.1  thorpej 	/*
    199  1.1  thorpej 	 * splsoftnet() must also block splsoftclock(), since we don't
    200  1.1  thorpej 	 * want timer-driven network events to occur while we're
    201  1.1  thorpej 	 * processing incoming packets.
    202  1.1  thorpej 	 */
    203  1.1  thorpej 	imask[IPL_SOFTNET] |= imask[IPL_SOFTCLOCK];
    204  1.1  thorpej 
    205  1.1  thorpej 	/*
    206  1.1  thorpej 	 * Enforce a heirarchy that gives "slow" device (or devices with
    207  1.1  thorpej 	 * limited input buffer space/"real-time" requirements) a better
    208  1.1  thorpej 	 * chance at not dropping data.
    209  1.1  thorpej 	 */
    210  1.1  thorpej 	imask[IPL_BIO] |= imask[IPL_SOFTNET];
    211  1.1  thorpej 	imask[IPL_NET] |= imask[IPL_BIO];
    212  1.1  thorpej 	imask[IPL_SOFTSERIAL] |= imask[IPL_NET];
    213  1.1  thorpej 	imask[IPL_TTY] |= imask[IPL_SOFTSERIAL];
    214  1.1  thorpej 
    215  1.1  thorpej 	/*
    216  1.1  thorpej 	 * splvm() blocks all interrupts that use the kernel memory
    217  1.1  thorpej 	 * allocation facilities.
    218  1.1  thorpej 	 */
    219  1.1  thorpej 	imask[IPL_IMP] |= imask[IPL_TTY];
    220  1.1  thorpej 
    221  1.1  thorpej 	/*
    222  1.1  thorpej 	 * Audio devices are not allowed to perform memory allocation
    223  1.1  thorpej 	 * in their interrupt routines, and they have fairly "real-time"
    224  1.1  thorpej 	 * requirements, so give them a high interrupt priority.
    225  1.1  thorpej 	 */
    226  1.1  thorpej 	imask[IPL_AUDIO] |= imask[IPL_IMP];
    227  1.1  thorpej 
    228  1.1  thorpej 	/*
    229  1.1  thorpej 	 * splclock() must block anything that uses the scheduler.
    230  1.1  thorpej 	 */
    231  1.1  thorpej 	imask[IPL_CLOCK] |= imask[IPL_AUDIO];
    232  1.1  thorpej 
    233  1.1  thorpej 	/*
    234  1.1  thorpej 	 * No separate statclock on the IQ80310.
    235  1.1  thorpej 	 */
    236  1.1  thorpej 	imask[IPL_STATCLOCK] |= imask[IPL_CLOCK];
    237  1.1  thorpej 
    238  1.1  thorpej 	/*
    239  1.1  thorpej 	 * splhigh() must block "everything".
    240  1.1  thorpej 	 */
    241  1.1  thorpej 	imask[IPL_HIGH] |= imask[IPL_STATCLOCK];
    242  1.1  thorpej 
    243  1.1  thorpej 	/*
    244  1.1  thorpej 	 * XXX We need serial drivers to run at the absolute highest priority
    245  1.1  thorpej 	 * in order to avoid overruns, so serial > high.
    246  1.1  thorpej 	 */
    247  1.1  thorpej 	imask[IPL_SERIAL] |= imask[IPL_HIGH];
    248  1.1  thorpej 
    249  1.1  thorpej 	/*
    250  1.1  thorpej 	 * Now compute which IRQs must be blocked when servicing any
    251  1.1  thorpej 	 * given IRQ.
    252  1.1  thorpej 	 */
    253  1.1  thorpej 	for (irq = 0; irq < NIRQ; irq++) {
    254  1.1  thorpej 		int irqs = (1U << irq);
    255  1.1  thorpej 		iq = &intrq[irq];
    256  1.1  thorpej 		if (TAILQ_FIRST(&iq->iq_list) != NULL)
    257  1.1  thorpej 			i80321_enable_irq(irq);
    258  1.1  thorpej 		for (ih = TAILQ_FIRST(&iq->iq_list); ih != NULL;
    259  1.1  thorpej 		     ih = TAILQ_NEXT(ih, ih_list))
    260  1.1  thorpej 			irqs |= imask[ih->ih_ipl];
    261  1.1  thorpej 		iq->iq_mask = irqs;
    262  1.1  thorpej 	}
    263  1.1  thorpej }
    264  1.1  thorpej 
    265  1.1  thorpej static void
    266  1.1  thorpej i80321_do_pending(void)
    267  1.1  thorpej {
    268  1.1  thorpej 	static __cpu_simple_lock_t processing = __SIMPLELOCK_UNLOCKED;
    269  1.1  thorpej 	int new, oldirqstate;
    270  1.1  thorpej 
    271  1.1  thorpej 	if (__cpu_simple_lock_try(&processing) == 0)
    272  1.1  thorpej 		return;
    273  1.1  thorpej 
    274  1.1  thorpej 	new = current_spl_level;
    275  1.1  thorpej 
    276  1.1  thorpej 	oldirqstate = disable_interrupts(I32_bit);
    277  1.1  thorpej 
    278  1.1  thorpej #define	DO_SOFTINT(si)							\
    279  1.1  thorpej 	if ((ipending & ~new) & SI_TO_IRQBIT(si)) {			\
    280  1.1  thorpej 		ipending &= ~SI_TO_IRQBIT(si);				\
    281  1.1  thorpej 		current_spl_level |= imask[si_to_ipl[(si)]];		\
    282  1.1  thorpej 		restore_interrupts(oldirqstate);			\
    283  1.1  thorpej 		softintr_dispatch(si);					\
    284  1.1  thorpej 		oldirqstate = disable_interrupts(I32_bit);		\
    285  1.1  thorpej 		current_spl_level = new;				\
    286  1.1  thorpej 	}
    287  1.1  thorpej 
    288  1.1  thorpej 	DO_SOFTINT(SI_SOFTSERIAL);
    289  1.1  thorpej 	DO_SOFTINT(SI_SOFTNET);
    290  1.1  thorpej 	DO_SOFTINT(SI_SOFTCLOCK);
    291  1.1  thorpej 	DO_SOFTINT(SI_SOFT);
    292  1.1  thorpej 
    293  1.1  thorpej 	__cpu_simple_unlock(&processing);
    294  1.1  thorpej 
    295  1.1  thorpej 	restore_interrupts(oldirqstate);
    296  1.1  thorpej }
    297  1.1  thorpej 
    298  1.1  thorpej int
    299  1.1  thorpej _splraise(int ipl)
    300  1.1  thorpej {
    301  1.1  thorpej 	int old, oldirqstate;
    302  1.1  thorpej 
    303  1.1  thorpej 	oldirqstate = disable_interrupts(I32_bit);
    304  1.1  thorpej 	old = current_spl_level;
    305  1.1  thorpej 	current_spl_level |= imask[ipl];
    306  1.1  thorpej 
    307  1.1  thorpej 	restore_interrupts(oldirqstate);
    308  1.1  thorpej 
    309  1.1  thorpej 	return (old);
    310  1.1  thorpej }
    311  1.1  thorpej 
    312  1.1  thorpej __inline void
    313  1.1  thorpej splx(int new)
    314  1.1  thorpej {
    315  1.1  thorpej 	int oldirqstate, hwpend;
    316  1.1  thorpej 
    317  1.1  thorpej 	current_spl_level = new;
    318  1.1  thorpej 
    319  1.1  thorpej 	/*
    320  1.1  thorpej 	 * If there are pending HW interrupts which are being
    321  1.1  thorpej 	 * unmasked, then enable them in the INTCTL register.
    322  1.1  thorpej 	 * This will cause them to come flooding in.
    323  1.1  thorpej 	 */
    324  1.1  thorpej 	hwpend = (ipending & ICU_INT_HWMASK) & ~new;
    325  1.1  thorpej 	if (hwpend != 0) {
    326  1.1  thorpej 		oldirqstate = disable_interrupts(I32_bit);
    327  1.1  thorpej 		intr_enabled |= hwpend;
    328  1.1  thorpej 		i80321_set_intrmask();
    329  1.1  thorpej 		restore_interrupts(oldirqstate);
    330  1.1  thorpej 	}
    331  1.1  thorpej 
    332  1.1  thorpej 	/* If there are software interrupts to process, do it. */
    333  1.1  thorpej 	if ((ipending & ~ICU_INT_HWMASK) & ~new)
    334  1.1  thorpej 		i80321_do_pending();
    335  1.1  thorpej }
    336  1.1  thorpej 
    337  1.1  thorpej int
    338  1.1  thorpej _spllower(int ipl)
    339  1.1  thorpej {
    340  1.1  thorpej 	int old = current_spl_level;
    341  1.1  thorpej 
    342  1.1  thorpej 	splx(imask[ipl]);
    343  1.1  thorpej 	return (old);
    344  1.1  thorpej }
    345  1.1  thorpej 
    346  1.1  thorpej void
    347  1.1  thorpej _setsoftintr(int si)
    348  1.1  thorpej {
    349  1.1  thorpej 	int oldirqstate;
    350  1.1  thorpej 
    351  1.1  thorpej 	oldirqstate = disable_interrupts(I32_bit);
    352  1.1  thorpej 	ipending |= SI_TO_IRQBIT(si);
    353  1.1  thorpej 	restore_interrupts(oldirqstate);
    354  1.1  thorpej 
    355  1.1  thorpej 	/* Process unmasked pending soft interrupts. */
    356  1.1  thorpej 	if ((ipending & ~ICU_INT_HWMASK) & ~current_spl_level)
    357  1.1  thorpej 		i80321_do_pending();
    358  1.1  thorpej }
    359  1.1  thorpej 
    360  1.1  thorpej /*
    361  1.1  thorpej  * i80321_icu_init:
    362  1.1  thorpej  *
    363  1.1  thorpej  *	Initialize the i80321 ICU.  Called early in bootstrap
    364  1.1  thorpej  *	to make sure the ICU is in a pristine state.
    365  1.1  thorpej  */
    366  1.1  thorpej void
    367  1.1  thorpej i80321_icu_init(void)
    368  1.1  thorpej {
    369  1.1  thorpej 
    370  1.1  thorpej 	intr_enabled = 0;	/* All interrupts disabled */
    371  1.1  thorpej 	i80321_set_intrmask();
    372  1.1  thorpej 
    373  1.1  thorpej 	intr_steer = 0;		/* All interrupts steered to IRQ */
    374  1.1  thorpej 	i80321_set_intrsteer();
    375  1.1  thorpej }
    376  1.1  thorpej 
    377  1.1  thorpej /*
    378  1.1  thorpej  * i80321_intr_init:
    379  1.1  thorpej  *
    380  1.1  thorpej  *	Initialize the rest of the interrupt subsystem, making it
    381  1.1  thorpej  *	ready to handle interrupts from devices.
    382  1.1  thorpej  */
    383  1.1  thorpej void
    384  1.1  thorpej i80321_intr_init(void)
    385  1.1  thorpej {
    386  1.1  thorpej 	struct intrq *iq;
    387  1.1  thorpej 	int i;
    388  1.1  thorpej 
    389  1.1  thorpej 	intr_enabled = 0;
    390  1.1  thorpej 
    391  1.1  thorpej 	for (i = 0; i < NIRQ; i++) {
    392  1.1  thorpej 		iq = &intrq[i];
    393  1.1  thorpej 		TAILQ_INIT(&iq->iq_list);
    394  1.1  thorpej 
    395  1.1  thorpej 		sprintf(iq->iq_name, "irq %d", i);
    396  1.1  thorpej 		evcnt_attach_dynamic(&iq->iq_ev, EVCNT_TYPE_INTR,
    397  1.1  thorpej 		    NULL, "iop321", iq->iq_name);
    398  1.1  thorpej 	}
    399  1.1  thorpej 
    400  1.1  thorpej 	i80321_intr_calculate_masks();
    401  1.1  thorpej 
    402  1.1  thorpej 	/* Enable IRQs (don't yet use FIQs). */
    403  1.1  thorpej 	enable_interrupts(I32_bit);
    404  1.1  thorpej }
    405  1.1  thorpej 
    406  1.1  thorpej void *
    407  1.1  thorpej i80321_intr_establish(int irq, int ipl, int (*func)(void *), void *arg)
    408  1.1  thorpej {
    409  1.1  thorpej 	struct intrq *iq;
    410  1.1  thorpej 	struct intrhand *ih;
    411  1.1  thorpej 	u_int oldirqstate;
    412  1.1  thorpej 
    413  1.1  thorpej 	if (irq < 0 || irq > NIRQ)
    414  1.1  thorpej 		panic("i80321_intr_establish: IRQ %d out of range", irq);
    415  1.1  thorpej 
    416  1.1  thorpej 	ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
    417  1.1  thorpej 	if (ih == NULL)
    418  1.1  thorpej 		return (NULL);
    419  1.1  thorpej 
    420  1.1  thorpej 	ih->ih_func = func;
    421  1.1  thorpej 	ih->ih_arg = arg;
    422  1.1  thorpej 	ih->ih_ipl = ipl;
    423  1.1  thorpej 	ih->ih_irq = irq;
    424  1.1  thorpej 
    425  1.1  thorpej 	iq = &intrq[irq];
    426  1.1  thorpej 
    427  1.1  thorpej 	/* All IOP321 interrupts are level-triggered. */
    428  1.1  thorpej 	iq->iq_ist = IST_LEVEL;
    429  1.1  thorpej 
    430  1.1  thorpej 	oldirqstate = disable_interrupts(I32_bit);
    431  1.1  thorpej 
    432  1.1  thorpej 	TAILQ_INSERT_TAIL(&iq->iq_list, ih, ih_list);
    433  1.1  thorpej 
    434  1.1  thorpej 	i80321_intr_calculate_masks();
    435  1.1  thorpej 
    436  1.1  thorpej 	restore_interrupts(oldirqstate);
    437  1.1  thorpej 
    438  1.1  thorpej 	return (ih);
    439  1.1  thorpej }
    440  1.1  thorpej 
    441  1.1  thorpej void
    442  1.1  thorpej i80321_intr_disestablish(void *cookie)
    443  1.1  thorpej {
    444  1.1  thorpej 	struct intrhand *ih = cookie;
    445  1.1  thorpej 	struct intrq *iq = &intrq[ih->ih_irq];
    446  1.1  thorpej 	int oldirqstate;
    447  1.1  thorpej 
    448  1.1  thorpej 	oldirqstate = disable_interrupts(I32_bit);
    449  1.1  thorpej 
    450  1.1  thorpej 	TAILQ_REMOVE(&iq->iq_list, ih, ih_list);
    451  1.1  thorpej 
    452  1.1  thorpej 	i80321_intr_calculate_masks();
    453  1.1  thorpej 
    454  1.1  thorpej 	restore_interrupts(oldirqstate);
    455  1.1  thorpej }
    456  1.1  thorpej 
    457  1.1  thorpej void
    458  1.1  thorpej i80321_intr_dispatch(struct clockframe *frame)
    459  1.1  thorpej {
    460  1.1  thorpej 	struct intrq *iq;
    461  1.1  thorpej 	struct intrhand *ih;
    462  1.1  thorpej 	int oldirqstate, pcpl, irq, ibit, hwpend;
    463  1.1  thorpej 
    464  1.1  thorpej 	pcpl = current_spl_level;
    465  1.1  thorpej 
    466  1.1  thorpej 	hwpend = i80321_iintsrc_read();
    467  1.1  thorpej 
    468  1.1  thorpej 	/*
    469  1.1  thorpej 	 * Disable all the interrupts that are pending.  We will
    470  1.1  thorpej 	 * reenable them once they are processed and not masked.
    471  1.1  thorpej 	 */
    472  1.1  thorpej 	intr_enabled &= ~hwpend;
    473  1.1  thorpej 	i80321_set_intrmask();
    474  1.1  thorpej 
    475  1.1  thorpej 	while (hwpend != 0) {
    476  1.1  thorpej 		irq = ffs(hwpend) - 1;
    477  1.1  thorpej 		ibit = (1U << irq);
    478  1.1  thorpej 
    479  1.1  thorpej 		hwpend &= ~ibit;
    480  1.1  thorpej 
    481  1.1  thorpej 		if (pcpl & ibit) {
    482  1.1  thorpej 			/*
    483  1.1  thorpej 			 * IRQ is masked; mark it as pending and check
    484  1.1  thorpej 			 * the next one.  Note: the IRQ is already disabled.
    485  1.1  thorpej 			 */
    486  1.1  thorpej 			ipending |= ibit;
    487  1.1  thorpej 			continue;
    488  1.1  thorpej 		}
    489  1.1  thorpej 
    490  1.1  thorpej 		ipending &= ~ibit;
    491  1.1  thorpej 
    492  1.1  thorpej 		iq = &intrq[irq];
    493  1.1  thorpej 		iq->iq_ev.ev_count++;
    494  1.1  thorpej 		uvmexp.intrs++;
    495  1.1  thorpej 		current_spl_level |= iq->iq_mask;
    496  1.1  thorpej 		oldirqstate = enable_interrupts(I32_bit);
    497  1.1  thorpej 		for (ih = TAILQ_FIRST(&iq->iq_list); ih != NULL;
    498  1.1  thorpej 		     ih = TAILQ_NEXT(ih, ih_list)) {
    499  1.1  thorpej 			(void) (*ih->ih_func)(ih->ih_arg ? ih->ih_arg : frame);
    500  1.1  thorpej 		}
    501  1.1  thorpej 		restore_interrupts(oldirqstate);
    502  1.1  thorpej 
    503  1.1  thorpej 		current_spl_level = pcpl;
    504  1.1  thorpej 
    505  1.1  thorpej 		/* Re-enable this interrupt now that's it's cleared. */
    506  1.1  thorpej 		intr_enabled |= ibit;
    507  1.1  thorpej 		i80321_set_intrmask();
    508  1.1  thorpej 	}
    509  1.1  thorpej 
    510  1.1  thorpej 	/* Check for pendings soft intrs. */
    511  1.1  thorpej 	if ((ipending & ~ICU_INT_HWMASK) & ~current_spl_level) {
    512  1.1  thorpej 		oldirqstate = enable_interrupts(I32_bit);
    513  1.1  thorpej 		i80321_do_pending();
    514  1.1  thorpej 		restore_interrupts(oldirqstate);
    515  1.1  thorpej 	}
    516  1.1  thorpej }
    517