2020-02-03 20:20:29 -05:00
|
|
|
#ifndef __IRQ_H
|
|
|
|
#define __IRQ_H
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#define CSR_MSTATUS_MIE 0x8
|
|
|
|
|
|
|
|
#define CSR_IRQ_MASK 0xBC0
|
|
|
|
#define CSR_IRQ_PENDING 0xFC0
|
|
|
|
|
|
|
|
#define CSR_DCACHE_INFO 0xCC0
|
|
|
|
|
|
|
|
#define csrr(reg) ({ unsigned long __tmp; \
|
2021-03-15 09:57:36 -04:00
|
|
|
asm volatile ("csrr %0, " #reg : "=r" (__tmp)); \
|
|
|
|
__tmp; })
|
2020-02-03 20:20:29 -05:00
|
|
|
|
|
|
|
#define csrw(reg, val) ({ \
|
2021-03-15 09:57:36 -04:00
|
|
|
if (__builtin_constant_p(val) && (unsigned long)(val) < 32) \
|
|
|
|
asm volatile ("csrw " #reg ", %0" : : "i" (val)); \
|
|
|
|
else \
|
|
|
|
asm volatile ("csrw " #reg ", %0" : : "r" (val)); })
|
2020-02-03 20:20:29 -05:00
|
|
|
|
|
|
|
#define csrs(reg, bit) ({ \
|
2021-03-15 09:57:36 -04:00
|
|
|
if (__builtin_constant_p(bit) && (unsigned long)(bit) < 32) \
|
|
|
|
asm volatile ("csrrs x0, " #reg ", %0" : : "i" (bit)); \
|
|
|
|
else \
|
|
|
|
asm volatile ("csrrs x0, " #reg ", %0" : : "r" (bit)); })
|
2020-02-03 20:20:29 -05:00
|
|
|
|
|
|
|
#define csrc(reg, bit) ({ \
|
2021-03-15 09:57:36 -04:00
|
|
|
if (__builtin_constant_p(bit) && (unsigned long)(bit) < 32) \
|
|
|
|
asm volatile ("csrrc x0, " #reg ", %0" : : "i" (bit)); \
|
|
|
|
else \
|
|
|
|
asm volatile ("csrrc x0, " #reg ", %0" : : "r" (bit)); })
|
|
|
|
|
|
|
|
static inline unsigned int irq_getie(void) {
|
|
|
|
return (csrr(mstatus) & CSR_MSTATUS_MIE) != 0;
|
2020-02-03 20:20:29 -05:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
static inline void irq_setie(unsigned int ie) {
|
|
|
|
if (ie) {
|
2023-08-14 00:47:22 -04:00
|
|
|
csrs(mstatus, CSR_MSTATUS_MIE);
|
2021-03-15 09:57:36 -04:00
|
|
|
} else {
|
2023-08-14 00:47:22 -04:00
|
|
|
csrc(mstatus, CSR_MSTATUS_MIE);
|
2021-03-15 09:57:36 -04:00
|
|
|
}
|
2020-02-03 20:20:29 -05:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
static inline unsigned int irq_getmask(void) {
|
|
|
|
unsigned int mask;
|
|
|
|
asm volatile ("csrr %0, %1" : "=r" (mask) : "i" (CSR_IRQ_MASK));
|
|
|
|
return mask;
|
2020-02-03 20:20:29 -05:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
static inline void irq_setmask(unsigned int mask) {
|
|
|
|
asm volatile ("csrw %0, %1" : : "i" (CSR_IRQ_MASK), "r" (mask));
|
2020-02-03 20:20:29 -05:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
static inline unsigned int irq_pending(void) {
|
|
|
|
unsigned int pending;
|
|
|
|
asm volatile ("csrr %0, %1" : "=r" (pending) : "i" (CSR_IRQ_PENDING));
|
|
|
|
return pending;
|
2020-02-03 20:20:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-05-30 05:44:13 -04:00
|
|
|
#endif /* __IRQ_H */
|