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