sysreg.h revision 1.32 1 1.32 riastrad /* $NetBSD: sysreg.h,v 1.32 2024/05/14 15:16:51 riastradh Exp $ */
2 1.4 maxv
3 1.4 maxv /*
4 1.1 matt * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 1.1 matt * All rights reserved.
6 1.1 matt *
7 1.1 matt * This code is derived from software contributed to The NetBSD Foundation
8 1.1 matt * by Matt Thomas of 3am Software Foundry.
9 1.1 matt *
10 1.1 matt * Redistribution and use in source and binary forms, with or without
11 1.1 matt * modification, are permitted provided that the following conditions
12 1.1 matt * are met:
13 1.1 matt * 1. Redistributions of source code must retain the above copyright
14 1.1 matt * notice, this list of conditions and the following disclaimer.
15 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 matt * notice, this list of conditions and the following disclaimer in the
17 1.1 matt * documentation and/or other materials provided with the distribution.
18 1.1 matt *
19 1.1 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 matt * POSSIBILITY OF SUCH DAMAGE.
30 1.1 matt */
31 1.1 matt
32 1.1 matt #ifndef _RISCV_SYSREG_H_
33 1.15 simonb #define _RISCV_SYSREG_H_
34 1.1 matt
35 1.1 matt #ifndef _KERNEL
36 1.1 matt #include <sys/param.h>
37 1.1 matt #endif
38 1.1 matt
39 1.13 skrll #include <riscv/reg.h>
40 1.13 skrll
41 1.15 simonb #define FCSR_FMASK 0 // no exception bits
42 1.27 skrll #define FCSR_FRM __BITS(7, 5)
43 1.15 simonb #define FCSR_FRM_RNE 0b000 // Round Nearest, ties to Even
44 1.15 simonb #define FCSR_FRM_RTZ 0b001 // Round Towards Zero
45 1.15 simonb #define FCSR_FRM_RDN 0b010 // Round DowN (-infinity)
46 1.15 simonb #define FCSR_FRM_RUP 0b011 // Round UP (+infinity)
47 1.15 simonb #define FCSR_FRM_RMM 0b100 // Round to nearest, ties to Max Magnitude
48 1.15 simonb #define FCSR_FRM_DYN 0b111 // Dynamic rounding
49 1.27 skrll #define FCSR_FFLAGS __BITS(4, 0) // Sticky bits
50 1.15 simonb #define FCSR_NV __BIT(4) // iNValid operation
51 1.15 simonb #define FCSR_DZ __BIT(3) // Divide by Zero
52 1.15 simonb #define FCSR_OF __BIT(2) // OverFlow
53 1.15 simonb #define FCSR_UF __BIT(1) // UnderFlow
54 1.15 simonb #define FCSR_NX __BIT(0) // iNeXact
55 1.1 matt
56 1.1 matt static inline uint32_t
57 1.29 skrll fcsr_read(void)
58 1.1 matt {
59 1.1 matt uint32_t __fcsr;
60 1.29 skrll asm volatile("frcsr %0" : "=r"(__fcsr) :: "memory");
61 1.1 matt return __fcsr;
62 1.1 matt }
63 1.1 matt
64 1.1 matt static inline uint32_t
65 1.29 skrll fcsr_write(uint32_t __new)
66 1.1 matt {
67 1.1 matt uint32_t __old;
68 1.29 skrll asm volatile("fscsr %0, %1" : "=r"(__old) : "r"(__new) : "memory");
69 1.1 matt return __old;
70 1.1 matt }
71 1.1 matt
72 1.1 matt static inline uint32_t
73 1.29 skrll fcsr_fflags_read(void)
74 1.1 matt {
75 1.1 matt uint32_t __old;
76 1.29 skrll asm volatile("frflags %0" : "=r"(__old) :: "memory");
77 1.32 riastrad return __old;
78 1.1 matt }
79 1.1 matt
80 1.1 matt static inline uint32_t
81 1.29 skrll fcsr_fflags_write(uint32_t __new)
82 1.1 matt {
83 1.1 matt uint32_t __old;
84 1.29 skrll asm volatile("fsflags %0, %1" : "=r"(__old) : "r"(__new) : "memory");
85 1.32 riastrad return __old;
86 1.1 matt }
87 1.1 matt
88 1.1 matt static inline uint32_t
89 1.29 skrll fcsr_frm_read(void)
90 1.1 matt {
91 1.1 matt uint32_t __old;
92 1.29 skrll asm volatile("frrm\t%0" : "=r"(__old) :: "memory");
93 1.32 riastrad return __old;
94 1.1 matt }
95 1.1 matt
96 1.1 matt static inline uint32_t
97 1.29 skrll fcsr_frm_write(uint32_t __new)
98 1.1 matt {
99 1.1 matt uint32_t __old;
100 1.29 skrll asm volatile("fsrm\t%0, %1" : "=r"(__old) : "r"(__new) : "memory");
101 1.32 riastrad return __old;
102 1.1 matt }
103 1.1 matt
104 1.25 simonb #define RISCVREG_READ_INLINE(regname) \
105 1.25 simonb static inline uintptr_t \
106 1.25 simonb csr_##regname##_read(void) \
107 1.25 simonb { \
108 1.25 simonb uintptr_t __rv; \
109 1.25 simonb asm volatile("csrr %0, " #regname : "=r"(__rv) :: "memory"); \
110 1.25 simonb return __rv; \
111 1.25 simonb }
112 1.25 simonb
113 1.25 simonb #define RISCVREG_WRITE_INLINE(regname) \
114 1.25 simonb static inline void \
115 1.25 simonb csr_##regname##_write(uintptr_t __val) \
116 1.25 simonb { \
117 1.25 simonb asm volatile("csrw " #regname ", %0" :: "r"(__val) : "memory"); \
118 1.25 simonb }
119 1.25 simonb
120 1.25 simonb #define RISCVREG_SET_INLINE(regname) \
121 1.25 simonb static inline void \
122 1.25 simonb csr_##regname##_set(uintptr_t __mask) \
123 1.25 simonb { \
124 1.25 simonb if (__builtin_constant_p(__mask) && __mask < 0x20) { \
125 1.25 simonb asm volatile("csrsi " #regname ", %0" :: "i"(__mask) : \
126 1.25 simonb "memory"); \
127 1.25 simonb } else { \
128 1.25 simonb asm volatile("csrs " #regname ", %0" :: "r"(__mask) : \
129 1.25 simonb "memory"); \
130 1.25 simonb } \
131 1.25 simonb }
132 1.25 simonb
133 1.25 simonb #define RISCVREG_CLEAR_INLINE(regname) \
134 1.25 simonb static inline void \
135 1.25 simonb csr_##regname##_clear(uintptr_t __mask) \
136 1.25 simonb { \
137 1.25 simonb if (__builtin_constant_p(__mask) && __mask < 0x20) { \
138 1.25 simonb asm volatile("csrci " #regname ", %0" :: "i"(__mask) : \
139 1.25 simonb "memory"); \
140 1.25 simonb } else { \
141 1.25 simonb asm volatile("csrc " #regname ", %0" :: "r"(__mask) : \
142 1.25 simonb "memory"); \
143 1.25 simonb } \
144 1.25 simonb }
145 1.25 simonb
146 1.25 simonb #define RISCVREG_READ_WRITE_INLINE(regname) \
147 1.25 simonb RISCVREG_READ_INLINE(regname) \
148 1.25 simonb RISCVREG_WRITE_INLINE(regname)
149 1.25 simonb #define RISCVREG_SET_CLEAR_INLINE(regname) \
150 1.25 simonb RISCVREG_SET_INLINE(regname) \
151 1.25 simonb RISCVREG_CLEAR_INLINE(regname)
152 1.25 simonb #define RISCVREG_READ_SET_CLEAR_INLINE(regname) \
153 1.25 simonb RISCVREG_READ_INLINE(regname) \
154 1.25 simonb RISCVREG_SET_CLEAR_INLINE(regname)
155 1.25 simonb #define RISCVREG_READ_WRITE_SET_CLEAR_INLINE(regname) \
156 1.25 simonb RISCVREG_READ_WRITE_INLINE(regname) \
157 1.25 simonb RISCVREG_SET_CLEAR_INLINE(regname)
158 1.25 simonb
159 1.9 skrll /* Supervisor Status Register */
160 1.25 simonb RISCVREG_READ_SET_CLEAR_INLINE(sstatus) // supervisor status register
161 1.9 skrll #ifdef _LP64
162 1.27 skrll #define SR_WPRI __BITS(62, 34) | __BITS(31, 20) | \
163 1.27 skrll __BIT(17) | __BITS(12, 11) | __BIT(7) | __BITS(4, 2) | \
164 1.27 skrll __BIT(0)
165 1.26 simonb #define SR_SD __BIT(63) // any of FS or VS or XS dirty
166 1.9 skrll /* Bits 62-34 are WPRI */
167 1.27 skrll #define SR_UXL __BITS(33, 32) // U-mode XLEN
168 1.26 simonb #define SR_UXL_32 1 // XLEN == 32
169 1.26 simonb #define SR_UXL_64 2 // XLEN == 64
170 1.26 simonb #define SR_UXL_128 3 // XLEN == 128
171 1.9 skrll /* Bits 31-20 are WPRI*/
172 1.9 skrll #else
173 1.27 skrll #define SR_WPRI __BITS(30, 20) | \
174 1.27 skrll __BIT(17) | __BITS(12, 11) | __BIT(7) | __BITS(4, 2) | \
175 1.27 skrll __BIT(0)
176 1.26 simonb #define SR_SD __BIT(31) // any of FS or VS or XS dirty
177 1.9 skrll /* Bits 30-20 are WPRI*/
178 1.9 skrll #endif /* _LP64 */
179 1.9 skrll
180 1.9 skrll /* Both RV32 and RV64 have the bottom 20 bits shared */
181 1.26 simonb #define SR_MXR __BIT(19) // Make eXecutable Readable
182 1.26 simonb #define SR_SUM __BIT(18) // permit Supervisor User Memory access
183 1.9 skrll /* Bit 17 is WPRI */
184 1.27 skrll #define SR_XS __BITS(16, 15) // Vector extension state
185 1.26 simonb #define SR_XS_OFF 0 // All off
186 1.26 simonb #define SR_XS_SOME_ON 1 // None dirty or clean, some on
187 1.26 simonb #define SR_XS_SOME_CLEAN 2 // None dirty, some clean
188 1.26 simonb #define SR_XS_SOME_DIRTY 3 // Some dirty
189 1.27 skrll #define SR_FS __BITS(14, 13) // Floating-point unit state
190 1.26 simonb #define SR_FS_OFF 0 // Off
191 1.26 simonb #define SR_FS_INITIAL 1 // Initial
192 1.26 simonb #define SR_FS_CLEAN 2 // Clean
193 1.26 simonb #define SR_FS_DIRTY 3 // Dirty
194 1.26 simonb /* Bits 12-11 are WPRI */
195 1.31 andvar #define SR_VS __BITS(10, 9) // User-mode extension state
196 1.26 simonb #define SR_VS_OFF SR_FS_OFF // Off
197 1.26 simonb #define SR_VS_INITIAL SR_FS_INITIAL // Initial
198 1.26 simonb #define SR_VS_CLEAN SR_FS_CLEAN // Clean
199 1.26 simonb #define SR_VS_DIRTY SR_FS_DIRTY // Dirty
200 1.26 simonb #define SR_SPP __BIT(8) // Priv level before supervisor mode
201 1.26 simonb /* Bit 7 is WPRI */
202 1.26 simonb #define SR_UBE __BIT(6) // User-mode endianness
203 1.26 simonb #define SR_SPIE __BIT(5) // S-Mode interrupts enabled before trap
204 1.26 simonb /* Bits 4-2 are WPRI */
205 1.26 simonb #define SR_SIE __BIT(1) // Supervisor mode interrupt enable
206 1.26 simonb /* Bit 0 is WPRI */
207 1.9 skrll
208 1.9 skrll /* Supervisor interrupt registers */
209 1.11 christos /* ... interrupt pending register (sip) */
210 1.25 simonb RISCVREG_READ_SET_CLEAR_INLINE(sip) // supervisor interrupt pending
211 1.19 skrll /* Bit (XLEN-1) - 10 is WIRI */
212 1.26 simonb #define SIP_SEIP __BIT(9) // S-mode interrupt pending
213 1.26 simonb /* Bit 8-6 is WIRI */
214 1.26 simonb #define SIP_STIP __BIT(5) // S-mode timer interrupt pending
215 1.26 simonb /* Bit 4-2 is WIRI */
216 1.26 simonb #define SIP_SSIP __BIT(1) // S-mode software interrupt pending
217 1.26 simonb /* Bit 0 is WIRI */
218 1.9 skrll
219 1.11 christos /* ... interrupt-enable register (sie) */
220 1.25 simonb RISCVREG_READ_SET_CLEAR_INLINE(sie) // supervisor interrupt enable
221 1.9 skrll /* Bit (XLEN-1) - 10 is WIRI */
222 1.26 simonb #define SIE_SEIE __BIT(9) // S-mode interrupt enable
223 1.26 simonb /* Bit 8-6 is WIRI */
224 1.26 simonb #define SIE_STIE __BIT(5) // S-mode timer interrupt enable
225 1.26 simonb /* Bit 4-2 is WIRI */
226 1.26 simonb #define SIE_SSIE __BIT(1) // S-mode software interrupt enable
227 1.26 simonb /* Bit 0 is WIRI */
228 1.9 skrll
229 1.29 skrll // U-mode sstatus values
230 1.1 matt #ifdef _LP64
231 1.29 skrll #define SR_USER64 (SR_SPIE | __SHIFTIN(SR_UXL_64, SR_UXL))
232 1.29 skrll #define SR_USER32 (SR_SPIE | __SHIFTIN(SR_UXL_32, SR_UXL))
233 1.1 matt #else
234 1.29 skrll #define SR_USER (SR_SPIE)
235 1.1 matt #endif
236 1.1 matt
237 1.1 matt // Cause register
238 1.27 skrll #define CAUSE_INTERRUPT_P(cause) ((cause) & __BIT(XLEN - 1))
239 1.27 skrll #define CAUSE_CODE(cause) ((cause) & __BITS(XLEN - 2, 0))
240 1.20 simonb
241 1.20 simonb // Cause register - exceptions
242 1.15 simonb #define CAUSE_FETCH_MISALIGNED 0
243 1.15 simonb #define CAUSE_FETCH_ACCESS 1
244 1.15 simonb #define CAUSE_ILLEGAL_INSTRUCTION 2
245 1.15 simonb #define CAUSE_BREAKPOINT 3
246 1.15 simonb #define CAUSE_LOAD_MISALIGNED 4
247 1.15 simonb #define CAUSE_LOAD_ACCESS 5
248 1.15 simonb #define CAUSE_STORE_MISALIGNED 6
249 1.15 simonb #define CAUSE_STORE_ACCESS 7
250 1.15 simonb #define CAUSE_USER_ECALL 8
251 1.20 simonb #define CAUSE_SYSCALL CAUSE_USER_ECALL /* convenience alias */
252 1.15 simonb #define CAUSE_SUPERVISOR_ECALL 9
253 1.6 skrll /* 10 is reserved */
254 1.15 simonb #define CAUSE_MACHINE_ECALL 11
255 1.15 simonb #define CAUSE_FETCH_PAGE_FAULT 12
256 1.15 simonb #define CAUSE_LOAD_PAGE_FAULT 13
257 1.6 skrll /* 14 is Reserved */
258 1.15 simonb #define CAUSE_STORE_PAGE_FAULT 15
259 1.20 simonb /* >= 16 is reserved/custom */
260 1.20 simonb
261 1.24 skrll // Cause register - interrupts
262 1.29 skrll #define IRQ_SUPERVISOR_SOFTWARE 1
263 1.29 skrll #define IRQ_VIRTUAL_SUPERVISOR_SOFTWARE 2
264 1.29 skrll #define IRQ_MACHINE_SOFTWARE 3
265 1.29 skrll #define IRQ_SUPERVISOR_TIMER 5
266 1.29 skrll #define IRQ_VIRTUAL_SUPERVISOR_TIMER 6
267 1.29 skrll #define IRQ_MACHINE_TIMER 7
268 1.29 skrll #define IRQ_SUPERVISOR_EXTERNAL 9
269 1.29 skrll #define IRQ_VIRTUAL_SUPERVISOR_EXTERNAL 10
270 1.29 skrll #define IRQ_MACHINE_EXTERNAL 11
271 1.29 skrll #define IRQ_SUPERVISOR_GUEST_EXTERNAL 12
272 1.29 skrll #define IRQ_NSOURCES 16
273 1.1 matt
274 1.25 simonb RISCVREG_READ_INLINE(time)
275 1.25 simonb #ifdef _LP64
276 1.25 simonb RISCVREG_READ_INLINE(cycle)
277 1.25 simonb #else /* !_LP64 */
278 1.1 matt static inline uint64_t
279 1.25 simonb csr_cycle_read(void)
280 1.1 matt {
281 1.1 matt uint32_t __hi0, __hi1, __lo0;
282 1.1 matt do {
283 1.29 skrll asm volatile(
284 1.5 skrll "csrr\t%[__hi0], cycleh"
285 1.1 matt "\n\t" "csrr\t%[__lo0], cycle"
286 1.1 matt "\n\t" "csrr\t%[__hi1], cycleh"
287 1.1 matt : [__hi0] "=r"(__hi0),
288 1.1 matt [__lo0] "=r"(__lo0),
289 1.1 matt [__hi1] "=r"(__hi1));
290 1.1 matt } while (__hi0 != __hi1);
291 1.27 skrll return
292 1.27 skrll __SHIFTIN(__hi0, __BITS(63, 32)) |
293 1.27 skrll __SHIFTIN(__lo0, __BITS(31, 0));
294 1.1 matt }
295 1.25 simonb #endif /* !_LP64 */
296 1.1 matt
297 1.4 maxv #ifdef _LP64
298 1.27 skrll #define SATP_MODE __BITS(63, 60) // Translation mode
299 1.26 simonb #define SATP_MODE_BARE 0 // No translation or protection
300 1.26 simonb /* modes 1-7 reserved for standard use */
301 1.26 simonb #define SATP_MODE_SV39 8 // Page-based 39-bit virt addr
302 1.26 simonb #define SATP_MODE_SV48 9 // Page-based 48-bit virt addr
303 1.26 simonb #define SATP_MODE_SV57 10 // Page-based 57-bit virt addr
304 1.26 simonb #define SATP_MODE_SV64 11 // Page-based 64-bit virt addr
305 1.26 simonb /* modes 12-13 reserved for standard use */
306 1.26 simonb /* modes 14-15 designated for custom use */
307 1.27 skrll #define SATP_ASID __BITS(59, 44) // Address Space Identifier
308 1.27 skrll #define SATP_PPN __BITS(43, 0) // Physical Page Number
309 1.4 maxv #else
310 1.26 simonb #define SATP_MODE __BIT(31) // Translation mode
311 1.26 simonb #define SATP_MODE_BARE 0 // No translation or protection
312 1.26 simonb #define SATP_MODE_SV32 1 // Page-based 32-bit virt addr
313 1.27 skrll #define SATP_ASID __BITS(30, 22) // Address Space Identifier
314 1.27 skrll #define SATP_PPN __BITS(21, 0) // Physical Page Number
315 1.4 maxv #endif
316 1.2 matt
317 1.25 simonb RISCVREG_READ_WRITE_INLINE(satp)
318 1.13 skrll
319 1.25 simonb /* Fake "ASID" CSR (a field of SATP register) functions */
320 1.2 matt static inline uint32_t
321 1.25 simonb csr_asid_read(void)
322 1.2 matt {
323 1.25 simonb uintptr_t satp = csr_satp_read();
324 1.4 maxv return __SHIFTOUT(satp, SATP_ASID);
325 1.2 matt }
326 1.2 matt
327 1.2 matt static inline void
328 1.25 simonb csr_asid_write(uint32_t asid)
329 1.2 matt {
330 1.27 skrll uintptr_t satp = csr_satp_read();
331 1.4 maxv satp &= ~SATP_ASID;
332 1.14 skrll satp |= __SHIFTIN(asid, SATP_ASID);
333 1.25 simonb csr_satp_write(satp);
334 1.2 matt }
335 1.2 matt
336 1.1 matt #endif /* _RISCV_SYSREG_H_ */
337