Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
csr.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 BayLibre, SAS
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef CSR_H_
8#define CSR_H_
9
10#define MSTATUS_UIE 0x00000001
11#define MSTATUS_SIE 0x00000002
12#define MSTATUS_HIE 0x00000004
13#define MSTATUS_MIE 0x00000008
14#define MSTATUS_UPIE 0x00000010
15#define MSTATUS_SPIE 0x00000020
16#define MSTATUS_HPIE 0x00000040
17#define MSTATUS_MPIE 0x00000080
18#define MSTATUS_SPP 0x00000100
19#define MSTATUS_HPP 0x00000600
20#define MSTATUS_MPP 0x00001800
21#define MSTATUS_FS 0x00006000
22#define MSTATUS_XS 0x00018000
23#define MSTATUS_MPRV 0x00020000
24#define MSTATUS_SUM 0x00040000
25#define MSTATUS_MXR 0x00080000
26#define MSTATUS_TVM 0x00100000
27#define MSTATUS_TW 0x00200000
28#define MSTATUS_TSR 0x00400000
29#define MSTATUS32_SD 0x80000000
30#define MSTATUS_UXL 0x0000000300000000
31#define MSTATUS_SXL 0x0000000C00000000
32#define MSTATUS64_SD 0x8000000000000000
33
34#define SSTATUS_UIE 0x00000001
35#define SSTATUS_SIE 0x00000002
36#define SSTATUS_UPIE 0x00000010
37#define SSTATUS_SPIE 0x00000020
38#define SSTATUS_SPP 0x00000100
39#define SSTATUS_FS 0x00006000
40#define SSTATUS_XS 0x00018000
41#define SSTATUS_SUM 0x00040000
42#define SSTATUS_MXR 0x00080000
43#define SSTATUS32_SD 0x80000000
44#define SSTATUS_UXL 0x0000000300000000
45#define SSTATUS64_SD 0x8000000000000000
46
47#define DCSR_XDEBUGVER (3U<<30)
48#define DCSR_NDRESET (1<<29)
49#define DCSR_FULLRESET (1<<28)
50#define DCSR_EBREAKM (1<<15)
51#define DCSR_EBREAKH (1<<14)
52#define DCSR_EBREAKS (1<<13)
53#define DCSR_EBREAKU (1<<12)
54#define DCSR_STOPCYCLE (1<<10)
55#define DCSR_STOPTIME (1<<9)
56#define DCSR_CAUSE (7<<6)
57#define DCSR_DEBUGINT (1<<5)
58#define DCSR_HALT (1<<3)
59#define DCSR_STEP (1<<2)
60#define DCSR_PRV (3<<0)
61
62#define DCSR_CAUSE_NONE 0
63#define DCSR_CAUSE_SWBP 1
64#define DCSR_CAUSE_HWBP 2
65#define DCSR_CAUSE_DEBUGINT 3
66#define DCSR_CAUSE_STEP 4
67#define DCSR_CAUSE_HALT 5
68
69#define MCONTROL_TYPE(xlen) (0xfULL<<((xlen)-4))
70#define MCONTROL_DMODE(xlen) (1ULL<<((xlen)-5))
71#define MCONTROL_MASKMAX(xlen) (0x3fULL<<((xlen)-11))
72
73#define MCONTROL_SELECT (1<<19)
74#define MCONTROL_TIMING (1<<18)
75#define MCONTROL_ACTION (0x3f<<12)
76#define MCONTROL_CHAIN (1<<11)
77#define MCONTROL_MATCH (0xf<<7)
78#define MCONTROL_M (1<<6)
79#define MCONTROL_H (1<<5)
80#define MCONTROL_S (1<<4)
81#define MCONTROL_U (1<<3)
82#define MCONTROL_EXECUTE (1<<2)
83#define MCONTROL_STORE (1<<1)
84#define MCONTROL_LOAD (1<<0)
85
86#define MCONTROL_TYPE_NONE 0
87#define MCONTROL_TYPE_MATCH 2
88
89#define MCONTROL_ACTION_DEBUG_EXCEPTION 0
90#define MCONTROL_ACTION_DEBUG_MODE 1
91#define MCONTROL_ACTION_TRACE_START 2
92#define MCONTROL_ACTION_TRACE_STOP 3
93#define MCONTROL_ACTION_TRACE_EMIT 4
94
95#define MCONTROL_MATCH_EQUAL 0
96#define MCONTROL_MATCH_NAPOT 1
97#define MCONTROL_MATCH_GE 2
98#define MCONTROL_MATCH_LT 3
99#define MCONTROL_MATCH_MASK_LOW 4
100#define MCONTROL_MATCH_MASK_HIGH 5
101
102#define MIP_SSIP (1 << IRQ_S_SOFT)
103#define MIP_HSIP (1 << IRQ_H_SOFT)
104#define MIP_MSIP (1 << IRQ_M_SOFT)
105#define MIP_STIP (1 << IRQ_S_TIMER)
106#define MIP_HTIP (1 << IRQ_H_TIMER)
107#define MIP_MTIP (1 << IRQ_M_TIMER)
108#define MIP_SEIP (1 << IRQ_S_EXT)
109#define MIP_HEIP (1 << IRQ_H_EXT)
110#define MIP_MEIP (1 << IRQ_M_EXT)
111
112#define SIP_SSIP MIP_SSIP
113#define SIP_STIP MIP_STIP
114
115#define PRV_U 0
116#define PRV_S 1
117#define PRV_H 2
118#define PRV_M 3
119
120#define SATP32_MODE 0x80000000
121#define SATP32_ASID 0x7FC00000
122#define SATP32_PPN 0x003FFFFF
123#define SATP64_MODE 0xF000000000000000
124#define SATP64_ASID 0x0FFFF00000000000
125#define SATP64_PPN 0x00000FFFFFFFFFFF
126
127#define SATP_MODE_OFF 0
128#define SATP_MODE_SV32 1
129#define SATP_MODE_SV39 8
130#define SATP_MODE_SV48 9
131#define SATP_MODE_SV57 10
132#define SATP_MODE_SV64 11
133
134#define PMP_R 0x01
135#define PMP_W 0x02
136#define PMP_X 0x04
137#define PMP_A 0x18
138#define PMP_L 0x80
139#define PMP_SHIFT 2
140
141#define PMP_TOR 0x08
142#define PMP_NA4 0x10
143#define PMP_NAPOT 0x18
144
145#define IRQ_S_SOFT 1
146#define IRQ_H_SOFT 2
147#define IRQ_M_SOFT 3
148#define IRQ_S_TIMER 5
149#define IRQ_H_TIMER 6
150#define IRQ_M_TIMER 7
151#define IRQ_S_EXT 9
152#define IRQ_H_EXT 10
153#define IRQ_M_EXT 11
154#define IRQ_COP 12
155#define IRQ_HOST 13
156
157#define DEFAULT_RSTVEC 0x00001000
158#define CLINT_BASE 0x02000000
159#define CLINT_SIZE 0x000c0000
160#define EXT_IO_BASE 0x40000000
161#define DRAM_BASE 0x80000000
162
163/* page table entry (PTE) fields */
164#define PTE_V 0x001 /* Valid */
165#define PTE_R 0x002 /* Read */
166#define PTE_W 0x004 /* Write */
167#define PTE_X 0x008 /* Execute */
168#define PTE_U 0x010 /* User */
169#define PTE_G 0x020 /* Global */
170#define PTE_A 0x040 /* Accessed */
171#define PTE_D 0x080 /* Dirty */
172#define PTE_SOFT 0x300 /* Reserved for Software */
173
174#define PTE_PPN_SHIFT 10
175
176#define PTE_TABLE(PTE) (((PTE) & (PTE_V | PTE_R | PTE_W | PTE_X)) == PTE_V)
177
178#define INSERT_FIELD(val, which, fieldval) \
179( \
180 ((val) & ~(which)) | ((fieldval) * ((which) & ~((which)-1))) \
181) \
182
183#define csr_read(csr) \
184({ \
185 register unsigned long __v; \
186 __asm__ volatile ("csrr %0, " #csr \
187 : "=r" (__v)); \
188 __v; \
189})
190
191#define csr_write(csr, val) \
192({ \
193 unsigned long __v = (unsigned long)(val); \
194 __asm__ volatile ("csrw " #csr ", %0" \
195 : : "rK" (__v) \
196 : "memory"); \
197})
198
199
200#define csr_read_set(csr, val) \
201({ \
202 unsigned long __v = (unsigned long)(val); \
203 __asm__ volatile ("csrrs %0, " #csr ", %1" \
204 : "=r" (__v) : "rK" (__v) \
205 : "memory"); \
206 __v; \
207})
208
209#define csr_set(csr, val) \
210({ \
211 unsigned long __v = (unsigned long)(val); \
212 __asm__ volatile ("csrs " #csr ", %0" \
213 : : "rK" (__v) \
214 : "memory"); \
215})
216
217#define csr_read_clear(csr, val) \
218({ \
219 unsigned long __v = (unsigned long)(val); \
220 __asm__ volatile ("csrrc %0, " #csr ", %1" \
221 : "=r" (__v) : "rK" (__v) \
222 : "memory"); \
223 __v; \
224})
225
226#define csr_clear(csr, val) \
227({ \
228 unsigned long __v = (unsigned long)(val); \
229 __asm__ volatile ("csrc " #csr ", %0" \
230 : : "rK" (__v) \
231 : "memory"); \
232})
233
234#endif /* CSR_H_ */