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
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
32extern "C" {
33#endif
34
38#define CPU_CSR_MCAUSE_CAUSE_MSK (0x0fffu)
39
40extern volatile int riscv_in_isr;
41
45static 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
62static 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
80static 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
95static inline __attribute__((always_inline)) bool irq_is_in(void)
96{
97 return riscv_in_isr;
98}
99
100static 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 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.