irq_arch.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 Ken Rabold
3  * 2020 Inria
4  * 2020 Otto-von-Guericke-Universität Magdeburg
5  *
6  * This file is subject to the terms and conditions of the GNU Lesser General
7  * Public License v2.1. See the file LICENSE in the top level directory for more
8  * details.
9  */
10 
23 #ifndef IRQ_ARCH_H
24 #define IRQ_ARCH_H
25 
26 #include <stdint.h>
27 
28 #include "irq.h"
29 #include "vendor/riscv_csr.h"
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
38 #define CPU_CSR_MCAUSE_CAUSE_MSK (0x0fffu)
39 
40 extern volatile int riscv_in_isr;
41 
45 static inline __attribute__((always_inline)) unsigned int irq_enable(void)
46 {
47  /* Enable all interrupts */
48  unsigned state;
49 
50  __asm__ volatile (
51  "csrrs %[dest], mstatus, %[mask]"
52  :[dest] "=r" (state)
53  :[mask] "i" (MSTATUS_MIE)
54  : "memory"
55  );
56  return state;
57 }
58 
62 static inline __attribute__((always_inline)) unsigned int irq_disable(void)
63 {
64 
65  unsigned int state;
66 
67  __asm__ volatile (
68  "csrrc %[dest], mstatus, %[mask]"
69  :[dest] "=r" (state)
70  :[mask] "i" (MSTATUS_MIE)
71  : "memory"
72  );
73 
74  return state;
75 }
76 
80 static inline __attribute__((always_inline)) void irq_restore(
81  unsigned int state)
82 {
83  /* Restore all interrupts to given state */
84  __asm__ volatile (
85  "csrw mstatus, %[state]"
86  : /* no outputs */
87  :[state] "r" (state)
88  : "memory"
89  );
90 }
91 
95 static inline __attribute__((always_inline)) bool irq_is_in(void)
96 {
97  return riscv_in_isr;
98 }
99 
100 static inline __attribute__((always_inline)) bool irq_is_enabled(void)
101 {
102  unsigned state;
103 
104  __asm__ volatile (
105  "csrr %[dest], mstatus"
106  :[dest] "=r" (state)
107  : /* no inputs */
108  : "memory"
109  );
110  return (state & MSTATUS_MIE);
111 }
112 
113 #ifdef __cplusplus
114 }
115 #endif
116 
117 #endif /* IRQ_ARCH_H */
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:80
static unsigned int irq_disable(void)
Disable all maskable interrupts.
Definition: irq_arch.h:62
static bool irq_is_in(void)
See if the current context is inside an ISR.
Definition: irq_arch.h:95
static unsigned int irq_enable(void)
Enable all maskable interrupts.
Definition: irq_arch.h:45