Home | History | Annotate | Line # | Download | only in xscale
ixp425_intr.c revision 1.9
      1  1.9      scw /*	$NetBSD: ixp425_intr.c,v 1.9 2004/02/13 15:49:02 scw Exp $ */
      2  1.1   ichiro 
      3  1.1   ichiro /*
      4  1.1   ichiro  * Copyright (c) 2003
      5  1.1   ichiro  *	Ichiro FUKUHARA <ichiro (at) ichiro.org>.
      6  1.1   ichiro  * All rights reserved.
      7  1.1   ichiro  *
      8  1.1   ichiro  * Redistribution and use in source and binary forms, with or without
      9  1.1   ichiro  * modification, are permitted provided that the following conditions
     10  1.1   ichiro  * are met:
     11  1.1   ichiro  * 1. Redistributions of source code must retain the above copyright
     12  1.1   ichiro  *    notice, this list of conditions and the following disclaimer.
     13  1.1   ichiro  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1   ichiro  *    notice, this list of conditions and the following disclaimer in the
     15  1.1   ichiro  *    documentation and/or other materials provided with the distribution.
     16  1.1   ichiro  * 3. All advertising materials mentioning features or use of this software
     17  1.1   ichiro  *    must display the following acknowledgement:
     18  1.1   ichiro  *	This product includes software developed by Ichiro FUKUHARA.
     19  1.1   ichiro  * 4. The name of the company nor the name of the author may be used to
     20  1.1   ichiro  *    endorse or promote products derived from this software without specific
     21  1.1   ichiro  *    prior written permission.
     22  1.1   ichiro  *
     23  1.1   ichiro  * THIS SOFTWARE IS PROVIDED BY ICHIRO FUKUHARA ``AS IS'' AND ANY EXPRESS OR
     24  1.1   ichiro  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  1.1   ichiro  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  1.1   ichiro  * IN NO EVENT SHALL ICHIRO FUKUHARA OR THE VOICES IN HIS HEAD BE LIABLE FOR
     27  1.1   ichiro  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1   ichiro  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1   ichiro  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1   ichiro  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1   ichiro  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1   ichiro  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1   ichiro  * SUCH DAMAGE.
     34  1.1   ichiro  */
     35  1.1   ichiro /*
     36  1.1   ichiro  * Copyright (c) 2001, 2002 Wasabi Systems, Inc.
     37  1.1   ichiro  * All rights reserved.
     38  1.1   ichiro  *
     39  1.1   ichiro  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
     40  1.1   ichiro  *
     41  1.1   ichiro  * Redistribution and use in source and binary forms, with or without
     42  1.1   ichiro  * modification, are permitted provided that the following conditions
     43  1.1   ichiro  * are met:
     44  1.1   ichiro  * 1. Redistributions of source code must retain the above copyright
     45  1.1   ichiro  *    notice, this list of conditions and the following disclaimer.
     46  1.1   ichiro  * 2. Redistributions in binary form must reproduce the above copyright
     47  1.1   ichiro  *    notice, this list of conditions and the following disclaimer in the
     48  1.1   ichiro  *    documentation and/or other materials provided with the distribution.
     49  1.1   ichiro  * 3. All advertising materials mentioning features or use of this software
     50  1.1   ichiro  *    must display the following acknowledgement:
     51  1.1   ichiro  *      This product includes software developed for the NetBSD Project by
     52  1.1   ichiro  *      Wasabi Systems, Inc.
     53  1.1   ichiro  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     54  1.1   ichiro  *    or promote products derived from this software without specific prior
     55  1.1   ichiro  *    written permission.
     56  1.1   ichiro  *
     57  1.1   ichiro  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     58  1.1   ichiro  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     59  1.1   ichiro  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     60  1.1   ichiro  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     61  1.1   ichiro  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     62  1.1   ichiro  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     63  1.1   ichiro  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     64  1.1   ichiro  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     65  1.1   ichiro  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     66  1.1   ichiro  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     67  1.1   ichiro  * POSSIBILITY OF SUCH DAMAGE.
     68  1.1   ichiro  */
     69  1.1   ichiro 
     70  1.1   ichiro #include <sys/cdefs.h>
     71  1.9      scw __KERNEL_RCSID(0, "$NetBSD: ixp425_intr.c,v 1.9 2004/02/13 15:49:02 scw Exp $");
     72  1.1   ichiro 
     73  1.1   ichiro #ifndef EVBARM_SPL_NOINLINE
     74  1.1   ichiro #define	EVBARM_SPL_NOINLINE
     75  1.1   ichiro #endif
     76  1.1   ichiro 
     77  1.1   ichiro /*
     78  1.1   ichiro  * Interrupt support for the Intel IXP425 NetworkProcessor.
     79  1.1   ichiro  */
     80  1.1   ichiro 
     81  1.1   ichiro #include <sys/param.h>
     82  1.1   ichiro #include <sys/systm.h>
     83  1.1   ichiro #include <sys/malloc.h>
     84  1.1   ichiro 
     85  1.1   ichiro #include <uvm/uvm_extern.h>
     86  1.1   ichiro 
     87  1.1   ichiro #include <machine/bus.h>
     88  1.1   ichiro #include <machine/intr.h>
     89  1.1   ichiro 
     90  1.1   ichiro #include <arm/cpufunc.h>
     91  1.1   ichiro 
     92  1.1   ichiro #include <arm/xscale/ixp425reg.h>
     93  1.1   ichiro #include <arm/xscale/ixp425var.h>
     94  1.1   ichiro 
     95  1.1   ichiro /* Interrupt handler queues. */
     96  1.1   ichiro struct intrq intrq[NIRQ];
     97  1.1   ichiro 
     98  1.1   ichiro /* Interrupts to mask at each level. */
     99  1.1   ichiro int ixp425_imask[NIPL];
    100  1.1   ichiro 
    101  1.1   ichiro /* Current interrupt priority level. */
    102  1.1   ichiro __volatile int current_spl_level;
    103  1.1   ichiro 
    104  1.1   ichiro /* Interrupts pending. */
    105  1.1   ichiro __volatile int ixp425_ipending;
    106  1.1   ichiro 
    107  1.1   ichiro /* Software copy of the IRQs we have enabled. */
    108  1.1   ichiro __volatile uint32_t intr_enabled;
    109  1.1   ichiro 
    110  1.1   ichiro /* Mask if interrupts steered to FIQs. */
    111  1.1   ichiro uint32_t intr_steer;
    112  1.1   ichiro 
    113  1.1   ichiro /*
    114  1.1   ichiro  * Map a software interrupt queue index
    115  1.9      scw  *
    116  1.9      scw  * XXX: !NOTE! :XXX
    117  1.9      scw  * We 'borrow' bits from the interrupt status register for interrupt sources
    118  1.9      scw  * which are not used by the current IXP425 port. Should any of the following
    119  1.9      scw  * interrupt sources be used at some future time, this must be revisited.
    120  1.9      scw  *
    121  1.9      scw  *  Bit#31: SW Interrupt 1
    122  1.9      scw  *  Bit#30: SW Interrupt 0
    123  1.9      scw  *  Bit#14: Timestamp Timer
    124  1.9      scw  *  Bit#11: General-purpose Timer 1
    125  1.9      scw  */
    126  1.1   ichiro  */
    127  1.1   ichiro static const uint32_t si_to_irqbit[SI_NQUEUES] = {
    128  1.1   ichiro 	IXP425_INT_bit31,		/* SI_SOFT */
    129  1.1   ichiro 	IXP425_INT_bit30,		/* SI_SOFTCLOCK */
    130  1.9      scw 	IXP425_INT_bit14,		/* SI_SOFTNET */
    131  1.9      scw 	IXP425_INT_bit11,		/* SI_SOFTSERIAL */
    132  1.1   ichiro };
    133  1.1   ichiro 
    134  1.1   ichiro #define	SI_TO_IRQBIT(si)	(1U << si_to_irqbit[(si)])
    135  1.1   ichiro 
    136  1.1   ichiro /*
    137  1.1   ichiro  * Map a software interrupt queue to an interrupt priority level.
    138  1.1   ichiro  */
    139  1.1   ichiro static const int si_to_ipl[SI_NQUEUES] = {
    140  1.1   ichiro 	IPL_SOFT,		/* SI_SOFT */
    141  1.1   ichiro 	IPL_SOFTCLOCK,		/* SI_SOFTCLOCK */
    142  1.1   ichiro 	IPL_SOFTNET,		/* SI_SOFTNET */
    143  1.1   ichiro 	IPL_SOFTSERIAL,		/* SI_SOFTSERIAL */
    144  1.1   ichiro };
    145  1.1   ichiro 
    146  1.1   ichiro void	ixp425_intr_dispatch(struct clockframe *frame);
    147  1.1   ichiro 
    148  1.1   ichiro static __inline uint32_t
    149  1.1   ichiro ixp425_irq_read(void)
    150  1.1   ichiro {
    151  1.1   ichiro 	return IXPREG(IXP425_INT_STATUS) & intr_enabled;
    152  1.1   ichiro }
    153  1.1   ichiro 
    154  1.1   ichiro static __inline void
    155  1.1   ichiro ixp425_set_intrsteer(void)
    156  1.1   ichiro {
    157  1.1   ichiro 	IXPREG(IXP425_INT_SELECT) = intr_steer & IXP425_INT_HWMASK;
    158  1.1   ichiro }
    159  1.1   ichiro 
    160  1.1   ichiro static __inline void
    161  1.1   ichiro ixp425_enable_irq(int irq)
    162  1.1   ichiro {
    163  1.1   ichiro 
    164  1.1   ichiro 	intr_enabled |= (1U << irq);
    165  1.1   ichiro 	ixp425_set_intrmask();
    166  1.1   ichiro }
    167  1.1   ichiro 
    168  1.1   ichiro static __inline void
    169  1.1   ichiro ixp425_disable_irq(int irq)
    170  1.1   ichiro {
    171  1.1   ichiro 
    172  1.1   ichiro 	intr_enabled &= ~(1U << irq);
    173  1.1   ichiro 	ixp425_set_intrmask();
    174  1.1   ichiro }
    175  1.1   ichiro 
    176  1.4      scw static __inline u_int32_t
    177  1.4      scw ixp425_irq2gpio_bit(int irq)
    178  1.4      scw {
    179  1.4      scw 
    180  1.4      scw 	static const u_int8_t int2gpio[32] __attribute__ ((aligned(32))) = {
    181  1.4      scw 		0xff, 0xff, 0xff, 0xff, 0xff, 0xff,	/* INT#0 -> INT#5 */
    182  1.4      scw 		0x00, 0x01,				/* GPIO#0 -> GPIO#1 */
    183  1.4      scw 		0xff, 0xff, 0xff, 0xff, 0xff, 0xff,	/* INT#8 -> INT#13 */
    184  1.4      scw 		0xff, 0xff, 0xff, 0xff, 0xff,		/* INT#14 -> INT#18 */
    185  1.4      scw 		0x02, 0x03, 0x04, 0x05, 0x06, 0x07,	/* GPIO#2 -> GPIO#7 */
    186  1.4      scw 		0x08, 0x09, 0x0a, 0x0b, 0x0c,		/* GPIO#8 -> GPIO#12 */
    187  1.4      scw 		0xff, 0xff				/* INT#30 -> INT#31 */
    188  1.4      scw 	};
    189  1.4      scw 
    190  1.4      scw #ifdef DEBUG
    191  1.4      scw 	if (int2gpio[irq] == 0xff)
    192  1.4      scw 		panic("ixp425_irq2gpio_bit: bad GPIO irq: %d\n", irq);
    193  1.4      scw #endif
    194  1.4      scw 	return (1U << int2gpio[irq]);
    195  1.4      scw }
    196  1.4      scw 
    197  1.1   ichiro /*
    198  1.1   ichiro  * NOTE: This routine must be called with interrupts disabled in the CPSR.
    199  1.1   ichiro  */
    200  1.1   ichiro static void
    201  1.1   ichiro ixp425_intr_calculate_masks(void)
    202  1.1   ichiro {
    203  1.1   ichiro 	struct intrq *iq;
    204  1.1   ichiro 	struct intrhand *ih;
    205  1.1   ichiro 	int irq, ipl;
    206  1.1   ichiro 
    207  1.1   ichiro 	/* First, figure out which IPLs each IRQ has. */
    208  1.1   ichiro 	for (irq = 0; irq < NIRQ; irq++) {
    209  1.1   ichiro 		int levels = 0;
    210  1.1   ichiro 		iq = &intrq[irq];
    211  1.1   ichiro 		ixp425_disable_irq(irq);
    212  1.1   ichiro 		for (ih = TAILQ_FIRST(&iq->iq_list); ih != NULL;
    213  1.1   ichiro 		     ih = TAILQ_NEXT(ih, ih_list))
    214  1.1   ichiro 			levels |= (1U << ih->ih_ipl);
    215  1.1   ichiro 		iq->iq_levels = levels;
    216  1.1   ichiro 	}
    217  1.1   ichiro 
    218  1.1   ichiro 	/* Next, figure out which IRQs are used by each IPL. */
    219  1.1   ichiro 	for (ipl = 0; ipl < NIPL; ipl++) {
    220  1.1   ichiro 		int irqs = 0;
    221  1.1   ichiro 		for (irq = 0; irq < NIRQ; irq++) {
    222  1.1   ichiro 			if (intrq[irq].iq_levels & (1U << ipl))
    223  1.1   ichiro 				irqs |= (1U << irq);
    224  1.1   ichiro 		}
    225  1.1   ichiro 		ixp425_imask[ipl] = irqs;
    226  1.1   ichiro 	}
    227  1.1   ichiro 
    228  1.1   ichiro 	ixp425_imask[IPL_NONE] = 0;
    229  1.1   ichiro 
    230  1.1   ichiro 	/*
    231  1.1   ichiro 	 * Initialize the soft interrupt masks to block themselves.
    232  1.1   ichiro 	 */
    233  1.1   ichiro 	ixp425_imask[IPL_SOFT] = SI_TO_IRQBIT(SI_SOFT);
    234  1.1   ichiro 	ixp425_imask[IPL_SOFTCLOCK] = SI_TO_IRQBIT(SI_SOFTCLOCK);
    235  1.1   ichiro 	ixp425_imask[IPL_SOFTNET] = SI_TO_IRQBIT(SI_SOFTNET);
    236  1.1   ichiro 	ixp425_imask[IPL_SOFTSERIAL] = SI_TO_IRQBIT(SI_SOFTSERIAL);
    237  1.1   ichiro 
    238  1.1   ichiro 	/*
    239  1.1   ichiro 	 * splsoftclock() is the only interface that users of the
    240  1.1   ichiro 	 * generic software interrupt facility have to block their
    241  1.1   ichiro 	 * soft intrs, so splsoftclock() must also block IPL_SOFT.
    242  1.1   ichiro 	 */
    243  1.1   ichiro 	ixp425_imask[IPL_SOFTCLOCK] |= ixp425_imask[IPL_SOFT];
    244  1.1   ichiro 
    245  1.1   ichiro 	/*
    246  1.1   ichiro 	 * splsoftnet() must also block splsoftclock(), since we don't
    247  1.1   ichiro 	 * want timer-driven network events to occur while we're
    248  1.1   ichiro 	 * processing incoming packets.
    249  1.1   ichiro 	 */
    250  1.1   ichiro 	ixp425_imask[IPL_SOFTNET] |= ixp425_imask[IPL_SOFTCLOCK];
    251  1.1   ichiro 
    252  1.1   ichiro 	/*
    253  1.1   ichiro 	 * Enforce a heirarchy that gives "slow" device (or devices with
    254  1.1   ichiro 	 * limited input buffer space/"real-time" requirements) a better
    255  1.1   ichiro 	 * chance at not dropping data.
    256  1.1   ichiro 	 */
    257  1.1   ichiro 	ixp425_imask[IPL_BIO] |= ixp425_imask[IPL_SOFTNET];
    258  1.1   ichiro 	ixp425_imask[IPL_NET] |= ixp425_imask[IPL_BIO];
    259  1.1   ichiro 	ixp425_imask[IPL_SOFTSERIAL] |= ixp425_imask[IPL_NET];
    260  1.1   ichiro 	ixp425_imask[IPL_TTY] |= ixp425_imask[IPL_SOFTSERIAL];
    261  1.1   ichiro 
    262  1.1   ichiro 	/*
    263  1.1   ichiro 	 * splvm() blocks all interrupts that use the kernel memory
    264  1.1   ichiro 	 * allocation facilities.
    265  1.1   ichiro 	 */
    266  1.2  thorpej 	ixp425_imask[IPL_VM] |= ixp425_imask[IPL_TTY];
    267  1.1   ichiro 
    268  1.1   ichiro 	/*
    269  1.1   ichiro 	 * Audio devices are not allowed to perform memory allocation
    270  1.1   ichiro 	 * in their interrupt routines, and they have fairly "real-time"
    271  1.1   ichiro 	 * requirements, so give them a high interrupt priority.
    272  1.1   ichiro 	 */
    273  1.2  thorpej 	ixp425_imask[IPL_AUDIO] |= ixp425_imask[IPL_VM];
    274  1.1   ichiro 
    275  1.1   ichiro 	/*
    276  1.1   ichiro 	 * splclock() must block anything that uses the scheduler.
    277  1.1   ichiro 	 */
    278  1.1   ichiro 	ixp425_imask[IPL_CLOCK] |= ixp425_imask[IPL_AUDIO];
    279  1.1   ichiro 
    280  1.1   ichiro 	/*
    281  1.1   ichiro 	 * No separate statclock on the IQ80310.
    282  1.1   ichiro 	 */
    283  1.1   ichiro 	ixp425_imask[IPL_STATCLOCK] |= ixp425_imask[IPL_CLOCK];
    284  1.1   ichiro 
    285  1.1   ichiro 	/*
    286  1.1   ichiro 	 * splhigh() must block "everything".
    287  1.1   ichiro 	 */
    288  1.1   ichiro 	ixp425_imask[IPL_HIGH] |= ixp425_imask[IPL_STATCLOCK];
    289  1.1   ichiro 
    290  1.1   ichiro 	/*
    291  1.1   ichiro 	 * XXX We need serial drivers to run at the absolute highest priority
    292  1.1   ichiro 	 * in order to avoid overruns, so serial > high.
    293  1.1   ichiro 	 */
    294  1.1   ichiro 	ixp425_imask[IPL_SERIAL] |= ixp425_imask[IPL_HIGH];
    295  1.1   ichiro 
    296  1.1   ichiro 	/*
    297  1.1   ichiro 	 * Now compute which IRQs must be blocked when servicing any
    298  1.1   ichiro 	 * given IRQ.
    299  1.1   ichiro 	 */
    300  1.1   ichiro 	for (irq = 0; irq < NIRQ; irq++) {
    301  1.1   ichiro 		int irqs = (1U << irq);
    302  1.1   ichiro 		iq = &intrq[irq];
    303  1.1   ichiro 		if (TAILQ_FIRST(&iq->iq_list) != NULL)
    304  1.1   ichiro 			ixp425_enable_irq(irq);
    305  1.1   ichiro 		for (ih = TAILQ_FIRST(&iq->iq_list); ih != NULL;
    306  1.1   ichiro 		     ih = TAILQ_NEXT(ih, ih_list))
    307  1.1   ichiro 			irqs |= ixp425_imask[ih->ih_ipl];
    308  1.1   ichiro 		iq->iq_mask = irqs;
    309  1.1   ichiro 	}
    310  1.1   ichiro }
    311  1.1   ichiro 
    312  1.1   ichiro __inline void
    313  1.1   ichiro ixp425_do_pending(void)
    314  1.1   ichiro {
    315  1.1   ichiro 	static __cpu_simple_lock_t processing = __SIMPLELOCK_UNLOCKED;
    316  1.1   ichiro 	int new, oldirqstate;
    317  1.1   ichiro 
    318  1.1   ichiro 	if (__cpu_simple_lock_try(&processing) == 0)
    319  1.1   ichiro 		return;
    320  1.1   ichiro 
    321  1.1   ichiro 	new = current_spl_level;
    322  1.1   ichiro 
    323  1.1   ichiro 	oldirqstate = disable_interrupts(I32_bit);
    324  1.1   ichiro 
    325  1.1   ichiro #define	DO_SOFTINT(si)							\
    326  1.1   ichiro 	if ((ixp425_ipending & ~new) & SI_TO_IRQBIT(si)) {		\
    327  1.1   ichiro 		ixp425_ipending &= ~SI_TO_IRQBIT(si);			\
    328  1.1   ichiro 		current_spl_level |= ixp425_imask[si_to_ipl[(si)]];	\
    329  1.1   ichiro 		restore_interrupts(oldirqstate);			\
    330  1.1   ichiro 		softintr_dispatch(si);					\
    331  1.1   ichiro 		oldirqstate = disable_interrupts(I32_bit);		\
    332  1.1   ichiro 		current_spl_level = new;				\
    333  1.1   ichiro 	}
    334  1.1   ichiro 
    335  1.1   ichiro 	DO_SOFTINT(SI_SOFTSERIAL);
    336  1.1   ichiro 	DO_SOFTINT(SI_SOFTNET);
    337  1.1   ichiro 	DO_SOFTINT(SI_SOFTCLOCK);
    338  1.1   ichiro 	DO_SOFTINT(SI_SOFT);
    339  1.1   ichiro 
    340  1.1   ichiro 	__cpu_simple_unlock(&processing);
    341  1.1   ichiro 
    342  1.1   ichiro 	restore_interrupts(oldirqstate);
    343  1.1   ichiro }
    344  1.1   ichiro 
    345  1.1   ichiro void
    346  1.1   ichiro splx(int new)
    347  1.1   ichiro {
    348  1.1   ichiro 
    349  1.1   ichiro 	ixp425_splx(new);
    350  1.1   ichiro }
    351  1.1   ichiro 
    352  1.1   ichiro int
    353  1.1   ichiro _spllower(int ipl)
    354  1.1   ichiro {
    355  1.1   ichiro 
    356  1.1   ichiro 	return (ixp425_spllower(ipl));
    357  1.1   ichiro }
    358  1.1   ichiro 
    359  1.1   ichiro int
    360  1.1   ichiro _splraise(int ipl)
    361  1.1   ichiro {
    362  1.1   ichiro 
    363  1.1   ichiro 	return (ixp425_splraise(ipl));
    364  1.1   ichiro }
    365  1.1   ichiro 
    366  1.1   ichiro void
    367  1.1   ichiro _setsoftintr(int si)
    368  1.1   ichiro {
    369  1.1   ichiro 	int oldirqstate;
    370  1.1   ichiro 
    371  1.1   ichiro 	oldirqstate = disable_interrupts(I32_bit);
    372  1.1   ichiro 	ixp425_ipending |= SI_TO_IRQBIT(si);
    373  1.1   ichiro 	restore_interrupts(oldirqstate);
    374  1.1   ichiro 
    375  1.1   ichiro 	/* Process unmasked pending soft interrupts. */
    376  1.1   ichiro 	if ((ixp425_ipending & INT_SWMASK) & ~current_spl_level)
    377  1.1   ichiro 		ixp425_do_pending();
    378  1.1   ichiro }
    379  1.1   ichiro 
    380  1.1   ichiro /*
    381  1.1   ichiro  * ixp425_icu_init:
    382  1.1   ichiro  *
    383  1.1   ichiro  * 	Called early in bootstrap to make clear interrupt register
    384  1.1   ichiro  */
    385  1.1   ichiro void
    386  1.1   ichiro ixp425_icu_init(void)
    387  1.1   ichiro {
    388  1.1   ichiro 
    389  1.1   ichiro 	intr_enabled = 0;	/* All interrupts disabled */
    390  1.1   ichiro 	ixp425_set_intrmask();
    391  1.1   ichiro 
    392  1.1   ichiro 	intr_steer = 0;		/* All interrupts steered to IRQ */
    393  1.1   ichiro 	ixp425_set_intrsteer();
    394  1.1   ichiro }
    395  1.1   ichiro 
    396  1.1   ichiro /*
    397  1.1   ichiro  * ixp425_intr_init:
    398  1.1   ichiro  *
    399  1.1   ichiro  *	Initialize the rest of the interrupt subsystem, making it
    400  1.1   ichiro  *	ready to handle interrupts from devices.
    401  1.1   ichiro  */
    402  1.1   ichiro void
    403  1.1   ichiro ixp425_intr_init(void)
    404  1.1   ichiro {
    405  1.1   ichiro 	struct intrq *iq;
    406  1.1   ichiro 	int i;
    407  1.1   ichiro 
    408  1.1   ichiro 	intr_enabled = 0;
    409  1.1   ichiro 
    410  1.1   ichiro 	for (i = 0; i < NIRQ; i++) {
    411  1.1   ichiro 		iq = &intrq[i];
    412  1.1   ichiro 		TAILQ_INIT(&iq->iq_list);
    413  1.1   ichiro 
    414  1.1   ichiro 		sprintf(iq->iq_name, "irq %d", i);
    415  1.1   ichiro 		evcnt_attach_dynamic(&iq->iq_ev, EVCNT_TYPE_INTR,
    416  1.1   ichiro 				     NULL, "ixp425", iq->iq_name);
    417  1.1   ichiro 	}
    418  1.1   ichiro 
    419  1.1   ichiro 	ixp425_intr_calculate_masks();
    420  1.1   ichiro 
    421  1.1   ichiro 	/* Enable IRQs (don't yet use FIQs). */
    422  1.1   ichiro 	enable_interrupts(I32_bit);
    423  1.1   ichiro }
    424  1.1   ichiro 
    425  1.1   ichiro void *
    426  1.1   ichiro ixp425_intr_establish(int irq, int ipl, int (*func)(void *), void *arg)
    427  1.1   ichiro {
    428  1.1   ichiro 	struct intrq *iq;
    429  1.1   ichiro 	struct intrhand *ih;
    430  1.1   ichiro 	u_int oldirqstate;
    431  1.1   ichiro 
    432  1.1   ichiro 	if (irq < 0 || irq > NIRQ)
    433  1.1   ichiro 		panic("ixp425_intr_establish: IRQ %d out of range", irq);
    434  1.1   ichiro #ifdef DEBUG
    435  1.1   ichiro 	printf("ixp425_intr_establish(irq=%d, ipl=%d, func=%08x, arg=%08x)\n",
    436  1.1   ichiro                irq, ipl, (u_int32_t) func, (u_int32_t) arg);
    437  1.1   ichiro #endif
    438  1.1   ichiro 
    439  1.1   ichiro 	ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
    440  1.1   ichiro 	if (ih == NULL)
    441  1.1   ichiro 		return (NULL);
    442  1.1   ichiro 
    443  1.1   ichiro 	ih->ih_func = func;
    444  1.1   ichiro 	ih->ih_arg = arg;
    445  1.1   ichiro 	ih->ih_ipl = ipl;
    446  1.1   ichiro 	ih->ih_irq = irq;
    447  1.1   ichiro 
    448  1.1   ichiro 	iq = &intrq[irq];
    449  1.1   ichiro 
    450  1.1   ichiro 	/* All IXP425 interrupts are level-triggered. */
    451  1.1   ichiro 	iq->iq_ist = IST_LEVEL; /* XXX */
    452  1.1   ichiro 
    453  1.1   ichiro 	oldirqstate = disable_interrupts(I32_bit);
    454  1.1   ichiro 
    455  1.1   ichiro 	TAILQ_INSERT_TAIL(&iq->iq_list, ih, ih_list);
    456  1.1   ichiro 
    457  1.1   ichiro 	ixp425_intr_calculate_masks();
    458  1.1   ichiro 
    459  1.1   ichiro 	restore_interrupts(oldirqstate);
    460  1.1   ichiro 
    461  1.1   ichiro 	return (ih);
    462  1.1   ichiro }
    463  1.1   ichiro 
    464  1.1   ichiro void
    465  1.1   ichiro ixp425_intr_disestablish(void *cookie)
    466  1.1   ichiro {
    467  1.1   ichiro 	struct intrhand *ih = cookie;
    468  1.1   ichiro 	struct intrq *iq = &intrq[ih->ih_irq];
    469  1.1   ichiro 	int oldirqstate;
    470  1.1   ichiro 
    471  1.1   ichiro 	oldirqstate = disable_interrupts(I32_bit);
    472  1.1   ichiro 
    473  1.1   ichiro 	TAILQ_REMOVE(&iq->iq_list, ih, ih_list);
    474  1.1   ichiro 
    475  1.1   ichiro 	ixp425_intr_calculate_masks();
    476  1.1   ichiro 
    477  1.1   ichiro 	restore_interrupts(oldirqstate);
    478  1.1   ichiro }
    479  1.1   ichiro 
    480  1.1   ichiro void
    481  1.1   ichiro ixp425_intr_dispatch(struct clockframe *frame)
    482  1.1   ichiro {
    483  1.1   ichiro 	struct intrq *iq;
    484  1.1   ichiro 	struct intrhand *ih;
    485  1.7      scw 	int oldirqstate, pcpl, irq, ibit, hwpend;
    486  1.1   ichiro 
    487  1.1   ichiro 	pcpl = current_spl_level;
    488  1.1   ichiro 
    489  1.1   ichiro 	hwpend = ixp425_irq_read();
    490  1.1   ichiro 
    491  1.1   ichiro 	/*
    492  1.1   ichiro 	 * Disable all the interrupts that are pending.  We will
    493  1.1   ichiro 	 * reenable them once they are processed and not masked.
    494  1.1   ichiro 	 */
    495  1.1   ichiro 	intr_enabled &= ~hwpend;
    496  1.1   ichiro 	ixp425_set_intrmask();
    497  1.1   ichiro 
    498  1.1   ichiro 	while (hwpend != 0) {
    499  1.1   ichiro 		irq = ffs(hwpend) - 1;
    500  1.1   ichiro 		ibit = (1U << irq);
    501  1.1   ichiro 
    502  1.1   ichiro 		hwpend &= ~ibit;
    503  1.1   ichiro 
    504  1.1   ichiro 		if (pcpl & ibit) {
    505  1.1   ichiro 			/*
    506  1.1   ichiro 			 * IRQ is masked; mark it as pending and check
    507  1.1   ichiro 			 * the next one.  Note: the IRQ is already disabled.
    508  1.1   ichiro 			 */
    509  1.1   ichiro 			ixp425_ipending |= ibit;
    510  1.1   ichiro 			continue;
    511  1.1   ichiro 		}
    512  1.1   ichiro 
    513  1.1   ichiro 		ixp425_ipending &= ~ibit;
    514  1.1   ichiro 
    515  1.1   ichiro 		iq = &intrq[irq];
    516  1.1   ichiro 		iq->iq_ev.ev_count++;
    517  1.1   ichiro 		uvmexp.intrs++;
    518  1.1   ichiro 		current_spl_level |= iq->iq_mask;
    519  1.4      scw 
    520  1.4      scw 		/* Clear down non-level triggered GPIO interrupts now */
    521  1.4      scw 		if ((ibit & IXP425_INT_GPIOMASK) && iq->iq_ist != IST_LEVEL) {
    522  1.4      scw 			IXPREG(IXP425_GPIO_VBASE + IXP425_GPIO_GPISR) =
    523  1.4      scw 			    ixp425_irq2gpio_bit(irq);
    524  1.4      scw 		}
    525  1.4      scw 
    526  1.1   ichiro 		oldirqstate = enable_interrupts(I32_bit);
    527  1.7      scw 		for (ih = TAILQ_FIRST(&iq->iq_list); ih != NULL;
    528  1.1   ichiro 		     ih = TAILQ_NEXT(ih, ih_list)) {
    529  1.7      scw 			(void) (*ih->ih_func)(ih->ih_arg ? ih->ih_arg : frame);
    530  1.1   ichiro 		}
    531  1.1   ichiro 		restore_interrupts(oldirqstate);
    532  1.4      scw 
    533  1.4      scw 		/* Clear down level triggered GPIO interrupts now */
    534  1.4      scw 		if ((ibit & IXP425_INT_GPIOMASK) && iq->iq_ist == IST_LEVEL) {
    535  1.4      scw 			IXPREG(IXP425_GPIO_VBASE + IXP425_GPIO_GPISR) =
    536  1.4      scw 			    ixp425_irq2gpio_bit(irq);
    537  1.4      scw 		}
    538  1.1   ichiro 
    539  1.1   ichiro 		current_spl_level = pcpl;
    540  1.1   ichiro 
    541  1.1   ichiro 		/* Re-enable this interrupt now that's it's cleared. */
    542  1.1   ichiro 		intr_enabled |= ibit;
    543  1.1   ichiro 		ixp425_set_intrmask();
    544  1.8      scw 
    545  1.8      scw 		/*
    546  1.8      scw 		 * Don't forget to include interrupts which may have
    547  1.8      scw 		 * arrived in the meantime.
    548  1.8      scw 		 */
    549  1.8      scw 		hwpend |= ((ixp425_ipending & IXP425_INT_HWMASK) & ~pcpl);
    550  1.1   ichiro 	}
    551  1.1   ichiro 
    552  1.1   ichiro 	/* Check for pendings soft intrs. */
    553  1.1   ichiro 	if ((ixp425_ipending & INT_SWMASK) & ~current_spl_level) {
    554  1.1   ichiro 		oldirqstate = enable_interrupts(I32_bit);
    555  1.1   ichiro 		ixp425_do_pending();
    556  1.1   ichiro 		restore_interrupts(oldirqstate);
    557  1.1   ichiro 	}
    558  1.1   ichiro }
    559