xref: /linux/arch/loongarch/include/asm/kvm_csr.h (revision 64dd3b6a79f0907d36de481b0f15fab323a53e5a)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2020-2023 Loongson Technology Corporation Limited
4  */
5 
6 #ifndef __ASM_LOONGARCH_KVM_CSR_H__
7 #define __ASM_LOONGARCH_KVM_CSR_H__
8 
9 #include <linux/uaccess.h>
10 #include <linux/kvm_host.h>
11 #include <asm/loongarch.h>
12 #include <asm/kvm_vcpu.h>
13 
14 #define gcsr_read(csr)						\
15 ({								\
16 	register unsigned long __v;				\
17 	__asm__ __volatile__(					\
18 		" gcsrrd %[val], %[reg]\n\t"			\
19 		: [val] "=r" (__v)				\
20 		: [reg] "i" (csr)				\
21 		: "memory");					\
22 	__v;							\
23 })
24 
25 #define gcsr_write(v, csr)					\
26 ({								\
27 	register unsigned long __v = v;				\
28 	__asm__ __volatile__ (					\
29 		" gcsrwr %[val], %[reg]\n\t"			\
30 		: [val] "+r" (__v)				\
31 		: [reg] "i" (csr)				\
32 		: "memory");					\
33 	__v;							\
34 })
35 
36 #define gcsr_xchg(v, m, csr)					\
37 ({								\
38 	register unsigned long __v = v;				\
39 	__asm__ __volatile__(					\
40 		" gcsrxchg %[val], %[mask], %[reg]\n\t"		\
41 		: [val] "+r" (__v)				\
42 		: [mask] "r" (m), [reg] "i" (csr)		\
43 		: "memory");					\
44 	__v;							\
45 })
46 
47 /* Guest CSRS read and write */
48 #define read_gcsr_crmd()		gcsr_read(LOONGARCH_CSR_CRMD)
49 #define write_gcsr_crmd(val)		gcsr_write(val, LOONGARCH_CSR_CRMD)
50 #define read_gcsr_prmd()		gcsr_read(LOONGARCH_CSR_PRMD)
51 #define write_gcsr_prmd(val)		gcsr_write(val, LOONGARCH_CSR_PRMD)
52 #define read_gcsr_euen()		gcsr_read(LOONGARCH_CSR_EUEN)
53 #define write_gcsr_euen(val)		gcsr_write(val, LOONGARCH_CSR_EUEN)
54 #define read_gcsr_misc()		gcsr_read(LOONGARCH_CSR_MISC)
55 #define write_gcsr_misc(val)		gcsr_write(val, LOONGARCH_CSR_MISC)
56 #define read_gcsr_ecfg()		gcsr_read(LOONGARCH_CSR_ECFG)
57 #define write_gcsr_ecfg(val)		gcsr_write(val, LOONGARCH_CSR_ECFG)
58 #define read_gcsr_estat()		gcsr_read(LOONGARCH_CSR_ESTAT)
59 #define write_gcsr_estat(val)		gcsr_write(val, LOONGARCH_CSR_ESTAT)
60 #define read_gcsr_era()			gcsr_read(LOONGARCH_CSR_ERA)
61 #define write_gcsr_era(val)		gcsr_write(val, LOONGARCH_CSR_ERA)
62 #define read_gcsr_badv()		gcsr_read(LOONGARCH_CSR_BADV)
63 #define write_gcsr_badv(val)		gcsr_write(val, LOONGARCH_CSR_BADV)
64 #define read_gcsr_badi()		gcsr_read(LOONGARCH_CSR_BADI)
65 #define write_gcsr_badi(val)		gcsr_write(val, LOONGARCH_CSR_BADI)
66 #define read_gcsr_eentry()		gcsr_read(LOONGARCH_CSR_EENTRY)
67 #define write_gcsr_eentry(val)		gcsr_write(val, LOONGARCH_CSR_EENTRY)
68 
69 #define read_gcsr_asid()		gcsr_read(LOONGARCH_CSR_ASID)
70 #define write_gcsr_asid(val)		gcsr_write(val, LOONGARCH_CSR_ASID)
71 #define read_gcsr_pgdl()		gcsr_read(LOONGARCH_CSR_PGDL)
72 #define write_gcsr_pgdl(val)		gcsr_write(val, LOONGARCH_CSR_PGDL)
73 #define read_gcsr_pgdh()		gcsr_read(LOONGARCH_CSR_PGDH)
74 #define write_gcsr_pgdh(val)		gcsr_write(val, LOONGARCH_CSR_PGDH)
75 #define write_gcsr_pgd(val)		gcsr_write(val, LOONGARCH_CSR_PGD)
76 #define read_gcsr_pgd()			gcsr_read(LOONGARCH_CSR_PGD)
77 #define read_gcsr_pwctl0()		gcsr_read(LOONGARCH_CSR_PWCTL0)
78 #define write_gcsr_pwctl0(val)		gcsr_write(val, LOONGARCH_CSR_PWCTL0)
79 #define read_gcsr_pwctl1()		gcsr_read(LOONGARCH_CSR_PWCTL1)
80 #define write_gcsr_pwctl1(val)		gcsr_write(val, LOONGARCH_CSR_PWCTL1)
81 #define read_gcsr_stlbpgsize()		gcsr_read(LOONGARCH_CSR_STLBPGSIZE)
82 #define write_gcsr_stlbpgsize(val)	gcsr_write(val, LOONGARCH_CSR_STLBPGSIZE)
83 #define read_gcsr_rvacfg()		gcsr_read(LOONGARCH_CSR_RVACFG)
84 #define write_gcsr_rvacfg(val)		gcsr_write(val, LOONGARCH_CSR_RVACFG)
85 
86 #define read_gcsr_cpuid()		gcsr_read(LOONGARCH_CSR_CPUID)
87 #define write_gcsr_cpuid(val)		gcsr_write(val, LOONGARCH_CSR_CPUID)
88 #define read_gcsr_prcfg1()		gcsr_read(LOONGARCH_CSR_PRCFG1)
89 #define write_gcsr_prcfg1(val)		gcsr_write(val, LOONGARCH_CSR_PRCFG1)
90 #define read_gcsr_prcfg2()		gcsr_read(LOONGARCH_CSR_PRCFG2)
91 #define write_gcsr_prcfg2(val)		gcsr_write(val, LOONGARCH_CSR_PRCFG2)
92 #define read_gcsr_prcfg3()		gcsr_read(LOONGARCH_CSR_PRCFG3)
93 #define write_gcsr_prcfg3(val)		gcsr_write(val, LOONGARCH_CSR_PRCFG3)
94 
95 #define read_gcsr_kscratch0()		gcsr_read(LOONGARCH_CSR_KS0)
96 #define write_gcsr_kscratch0(val)	gcsr_write(val, LOONGARCH_CSR_KS0)
97 #define read_gcsr_kscratch1()		gcsr_read(LOONGARCH_CSR_KS1)
98 #define write_gcsr_kscratch1(val)	gcsr_write(val, LOONGARCH_CSR_KS1)
99 #define read_gcsr_kscratch2()		gcsr_read(LOONGARCH_CSR_KS2)
100 #define write_gcsr_kscratch2(val)	gcsr_write(val, LOONGARCH_CSR_KS2)
101 #define read_gcsr_kscratch3()		gcsr_read(LOONGARCH_CSR_KS3)
102 #define write_gcsr_kscratch3(val)	gcsr_write(val, LOONGARCH_CSR_KS3)
103 #define read_gcsr_kscratch4()		gcsr_read(LOONGARCH_CSR_KS4)
104 #define write_gcsr_kscratch4(val)	gcsr_write(val, LOONGARCH_CSR_KS4)
105 #define read_gcsr_kscratch5()		gcsr_read(LOONGARCH_CSR_KS5)
106 #define write_gcsr_kscratch5(val)	gcsr_write(val, LOONGARCH_CSR_KS5)
107 #define read_gcsr_kscratch6()		gcsr_read(LOONGARCH_CSR_KS6)
108 #define write_gcsr_kscratch6(val)	gcsr_write(val, LOONGARCH_CSR_KS6)
109 #define read_gcsr_kscratch7()		gcsr_read(LOONGARCH_CSR_KS7)
110 #define write_gcsr_kscratch7(val)	gcsr_write(val, LOONGARCH_CSR_KS7)
111 
112 #define read_gcsr_timerid()		gcsr_read(LOONGARCH_CSR_TMID)
113 #define write_gcsr_timerid(val)		gcsr_write(val, LOONGARCH_CSR_TMID)
114 #define read_gcsr_timercfg()		gcsr_read(LOONGARCH_CSR_TCFG)
115 #define write_gcsr_timercfg(val)	gcsr_write(val, LOONGARCH_CSR_TCFG)
116 #define read_gcsr_timertick()		gcsr_read(LOONGARCH_CSR_TVAL)
117 #define write_gcsr_timertick(val)	gcsr_write(val, LOONGARCH_CSR_TVAL)
118 #define read_gcsr_timeroffset()		gcsr_read(LOONGARCH_CSR_CNTC)
119 #define write_gcsr_timeroffset(val)	gcsr_write(val, LOONGARCH_CSR_CNTC)
120 
121 #define read_gcsr_llbctl()		gcsr_read(LOONGARCH_CSR_LLBCTL)
122 #define write_gcsr_llbctl(val)		gcsr_write(val, LOONGARCH_CSR_LLBCTL)
123 
124 #define read_gcsr_tlbidx()		gcsr_read(LOONGARCH_CSR_TLBIDX)
125 #define write_gcsr_tlbidx(val)		gcsr_write(val, LOONGARCH_CSR_TLBIDX)
126 #define read_gcsr_tlbrentry()		gcsr_read(LOONGARCH_CSR_TLBRENTRY)
127 #define write_gcsr_tlbrentry(val)	gcsr_write(val, LOONGARCH_CSR_TLBRENTRY)
128 #define read_gcsr_tlbrbadv()		gcsr_read(LOONGARCH_CSR_TLBRBADV)
129 #define write_gcsr_tlbrbadv(val)	gcsr_write(val, LOONGARCH_CSR_TLBRBADV)
130 #define read_gcsr_tlbrera()		gcsr_read(LOONGARCH_CSR_TLBRERA)
131 #define write_gcsr_tlbrera(val)		gcsr_write(val, LOONGARCH_CSR_TLBRERA)
132 #define read_gcsr_tlbrsave()		gcsr_read(LOONGARCH_CSR_TLBRSAVE)
133 #define write_gcsr_tlbrsave(val)	gcsr_write(val, LOONGARCH_CSR_TLBRSAVE)
134 #define read_gcsr_tlbrelo0()		gcsr_read(LOONGARCH_CSR_TLBRELO0)
135 #define write_gcsr_tlbrelo0(val)	gcsr_write(val, LOONGARCH_CSR_TLBRELO0)
136 #define read_gcsr_tlbrelo1()		gcsr_read(LOONGARCH_CSR_TLBRELO1)
137 #define write_gcsr_tlbrelo1(val)	gcsr_write(val, LOONGARCH_CSR_TLBRELO1)
138 #define read_gcsr_tlbrehi()		gcsr_read(LOONGARCH_CSR_TLBREHI)
139 #define write_gcsr_tlbrehi(val)		gcsr_write(val, LOONGARCH_CSR_TLBREHI)
140 #define read_gcsr_tlbrprmd()		gcsr_read(LOONGARCH_CSR_TLBRPRMD)
141 #define write_gcsr_tlbrprmd(val)	gcsr_write(val, LOONGARCH_CSR_TLBRPRMD)
142 
143 #define read_gcsr_directwin0()		gcsr_read(LOONGARCH_CSR_DMWIN0)
144 #define write_gcsr_directwin0(val)	gcsr_write(val, LOONGARCH_CSR_DMWIN0)
145 #define read_gcsr_directwin1()		gcsr_read(LOONGARCH_CSR_DMWIN1)
146 #define write_gcsr_directwin1(val)	gcsr_write(val, LOONGARCH_CSR_DMWIN1)
147 #define read_gcsr_directwin2()		gcsr_read(LOONGARCH_CSR_DMWIN2)
148 #define write_gcsr_directwin2(val)	gcsr_write(val, LOONGARCH_CSR_DMWIN2)
149 #define read_gcsr_directwin3()		gcsr_read(LOONGARCH_CSR_DMWIN3)
150 #define write_gcsr_directwin3(val)	gcsr_write(val, LOONGARCH_CSR_DMWIN3)
151 
152 /* Guest related CSRs */
153 #define read_csr_gtlbc()		csr_read64(LOONGARCH_CSR_GTLBC)
154 #define write_csr_gtlbc(val)		csr_write64(val, LOONGARCH_CSR_GTLBC)
155 #define read_csr_trgp()			csr_read64(LOONGARCH_CSR_TRGP)
156 #define read_csr_gcfg()			csr_read64(LOONGARCH_CSR_GCFG)
157 #define write_csr_gcfg(val)		csr_write64(val, LOONGARCH_CSR_GCFG)
158 #define read_csr_gstat()		csr_read64(LOONGARCH_CSR_GSTAT)
159 #define write_csr_gstat(val)		csr_write64(val, LOONGARCH_CSR_GSTAT)
160 #define read_csr_gintc()		csr_read64(LOONGARCH_CSR_GINTC)
161 #define write_csr_gintc(val)		csr_write64(val, LOONGARCH_CSR_GINTC)
162 #define read_csr_gcntc()		csr_read64(LOONGARCH_CSR_GCNTC)
163 #define write_csr_gcntc(val)		csr_write64(val, LOONGARCH_CSR_GCNTC)
164 
165 #define __BUILD_GCSR_OP(name)		__BUILD_CSR_COMMON(gcsr_##name)
166 
167 __BUILD_CSR_OP(gcfg)
168 __BUILD_CSR_OP(gstat)
169 __BUILD_CSR_OP(gtlbc)
170 __BUILD_CSR_OP(gintc)
171 __BUILD_GCSR_OP(llbctl)
172 __BUILD_GCSR_OP(tlbidx)
173 
174 #define set_gcsr_estat(val)	\
175 	gcsr_xchg(val, val, LOONGARCH_CSR_ESTAT)
176 #define clear_gcsr_estat(val)	\
177 	gcsr_xchg(~(val), val, LOONGARCH_CSR_ESTAT)
178 
179 #define kvm_read_hw_gcsr(id)		gcsr_read(id)
180 #define kvm_write_hw_gcsr(id, val)	gcsr_write(val, id)
181 
182 #define kvm_save_hw_gcsr(csr, gid)	(csr->csrs[gid] = gcsr_read(gid))
183 #define kvm_restore_hw_gcsr(csr, gid)	(gcsr_write(csr->csrs[gid], gid))
184 
185 #define kvm_read_clear_hw_gcsr(csr, gid)	(csr->csrs[gid] = gcsr_write(0, gid))
186 
187 int kvm_emu_iocsr(larch_inst inst, struct kvm_run *run, struct kvm_vcpu *vcpu);
188 
kvm_read_sw_gcsr(struct loongarch_csrs * csr,int gid)189 static __always_inline unsigned long kvm_read_sw_gcsr(struct loongarch_csrs *csr, int gid)
190 {
191 	return csr->csrs[gid];
192 }
193 
kvm_write_sw_gcsr(struct loongarch_csrs * csr,int gid,unsigned long val)194 static __always_inline void kvm_write_sw_gcsr(struct loongarch_csrs *csr, int gid, unsigned long val)
195 {
196 	csr->csrs[gid] = val;
197 }
198 
kvm_set_sw_gcsr(struct loongarch_csrs * csr,int gid,unsigned long val)199 static __always_inline void kvm_set_sw_gcsr(struct loongarch_csrs *csr,
200 					    int gid, unsigned long val)
201 {
202 	csr->csrs[gid] |= val;
203 }
204 
kvm_change_sw_gcsr(struct loongarch_csrs * csr,int gid,unsigned long mask,unsigned long val)205 static __always_inline void kvm_change_sw_gcsr(struct loongarch_csrs *csr,
206 					       int gid, unsigned long mask, unsigned long val)
207 {
208 	unsigned long _mask = mask;
209 
210 	csr->csrs[gid] &= ~_mask;
211 	csr->csrs[gid] |= val & _mask;
212 }
213 
214 #define KVM_PMU_EVENT_ENABLED	(CSR_PERFCTRL_PLV0 | CSR_PERFCTRL_PLV1 | \
215 					CSR_PERFCTRL_PLV2 | CSR_PERFCTRL_PLV3)
216 
217 #endif	/* __ASM_LOONGARCH_KVM_CSR_H__ */
218