Loading...
Searching...
No Matches
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
21
22#include <stdint.h>
23
24#include "irq.h"
25#include "vendor/riscv_csr.h"
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
34#define CPU_CSR_MCAUSE_CAUSE_MSK (0x0fffu)
35
41#ifndef CONFIG_PRINT_VERBOSE_TRAP_INFO
42# define CONFIG_PRINT_VERBOSE_TRAP_INFO 0
43#endif
44
45extern volatile int riscv_in_isr;
46
50static inline __attribute__((always_inline)) unsigned int irq_enable(void)
51{
52 /* Enable all interrupts */
53 unsigned state;
54
55 __asm__ volatile (
56 "csrrs %[dest], mstatus, %[mask]"
57 :[dest] "=r" (state)
58 :[mask] "i" (MSTATUS_MIE)
59 : "memory"
60 );
61 return state;
62}
63
67static inline __attribute__((always_inline)) unsigned int irq_disable(void)
68{
69
70 unsigned int state;
71
72 __asm__ volatile (
73 "csrrc %[dest], mstatus, %[mask]"
74 :[dest] "=r" (state)
75 :[mask] "i" (MSTATUS_MIE)
76 : "memory"
77 );
78
79 return state;
80}
81
85static inline __attribute__((always_inline)) void irq_restore(
86 unsigned int state)
87{
88 /* Restore all interrupts to given state */
89 __asm__ volatile (
90 "csrw mstatus, %[state]"
91 : /* no outputs */
92 :[state] "r" (state)
93 : "memory"
94 );
95}
96
100static inline __attribute__((always_inline)) bool irq_is_in(void)
101{
102 return riscv_in_isr;
103}
104
105static inline __attribute__((always_inline)) bool irq_is_enabled(void)
106{
107 unsigned state;
108
109 __asm__ volatile (
110 "csrr %[dest], mstatus"
111 :[dest] "=r" (state)
112 : /* no inputs */
113 : "memory"
114 );
115 return (state & MSTATUS_MIE);
116}
117
118#ifdef __cplusplus
119}
120#endif
121
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.