blob: 986f951c31ac47312898da6883e5c1ae03045eb1 [file] [log] [blame]
Bin Menga27264c2019-07-10 23:43:12 -07001/* SPDX-License-Identifier: GPL-2.0-only */
Bin Meng055700e2018-09-26 06:55:14 -07002/*
3 * Copyright (C) 2015 Regents of the University of California
4 *
5 * Taken from Linux arch/riscv/include/asm/csr.h
6 */
7
8#ifndef _ASM_RISCV_CSR_H
9#define _ASM_RISCV_CSR_H
10
Bin Menga27264c2019-07-10 23:43:12 -070011#include <asm/asm.h>
Baruch Siach401885a2018-11-11 12:31:01 +020012#include <linux/const.h>
13
Bin Meng055700e2018-09-26 06:55:14 -070014/* Status register flags */
15#define SR_SIE _AC(0x00000002, UL) /* Supervisor Interrupt Enable */
16#define SR_SPIE _AC(0x00000020, UL) /* Previous Supervisor IE */
17#define SR_SPP _AC(0x00000100, UL) /* Previously Supervisor */
Sean Anderson7f4b6662020-06-24 06:41:19 -040018#ifdef CONFIG_RISCV_PRIV_1_9
19#define SR_PUM _AC(0x00040000, UL) /* Protect User Memory Access */
20#else
Bin Menga27264c2019-07-10 23:43:12 -070021#define SR_SUM _AC(0x00040000, UL) /* Supervisor User Memory Access */
Sean Anderson7f4b6662020-06-24 06:41:19 -040022#endif
Bin Meng055700e2018-09-26 06:55:14 -070023
24#define SR_FS _AC(0x00006000, UL) /* Floating-point Status */
25#define SR_FS_OFF _AC(0x00000000, UL)
26#define SR_FS_INITIAL _AC(0x00002000, UL)
27#define SR_FS_CLEAN _AC(0x00004000, UL)
28#define SR_FS_DIRTY _AC(0x00006000, UL)
29
30#define SR_XS _AC(0x00018000, UL) /* Extension Status */
31#define SR_XS_OFF _AC(0x00000000, UL)
32#define SR_XS_INITIAL _AC(0x00008000, UL)
33#define SR_XS_CLEAN _AC(0x00010000, UL)
34#define SR_XS_DIRTY _AC(0x00018000, UL)
35
Sean Anderson7f4b6662020-06-24 06:41:19 -040036#ifdef CONFIG_RISCV_PRIV_1_9
37#define SR_VM _AC(0x1F000000, UL) /* Virtualization Management */
38#define SR_VM_MODE_BARE _AC(0x00000000, UL) /* No translation or protection */
39#define SR_VM_MODE_BB _AC(0x01000000, UL) /* Single base-and-bound */
40/* Separate instruction and data base-and-bound */
41#define SR_VM_MODE_BBID _AC(0x02000000, UL)
42#ifndef CONFIG_64BIT
43#define SR_VM_MODE_32 _AC(0x08000000, UL)
44#define SR_VM_MODE SR_VM_MODE_32
45#else
46#define SR_VM_MODE_39 _AC(0x09000000, UL)
47#define SR_VM_MODE_48 _AC(0x0A000000, UL)
48#define SR_VM_MODE SR_VM_MODE_39
49#endif
50#endif
51
Bin Meng055700e2018-09-26 06:55:14 -070052#ifndef CONFIG_64BIT
53#define SR_SD _AC(0x80000000, UL) /* FS/XS dirty */
54#else
55#define SR_SD _AC(0x8000000000000000, UL) /* FS/XS dirty */
56#endif
57
58/* SATP flags */
Sean Anderson7f4b6662020-06-24 06:41:19 -040059#ifndef CONFIG_RISCV_PRIV_1_9
Bin Menga27264c2019-07-10 23:43:12 -070060#ifndef CONFIG_64BIT
Bin Meng055700e2018-09-26 06:55:14 -070061#define SATP_PPN _AC(0x003FFFFF, UL)
62#define SATP_MODE_32 _AC(0x80000000, UL)
63#define SATP_MODE SATP_MODE_32
64#else
65#define SATP_PPN _AC(0x00000FFFFFFFFFFF, UL)
66#define SATP_MODE_39 _AC(0x8000000000000000, UL)
67#define SATP_MODE SATP_MODE_39
68#endif
Sean Anderson7f4b6662020-06-24 06:41:19 -040069#endif
Bin Meng055700e2018-09-26 06:55:14 -070070
Bin Menga27264c2019-07-10 23:43:12 -070071/* SCAUSE */
72#define SCAUSE_IRQ_FLAG (_AC(1, UL) << (__riscv_xlen - 1))
73
74#define IRQ_U_SOFT 0
75#define IRQ_S_SOFT 1
76#define IRQ_M_SOFT 3
77#define IRQ_U_TIMER 4
78#define IRQ_S_TIMER 5
79#define IRQ_M_TIMER 7
80#define IRQ_U_EXT 8
81#define IRQ_S_EXT 9
82#define IRQ_M_EXT 11
Bin Meng055700e2018-09-26 06:55:14 -070083
84#define EXC_INST_MISALIGNED 0
85#define EXC_INST_ACCESS 1
86#define EXC_BREAKPOINT 3
87#define EXC_LOAD_ACCESS 5
88#define EXC_STORE_ACCESS 7
89#define EXC_SYSCALL 8
90#define EXC_INST_PAGE_FAULT 12
91#define EXC_LOAD_PAGE_FAULT 13
92#define EXC_STORE_PAGE_FAULT 15
93
Bin Menga27264c2019-07-10 23:43:12 -070094/* SIE (Interrupt Enable) and SIP (Interrupt Pending) flags */
95#define MIE_MSIE (_AC(0x1, UL) << IRQ_M_SOFT)
96#define SIE_SSIE (_AC(0x1, UL) << IRQ_S_SOFT)
97#define SIE_STIE (_AC(0x1, UL) << IRQ_S_TIMER)
98#define SIE_SEIE (_AC(0x1, UL) << IRQ_S_EXT)
Bin Meng055700e2018-09-26 06:55:14 -070099
Bin Mengf9426362019-07-10 23:43:13 -0700100#define CSR_FCSR 0x003
Bin Menga27264c2019-07-10 23:43:12 -0700101#define CSR_CYCLE 0xc00
102#define CSR_TIME 0xc01
103#define CSR_INSTRET 0xc02
104#define CSR_SSTATUS 0x100
105#define CSR_SIE 0x104
106#define CSR_STVEC 0x105
107#define CSR_SCOUNTEREN 0x106
108#define CSR_SSCRATCH 0x140
109#define CSR_SEPC 0x141
110#define CSR_SCAUSE 0x142
111#define CSR_STVAL 0x143
112#define CSR_SIP 0x144
Sean Anderson7f4b6662020-06-24 06:41:19 -0400113#ifdef CONFIG_RISCV_PRIV_1_9
114#define CSR_SPTBR 0x180
115#else
Bin Menga27264c2019-07-10 23:43:12 -0700116#define CSR_SATP 0x180
Sean Anderson7f4b6662020-06-24 06:41:19 -0400117#endif
Bin Mengf9426362019-07-10 23:43:13 -0700118#define CSR_MSTATUS 0x300
119#define CSR_MISA 0x301
120#define CSR_MIE 0x304
121#define CSR_MTVEC 0x305
Sean Anderson7f4b6662020-06-24 06:41:19 -0400122#ifdef CONFIG_RISCV_PRIV_1_9
123#define CSR_MUCOUNTEREN 0x320
124#define CSR_MSCOUNTEREN 0x321
125#define CSR_MHCOUNTEREN 0x322
126#else
Bin Mengf9426362019-07-10 23:43:13 -0700127#define CSR_MCOUNTEREN 0x306
Sean Anderson7f4b6662020-06-24 06:41:19 -0400128#endif
Bin Mengf9426362019-07-10 23:43:13 -0700129#define CSR_MSCRATCH 0x340
130#define CSR_MEPC 0x341
131#define CSR_MCAUSE 0x342
132#define CSR_MTVAL 0x343
133#define CSR_MIP 0x344
Sean Anderson7f4b6662020-06-24 06:41:19 -0400134#ifdef CONFIG_RISCV_PRIV_1_9
135#define CSR_MBASE 0x380
136#define CSR_MBOUND 0x381
137#define CSR_MIBASE 0x382
138#define CSR_MIBOUND 0x383
139#define CSR_MDBASE 0x384
140#define CSR_MDBOUND 0x385
141#endif
Bin Menga27264c2019-07-10 23:43:12 -0700142#define CSR_CYCLEH 0xc80
143#define CSR_TIMEH 0xc81
144#define CSR_INSTRETH 0xc82
Leo Yu-Chi Lianga5dda2b2023-12-26 14:17:32 +0800145#define CSR_MARCHID 0xf12
Bin Mengf9426362019-07-10 23:43:13 -0700146#define CSR_MHARTID 0xf14
Bin Menga27264c2019-07-10 23:43:12 -0700147
148#ifndef __ASSEMBLY__
Bin Meng9e9e6fe2018-12-12 06:12:39 -0800149
Bin Meng055700e2018-09-26 06:55:14 -0700150#define csr_swap(csr, val) \
151({ \
152 unsigned long __v = (unsigned long)(val); \
Bin Menga27264c2019-07-10 23:43:12 -0700153 __asm__ __volatile__ ("csrrw %0, " __ASM_STR(csr) ", %1"\
Bin Meng055700e2018-09-26 06:55:14 -0700154 : "=r" (__v) : "rK" (__v) \
155 : "memory"); \
156 __v; \
157})
158
159#define csr_read(csr) \
160({ \
161 register unsigned long __v; \
Bin Menga27264c2019-07-10 23:43:12 -0700162 __asm__ __volatile__ ("csrr %0, " __ASM_STR(csr) \
Bin Meng055700e2018-09-26 06:55:14 -0700163 : "=r" (__v) : \
164 : "memory"); \
165 __v; \
166})
167
168#define csr_write(csr, val) \
169({ \
170 unsigned long __v = (unsigned long)(val); \
Bin Menga27264c2019-07-10 23:43:12 -0700171 __asm__ __volatile__ ("csrw " __ASM_STR(csr) ", %0" \
Bin Meng055700e2018-09-26 06:55:14 -0700172 : : "rK" (__v) \
173 : "memory"); \
174})
175
176#define csr_read_set(csr, val) \
177({ \
178 unsigned long __v = (unsigned long)(val); \
Bin Menga27264c2019-07-10 23:43:12 -0700179 __asm__ __volatile__ ("csrrs %0, " __ASM_STR(csr) ", %1"\
Bin Meng055700e2018-09-26 06:55:14 -0700180 : "=r" (__v) : "rK" (__v) \
181 : "memory"); \
182 __v; \
183})
184
185#define csr_set(csr, val) \
186({ \
187 unsigned long __v = (unsigned long)(val); \
Bin Menga27264c2019-07-10 23:43:12 -0700188 __asm__ __volatile__ ("csrs " __ASM_STR(csr) ", %0" \
Bin Meng055700e2018-09-26 06:55:14 -0700189 : : "rK" (__v) \
190 : "memory"); \
191})
192
193#define csr_read_clear(csr, val) \
194({ \
195 unsigned long __v = (unsigned long)(val); \
Bin Menga27264c2019-07-10 23:43:12 -0700196 __asm__ __volatile__ ("csrrc %0, " __ASM_STR(csr) ", %1"\
Bin Meng055700e2018-09-26 06:55:14 -0700197 : "=r" (__v) : "rK" (__v) \
198 : "memory"); \
199 __v; \
200})
201
202#define csr_clear(csr, val) \
203({ \
204 unsigned long __v = (unsigned long)(val); \
Bin Menga27264c2019-07-10 23:43:12 -0700205 __asm__ __volatile__ ("csrc " __ASM_STR(csr) ", %0" \
Bin Meng055700e2018-09-26 06:55:14 -0700206 : : "rK" (__v) \
207 : "memory"); \
208})
209
210#endif /* __ASSEMBLY__ */
211
212#endif /* _ASM_RISCV_CSR_H */