Loading...
Searching...
No Matches
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
24
25#include <stdint.h>
26
27#include "irq.h"
28#include "vendor/riscv_csr.h"
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
37#define CPU_CSR_MCAUSE_CAUSE_MSK (0x0fffu)
38
39extern volatile int riscv_in_isr;
40
44static 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
61static 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
79static 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
94static inline __attribute__((always_inline)) bool irq_is_in(void)
95{
96 return riscv_in_isr;
97}
98
99static 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 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.