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 
22 #include <stdint.h>
23 
24 #include "irq.h"
25 #include "vendor/riscv_csr.h"
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
34 #define CPU_CSR_MCAUSE_CAUSE_MSK (0x0fffu)
35 
36 extern volatile int riscv_in_isr;
37 
41 static 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 
58 static 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 
76 static 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 
91 static inline __attribute__((always_inline)) bool irq_is_in(void)
92 {
93  return riscv_in_isr;
94 }
95 
96 static 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 bool irq_is_enabled(void)
Test if IRQs are currently enabled.
IRQ driver interface.
static void irq_restore(unsigned int state)
Restore the state of the IRQ flags.
Definition: irq_arch.h:76
static unsigned int irq_disable(void)
Disable all maskable interrupts.
Definition: irq_arch.h:58
static bool irq_is_in(void)
See if the current context is inside an ISR.
Definition: irq_arch.h:91
static unsigned int irq_enable(void)
Enable all maskable interrupts.
Definition: irq_arch.h:41