Loading...
Searching...
No Matches
irq_arch.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2017 Ken Rabold
3 * SPDX-FileCopyrightText: 2020 Inria
4 * SPDX-FileCopyrightText: 2020 Otto-von-Guericke-Universität Magdeburg
5 * SPDX-License-Identifier: LGPL-2.1-only
6 */
7
8#pragma once
9
21
22#include <stdint.h>
23
24#include "irq.h"
25#include "vendor/riscv_csr.h"
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
34#define CPU_CSR_MCAUSE_CAUSE_MSK (0x0fffu)
35
36extern volatile int riscv_in_isr;
37
41static inline __attribute__((always_inline)) unsigned int irq_enable(void)
42{
43 /* Enable all interrupts */
44 unsigned state;
45
46 __asm__ volatile (
47 "csrrs %[dest], mstatus, %[mask]"
48 :[dest] "=r" (state)
49 :[mask] "i" (MSTATUS_MIE)
50 : "memory"
51 );
52 return state;
53}
54
58static inline __attribute__((always_inline)) unsigned int irq_disable(void)
59{
60
61 unsigned int state;
62
63 __asm__ volatile (
64 "csrrc %[dest], mstatus, %[mask]"
65 :[dest] "=r" (state)
66 :[mask] "i" (MSTATUS_MIE)
67 : "memory"
68 );
69
70 return state;
71}
72
76static inline __attribute__((always_inline)) void irq_restore(
77 unsigned int state)
78{
79 /* Restore all interrupts to given state */
80 __asm__ volatile (
81 "csrw mstatus, %[state]"
82 : /* no outputs */
83 :[state] "r" (state)
84 : "memory"
85 );
86}
87
91static inline __attribute__((always_inline)) bool irq_is_in(void)
92{
93 return riscv_in_isr;
94}
95
96static inline __attribute__((always_inline)) bool irq_is_enabled(void)
97{
98 unsigned state;
99
100 __asm__ volatile (
101 "csrr %[dest], mstatus"
102 :[dest] "=r" (state)
103 : /* no inputs */
104 : "memory"
105 );
106 return (state & MSTATUS_MIE);
107}
108
109#ifdef __cplusplus
110}
111#endif
112
MAYBE_INLINE void irq_restore(unsigned state)
This function restores the IRQ disable bit in the status register to the value contained within passe...
MAYBE_INLINE unsigned irq_disable(void)
This function sets the IRQ disable bit in the status register.
MAYBE_INLINE bool irq_is_enabled(void)
Test if IRQs are currently enabled.
MAYBE_INLINE unsigned irq_enable(void)
This function clears the IRQ disable bit in the status register.
MAYBE_INLINE bool irq_is_in(void)
Check whether called from interrupt service routine.
IRQ driver interface.