software/libbase/isr.c: Cleanup code a bit.

This commit is contained in:
Florent Kermarrec 2024-06-14 11:47:02 +02:00
parent 6164a55c6b
commit 38e060c354
1 changed files with 110 additions and 97 deletions

View File

@ -3,7 +3,6 @@
// This file is Copyright (c) 2020 Raptor Engineering, LLC <sales@raptorengineering.com>
// License: BSD
#include <generated/csr.h>
#include <generated/soc.h>
#include <irq.h>
@ -19,7 +18,11 @@ void isr(void);
#ifdef CONFIG_CPU_HAS_INTERRUPT
#if defined(__blackparrot__) /*TODO: Update this function for BP*/ //
/*************************************/
/* ISR Handling for BlackParrot CPU. */
/*************************************/
#if defined(__blackparrot__) /*TODO: Update this function for BP.*/
void isr(void)
{
static int onetime = 0;
@ -29,38 +32,39 @@ void isr(void)
onetime++;
}
}
/***********************************************************/
/* ISR and PLIC Initialization for RISC-V PLIC-based CPUs. */
/***********************************************************/
#elif defined(__riscv_plic__)
// PLIC initialization.
void plic_init(void);
/* PLIC initialization. */
void plic_init(void)
{
int i;
// Set priorities for the first 8 external interrupts to 1.
/* Set priorities for the first 8 external interrupts to 1. */
for (i = 0; i < 8; i++)
*((unsigned int *)PLIC_BASE + PLIC_EXT_IRQ_BASE + i) = 1;
*((unsigned int *)(PLIC_BASE + PLIC_EXT_IRQ_BASE + i)) = 1;
// Enable the first 8 external interrupts
/* Enable the first 8 external interrupts. */
*((unsigned int *)PLIC_ENABLED) = 0xff << PLIC_EXT_IRQ_BASE;
// Set priority threshold to 0 (any priority > 0 triggers an interrupt).
/* Set priority threshold to 0 (any priority > 0 triggers an interrupt). */
*((unsigned int *)PLIC_THRSHLD) = 0;
}
// Interrupt Service Routine.
/* Interrupt Service Routine. */
void isr(void)
{
unsigned int claim;
// Claim and handle pending interrupts.
/* Claim and handle pending interrupts. */
while ((claim = *((unsigned int *)PLIC_CLAIM))) {
switch (claim - PLIC_EXT_IRQ_BASE) {
case UART_INTERRUPT:
uart_isr(); // Handle UART interrupt.
uart_isr(); /* Handle UART interrupt. */
break;
default:
// Unhandled interrupt source, print diagnostic information.
/* Unhandled interrupt source, print diagnostic information. */
printf("## PLIC: Unhandled claim: %d\n", claim);
printf("# plic_enabled: %08x\n", irq_getmask());
printf("# plic_pending: %08x\n", irq_pending());
@ -72,10 +76,14 @@ void isr(void)
printf("###########################\n\n");
break;
}
// Acknowledge the interrupt.
/* Acknowledge the interrupt. */
*((unsigned int *)PLIC_CLAIM) = claim;
}
}
/************************************************/
/* ISR Handling for CV32E40P and CV32E41P CPUs. */
/************************************************/
#elif defined(__cv32e40p__) || defined(__cv32e41p__)
#define FIRQ_OFFSET 16
@ -111,6 +119,9 @@ void isr(void)
}
}
/***********************************/
/* ISR Handling for Microwatt CPU. */
/***********************************/
#elif defined(__microwatt__)
void isr(uint64_t vec)
@ -119,19 +130,18 @@ void isr(uint64_t vec)
return isr_dec();
if (vec == 0x500) {
// Read interrupt source
/* Read interrupt source. */
uint32_t xirr = xics_icp_readw(PPC_XICS_XIRR);
uint32_t irq_source = xirr & 0x00ffffff;
__attribute__((unused)) unsigned int irqs;
// Handle IPI interrupts separately
/* Handle IPI interrupts separately. */
if (irq_source == 2) {
// IPI interrupt
/* IPI interrupt. */
xics_icp_writeb(PPC_XICS_MFRR, 0xff);
}
else {
// External interrupt
} else {
/* External interrupt. */
irqs = irq_pending() & irq_getmask();
#ifndef UART_POLLING
@ -140,7 +150,7 @@ void isr(uint64_t vec)
#endif
}
// Clear interrupt
/* Clear interrupt. */
xics_icp_writew(PPC_XICS_XIRR, xirr);
return;
@ -149,10 +159,13 @@ void isr(uint64_t vec)
void isr_dec(void)
{
// For now, just set DEC back to a large enough value to slow the flood of DEC-initiated timer interrupts
/* Set DEC back to a large enough value to slow the flood of DEC-initiated timer interrupts. */
mtdec(0x000000000ffffff);
}
/*******************************************************/
/* Generic ISR Handling for CPUs with Interrupt Table. */
/*******************************************************/
#else
struct irq_table
@ -179,12 +192,12 @@ int irq_detach(unsigned int irq)
return irq_attach(irq, NULL);
}
/* Interrupt Service Routine. */
void isr(void)
{
unsigned int irqs = irq_pending() & irq_getmask();
while (irqs)
{
while (irqs) {
const unsigned int irq = __builtin_ctz(irqs);
if ((irq < CONFIG_CPU_INTERRUPTS) && irq_table[irq].isr)
irq_table[irq].isr();
@ -192,7 +205,7 @@ void isr(void)
irq_setmask(irq_getmask() & ~(1 << irq));
printf("\n*** disabled spurious irq %d ***\n", irq);
}
irqs &= irqs - 1; // clear this irq (the first bit set)
irqs &= irqs - 1; /* Clear this IRQ (the first bit set). */
}
}
#endif