#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: iq80310_intr.c,v 1.36 2020/11/21 15:30:07 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/i80200reg.h>
#include <arm/xscale/i80200var.h>
#include <evbarm/iq80310/iq80310reg.h>
#include <evbarm/iq80310/iq80310var.h>
#include <evbarm/iq80310/obiovar.h>
struct intrq intrq[NIRQ];
int iq80310_imask[NIPL];
volatile int iq80310_ipending;
uint32_t intr_enabled;
#ifdef __HAVE_FAST_SOFTINTS
#define SI_TO_IRQBIT(si) (1U << (31 - (si)))
static const int si_to_ipl[SI_NQUEUES] = {
IPL_SOFT,
IPL_SOFTCLOCK,
IPL_SOFTNET,
IPL_SOFTSERIAL,
};
#endif
void iq80310_intr_dispatch(struct trapframe *frame);
static inline uint32_t
iq80310_intstat_read(void)
{
uint32_t intstat;
intstat = CPLD_READ(IQ80310_XINT3_STATUS) & 0x1f;
#if defined(IRQ_READ_XINT0)
if (IRQ_READ_XINT0)
intstat |= (CPLD_READ(IQ80310_XINT0_STATUS) & 0x7) << 5;
#endif
return (intstat & intr_enabled);
}
static inline void
iq80310_set_intrmask(void)
{
uint32_t disabled;
intr_enabled |= IRQ_BITS_ALWAYS_ON;
disabled = (~intr_enabled) & IRQ_BITS;
CPLD_WRITE(IQ80310_XINT_MASK, disabled & 0x1f);
}
static inline void
iq80310_enable_irq(int irq)
{
intr_enabled |= (1U << irq);
iq80310_set_intrmask();
}
static inline void
iq80310_disable_irq(int irq)
{
intr_enabled &= ~(1U << irq);
iq80310_set_intrmask();
}
static void
iq80310_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];
iq80310_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);
}
iq80310_imask[ipl] = irqs;
}
iq80310_imask[IPL_NONE] = 0;
iq80310_imask[IPL_SOFTCLOCK] = 0;
iq80310_imask[IPL_SOFTNET] = 0;
iq80310_imask[IPL_SOFTSERIAL] = 0;
iq80310_imask[IPL_SOFTNET] |= iq80310_imask[IPL_SOFTCLOCK];
iq80310_imask[IPL_BIO] |= iq80310_imask[IPL_SOFTNET];
iq80310_imask[IPL_NET] |= iq80310_imask[IPL_BIO];
iq80310_imask[IPL_SOFTSERIAL] |= iq80310_imask[IPL_NET];
iq80310_imask[IPL_TTY] |= iq80310_imask[IPL_SOFTSERIAL];
iq80310_imask[IPL_VM] |= iq80310_imask[IPL_TTY];
iq80310_imask[IPL_AUDIO] |= iq80310_imask[IPL_VM];
iq80310_imask[IPL_CLOCK] |= iq80310_imask[IPL_AUDIO];
#ifdef IPL_STATCLOCK
iq80310_imask[IPL_STATCLOCK] |= iq80310_imask[IPL_CLOCK];
#endif
#ifdef IPL_STATCLOCK
iq80310_imask[IPL_HIGH] |= iq80310_imask[IPL_STATCLOCK];
#else
iq80310_imask[IPL_HIGH] |= iq80310_imask[IPL_CLOCK];
#endif
iq80310_imask[IPL_SERIAL] |= iq80310_imask[IPL_HIGH];
for (irq = 0; irq < NIRQ; irq++) {
int irqs = (1U << irq);
iq = &intrq[irq];
if (TAILQ_FIRST(&iq->iq_list) != NULL)
iq80310_enable_irq(irq);
for (ih = TAILQ_FIRST(&iq->iq_list); ih != NULL;
ih = TAILQ_NEXT(ih, ih_list))
irqs |= iq80310_imask[ih->ih_ipl];
iq->iq_mask = irqs;
}
}
#ifdef __HAVE_FAST_SOFTINTS
void
iq80310_do_soft(void)
{
static __cpu_simple_lock_t processing = __SIMPLELOCK_UNLOCKED;
struct cpu_info * const ci = curcpu();
int new, oldirqstate;
if (__cpu_simple_lock_try(&processing) == 0)
return;
new = ci->ci_cpl;
oldirqstate = disable_interrupts(I32_bit);
#define DO_SOFTINT(si) \
if ((iq80310_ipending & ~new) & SI_TO_IRQBIT(si)) { \
iq80310_ipending &= ~SI_TO_IRQBIT(si); \
ci->ci_cpl |= iq80310_imask[si_to_ipl[(si)]]; \
restore_interrupts(oldirqstate); \
softintr_dispatch(si); \
oldirqstate = disable_interrupts(I32_bit); \
ci->ci_cpl = new; \
}
DO_SOFTINT(SI_SOFTSERIAL);
DO_SOFTINT(SI_SOFTNET);
DO_SOFTINT(SI_SOFTCLOCK);
DO_SOFTINT(SI_SOFT);
__cpu_simple_unlock(&processing);
restore_interrupts(oldirqstate);
}
#endif
int
_splraise(int ipl)
{
return (iq80310_splraise(ipl));
}
inline void
splx(int new)
{
return (iq80310_splx(new));
}
int
_spllower(int ipl)
{
return (iq80310_spllower(ipl));
}
#ifdef __HAVE_FAST_SOFTINTS
void
_setsoftintr(int si)
{
int oldirqstate;
oldirqstate = disable_interrupts(I32_bit);
iq80310_ipending |= SI_TO_IRQBIT(si);
restore_interrupts(oldirqstate);
if ((iq80310_ipending & ~IRQ_BITS) & ~curcpl())
iq80310_do_soft();
}
#endif
void
iq80310_intr_init(void)
{
struct intrq *iq;
int i;
intr_enabled |= IRQ_BITS_ALWAYS_ON;
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);
}
iq80310_intr_calculate_masks();
i80200_extirq_dispatch = iq80310_intr_dispatch;
i80200_intr_enable(INTCTL_IM | INTCTL_PM);
enable_interrupts(I32_bit);
}
void
iq80310_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, "iq80310", iq->iq_name);
}
}
void *
iq80310_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("iq80310_intr_establish: IRQ %d out of range", irq);
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);
iq80310_intr_calculate_masks();
restore_interrupts(oldirqstate);
return (ih);
}
void
iq80310_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);
iq80310_intr_calculate_masks();
restore_interrupts(oldirqstate);
}
void
iq80310_intr_dispatch(struct trapframe *frame)
{
struct intrq *iq;
struct intrhand *ih;
int oldirqstate, pcpl, irq, ibit, hwpend, rv;
struct cpu_info * const ci = curcpu();
#if 0
int stray;
stray = 1;
#endif
i80200_intr_disable(INTCTL_IM | INTCTL_PM);
pcpl = ci->ci_cpl;
for (hwpend = iq80310_intstat_read(); hwpend != 0;) {
irq = ffs(hwpend) - 1;
ibit = (1U << irq);
#if 0
stray = 0;
#endif
hwpend &= ~ibit;
if (pcpl & ibit) {
iq80310_ipending |= ibit;
continue;
}
iq80310_ipending &= ~ibit;
rv = 0;
iq = &intrq[irq];
iq->iq_ev.ev_count++;
ci->ci_data.cpu_nintr++;
ci->ci_cpl |= iq->iq_mask;
oldirqstate = enable_interrupts(I32_bit);
for (ih = TAILQ_FIRST(&iq->iq_list); ih != NULL;
ih = TAILQ_NEXT(ih, ih_list)) {
rv |= (*ih->ih_func)(ih->ih_arg ? ih->ih_arg : frame);
}
restore_interrupts(oldirqstate);
ci->ci_cpl = pcpl;
#if 0
if (rv == 0)
printf("Stray interrupt: IRQ %d\n", irq);
#endif
}
#if 0
if (stray)
printf("Stray external interrupt\n");
#endif
#ifdef __HAVE_FAST_SOFTINTS
if ((iq80310_ipending & ~IRQ_BITS) & ~ci->ci_cpl) {
oldirqstate = enable_interrupts(I32_bit);
iq80310_do_soft();
restore_interrupts(oldirqstate);
}
#endif
if ((iq80310_ipending & IRQ_BITS) == 0)
i80200_intr_enable(INTCTL_IM | INTCTL_PM);
}