2020-06-25 16:51:18 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 Ken Rabold
|
|
|
|
* 2020 Inria
|
|
|
|
* 2020 Otto-von-Guericke-Universität Magdeburg
|
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser General
|
|
|
|
* Public License v2.1. See the file LICENSE in the top level directory for more
|
|
|
|
* details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2020-09-09 21:37:15 +02:00
|
|
|
* @ingroup cpu_riscv_common
|
2020-06-25 16:51:18 +02:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief Implementation of the kernels irq interface
|
|
|
|
*
|
|
|
|
* @author Ken Rabold
|
|
|
|
* @author Alexandre Abadie <alexandre.abadie@inria.fr>
|
|
|
|
* @author Marian Buschsieweke <marian.buschsieweke@ovgu.de>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef IRQ_ARCH_H
|
|
|
|
#define IRQ_ARCH_H
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2020-09-09 21:37:15 +02:00
|
|
|
#include "irq.h"
|
|
|
|
#include "vendor/riscv_csr.h"
|
2020-06-25 16:51:18 +02:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2021-02-17 13:46:05 +01:00
|
|
|
/**
|
2021-08-14 14:48:25 +02:00
|
|
|
* @brief Bit mask for the MCAUSE register
|
|
|
|
*/
|
2021-02-17 13:46:05 +01:00
|
|
|
#define CPU_CSR_MCAUSE_CAUSE_MSK (0x0fffu)
|
|
|
|
|
2020-09-09 21:37:15 +02:00
|
|
|
extern volatile int riscv_in_isr;
|
2020-06-25 16:51:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Enable all maskable interrupts
|
|
|
|
*/
|
|
|
|
static inline __attribute__((always_inline)) unsigned int irq_enable(void)
|
|
|
|
{
|
|
|
|
/* Enable all interrupts */
|
|
|
|
unsigned state;
|
2021-01-15 11:53:28 +01:00
|
|
|
|
2020-06-25 16:51:18 +02:00
|
|
|
__asm__ volatile (
|
|
|
|
"csrrs %[dest], mstatus, %[mask]"
|
2021-01-15 11:53:28 +01:00
|
|
|
:[dest] "=r" (state)
|
|
|
|
:[mask] "i" (MSTATUS_MIE)
|
2020-06-25 16:51:18 +02:00
|
|
|
: "memory"
|
2021-01-15 11:53:28 +01:00
|
|
|
);
|
2020-06-25 16:51:18 +02:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Disable all maskable interrupts
|
|
|
|
*/
|
|
|
|
static inline __attribute__((always_inline)) unsigned int irq_disable(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
unsigned int state;
|
2021-01-15 11:53:28 +01:00
|
|
|
|
2020-06-25 16:51:18 +02:00
|
|
|
__asm__ volatile (
|
|
|
|
"csrrc %[dest], mstatus, %[mask]"
|
2021-01-15 11:53:28 +01:00
|
|
|
:[dest] "=r" (state)
|
|
|
|
:[mask] "i" (MSTATUS_MIE)
|
2020-06-25 16:51:18 +02:00
|
|
|
: "memory"
|
2021-01-15 11:53:28 +01:00
|
|
|
);
|
2020-06-25 16:51:18 +02:00
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Restore the state of the IRQ flags
|
|
|
|
*/
|
2021-01-15 11:53:28 +01:00
|
|
|
static inline __attribute__((always_inline)) void irq_restore(
|
|
|
|
unsigned int state)
|
2020-06-25 16:51:18 +02:00
|
|
|
{
|
|
|
|
/* Restore all interrupts to given state */
|
|
|
|
__asm__ volatile (
|
|
|
|
"csrw mstatus, %[state]"
|
|
|
|
: /* no outputs */
|
2021-01-15 11:53:28 +01:00
|
|
|
:[state] "r" (state)
|
2020-06-25 16:51:18 +02:00
|
|
|
: "memory"
|
2021-01-15 11:53:28 +01:00
|
|
|
);
|
2020-06-25 16:51:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief See if the current context is inside an ISR
|
|
|
|
*/
|
2021-12-08 15:53:15 +01:00
|
|
|
static inline __attribute__((always_inline)) bool irq_is_in(void)
|
2020-06-25 16:51:18 +02:00
|
|
|
{
|
2020-09-09 21:37:15 +02:00
|
|
|
return riscv_in_isr;
|
2020-06-25 16:51:18 +02:00
|
|
|
}
|
|
|
|
|
2021-12-08 15:53:15 +01:00
|
|
|
static inline __attribute__((always_inline)) bool irq_is_enabled(void)
|
2021-06-29 11:36:38 +02:00
|
|
|
{
|
|
|
|
unsigned state;
|
2021-08-14 14:48:25 +02:00
|
|
|
|
2021-06-29 11:36:38 +02:00
|
|
|
__asm__ volatile (
|
|
|
|
"csrr %[dest], mstatus"
|
|
|
|
:[dest] "=r" (state)
|
|
|
|
: /* no inputs */
|
|
|
|
: "memory"
|
|
|
|
);
|
|
|
|
return (state & MSTATUS_MIE);
|
|
|
|
}
|
|
|
|
|
2020-06-25 16:51:18 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* IRQ_ARCH_H */
|
|
|
|
/** @} */
|