blob: 9a2312705c234c89b85589cbcd252538cebbf557 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
/* SPDX-License-Identifier: copyleft-next-0.3.1 */
/* Copyright 2021 - 2022, Kim Kuparinen < kimi.h.kuparinen@gmail.com > */
#ifndef KMI_CSR_H
#define KMI_CSR_H
/**
* @file csr.h
* riscv64-specific header for CSR handling.
*/
/** @name satp register memory modes. */
/** @{ */
/** Value of Sv32 mode in \c satp register. */
#define SATP_MODE_Sv32 0x80000000
/** Value of Sv39 mode in \c satp register. */
#define SATP_MODE_Sv39 0x8000000000000000
/** Value of Sv48 mode in \c satp register. */
#define SATP_MODE_Sv48 0x9000000000000000
/** @} */
/** @name Unprivileged CSR registers. */
/** @{ */
/** Address of \c time CSR. */
#define CSR_TIME 0xc01
/** Address of \c timeh CSR. */
#define CSR_TIMEH 0xc81
/** @} */
/** @name Supervisor CSR registers. */
/** @{ */
/** Address of \c sstatus CSR. */
#define CSR_SSTATUS 0x100
/** Address of \c sie CSR. */
#define CSR_SIE 0x104
/** Address of \c stvec CSR. */
#define CSR_STVEC 0x105
/** Address of \c scounteren CSR. */
#define CSR_SCOUNTEREN 0x106
/** Address of \c senvcfg CSR. */
#define CSR_SENVCFG 0x10A
/** Address of \c sscratch CSR. */
#define CSR_SSCRATCH 0x140
/** Address of \c sepc CSR. */
#define CSR_SEPC 0x141
/** Address of \c scause CSR. */
#define CSR_SCAUSE 0x142
/** Address of \c stval CSR. */
#define CSR_STVAL 0x143
/** Address of \c sip CSR. */
#define CSR_SIP 0x144
/** Address of \c satp CSR. */
#define CSR_SATP 0x180
/** Address of \c scontext CSR. */
#define CSR_SCONTEXT 0x5A8
/** @} */
/** @name Exception causes. */
/** @{ */
/** Misaligned instruction. */
#define EXC_INST_MISALIGNED 0
/** Illegal instruction access. */
#define EXC_INST_ACCESS 1
/** Hardware breakpoint. */
#define EXC_BREAKPOINT 3
/** Illegal load address. */
#define EXC_LOAD_ACCESS 5
/** Illegal store address. */
#define EXC_STORE_ACCESS 7
/** System call. */
#define EXC_SYSCALL 8
/** Instruction page fault. */
#define EXC_INST_PAGE_FAULT 12
/** Load page fault. */
#define EXC_LOAD_PAGE_FAULT 13
/** Store page fault. */
#define EXC_STORE_PAGE_FAULT 15
/** @} */
/** @name Status CSR bits. */
/** @{ */
/** \c sstatus \c SUM bit. */
#define SSTATUS_SUM (1 << 18)
/** \c sstatus \c SPP bit. */
#define SSTATUS_SPP (1 << 8)
/** @} */
/**
* Helper macro for compiler vs. assembler string generation.
*
* Directly lifted from Linux:/arch/riscv/include/asm/asm.h:9-13.
*
* @param x Expression to be stringified.
*/
#if defined(__ASSEMBLY__)
#define __ASM_STR(x) x
#else
#define __ASM_STR(x) #x
#endif
/**
* Read from CSR.
*
* @param csr Name of CSR.
* @param res Location to where result should be written.
*/
#define csr_read(csr, res) \
__asm__ volatile ("csrr %0, " __ASM_STR(csr) : "=r" (res) : : "memory")
/**
* Write to CSR.
*
* @param csr Name of CSR.
* @param val Value to be written.
*/
#define csr_write(csr, val) \
__asm__ volatile ("csrw " __ASM_STR(csr) ", %0" : : "r" (val) : "memory")
/**
* Set bits in CSR.
*
* @param csr Name of CSR.
* @param val Mask of bits to set.
*/
#define csr_set(csr, val) \
__asm__ volatile ("csrs " __ASM_STR(csr) ", %0" : : "r" (val) : "memory")
/**
* Clear bits in CSR.
*
* @param csr Name of CSR.
* @param val Mask of bits to clear.
*/
#define csr_clear(csr, val) \
__asm__ volatile ("csrc " __ASM_STR(csr) ", %0" : : "r" (val) : "memory")
#endif /* KMI_CSR_H */
|