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 
11 #pragma once
12 
25 #include <stdint.h>
26 
27 #include "irq.h"
28 #include "vendor/riscv_csr.h"
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
37 #define CPU_CSR_MCAUSE_CAUSE_MSK (0x0fffu)
38 
39 extern volatile int riscv_in_isr;
40 
44 static inline __attribute__((always_inline)) unsigned int irq_enable(void)
45 {
46  /* Enable all interrupts */
47  unsigned state;
48 
49  __asm__ volatile (
50  "csrrs %[dest], mstatus, %[mask]"
51  :[dest] "=r" (state)
52  :[mask] "i" (MSTATUS_MIE)
53  : "memory"
54  );
55  return state;
56 }
57 
61 static inline __attribute__((always_inline)) unsigned int irq_disable(void)
62 {
63 
64  unsigned int state;
65 
66  __asm__ volatile (
67  "csrrc %[dest], mstatus, %[mask]"
68  :[dest] "=r" (state)
69  :[mask] "i" (MSTATUS_MIE)
70  : "memory"
71  );
72 
73  return state;
74 }
75 
79 static inline __attribute__((always_inline)) void irq_restore(
80  unsigned int state)
81 {
82  /* Restore all interrupts to given state */
83  __asm__ volatile (
84  "csrw mstatus, %[state]"
85  : /* no outputs */
86  :[state] "r" (state)
87  : "memory"
88  );
89 }
90 
94 static inline __attribute__((always_inline)) bool irq_is_in(void)
95 {
96  return riscv_in_isr;
97 }
98 
99 static inline __attribute__((always_inline)) bool irq_is_enabled(void)
100 {
101  unsigned state;
102 
103  __asm__ volatile (
104  "csrr %[dest], mstatus"
105  :[dest] "=r" (state)
106  : /* no inputs */
107  : "memory"
108  );
109  return (state & MSTATUS_MIE);
110 }
111 
112 #ifdef __cplusplus
113 }
114 #endif
115 
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:79
static unsigned int irq_disable(void)
Disable all maskable interrupts.
Definition: irq_arch.h:61
static bool irq_is_in(void)
See if the current context is inside an ISR.
Definition: irq_arch.h:94
static unsigned int irq_enable(void)
Enable all maskable interrupts.
Definition: irq_arch.h:44