Zephyr Project API 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
csr.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Michael Schaffner
3 * Copyright (c) 2020 BayLibre, SAS
4 *
5 * SPDX-License-Identifier: SHL-0.51
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9#ifndef CSR_H_
10#define CSR_H_
11
12#define MSTATUS_UIE 0x00000001
13#define MSTATUS_SIE 0x00000002
14#define MSTATUS_HIE 0x00000004
15#define MSTATUS_MIE 0x00000008
16#define MSTATUS_UPIE 0x00000010
17#define MSTATUS_SPIE 0x00000020
18#define MSTATUS_HPIE 0x00000040
19#define MSTATUS_MPIE 0x00000080
20#define MSTATUS_SPP 0x00000100
21#define MSTATUS_HPP 0x00000600
22#define MSTATUS_MPP 0x00001800
23#define MSTATUS_FS 0x00006000
24#define MSTATUS_XS 0x00018000
25#define MSTATUS_MPRV 0x00020000
26#define MSTATUS_SUM 0x00040000
27#define MSTATUS_MXR 0x00080000
28#define MSTATUS_TVM 0x00100000
29#define MSTATUS_TW 0x00200000
30#define MSTATUS_TSR 0x00400000
31#define MSTATUS32_SD 0x80000000
32#define MSTATUS_UXL 0x0000000300000000
33#define MSTATUS_SXL 0x0000000C00000000
34#define MSTATUS64_SD 0x8000000000000000
35
36#define SSTATUS_UIE 0x00000001
37#define SSTATUS_SIE 0x00000002
38#define SSTATUS_UPIE 0x00000010
39#define SSTATUS_SPIE 0x00000020
40#define SSTATUS_SPP 0x00000100
41#define SSTATUS_FS 0x00006000
42#define SSTATUS_XS 0x00018000
43#define SSTATUS_SUM 0x00040000
44#define SSTATUS_MXR 0x00080000
45#define SSTATUS32_SD 0x80000000
46#define SSTATUS_UXL 0x0000000300000000
47#define SSTATUS64_SD 0x8000000000000000
48
49#define DCSR_XDEBUGVER (3U<<30)
50#define DCSR_NDRESET (1<<29)
51#define DCSR_FULLRESET (1<<28)
52#define DCSR_EBREAKM (1<<15)
53#define DCSR_EBREAKH (1<<14)
54#define DCSR_EBREAKS (1<<13)
55#define DCSR_EBREAKU (1<<12)
56#define DCSR_STOPCYCLE (1<<10)
57#define DCSR_STOPTIME (1<<9)
58#define DCSR_CAUSE (7<<6)
59#define DCSR_DEBUGINT (1<<5)
60#define DCSR_HALT (1<<3)
61#define DCSR_STEP (1<<2)
62#define DCSR_PRV (3<<0)
63
64#define DCSR_CAUSE_NONE 0
65#define DCSR_CAUSE_SWBP 1
66#define DCSR_CAUSE_HWBP 2
67#define DCSR_CAUSE_DEBUGINT 3
68#define DCSR_CAUSE_STEP 4
69#define DCSR_CAUSE_HALT 5
70
71#define MCONTROL_TYPE(xlen) (0xfULL<<((xlen)-4))
72#define MCONTROL_DMODE(xlen) (1ULL<<((xlen)-5))
73#define MCONTROL_MASKMAX(xlen) (0x3fULL<<((xlen)-11))
74
75#define MCONTROL_SELECT (1<<19)
76#define MCONTROL_TIMING (1<<18)
77#define MCONTROL_ACTION (0x3f<<12)
78#define MCONTROL_CHAIN (1<<11)
79#define MCONTROL_MATCH (0xf<<7)
80#define MCONTROL_M (1<<6)
81#define MCONTROL_H (1<<5)
82#define MCONTROL_S (1<<4)
83#define MCONTROL_U (1<<3)
84#define MCONTROL_EXECUTE (1<<2)
85#define MCONTROL_STORE (1<<1)
86#define MCONTROL_LOAD (1<<0)
87
88#define MCONTROL_TYPE_NONE 0
89#define MCONTROL_TYPE_MATCH 2
90
91#define MCONTROL_ACTION_DEBUG_EXCEPTION 0
92#define MCONTROL_ACTION_DEBUG_MODE 1
93#define MCONTROL_ACTION_TRACE_START 2
94#define MCONTROL_ACTION_TRACE_STOP 3
95#define MCONTROL_ACTION_TRACE_EMIT 4
96
97#define MCONTROL_MATCH_EQUAL 0
98#define MCONTROL_MATCH_NAPOT 1
99#define MCONTROL_MATCH_GE 2
100#define MCONTROL_MATCH_LT 3
101#define MCONTROL_MATCH_MASK_LOW 4
102#define MCONTROL_MATCH_MASK_HIGH 5
103
104#define MIP_SSIP (1 << IRQ_S_SOFT)
105#define MIP_HSIP (1 << IRQ_H_SOFT)
106#define MIP_MSIP (1 << IRQ_M_SOFT)
107#define MIP_STIP (1 << IRQ_S_TIMER)
108#define MIP_HTIP (1 << IRQ_H_TIMER)
109#define MIP_MTIP (1 << IRQ_M_TIMER)
110#define MIP_SEIP (1 << IRQ_S_EXT)
111#define MIP_HEIP (1 << IRQ_H_EXT)
112#define MIP_MEIP (1 << IRQ_M_EXT)
113
114#define SIP_SSIP MIP_SSIP
115#define SIP_STIP MIP_STIP
116
117#define PRV_U 0
118#define PRV_S 1
119#define PRV_H 2
120#define PRV_M 3
121
122#define SATP32_MODE 0x80000000
123#define SATP32_ASID 0x7FC00000
124#define SATP32_PPN 0x003FFFFF
125#define SATP64_MODE 0xF000000000000000
126#define SATP64_ASID 0x0FFFF00000000000
127#define SATP64_PPN 0x00000FFFFFFFFFFF
128
129#define SATP_MODE_OFF 0
130#define SATP_MODE_SV32 1
131#define SATP_MODE_SV39 8
132#define SATP_MODE_SV48 9
133#define SATP_MODE_SV57 10
134#define SATP_MODE_SV64 11
135
139#define CSR_PMPCFG_BASE 0x3a0
143#define CSR_PMPADDR_BASE 0x3b0
144
145#define PMP_R 0x01
146#define PMP_W 0x02
147#define PMP_X 0x04
148#define PMP_A 0x18
149#define PMP_L 0x80
150#define PMP_SHIFT 2
151
152#define PMP_TOR 0x08
153#define PMP_NA4 0x10
154#define PMP_NAPOT 0x18
155
156#define IRQ_S_SOFT 1
157#define IRQ_H_SOFT 2
158#define IRQ_M_SOFT 3
159#define IRQ_S_TIMER 5
160#define IRQ_H_TIMER 6
161#define IRQ_M_TIMER 7
162#define IRQ_S_EXT 9
163#define IRQ_H_EXT 10
164#define IRQ_M_EXT 11
165#define IRQ_COP 12
166#define IRQ_HOST 13
167
168/* SMRNMI CSR addresses */
169#ifdef CONFIG_RISCV_SMRNMI_ENABLE_NMI_DELIVERY
170#define CSR_MNSCRATCH 0x740
171#define CSR_MNEPC 0x741
172#define CSR_MNCAUSE 0x742
173#define CSR_MNSTATUS 0x744
174
175/* MNSTATUS bit fields */
176#define MNSTATUS_NMIE 0x00000008 /* NMI Enable (bit 3) */
177#endif /* CONFIG_RISCV_SMRNMI_ENABLE_NMI_DELIVERY */
178
179#define DEFAULT_RSTVEC 0x00001000
180#define CLINT_BASE 0x02000000
181#define CLINT_SIZE 0x000c0000
182#define EXT_IO_BASE 0x40000000
183#define DRAM_BASE 0x80000000
184
185/* page table entry (PTE) fields */
186#define PTE_V 0x001 /* Valid */
187#define PTE_R 0x002 /* Read */
188#define PTE_W 0x004 /* Write */
189#define PTE_X 0x008 /* Execute */
190#define PTE_U 0x010 /* User */
191#define PTE_G 0x020 /* Global */
192#define PTE_A 0x040 /* Accessed */
193#define PTE_D 0x080 /* Dirty */
194#define PTE_SOFT 0x300 /* Reserved for Software */
195
196#define PTE_PPN_SHIFT 10
197
198#define PTE_TABLE(PTE) (((PTE) & (PTE_V | PTE_R | PTE_W | PTE_X)) == PTE_V)
199
200#define INSERT_FIELD(val, which, fieldval) \
201( \
202 ((val) & ~(which)) | ((fieldval) * ((which) & ~((which)-1))) \
203) \
204
205#ifdef CONFIG_RISCV_ISA_EXT_SMCSRIND
206
207#define MISELECT 0x350
208#define MIREG 0x351
209#define MIREG2 0x352
210#define MIREG3 0x353
211#define MIREG4 0x355
212#define MIREG5 0x356
213#define MIREG6 0x357
214
215#endif /* CONFIG_RISCV_ISA_EXT_SMCSRIND */
216
217#ifndef _ASMLANGUAGE
218
219#define csr_read(csr) \
220({ \
221 unsigned long __rv; \
222 __asm__ volatile ("csrr %0, " STRINGIFY(csr) \
223 : "=r" (__rv)); \
224 __rv; \
225})
226
230#define csr_read_imm(csr) \
231 ({ \
232 register unsigned long __rv; \
233 __asm__ volatile("csrr %0, %1" : "=r"(__rv) : "i"(csr)); \
234 __rv; \
235 })
236
237#define csr_write(csr, val) \
238 do { \
239 unsigned long __wv = (unsigned long)(val); \
240 __asm__ volatile ("csrw " STRINGIFY(csr) ", %0" \
241 : \
242 : "rK" (__wv) \
243 : "memory"); \
244 } while (0)
245
249#define csr_write_imm(csr, val) \
250 do { \
251 unsigned long __wv = (unsigned long)(val); \
252 __asm__ volatile("csrw %0, %1" : : "i"(csr), "rK"(__wv) : "memory"); \
253 } while (0)
254
255#define csr_read_set(csr, val) \
256({ \
257 unsigned long __rsv = (unsigned long)(val); \
258 __asm__ volatile ("csrrs %0, " STRINGIFY(csr) ", %1" \
259 : "=r" (__rsv) : "rK" (__rsv) \
260 : "memory"); \
261 __rsv; \
262})
263
264#define csr_set(csr, val) \
265 do { \
266 unsigned long __sv = (unsigned long)(val); \
267 __asm__ volatile ("csrs " STRINGIFY(csr) ", %0" \
268 : \
269 : "rK" (__sv) \
270 : "memory"); \
271 } while (0)
272
273#define csr_read_clear(csr, val) \
274({ \
275 unsigned long __rcv = (unsigned long)(val); \
276 __asm__ volatile ("csrrc %0, " STRINGIFY(csr) ", %1" \
277 : "=r" (__rcv) : "rK" (__rcv) \
278 : "memory"); \
279 __rcv; \
280})
281
282#define csr_clear(csr, val) \
283 do { \
284 unsigned long __cv = (unsigned long)(val); \
285 __asm__ volatile ("csrc " STRINGIFY(csr) ", %0" \
286 : \
287 : "rK" (__cv) \
288 : "memory"); \
289 } while (0)
290
291#define csr_swap(csr, val) \
292({ \
293 unsigned long __swv = (unsigned long)(val); \
294 __asm__ volatile ("csrrw %0, " STRINGIFY(csr) ", %1" \
295 : "=r" (__swv) : "rK" (__swv) \
296 : "memory"); \
297 __swv; \
298})
299
300#endif /* !_ASMLANGUAGE */
301
302#endif /* CSR_H_ */