xref: /netbsd-src/sys/arch/arm/footbridge/footbridge_irqhandler.c (revision 77e75c280a6ef9de02ed9c7c1ed432312348a840)
1*77e75c28Sskrll /*	$NetBSD: footbridge_irqhandler.c,v 1.28 2021/08/13 11:40:43 skrll Exp $	*/
2af8ce959Schris 
3af8ce959Schris /*
4689b975bSchris  * Copyright (c) 2001, 2002 Wasabi Systems, Inc.
5af8ce959Schris  * All rights reserved.
6af8ce959Schris  *
7689b975bSchris  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8689b975bSchris  *
9af8ce959Schris  * Redistribution and use in source and binary forms, with or without
10af8ce959Schris  * modification, are permitted provided that the following conditions
11af8ce959Schris  * are met:
12af8ce959Schris  * 1. Redistributions of source code must retain the above copyright
13af8ce959Schris  *    notice, this list of conditions and the following disclaimer.
14af8ce959Schris  * 2. Redistributions in binary form must reproduce the above copyright
15af8ce959Schris  *    notice, this list of conditions and the following disclaimer in the
16af8ce959Schris  *    documentation and/or other materials provided with the distribution.
17af8ce959Schris  * 3. All advertising materials mentioning features or use of this software
18af8ce959Schris  *    must display the following acknowledgement:
19689b975bSchris  *	This product includes software developed for the NetBSD Project by
20689b975bSchris  *	Wasabi Systems, Inc.
21689b975bSchris  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22689b975bSchris  *    or promote products derived from this software without specific prior
23689b975bSchris  *    written permission.
24af8ce959Schris  *
25689b975bSchris  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26689b975bSchris  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27689b975bSchris  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28689b975bSchris  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29689b975bSchris  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30689b975bSchris  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31689b975bSchris  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32689b975bSchris  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33689b975bSchris  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34689b975bSchris  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35689b975bSchris  * POSSIBILITY OF SUCH DAMAGE.
36af8ce959Schris  */
37af8ce959Schris 
3861578bc3Schris #ifndef ARM_SPL_NOINLINE
3961578bc3Schris #define	ARM_SPL_NOINLINE
4061578bc3Schris #endif
4161578bc3Schris 
4261578bc3Schris #include <sys/cdefs.h>
43*77e75c28Sskrll __KERNEL_RCSID(0,"$NetBSD: footbridge_irqhandler.c,v 1.28 2021/08/13 11:40:43 skrll Exp $");
4461578bc3Schris 
45af8ce959Schris #include "opt_irqstats.h"
46af8ce959Schris 
47af8ce959Schris #include <sys/param.h>
48af8ce959Schris #include <sys/systm.h>
4938fdb085Sthorpej #include <sys/kmem.h>
50af8ce959Schris 
51792b7ebdSmatt #include <machine/intr.h>
52af8ce959Schris #include <machine/cpu.h>
5361578bc3Schris #include <arm/footbridge/dc21285mem.h>
5461578bc3Schris #include <arm/footbridge/dc21285reg.h>
55af8ce959Schris 
5661578bc3Schris #include <dev/pci/pcivar.h>
57af8ce959Schris 
5861578bc3Schris #include "isa.h"
5961578bc3Schris #if NISA > 0
6061578bc3Schris #include <dev/isa/isavar.h>
6161578bc3Schris #endif
62af8ce959Schris 
6361578bc3Schris /* Interrupt handler queues. */
6461578bc3Schris static struct intrq footbridge_intrq[NIRQ];
65af8ce959Schris 
6661578bc3Schris /* Interrupts to mask at each level. */
6761578bc3Schris int footbridge_imask[NIPL];
68af8ce959Schris 
6961578bc3Schris /* Software copy of the IRQs we have enabled. */
705f1c88d7Sperry volatile uint32_t intr_enabled;
71e3a3a9f5Schris 
7261578bc3Schris /* Interrupts pending */
735f1c88d7Sperry volatile int footbridge_ipending;
7461578bc3Schris 
7561578bc3Schris void footbridge_intr_dispatch(struct clockframe *frame);
7661578bc3Schris 
77825088edSmatt const struct evcnt *footbridge_pci_intr_evcnt(void *, pci_intr_handle_t);
7861578bc3Schris 
7961578bc3Schris const struct evcnt *
footbridge_pci_intr_evcnt(void * pcv,pci_intr_handle_t ih)80825088edSmatt footbridge_pci_intr_evcnt(void *pcv, pci_intr_handle_t ih)
81af8ce959Schris {
8261578bc3Schris 	/* XXX check range is valid */
8361578bc3Schris #if NISA > 0
8461578bc3Schris 	if (ih >= 0x80 && ih <= 0x8f) {
8561578bc3Schris 		return isa_intr_evcnt(NULL, (ih & 0x0f));
8661578bc3Schris 	}
8761578bc3Schris #endif
8861578bc3Schris 	return &footbridge_intrq[ih].iq_ev;
8961578bc3Schris }
90af8ce959Schris 
915f1c88d7Sperry static inline void
footbridge_enable_irq(int irq)9261578bc3Schris footbridge_enable_irq(int irq)
9361578bc3Schris {
9461578bc3Schris 	intr_enabled |= (1U << irq);
9561578bc3Schris 	footbridge_set_intrmask();
9661578bc3Schris }
9761578bc3Schris 
985f1c88d7Sperry static inline void
footbridge_disable_irq(int irq)9961578bc3Schris footbridge_disable_irq(int irq)
10061578bc3Schris {
10161578bc3Schris 	intr_enabled &= ~(1U << irq);
10261578bc3Schris 	footbridge_set_intrmask();
103af8ce959Schris }
104af8ce959Schris 
105af8ce959Schris /*
10661578bc3Schris  * NOTE: This routine must be called with interrupts disabled in the CPSR.
107af8ce959Schris  */
10861578bc3Schris static void
footbridge_intr_calculate_masks(void)10961578bc3Schris footbridge_intr_calculate_masks(void)
110af8ce959Schris {
11161578bc3Schris 	struct intrq *iq;
11261578bc3Schris 	struct intrhand *ih;
11361578bc3Schris 	int irq, ipl;
11461578bc3Schris 
11561578bc3Schris 	/* First, figure out which IPLs each IRQ has. */
11661578bc3Schris 	for (irq = 0; irq < NIRQ; irq++) {
11761578bc3Schris 		int levels = 0;
11861578bc3Schris 		iq = &footbridge_intrq[irq];
11961578bc3Schris 		footbridge_disable_irq(irq);
120825088edSmatt 		TAILQ_FOREACH(ih, &iq->iq_list, ih_list) {
12161578bc3Schris 			levels |= (1U << ih->ih_ipl);
122825088edSmatt 		}
12361578bc3Schris 		iq->iq_levels = levels;
124af8ce959Schris 	}
125af8ce959Schris 
12661578bc3Schris 	/* Next, figure out which IRQs are used by each IPL. */
12761578bc3Schris 	for (ipl = 0; ipl < NIPL; ipl++) {
12861578bc3Schris 		int irqs = 0;
12961578bc3Schris 		for (irq = 0; irq < NIRQ; irq++) {
13061578bc3Schris 			if (footbridge_intrq[irq].iq_levels & (1U << ipl))
13161578bc3Schris 				irqs |= (1U << irq);
13261578bc3Schris 		}
13361578bc3Schris 		footbridge_imask[ipl] = irqs;
13461578bc3Schris 	}
13561578bc3Schris 
13661578bc3Schris 	/* IPL_NONE must open up all interrupts */
137825088edSmatt 	KASSERT(footbridge_imask[IPL_NONE] == 0);
138825088edSmatt 	KASSERT(footbridge_imask[IPL_SOFTCLOCK] == 0);
139825088edSmatt 	KASSERT(footbridge_imask[IPL_SOFTBIO] == 0);
140825088edSmatt 	KASSERT(footbridge_imask[IPL_SOFTNET] == 0);
141825088edSmatt 	KASSERT(footbridge_imask[IPL_SOFTSERIAL] == 0);
142af8ce959Schris 
14361578bc3Schris 	/*
144d6a98601Swiz 	 * Enforce a hierarchy that gives "slow" device (or devices with
14561578bc3Schris 	 * limited input buffer space/"real-time" requirements) a better
14661578bc3Schris 	 * chance at not dropping data.
14761578bc3Schris 	 */
1484b293a84Sad 	footbridge_imask[IPL_SCHED] |= footbridge_imask[IPL_VM];
1494b293a84Sad 	footbridge_imask[IPL_HIGH] |= footbridge_imask[IPL_SCHED];
15061578bc3Schris 
15161578bc3Schris 	/*
15261578bc3Schris 	 * Calculate the ipl level to go to when handling this interrupt
15361578bc3Schris 	 */
154825088edSmatt 	for (irq = 0, iq = footbridge_intrq; irq < NIRQ; irq++, iq++) {
15561578bc3Schris 		int irqs = (1U << irq);
156825088edSmatt 		if (!TAILQ_EMPTY(&iq->iq_list)) {
15761578bc3Schris 			footbridge_enable_irq(irq);
158825088edSmatt 			TAILQ_FOREACH(ih, &iq->iq_list, ih_list) {
15961578bc3Schris 				irqs |= footbridge_imask[ih->ih_ipl];
160825088edSmatt 			}
161825088edSmatt 		}
16261578bc3Schris 		iq->iq_mask = irqs;
16361578bc3Schris 	}
16461578bc3Schris }
16561578bc3Schris 
16661578bc3Schris int
_splraise(int ipl)16761578bc3Schris _splraise(int ipl)
16861578bc3Schris {
16961578bc3Schris     return (footbridge_splraise(ipl));
17061578bc3Schris }
17161578bc3Schris 
17261578bc3Schris /* this will always take us to the ipl passed in */
173af8ce959Schris void
splx(int new)17461578bc3Schris splx(int new)
17561578bc3Schris {
17661578bc3Schris     footbridge_splx(new);
17761578bc3Schris }
17861578bc3Schris 
17961578bc3Schris int
_spllower(int ipl)18061578bc3Schris _spllower(int ipl)
18161578bc3Schris {
18261578bc3Schris     return (footbridge_spllower(ipl));
18361578bc3Schris }
18461578bc3Schris 
185663096a6Smrg void
footbridge_intr_init(void)18661578bc3Schris footbridge_intr_init(void)
187af8ce959Schris {
18861578bc3Schris 	struct intrq *iq;
18961578bc3Schris 	int i;
190af8ce959Schris 
19161578bc3Schris 	intr_enabled = 0;
192825088edSmatt 	set_curcpl(0xffffffff);
19361578bc3Schris 	footbridge_ipending = 0;
19461578bc3Schris 	footbridge_set_intrmask();
19561578bc3Schris 
196825088edSmatt 	for (i = 0, iq = footbridge_intrq; i < NIRQ; i++, iq++) {
19761578bc3Schris 		TAILQ_INIT(&iq->iq_list);
198af8ce959Schris 	}
199af8ce959Schris 
20061578bc3Schris 	footbridge_intr_calculate_masks();
201af8ce959Schris 
20261578bc3Schris 	/* Enable IRQ's, we don't have any FIQ's*/
20361578bc3Schris 	enable_interrupts(I32_bit);
204af8ce959Schris }
205af8ce959Schris 
2067094c090Smatt void
footbridge_intr_evcnt_attach(void)2077094c090Smatt footbridge_intr_evcnt_attach(void)
2087094c090Smatt {
2097094c090Smatt 	struct intrq *iq;
2107094c090Smatt 	int i;
2117094c090Smatt 
2127094c090Smatt 	for (i = 0, iq = footbridge_intrq; i < NIRQ; i++, iq++) {
2137094c090Smatt 
2147094c090Smatt 		snprintf(iq->iq_name, sizeof(iq->iq_name), "irq %d", i);
2157094c090Smatt 		evcnt_attach_dynamic(&iq->iq_ev, EVCNT_TYPE_INTR,
2167094c090Smatt 		    NULL, "footbridge", iq->iq_name);
2177094c090Smatt 	}
2187094c090Smatt }
2197094c090Smatt 
220af8ce959Schris void *
footbridge_intr_claim(int irq,int ipl,const char * name,int (* func)(void *),void * arg)221172a6238She footbridge_intr_claim(int irq, int ipl, const char *name, int (*func)(void *), void *arg)
222af8ce959Schris {
22361578bc3Schris 	struct intrq *iq;
22461578bc3Schris 	struct intrhand *ih;
22561578bc3Schris 	u_int oldirqstate;
22661578bc3Schris 
22761578bc3Schris 	if (irq < 0 || irq > NIRQ)
22861578bc3Schris 		panic("footbridge_intr_establish: IRQ %d out of range", irq);
229af8ce959Schris 
23038fdb085Sthorpej 	ih = kmem_alloc(sizeof(*ih), KM_SLEEP);
23161578bc3Schris 	ih->ih_func = func;
23261578bc3Schris 	ih->ih_arg = arg;
23361578bc3Schris 	ih->ih_ipl = ipl;
23461578bc3Schris 	ih->ih_irq = irq;
23561578bc3Schris 
23661578bc3Schris 	iq = &footbridge_intrq[irq];
23761578bc3Schris 
23861578bc3Schris 	iq->iq_ist = IST_LEVEL;
23961578bc3Schris 
24061578bc3Schris 	oldirqstate = disable_interrupts(I32_bit);
24161578bc3Schris 
24261578bc3Schris 	TAILQ_INSERT_TAIL(&iq->iq_list, ih, ih_list);
24361578bc3Schris 
24461578bc3Schris 	footbridge_intr_calculate_masks();
24561578bc3Schris 
24661578bc3Schris 	/* detach the existing event counter and add the new name */
24761578bc3Schris 	evcnt_detach(&iq->iq_ev);
24861578bc3Schris 	evcnt_attach_dynamic(&iq->iq_ev, EVCNT_TYPE_INTR,
24961578bc3Schris 			NULL, "footbridge", name);
25061578bc3Schris 
25161578bc3Schris 	restore_interrupts(oldirqstate);
25261578bc3Schris 
253af8ce959Schris 	return(ih);
254af8ce959Schris }
255af8ce959Schris 
25661578bc3Schris void
footbridge_intr_disestablish(void * cookie)25761578bc3Schris footbridge_intr_disestablish(void *cookie)
258af8ce959Schris {
25961578bc3Schris 	struct intrhand *ih = cookie;
26061578bc3Schris 	struct intrq *iq = &footbridge_intrq[ih->ih_irq];
26161578bc3Schris 	int oldirqstate;
262af8ce959Schris 
26361578bc3Schris 	/* XXX need to free ih ? */
26461578bc3Schris 	oldirqstate = disable_interrupts(I32_bit);
26561578bc3Schris 
26661578bc3Schris 	TAILQ_REMOVE(&iq->iq_list, ih, ih_list);
26761578bc3Schris 
26861578bc3Schris 	footbridge_intr_calculate_masks();
26961578bc3Schris 
27061578bc3Schris 	restore_interrupts(oldirqstate);
271af8ce959Schris }
27261578bc3Schris 
footbridge_intstatus(void)273825088edSmatt static inline uint32_t footbridge_intstatus(void)
27461578bc3Schris {
2755f1c88d7Sperry 	return ((volatile uint32_t*)(DC21285_ARMCSR_VBASE))[IRQ_STATUS>>2];
27661578bc3Schris }
27761578bc3Schris 
27861578bc3Schris /* called with external interrupts disabled */
27961578bc3Schris void
footbridge_intr_dispatch(struct clockframe * frame)28061578bc3Schris footbridge_intr_dispatch(struct clockframe *frame)
28161578bc3Schris {
28261578bc3Schris 	struct intrq *iq;
28361578bc3Schris 	struct intrhand *ih;
284825088edSmatt 	int oldirqstate, irq, ibit, hwpend;
285825088edSmatt 	struct cpu_info * const ci = curcpu();
286825088edSmatt 	const int ppl = ci->ci_cpl;
287825088edSmatt 	const int imask = footbridge_imask[ppl];
28861578bc3Schris 
28961578bc3Schris 	hwpend = footbridge_intstatus();
29061578bc3Schris 
29161578bc3Schris 	/*
29261578bc3Schris 	 * Disable all the interrupts that are pending.  We will
29361578bc3Schris 	 * reenable them once they are processed and not masked.
29461578bc3Schris 	 */
29561578bc3Schris 	intr_enabled &= ~hwpend;
29661578bc3Schris 	footbridge_set_intrmask();
29761578bc3Schris 
29861578bc3Schris 	while (hwpend != 0) {
29961578bc3Schris 		int intr_rc = 0;
30061578bc3Schris 		irq = ffs(hwpend) - 1;
30161578bc3Schris 		ibit = (1U << irq);
30261578bc3Schris 
30361578bc3Schris 		hwpend &= ~ibit;
30461578bc3Schris 
305825088edSmatt 		if (imask & ibit) {
30661578bc3Schris 			/*
30761578bc3Schris 			 * IRQ is masked; mark it as pending and check
30861578bc3Schris 			 * the next one.  Note: the IRQ is already disabled.
30961578bc3Schris 			 */
31061578bc3Schris 			footbridge_ipending |= ibit;
31161578bc3Schris 			continue;
31261578bc3Schris 		}
31361578bc3Schris 
31461578bc3Schris 		footbridge_ipending &= ~ibit;
31561578bc3Schris 
31661578bc3Schris 		iq = &footbridge_intrq[irq];
31761578bc3Schris 		iq->iq_ev.ev_count++;
3186a66466fSmatt 		ci->ci_data.cpu_nintr++;
319825088edSmatt 		TAILQ_FOREACH(ih, &iq->iq_list, ih_list) {
320825088edSmatt 			ci->ci_cpl = ih->ih_ipl;
32161578bc3Schris 			oldirqstate = enable_interrupts(I32_bit);
32261578bc3Schris 			intr_rc = (*ih->ih_func)(ih->ih_arg ? ih->ih_arg : frame);
32361578bc3Schris 			restore_interrupts(oldirqstate);
324825088edSmatt 			if (intr_rc != 1)
325825088edSmatt 				break;
326825088edSmatt 		}
32761578bc3Schris 
328825088edSmatt 		ci->ci_cpl = ppl;
32961578bc3Schris 
33061578bc3Schris 		/* Re-enable this interrupt now that's it's cleared. */
33161578bc3Schris 		intr_enabled |= ibit;
33261578bc3Schris 		footbridge_set_intrmask();
33373ca5359Smatt 
334f05e6f1aSwiz 		/* also check for any new interrupts that may have occurred,
33573ca5359Smatt 		 * that we can handle at this spl level */
336825088edSmatt 		hwpend |= (footbridge_ipending & ICU_INT_HWMASK) & ~imask;
33761578bc3Schris 	}
33861578bc3Schris 
339825088edSmatt #ifdef __HAVE_FAST_SOFTINTS
340825088edSmatt 	cpu_dosoftints();
341825088edSmatt #endif /* __HAVE_FAST_SOFTINTS */
342af8ce959Schris }
343