xref: /linux/arch/riscv/include/asm/csr.h (revision c4bb3a2d641c02ac2c7aa45534b4cefdf9bf416b)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2015 Regents of the University of California
4  */
5 
6 #ifndef _ASM_RISCV_CSR_H
7 #define _ASM_RISCV_CSR_H
8 
9 #include <asm/asm.h>
10 #include <linux/bits.h>
11 
12 /* Status register flags */
13 #define SR_SIE		_AC(0x00000002, UL) /* Supervisor Interrupt Enable */
14 #define SR_MIE		_AC(0x00000008, UL) /* Machine Interrupt Enable */
15 #define SR_SPIE		_AC(0x00000020, UL) /* Previous Supervisor IE */
16 #define SR_MPIE		_AC(0x00000080, UL) /* Previous Machine IE */
17 #define SR_SPP		_AC(0x00000100, UL) /* Previously Supervisor */
18 #define SR_MPP		_AC(0x00001800, UL) /* Previously Machine */
19 #define SR_SUM		_AC(0x00040000, UL) /* Supervisor User Memory Access */
20 
21 #define SR_FS		_AC(0x00006000, UL) /* Floating-point Status */
22 #define SR_FS_OFF	_AC(0x00000000, UL)
23 #define SR_FS_INITIAL	_AC(0x00002000, UL)
24 #define SR_FS_CLEAN	_AC(0x00004000, UL)
25 #define SR_FS_DIRTY	_AC(0x00006000, UL)
26 
27 #define SR_VS		_AC(0x00000600, UL) /* Vector Status */
28 #define SR_VS_OFF	_AC(0x00000000, UL)
29 #define SR_VS_INITIAL	_AC(0x00000200, UL)
30 #define SR_VS_CLEAN	_AC(0x00000400, UL)
31 #define SR_VS_DIRTY	_AC(0x00000600, UL)
32 
33 #define SR_XS		_AC(0x00018000, UL) /* Extension Status */
34 #define SR_XS_OFF	_AC(0x00000000, UL)
35 #define SR_XS_INITIAL	_AC(0x00008000, UL)
36 #define SR_XS_CLEAN	_AC(0x00010000, UL)
37 #define SR_XS_DIRTY	_AC(0x00018000, UL)
38 
39 #define SR_FS_VS	(SR_FS | SR_VS) /* Vector and Floating-Point Unit */
40 
41 #ifndef CONFIG_64BIT
42 #define SR_SD		_AC(0x80000000, UL) /* FS/VS/XS dirty */
43 #else
44 #define SR_SD		_AC(0x8000000000000000, UL) /* FS/VS/XS dirty */
45 #endif
46 
47 #ifdef CONFIG_64BIT
48 #define SR_UXL		_AC(0x300000000, UL) /* XLEN mask for U-mode */
49 #define SR_UXL_32	_AC(0x100000000, UL) /* XLEN = 32 for U-mode */
50 #define SR_UXL_64	_AC(0x200000000, UL) /* XLEN = 64 for U-mode */
51 #endif
52 
53 /* SATP flags */
54 #ifndef CONFIG_64BIT
55 #define SATP_PPN	_AC(0x003FFFFF, UL)
56 #define SATP_MODE_32	_AC(0x80000000, UL)
57 #define SATP_MODE_SHIFT	31
58 #define SATP_ASID_BITS	9
59 #define SATP_ASID_SHIFT	22
60 #define SATP_ASID_MASK	_AC(0x1FF, UL)
61 #else
62 #define SATP_PPN	_AC(0x00000FFFFFFFFFFF, UL)
63 #define SATP_MODE_39	_AC(0x8000000000000000, UL)
64 #define SATP_MODE_48	_AC(0x9000000000000000, UL)
65 #define SATP_MODE_57	_AC(0xa000000000000000, UL)
66 #define SATP_MODE_SHIFT	60
67 #define SATP_ASID_BITS	16
68 #define SATP_ASID_SHIFT	44
69 #define SATP_ASID_MASK	_AC(0xFFFF, UL)
70 #endif
71 
72 /* Exception cause high bit - is an interrupt if set */
73 #define CAUSE_IRQ_FLAG		(_AC(1, UL) << (__riscv_xlen - 1))
74 
75 /* Interrupt causes (minus the high bit) */
76 #define IRQ_S_SOFT		1
77 #define IRQ_VS_SOFT		2
78 #define IRQ_M_SOFT		3
79 #define IRQ_S_TIMER		5
80 #define IRQ_VS_TIMER		6
81 #define IRQ_M_TIMER		7
82 #define IRQ_S_EXT		9
83 #define IRQ_VS_EXT		10
84 #define IRQ_M_EXT		11
85 #define IRQ_S_GEXT		12
86 #define IRQ_PMU_OVF		13
87 #define IRQ_LOCAL_MAX		(IRQ_PMU_OVF + 1)
88 #define IRQ_LOCAL_MASK		GENMASK((IRQ_LOCAL_MAX - 1), 0)
89 
90 /* Exception causes */
91 #define EXC_INST_MISALIGNED	0
92 #define EXC_INST_ACCESS		1
93 #define EXC_INST_ILLEGAL	2
94 #define EXC_BREAKPOINT		3
95 #define EXC_LOAD_MISALIGNED	4
96 #define EXC_LOAD_ACCESS		5
97 #define EXC_STORE_MISALIGNED	6
98 #define EXC_STORE_ACCESS	7
99 #define EXC_SYSCALL		8
100 #define EXC_HYPERVISOR_SYSCALL	9
101 #define EXC_SUPERVISOR_SYSCALL	10
102 #define EXC_INST_PAGE_FAULT	12
103 #define EXC_LOAD_PAGE_FAULT	13
104 #define EXC_STORE_PAGE_FAULT	15
105 #define EXC_INST_GUEST_PAGE_FAULT	20
106 #define EXC_LOAD_GUEST_PAGE_FAULT	21
107 #define EXC_VIRTUAL_INST_FAULT		22
108 #define EXC_STORE_GUEST_PAGE_FAULT	23
109 
110 /* PMP configuration */
111 #define PMP_R			0x01
112 #define PMP_W			0x02
113 #define PMP_X			0x04
114 #define PMP_A			0x18
115 #define PMP_A_TOR		0x08
116 #define PMP_A_NA4		0x10
117 #define PMP_A_NAPOT		0x18
118 #define PMP_L			0x80
119 
120 /* HSTATUS flags */
121 #ifdef CONFIG_64BIT
122 #define HSTATUS_HUPMM		_AC(0x3000000000000, UL)
123 #define HSTATUS_HUPMM_PMLEN_0	_AC(0x0000000000000, UL)
124 #define HSTATUS_HUPMM_PMLEN_7	_AC(0x2000000000000, UL)
125 #define HSTATUS_HUPMM_PMLEN_16	_AC(0x3000000000000, UL)
126 #define HSTATUS_VSXL		_AC(0x300000000, UL)
127 #define HSTATUS_VSXL_SHIFT	32
128 #endif
129 #define HSTATUS_VTSR		_AC(0x00400000, UL)
130 #define HSTATUS_VTW		_AC(0x00200000, UL)
131 #define HSTATUS_VTVM		_AC(0x00100000, UL)
132 #define HSTATUS_VGEIN		_AC(0x0003f000, UL)
133 #define HSTATUS_VGEIN_SHIFT	12
134 #define HSTATUS_HU		_AC(0x00000200, UL)
135 #define HSTATUS_SPVP		_AC(0x00000100, UL)
136 #define HSTATUS_SPV		_AC(0x00000080, UL)
137 #define HSTATUS_GVA		_AC(0x00000040, UL)
138 #define HSTATUS_VSBE		_AC(0x00000020, UL)
139 
140 /* HGATP flags */
141 #define HGATP_MODE_OFF		_AC(0, UL)
142 #define HGATP_MODE_SV32X4	_AC(1, UL)
143 #define HGATP_MODE_SV39X4	_AC(8, UL)
144 #define HGATP_MODE_SV48X4	_AC(9, UL)
145 #define HGATP_MODE_SV57X4	_AC(10, UL)
146 
147 #define HGATP32_MODE_SHIFT	31
148 #define HGATP32_VMID_SHIFT	22
149 #define HGATP32_VMID		GENMASK(28, 22)
150 #define HGATP32_PPN		GENMASK(21, 0)
151 
152 #define HGATP64_MODE_SHIFT	60
153 #define HGATP64_VMID_SHIFT	44
154 #define HGATP64_VMID		GENMASK(57, 44)
155 #define HGATP64_PPN		GENMASK(43, 0)
156 
157 #define HGATP_PAGE_SHIFT	12
158 
159 #ifdef CONFIG_64BIT
160 #define HGATP_PPN		HGATP64_PPN
161 #define HGATP_VMID_SHIFT	HGATP64_VMID_SHIFT
162 #define HGATP_VMID		HGATP64_VMID
163 #define HGATP_MODE_SHIFT	HGATP64_MODE_SHIFT
164 #else
165 #define HGATP_PPN		HGATP32_PPN
166 #define HGATP_VMID_SHIFT	HGATP32_VMID_SHIFT
167 #define HGATP_VMID		HGATP32_VMID
168 #define HGATP_MODE_SHIFT	HGATP32_MODE_SHIFT
169 #endif
170 
171 /* VSIP & HVIP relation */
172 #define VSIP_TO_HVIP_SHIFT	(IRQ_VS_SOFT - IRQ_S_SOFT)
173 #define VSIP_VALID_MASK		((_AC(1, UL) << IRQ_S_SOFT) | \
174 				 (_AC(1, UL) << IRQ_S_TIMER) | \
175 				 (_AC(1, UL) << IRQ_S_EXT) | \
176 				 (_AC(1, UL) << IRQ_PMU_OVF))
177 
178 /* AIA CSR bits */
179 #define TOPI_IID_SHIFT		16
180 #define TOPI_IID_MASK		GENMASK(11, 0)
181 #define TOPI_IPRIO_MASK		GENMASK(7, 0)
182 #define TOPI_IPRIO_BITS		8
183 
184 #define TOPEI_ID_SHIFT		16
185 #define TOPEI_ID_MASK		GENMASK(10, 0)
186 #define TOPEI_PRIO_MASK		GENMASK(10, 0)
187 
188 #define ISELECT_IPRIO0		0x30
189 #define ISELECT_IPRIO15		0x3f
190 #define ISELECT_MASK		GENMASK(8, 0)
191 
192 #define HVICTL_VTI		BIT(30)
193 #define HVICTL_IID		GENMASK(27, 16)
194 #define HVICTL_IID_SHIFT	16
195 #define HVICTL_DPR		BIT(9)
196 #define HVICTL_IPRIOM		BIT(8)
197 #define HVICTL_IPRIO		GENMASK(7, 0)
198 
199 /* xENVCFG flags */
200 #define ENVCFG_STCE			(_AC(1, ULL) << 63)
201 #define ENVCFG_PBMTE			(_AC(1, ULL) << 62)
202 #define ENVCFG_ADUE			(_AC(1, ULL) << 61)
203 #define ENVCFG_PMM			(_AC(0x3, ULL) << 32)
204 #define ENVCFG_PMM_PMLEN_0		(_AC(0x0, ULL) << 32)
205 #define ENVCFG_PMM_PMLEN_7		(_AC(0x2, ULL) << 32)
206 #define ENVCFG_PMM_PMLEN_16		(_AC(0x3, ULL) << 32)
207 #define ENVCFG_CBZE			(_AC(1, UL) << 7)
208 #define ENVCFG_CBCFE			(_AC(1, UL) << 6)
209 #define ENVCFG_CBIE_SHIFT		4
210 #define ENVCFG_CBIE			(_AC(0x3, UL) << ENVCFG_CBIE_SHIFT)
211 #define ENVCFG_CBIE_ILL			_AC(0x0, UL)
212 #define ENVCFG_CBIE_FLUSH		_AC(0x1, UL)
213 #define ENVCFG_CBIE_INV			_AC(0x3, UL)
214 #define ENVCFG_FIOM			_AC(0x1, UL)
215 
216 /* Smstateen bits */
217 #define SMSTATEEN0_AIA_IMSIC_SHIFT	58
218 #define SMSTATEEN0_AIA_IMSIC		(_ULL(1) << SMSTATEEN0_AIA_IMSIC_SHIFT)
219 #define SMSTATEEN0_AIA_SHIFT		59
220 #define SMSTATEEN0_AIA			(_ULL(1) << SMSTATEEN0_AIA_SHIFT)
221 #define SMSTATEEN0_AIA_ISEL_SHIFT	60
222 #define SMSTATEEN0_AIA_ISEL		(_ULL(1) << SMSTATEEN0_AIA_ISEL_SHIFT)
223 #define SMSTATEEN0_HSENVCFG_SHIFT	62
224 #define SMSTATEEN0_HSENVCFG		(_ULL(1) << SMSTATEEN0_HSENVCFG_SHIFT)
225 #define SMSTATEEN0_SSTATEEN0_SHIFT	63
226 #define SMSTATEEN0_SSTATEEN0		(_ULL(1) << SMSTATEEN0_SSTATEEN0_SHIFT)
227 
228 /* mseccfg bits */
229 #define MSECCFG_PMM			ENVCFG_PMM
230 #define MSECCFG_PMM_PMLEN_0		ENVCFG_PMM_PMLEN_0
231 #define MSECCFG_PMM_PMLEN_7		ENVCFG_PMM_PMLEN_7
232 #define MSECCFG_PMM_PMLEN_16		ENVCFG_PMM_PMLEN_16
233 
234 /* symbolic CSR names: */
235 #define CSR_CYCLE		0xc00
236 #define CSR_TIME		0xc01
237 #define CSR_INSTRET		0xc02
238 #define CSR_HPMCOUNTER3		0xc03
239 #define CSR_HPMCOUNTER4		0xc04
240 #define CSR_HPMCOUNTER5		0xc05
241 #define CSR_HPMCOUNTER6		0xc06
242 #define CSR_HPMCOUNTER7		0xc07
243 #define CSR_HPMCOUNTER8		0xc08
244 #define CSR_HPMCOUNTER9		0xc09
245 #define CSR_HPMCOUNTER10	0xc0a
246 #define CSR_HPMCOUNTER11	0xc0b
247 #define CSR_HPMCOUNTER12	0xc0c
248 #define CSR_HPMCOUNTER13	0xc0d
249 #define CSR_HPMCOUNTER14	0xc0e
250 #define CSR_HPMCOUNTER15	0xc0f
251 #define CSR_HPMCOUNTER16	0xc10
252 #define CSR_HPMCOUNTER17	0xc11
253 #define CSR_HPMCOUNTER18	0xc12
254 #define CSR_HPMCOUNTER19	0xc13
255 #define CSR_HPMCOUNTER20	0xc14
256 #define CSR_HPMCOUNTER21	0xc15
257 #define CSR_HPMCOUNTER22	0xc16
258 #define CSR_HPMCOUNTER23	0xc17
259 #define CSR_HPMCOUNTER24	0xc18
260 #define CSR_HPMCOUNTER25	0xc19
261 #define CSR_HPMCOUNTER26	0xc1a
262 #define CSR_HPMCOUNTER27	0xc1b
263 #define CSR_HPMCOUNTER28	0xc1c
264 #define CSR_HPMCOUNTER29	0xc1d
265 #define CSR_HPMCOUNTER30	0xc1e
266 #define CSR_HPMCOUNTER31	0xc1f
267 #define CSR_CYCLEH		0xc80
268 #define CSR_TIMEH		0xc81
269 #define CSR_INSTRETH		0xc82
270 #define CSR_HPMCOUNTER3H	0xc83
271 #define CSR_HPMCOUNTER4H	0xc84
272 #define CSR_HPMCOUNTER5H	0xc85
273 #define CSR_HPMCOUNTER6H	0xc86
274 #define CSR_HPMCOUNTER7H	0xc87
275 #define CSR_HPMCOUNTER8H	0xc88
276 #define CSR_HPMCOUNTER9H	0xc89
277 #define CSR_HPMCOUNTER10H	0xc8a
278 #define CSR_HPMCOUNTER11H	0xc8b
279 #define CSR_HPMCOUNTER12H	0xc8c
280 #define CSR_HPMCOUNTER13H	0xc8d
281 #define CSR_HPMCOUNTER14H	0xc8e
282 #define CSR_HPMCOUNTER15H	0xc8f
283 #define CSR_HPMCOUNTER16H	0xc90
284 #define CSR_HPMCOUNTER17H	0xc91
285 #define CSR_HPMCOUNTER18H	0xc92
286 #define CSR_HPMCOUNTER19H	0xc93
287 #define CSR_HPMCOUNTER20H	0xc94
288 #define CSR_HPMCOUNTER21H	0xc95
289 #define CSR_HPMCOUNTER22H	0xc96
290 #define CSR_HPMCOUNTER23H	0xc97
291 #define CSR_HPMCOUNTER24H	0xc98
292 #define CSR_HPMCOUNTER25H	0xc99
293 #define CSR_HPMCOUNTER26H	0xc9a
294 #define CSR_HPMCOUNTER27H	0xc9b
295 #define CSR_HPMCOUNTER28H	0xc9c
296 #define CSR_HPMCOUNTER29H	0xc9d
297 #define CSR_HPMCOUNTER30H	0xc9e
298 #define CSR_HPMCOUNTER31H	0xc9f
299 
300 #define CSR_SCOUNTOVF		0xda0
301 
302 #define CSR_SSTATUS		0x100
303 #define CSR_SIE			0x104
304 #define CSR_STVEC		0x105
305 #define CSR_SCOUNTEREN		0x106
306 #define CSR_SENVCFG		0x10a
307 #define CSR_SSTATEEN0		0x10c
308 #define CSR_SSCRATCH		0x140
309 #define CSR_SEPC		0x141
310 #define CSR_SCAUSE		0x142
311 #define CSR_STVAL		0x143
312 #define CSR_SIP			0x144
313 #define CSR_SATP		0x180
314 
315 #define CSR_STIMECMP		0x14D
316 #define CSR_STIMECMPH		0x15D
317 
318 /* Supervisor-Level Window to Indirectly Accessed Registers (AIA) */
319 #define CSR_SISELECT		0x150
320 #define CSR_SIREG		0x151
321 
322 /* Supervisor-Level Interrupts (AIA) */
323 #define CSR_STOPEI		0x15c
324 #define CSR_STOPI		0xdb0
325 
326 /* Supervisor-Level High-Half CSRs (AIA) */
327 #define CSR_SIEH		0x114
328 #define CSR_SIPH		0x154
329 
330 #define CSR_VSSTATUS		0x200
331 #define CSR_VSIE		0x204
332 #define CSR_VSTVEC		0x205
333 #define CSR_VSSCRATCH		0x240
334 #define CSR_VSEPC		0x241
335 #define CSR_VSCAUSE		0x242
336 #define CSR_VSTVAL		0x243
337 #define CSR_VSIP		0x244
338 #define CSR_VSATP		0x280
339 #define CSR_VSTIMECMP		0x24D
340 #define CSR_VSTIMECMPH		0x25D
341 
342 #define CSR_HSTATUS		0x600
343 #define CSR_HEDELEG		0x602
344 #define CSR_HIDELEG		0x603
345 #define CSR_HIE			0x604
346 #define CSR_HTIMEDELTA		0x605
347 #define CSR_HCOUNTEREN		0x606
348 #define CSR_HGEIE		0x607
349 #define CSR_HENVCFG		0x60a
350 #define CSR_HTIMEDELTAH		0x615
351 #define CSR_HENVCFGH		0x61a
352 #define CSR_HTVAL		0x643
353 #define CSR_HIP			0x644
354 #define CSR_HVIP		0x645
355 #define CSR_HTINST		0x64a
356 #define CSR_HGATP		0x680
357 #define CSR_HGEIP		0xe12
358 
359 /* Virtual Interrupts and Interrupt Priorities (H-extension with AIA) */
360 #define CSR_HVIEN		0x608
361 #define CSR_HVICTL		0x609
362 #define CSR_HVIPRIO1		0x646
363 #define CSR_HVIPRIO2		0x647
364 
365 /* VS-Level Window to Indirectly Accessed Registers (H-extension with AIA) */
366 #define CSR_VSISELECT		0x250
367 #define CSR_VSIREG		0x251
368 
369 /* VS-Level Interrupts (H-extension with AIA) */
370 #define CSR_VSTOPEI		0x25c
371 #define CSR_VSTOPI		0xeb0
372 
373 /* Hypervisor and VS-Level High-Half CSRs (H-extension with AIA) */
374 #define CSR_HIDELEGH		0x613
375 #define CSR_HVIENH		0x618
376 #define CSR_HVIPH		0x655
377 #define CSR_HVIPRIO1H		0x656
378 #define CSR_HVIPRIO2H		0x657
379 #define CSR_VSIEH		0x214
380 #define CSR_VSIPH		0x254
381 
382 /* Hypervisor stateen CSRs */
383 #define CSR_HSTATEEN0		0x60c
384 #define CSR_HSTATEEN0H		0x61c
385 
386 #define CSR_MSTATUS		0x300
387 #define CSR_MISA		0x301
388 #define CSR_MIDELEG		0x303
389 #define CSR_MIE			0x304
390 #define CSR_MTVEC		0x305
391 #define CSR_MENVCFG		0x30a
392 #define CSR_MENVCFGH		0x31a
393 #define CSR_MSCRATCH		0x340
394 #define CSR_MEPC		0x341
395 #define CSR_MCAUSE		0x342
396 #define CSR_MTVAL		0x343
397 #define CSR_MIP			0x344
398 #define CSR_PMPCFG0		0x3a0
399 #define CSR_PMPADDR0		0x3b0
400 #define CSR_MSECCFG		0x747
401 #define CSR_MSECCFGH		0x757
402 #define CSR_MVENDORID		0xf11
403 #define CSR_MARCHID		0xf12
404 #define CSR_MIMPID		0xf13
405 #define CSR_MHARTID		0xf14
406 
407 /* Machine-Level Window to Indirectly Accessed Registers (AIA) */
408 #define CSR_MISELECT		0x350
409 #define CSR_MIREG		0x351
410 
411 /* Machine-Level Interrupts (AIA) */
412 #define CSR_MTOPEI		0x35c
413 #define CSR_MTOPI		0xfb0
414 
415 /* Virtual Interrupts for Supervisor Level (AIA) */
416 #define CSR_MVIEN		0x308
417 #define CSR_MVIP		0x309
418 
419 /* Machine-Level High-Half CSRs (AIA) */
420 #define CSR_MIDELEGH		0x313
421 #define CSR_MIEH		0x314
422 #define CSR_MVIENH		0x318
423 #define CSR_MVIPH		0x319
424 #define CSR_MIPH		0x354
425 
426 #define CSR_VSTART		0x8
427 #define CSR_VCSR		0xf
428 #define CSR_VL			0xc20
429 #define CSR_VTYPE		0xc21
430 #define CSR_VLENB		0xc22
431 
432 /* Scalar Crypto Extension - Entropy */
433 #define CSR_SEED		0x015
434 #define SEED_OPST_MASK		_AC(0xC0000000, UL)
435 #define SEED_OPST_BIST		_AC(0x00000000, UL)
436 #define SEED_OPST_WAIT		_AC(0x40000000, UL)
437 #define SEED_OPST_ES16		_AC(0x80000000, UL)
438 #define SEED_OPST_DEAD		_AC(0xC0000000, UL)
439 #define SEED_ENTROPY_MASK	_AC(0xFFFF, UL)
440 
441 #ifdef CONFIG_RISCV_M_MODE
442 # define CSR_STATUS	CSR_MSTATUS
443 # define CSR_IE		CSR_MIE
444 # define CSR_TVEC	CSR_MTVEC
445 # define CSR_ENVCFG	CSR_MENVCFG
446 # define CSR_SCRATCH	CSR_MSCRATCH
447 # define CSR_EPC	CSR_MEPC
448 # define CSR_CAUSE	CSR_MCAUSE
449 # define CSR_TVAL	CSR_MTVAL
450 # define CSR_IP		CSR_MIP
451 
452 # define CSR_IEH		CSR_MIEH
453 # define CSR_ISELECT	CSR_MISELECT
454 # define CSR_IREG	CSR_MIREG
455 # define CSR_IPH		CSR_MIPH
456 # define CSR_TOPEI	CSR_MTOPEI
457 # define CSR_TOPI	CSR_MTOPI
458 
459 # define SR_IE		SR_MIE
460 # define SR_PIE		SR_MPIE
461 # define SR_PP		SR_MPP
462 
463 # define RV_IRQ_SOFT		IRQ_M_SOFT
464 # define RV_IRQ_TIMER	IRQ_M_TIMER
465 # define RV_IRQ_EXT		IRQ_M_EXT
466 #else /* CONFIG_RISCV_M_MODE */
467 # define CSR_STATUS	CSR_SSTATUS
468 # define CSR_IE		CSR_SIE
469 # define CSR_TVEC	CSR_STVEC
470 # define CSR_ENVCFG	CSR_SENVCFG
471 # define CSR_SCRATCH	CSR_SSCRATCH
472 # define CSR_EPC	CSR_SEPC
473 # define CSR_CAUSE	CSR_SCAUSE
474 # define CSR_TVAL	CSR_STVAL
475 # define CSR_IP		CSR_SIP
476 
477 # define CSR_IEH		CSR_SIEH
478 # define CSR_ISELECT	CSR_SISELECT
479 # define CSR_IREG	CSR_SIREG
480 # define CSR_IPH		CSR_SIPH
481 # define CSR_TOPEI	CSR_STOPEI
482 # define CSR_TOPI	CSR_STOPI
483 
484 # define SR_IE		SR_SIE
485 # define SR_PIE		SR_SPIE
486 # define SR_PP		SR_SPP
487 
488 # define RV_IRQ_SOFT		IRQ_S_SOFT
489 # define RV_IRQ_TIMER	IRQ_S_TIMER
490 # define RV_IRQ_EXT		IRQ_S_EXT
491 # define RV_IRQ_PMU	IRQ_PMU_OVF
492 # define SIP_LCOFIP     (_AC(0x1, UL) << IRQ_PMU_OVF)
493 
494 #endif /* !CONFIG_RISCV_M_MODE */
495 
496 /* IE/IP (Supervisor/Machine Interrupt Enable/Pending) flags */
497 #define IE_SIE		(_AC(0x1, UL) << RV_IRQ_SOFT)
498 #define IE_TIE		(_AC(0x1, UL) << RV_IRQ_TIMER)
499 #define IE_EIE		(_AC(0x1, UL) << RV_IRQ_EXT)
500 
501 #ifndef __ASSEMBLY__
502 
503 #define csr_swap(csr, val)					\
504 ({								\
505 	unsigned long __v = (unsigned long)(val);		\
506 	__asm__ __volatile__ ("csrrw %0, " __ASM_STR(csr) ", %1"\
507 			      : "=r" (__v) : "rK" (__v)		\
508 			      : "memory");			\
509 	__v;							\
510 })
511 
512 #define csr_read(csr)						\
513 ({								\
514 	register unsigned long __v;				\
515 	__asm__ __volatile__ ("csrr %0, " __ASM_STR(csr)	\
516 			      : "=r" (__v) :			\
517 			      : "memory");			\
518 	__v;							\
519 })
520 
521 #define csr_write(csr, val)					\
522 ({								\
523 	unsigned long __v = (unsigned long)(val);		\
524 	__asm__ __volatile__ ("csrw " __ASM_STR(csr) ", %0"	\
525 			      : : "rK" (__v)			\
526 			      : "memory");			\
527 })
528 
529 #define csr_read_set(csr, val)					\
530 ({								\
531 	unsigned long __v = (unsigned long)(val);		\
532 	__asm__ __volatile__ ("csrrs %0, " __ASM_STR(csr) ", %1"\
533 			      : "=r" (__v) : "rK" (__v)		\
534 			      : "memory");			\
535 	__v;							\
536 })
537 
538 #define csr_set(csr, val)					\
539 ({								\
540 	unsigned long __v = (unsigned long)(val);		\
541 	__asm__ __volatile__ ("csrs " __ASM_STR(csr) ", %0"	\
542 			      : : "rK" (__v)			\
543 			      : "memory");			\
544 })
545 
546 #define csr_read_clear(csr, val)				\
547 ({								\
548 	unsigned long __v = (unsigned long)(val);		\
549 	__asm__ __volatile__ ("csrrc %0, " __ASM_STR(csr) ", %1"\
550 			      : "=r" (__v) : "rK" (__v)		\
551 			      : "memory");			\
552 	__v;							\
553 })
554 
555 #define csr_clear(csr, val)					\
556 ({								\
557 	unsigned long __v = (unsigned long)(val);		\
558 	__asm__ __volatile__ ("csrc " __ASM_STR(csr) ", %0"	\
559 			      : : "rK" (__v)			\
560 			      : "memory");			\
561 })
562 
563 #endif /* __ASSEMBLY__ */
564 
565 #endif /* _ASM_RISCV_CSR_H */
566