#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ixp425_intr.c,v 1.28 2020/11/20 18:49:45 thorpej Exp $");
#ifndef EVBARM_SPL_NOINLINE
#define EVBARM_SPL_NOINLINE
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kmem.h>
#include <sys/bus.h>
#include <machine/intr.h>
#include <arm/cpufunc.h>
#include <arm/xscale/ixp425reg.h>
#include <arm/xscale/ixp425var.h>
struct intrq intrq[NIRQ];
int ixp425_imask[NIPL];
volatile int ixp425_ipending;
volatile uint32_t intr_enabled;
uint32_t intr_steer;
#ifdef __HAVE_FAST_SOFTINTS
static const uint32_t si_to_irqbit[SI_NQUEUES] = {
IXP425_INT_bit31,
IXP425_INT_bit30,
IXP425_INT_bit14,
IXP425_INT_bit11,
};
#define SI_TO_IRQBIT(si) (1U << si_to_irqbit[(si)])
static const int si_to_ipl[] = {
[SI_SOFTCLOCK] = IPL_SOFTCLOCK,
[SI_SOFTBIO] = IPL_SOFTBIO,
[SI_SOFTNET] = IPL_SOFTNET,
[SI_SOFTSERIAL] = IPL_SOFTSERIAL,
};
#endif
void ixp425_intr_dispatch(struct clockframe *frame);
static inline uint32_t
ixp425_irq_read(void)
{
return IXPREG(IXP425_INT_STATUS) & intr_enabled;
}
static inline void
ixp425_set_intrsteer(void)
{
IXPREG(IXP425_INT_SELECT) = intr_steer & IXP425_INT_HWMASK;
}
static inline void
ixp425_enable_irq(int irq)
{
intr_enabled |= (1U << irq);
ixp425_set_intrmask();
}
static inline void
ixp425_disable_irq(int irq)
{
intr_enabled &= ~(1U << irq);
ixp425_set_intrmask();
}
static inline uint32_t
ixp425_irq2gpio_bit(int irq)
{
static const uint8_t int2gpio[32] __attribute__ ((aligned(32))) = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0x00, 0x01,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff,
0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c,
0xff, 0xff
};
#ifdef DEBUG
if (int2gpio[irq] == 0xff)
panic("ixp425_irq2gpio_bit: bad GPIO irq: %d\n", irq);
#endif
return (1U << int2gpio[irq]);
}
static void
ixp425_intr_calculate_masks(void)
{
struct intrq *iq;
struct intrhand *ih;
int irq, ipl;
for (irq = 0; irq < NIRQ; irq++) {
int levels = 0;
iq = &intrq[irq];
ixp425_disable_irq(irq);
for (ih = TAILQ_FIRST(&iq->iq_list); ih != NULL;
ih = TAILQ_NEXT(ih, ih_list))
levels |= (1U << ih->ih_ipl);
iq->iq_levels = levels;
}
for (ipl = 0; ipl < NIPL; ipl++) {
int irqs = 0;
for (irq = 0; irq < NIRQ; irq++) {
if (intrq[irq].iq_levels & (1U << ipl))
irqs |= (1U << irq);
}
ixp425_imask[ipl] = irqs;
}
KASSERT(ixp425_imask[IPL_NONE] == 0);
#ifdef __HAVE_FAST_SOFTINTS
ixp425_imask[IPL_SOFTCLOCK] = SI_TO_IRQBIT(SI_SOFTCLOCK);
ixp425_imask[IPL_SOFTBIO] = SI_TO_IRQBIT(SI_SOFTBIO);
ixp425_imask[IPL_SOFTNET] = SI_TO_IRQBIT(SI_SOFTNET);
ixp425_imask[IPL_SOFTSERIAL] = SI_TO_IRQBIT(SI_SOFTSERIAL);
#else
KASSERT(ixp425_imask[IPL_SOFTCLOCK] == 0);
KASSERT(ixp425_imask[IPL_SOFTBIO] == 0);
KASSERT(ixp425_imask[IPL_SOFTNET] == 0);
KASSERT(ixp425_imask[IPL_SOFTSERIAL] == 0);
#endif
ixp425_imask[IPL_SCHED] |= ixp425_imask[IPL_VM];
ixp425_imask[IPL_HIGH] |= ixp425_imask[IPL_SCHED];
for (irq = 0; irq < NIRQ; irq++) {
int irqs = (1U << irq);
iq = &intrq[irq];
if (TAILQ_FIRST(&iq->iq_list) != NULL)
ixp425_enable_irq(irq);
for (ih = TAILQ_FIRST(&iq->iq_list); ih != NULL;
ih = TAILQ_NEXT(ih, ih_list))
irqs |= ixp425_imask[ih->ih_ipl];
iq->iq_mask = irqs;
}
}
void
splx(int new)
{
ixp425_splx(new);
}
int
_spllower(int ipl)
{
return (ixp425_spllower(ipl));
}
int
_splraise(int ipl)
{
return (ixp425_splraise(ipl));
}
void
ixp425_icu_init(void)
{
intr_enabled = 0;
ixp425_set_intrmask();
intr_steer = 0;
ixp425_set_intrsteer();
}
void
ixp425_intr_init(void)
{
struct intrq *iq;
int i;
intr_enabled = 0;
for (i = 0; i < NIRQ; i++) {
iq = &intrq[i];
TAILQ_INIT(&iq->iq_list);
snprintf(iq->iq_name, sizeof(iq->iq_name), "irq %d", i);
}
ixp425_intr_calculate_masks();
enable_interrupts(I32_bit);
}
void
ixp425_intr_evcnt_attach(void)
{
struct intrq *iq;
int i;
for (i = 0; i < NIRQ; i++) {
iq = &intrq[i];
evcnt_attach_dynamic(&iq->iq_ev, EVCNT_TYPE_INTR,
NULL, "ixp425", iq->iq_name);
}
}
void *
ixp425_intr_establish(int irq, int ipl, int (*func)(void *), void *arg)
{
struct intrq *iq;
struct intrhand *ih;
u_int oldirqstate;
if (irq < 0 || irq > NIRQ)
panic("ixp425_intr_establish: IRQ %d out of range", irq);
#ifdef DEBUG
printf("ixp425_intr_establish(irq=%d, ipl=%d, func=%08x, arg=%08x)\n",
irq, ipl, (uint32_t) func, (uint32_t) arg);
#endif
ih = kmem_alloc(sizeof(*ih), KM_SLEEP);
ih->ih_func = func;
ih->ih_arg = arg;
ih->ih_ipl = ipl;
ih->ih_irq = irq;
iq = &intrq[irq];
iq->iq_ist = IST_LEVEL;
oldirqstate = disable_interrupts(I32_bit);
TAILQ_INSERT_TAIL(&iq->iq_list, ih, ih_list);
ixp425_intr_calculate_masks();
restore_interrupts(oldirqstate);
return (ih);
}
void
ixp425_intr_disestablish(void *cookie)
{
struct intrhand *ih = cookie;
struct intrq *iq = &intrq[ih->ih_irq];
int oldirqstate;
oldirqstate = disable_interrupts(I32_bit);
TAILQ_REMOVE(&iq->iq_list, ih, ih_list);
ixp425_intr_calculate_masks();
restore_interrupts(oldirqstate);
}
void
ixp425_intr_dispatch(struct clockframe *frame)
{
struct intrq *iq;
struct intrhand *ih;
int oldirqstate, irq, ibit, hwpend;
struct cpu_info * const ci = curcpu();
const int ppl = ci->ci_cpl;
const uint32_t imask = ixp425_imask[ppl];
hwpend = ixp425_irq_read();
intr_enabled &= ~hwpend;
ixp425_set_intrmask();
while (hwpend != 0) {
irq = ffs(hwpend) - 1;
ibit = (1U << irq);
hwpend &= ~ibit;
if (imask & ibit) {
ixp425_ipending |= ibit;
continue;
}
ixp425_ipending &= ~ibit;
iq = &intrq[irq];
iq->iq_ev.ev_count++;
ci->ci_data.cpu_nintr++;
if ((ibit & IXP425_INT_GPIOMASK) && iq->iq_ist != IST_LEVEL) {
IXPREG(IXP425_GPIO_VBASE + IXP425_GPIO_GPISR) =
ixp425_irq2gpio_bit(irq);
}
TAILQ_FOREACH(ih, &iq->iq_list, ih_list) {
ci->ci_cpl = ih->ih_ipl;
oldirqstate = enable_interrupts(I32_bit);
(void) (*ih->ih_func)(ih->ih_arg ? ih->ih_arg : frame);
restore_interrupts(oldirqstate);
}
if ((ibit & IXP425_INT_GPIOMASK) && iq->iq_ist == IST_LEVEL) {
IXPREG(IXP425_GPIO_VBASE + IXP425_GPIO_GPISR) =
ixp425_irq2gpio_bit(irq);
}
ci->ci_cpl = ppl;
intr_enabled |= ibit;
ixp425_set_intrmask();
hwpend |= ((ixp425_ipending & IXP425_INT_HWMASK) & ~imask);
}
#ifdef __HAVE_FAST_SOFTINTS
cpu_dosoftints();
#endif
}