2022-06-07 02:46:23 -04:00
|
|
|
/*
|
2023-07-19 04:06:02 -04:00
|
|
|
* SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD
|
2022-06-07 02:46:23 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2020-11-05 23:03:03 -05:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <assert.h>
|
2022-06-07 02:46:23 -04:00
|
|
|
#include "soc/soc.h"
|
2020-11-05 23:03:03 -05:00
|
|
|
#include "riscv/interrupt.h"
|
|
|
|
#include "soc/interrupt_reg.h"
|
|
|
|
#include "riscv/csr.h"
|
|
|
|
#include "esp_attr.h"
|
2023-07-19 04:06:02 -04:00
|
|
|
#include "riscv/rv_utils.h"
|
|
|
|
|
|
|
|
|
2023-08-14 03:44:24 -04:00
|
|
|
#if SOC_INT_CLIC_SUPPORTED
|
2023-07-19 04:06:02 -04:00
|
|
|
|
2023-08-14 03:44:24 -04:00
|
|
|
/**
|
|
|
|
* If the target is using the CLIC as the interrupt controller, we have 32 external interrupt lines and 16 internal
|
|
|
|
* lines. Let's consider the internal ones reserved and not mappable to any handler.
|
|
|
|
*/
|
|
|
|
#define RV_EXTERNAL_INT_COUNT 32
|
|
|
|
#define RV_EXTERNAL_INT_OFFSET (CLIC_EXT_INTR_NUM_OFFSET)
|
2020-11-05 23:03:03 -05:00
|
|
|
|
2023-08-14 03:44:24 -04:00
|
|
|
#else // !SOC_INT_CLIC_SUPPORTED
|
2020-11-05 23:03:03 -05:00
|
|
|
|
2023-08-14 03:44:24 -04:00
|
|
|
/**
|
|
|
|
* In the case of INTC, all the interrupt lines are dedicated to external peripherals, so the offset is 0.
|
|
|
|
* In the case of PLIC, the reserved interrupts are not contiguous, moreover, they are already marked as
|
|
|
|
* unusable by the interrupt allocator, so the offset can also be 0 here.
|
|
|
|
*/
|
|
|
|
#define RV_EXTERNAL_INT_COUNT 32
|
|
|
|
#define RV_EXTERNAL_INT_OFFSET 0
|
2020-11-05 23:03:03 -05:00
|
|
|
|
2023-08-14 03:44:24 -04:00
|
|
|
/* Since DR_REG_INTERRUPT_CORE0_BASE is not defined on some single-core targets, use the former
|
|
|
|
* DR_REG_INTERRUPT_BASE macro instead. */
|
|
|
|
#ifndef DR_REG_INTERRUPT_CORE0_BASE
|
|
|
|
#define DR_REG_INTERRUPT_CORE0_BASE DR_REG_INTERRUPT_BASE
|
|
|
|
#endif // DR_REG_INTERRUPT_CORE0_BASE
|
|
|
|
|
|
|
|
|
|
|
|
#endif // SOC_INT_CLIC_SUPPORTED
|
2020-11-05 23:03:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
intr_handler_t handler;
|
|
|
|
void *arg;
|
|
|
|
} intr_handler_item_t;
|
|
|
|
|
2023-08-14 03:44:24 -04:00
|
|
|
static intr_handler_item_t s_intr_handlers[SOC_CPU_CORES_NUM][RV_EXTERNAL_INT_COUNT];
|
|
|
|
|
|
|
|
|
|
|
|
static inline void assert_valid_rv_int_num(int rv_int_num)
|
|
|
|
{
|
|
|
|
#if !SOC_INT_CLIC_SUPPORTED
|
|
|
|
assert(rv_int_num != 0 && "Invalid CPU interrupt number");
|
2023-07-19 04:06:02 -04:00
|
|
|
#endif
|
2023-08-14 03:44:24 -04:00
|
|
|
assert(rv_int_num < RV_EXTERNAL_INT_COUNT && "Invalid CPU interrupt number");
|
|
|
|
}
|
2020-11-05 23:03:03 -05:00
|
|
|
|
2023-08-14 03:44:24 -04:00
|
|
|
|
|
|
|
static intr_handler_item_t* intr_get_item(int int_no)
|
2020-11-05 23:03:03 -05:00
|
|
|
{
|
|
|
|
assert_valid_rv_int_num(int_no);
|
|
|
|
|
2023-08-14 03:44:24 -04:00
|
|
|
const uint32_t id = rv_utils_get_core_id();
|
|
|
|
|
|
|
|
return &s_intr_handlers[id][int_no];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************** Software interrupt dispatcher ***************************/
|
|
|
|
|
|
|
|
void intr_handler_set(int int_no, intr_handler_t fn, void *arg)
|
|
|
|
{
|
|
|
|
intr_handler_item_t* item = intr_get_item(int_no);
|
|
|
|
|
|
|
|
*item = (intr_handler_item_t) {
|
2020-11-05 23:03:03 -05:00
|
|
|
.handler = fn,
|
|
|
|
.arg = arg
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
intr_handler_t intr_handler_get(int rv_int_num)
|
|
|
|
{
|
2023-08-14 03:44:24 -04:00
|
|
|
const intr_handler_item_t* item = intr_get_item(rv_int_num);
|
|
|
|
return item->handler;
|
2020-11-05 23:03:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void *intr_handler_get_arg(int rv_int_num)
|
|
|
|
{
|
2023-08-14 03:44:24 -04:00
|
|
|
const intr_handler_item_t* item = intr_get_item(rv_int_num);
|
|
|
|
return item->arg;
|
2020-11-05 23:03:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* called from vectors.S */
|
|
|
|
void _global_interrupt_handler(intptr_t sp, int mcause)
|
|
|
|
{
|
2023-08-14 03:44:24 -04:00
|
|
|
/* mcause contains the interrupt number that triggered the current interrupt, this number
|
|
|
|
* also take into account local/internal interrupt, however, this should not happen in practice,
|
|
|
|
* since we never map any peripheral to those. */
|
|
|
|
assert(mcause >= RV_EXTERNAL_INT_OFFSET && "Interrupt sources must not be mapped to local interrupts");
|
|
|
|
const intr_handler_item_t* item = intr_get_item(mcause - RV_EXTERNAL_INT_OFFSET);
|
|
|
|
if (item->handler) {
|
|
|
|
(*item->handler)(item->arg);
|
2020-11-05 23:03:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************** RISC-V interrupt enable/disable ***************************/
|
|
|
|
|
|
|
|
void intr_matrix_route(int intr_src, int intr_num)
|
|
|
|
{
|
2023-08-14 03:44:24 -04:00
|
|
|
assert_valid_rv_int_num(intr_num);
|
|
|
|
|
|
|
|
if (rv_utils_get_core_id() == 0) {
|
|
|
|
REG_WRITE(DR_REG_INTERRUPT_CORE0_BASE + 4 * intr_src, intr_num + RV_EXTERNAL_INT_OFFSET);
|
|
|
|
}
|
|
|
|
#if SOC_CPU_CORES_NUM > 1
|
|
|
|
else {
|
|
|
|
REG_WRITE(DR_REG_INTERRUPT_CORE1_BASE + 4 * intr_src, intr_num + RV_EXTERNAL_INT_OFFSET);
|
|
|
|
}
|
|
|
|
#endif // SOC_CPU_CORES_NUM > 1
|
2020-11-05 23:03:03 -05:00
|
|
|
}
|
|
|
|
|
2023-07-19 04:06:02 -04:00
|
|
|
// CLIC for each interrupt line provides a IE register
|
|
|
|
// this api is not used
|
|
|
|
#if !SOC_INT_CLIC_SUPPORTED
|
2020-11-05 23:03:03 -05:00
|
|
|
uint32_t esprv_intc_get_interrupt_unmask(void)
|
|
|
|
{
|
|
|
|
return REG_READ(INTERRUPT_CORE0_CPU_INT_ENABLE_REG);
|
|
|
|
}
|
2023-07-19 04:06:02 -04:00
|
|
|
#endif
|
2020-11-05 23:03:03 -05:00
|
|
|
|
2022-06-07 02:46:23 -04:00
|
|
|
/*************************** ESP-RV Interrupt Controller ***************************/
|
|
|
|
|
2023-08-14 03:44:24 -04:00
|
|
|
|
2023-07-19 04:06:02 -04:00
|
|
|
#if SOC_INT_CLIC_SUPPORTED
|
2023-08-14 03:44:24 -04:00
|
|
|
|
|
|
|
enum intr_type esprv_intc_int_get_type(int rv_int_num)
|
|
|
|
{
|
|
|
|
uint32_t intr_type_reg = REG_GET_FIELD(CLIC_INT_CTRL_REG(rv_int_num + RV_EXTERNAL_INT_OFFSET), CLIC_INT_ATTR_TRIG);
|
2023-07-19 04:06:02 -04:00
|
|
|
return (intr_type_reg & 1) ? INTR_TYPE_EDGE : INTR_TYPE_LEVEL;
|
2022-06-07 02:46:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int esprv_intc_int_get_priority(int rv_int_num)
|
|
|
|
{
|
2023-08-14 03:44:24 -04:00
|
|
|
uint32_t intr_priority_reg = REG_GET_FIELD(CLIC_INT_CTRL_REG(rv_int_num + RV_EXTERNAL_INT_OFFSET), CLIC_INT_CTL);
|
2023-07-19 04:06:02 -04:00
|
|
|
return (intr_priority_reg >> (8 - NLBITS));
|
2023-08-14 03:44:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool esprv_intc_int_is_vectored(int rv_int_num)
|
|
|
|
{
|
|
|
|
const uint32_t shv = REG_GET_FIELD(CLIC_INT_CTRL_REG(rv_int_num + RV_EXTERNAL_INT_OFFSET), CLIC_INT_ATTR_SHV);
|
|
|
|
return shv != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void esprv_intc_int_set_vectored(int rv_int_num, bool vectored)
|
|
|
|
{
|
|
|
|
REG_SET_FIELD(CLIC_INT_CTRL_REG(rv_int_num + RV_EXTERNAL_INT_OFFSET), CLIC_INT_ATTR_SHV, vectored ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#else // !SOC_INT_CLIC_SUPPORTED
|
|
|
|
|
|
|
|
enum intr_type esprv_intc_int_get_type(int rv_int_num)
|
|
|
|
{
|
|
|
|
uint32_t intr_type_reg = REG_READ(INTERRUPT_CORE0_CPU_INT_TYPE_REG);
|
|
|
|
return (intr_type_reg & (1 << rv_int_num)) ? INTR_TYPE_EDGE : INTR_TYPE_LEVEL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int esprv_intc_int_get_priority(int rv_int_num)
|
|
|
|
{
|
2022-06-07 02:46:23 -04:00
|
|
|
uint32_t intr_priority_reg = REG_READ(INTC_INT_PRIO_REG(rv_int_num));
|
|
|
|
return intr_priority_reg;
|
|
|
|
}
|
|
|
|
|
2023-08-14 03:44:24 -04:00
|
|
|
#endif // SOC_INT_CLIC_SUPPORTED
|
|
|
|
|
2020-11-05 23:03:03 -05:00
|
|
|
/*************************** Exception names. Used in .gdbinit file. ***************************/
|
|
|
|
|
|
|
|
const char *riscv_excp_names[16] __attribute__((used)) = {
|
|
|
|
"misaligned_fetch",
|
|
|
|
"fault_fetch",
|
|
|
|
"illegal_instruction",
|
|
|
|
"breakpoint",
|
|
|
|
"misaligned_load",
|
|
|
|
"fault_load",
|
|
|
|
"misaligned_store",
|
|
|
|
"fault_store",
|
|
|
|
"user_ecall",
|
|
|
|
"supervisor_ecall",
|
|
|
|
"hypervisor_ecall",
|
|
|
|
"machine_ecall",
|
|
|
|
"exec_page_fault",
|
|
|
|
"load_page_fault",
|
|
|
|
"reserved",
|
|
|
|
"store_page_fault"
|
|
|
|
};
|