xref: /openbmc/qemu/target/loongarch/cpu.h (revision d1b223dd)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * QEMU LoongArch CPU
4  *
5  * Copyright (c) 2021 Loongson Technology Corporation Limited
6  */
7 
8 #ifndef LOONGARCH_CPU_H
9 #define LOONGARCH_CPU_H
10 
11 #include "qemu/int128.h"
12 #include "exec/cpu-defs.h"
13 #include "fpu/softfloat-types.h"
14 #include "hw/registerfields.h"
15 #include "qemu/timer.h"
16 #ifndef CONFIG_USER_ONLY
17 #include "exec/memory.h"
18 #endif
19 #include "cpu-csr.h"
20 #include "cpu-qom.h"
21 
22 #define IOCSRF_TEMP             0
23 #define IOCSRF_NODECNT          1
24 #define IOCSRF_MSI              2
25 #define IOCSRF_EXTIOI           3
26 #define IOCSRF_CSRIPI           4
27 #define IOCSRF_FREQCSR          5
28 #define IOCSRF_FREQSCALE        6
29 #define IOCSRF_DVFSV1           7
30 #define IOCSRF_GMOD             9
31 #define IOCSRF_VM               11
32 
33 #define VERSION_REG             0x0
34 #define FEATURE_REG             0x8
35 #define VENDOR_REG              0x10
36 #define CPUNAME_REG             0x20
37 #define MISC_FUNC_REG           0x420
38 #define IOCSRM_EXTIOI_EN        48
39 
40 #define IOCSR_MEM_SIZE          0x428
41 
42 #define FCSR0_M1    0x1f         /* FCSR1 mask, Enables */
43 #define FCSR0_M2    0x1f1f0000   /* FCSR2 mask, Cause and Flags */
44 #define FCSR0_M3    0x300        /* FCSR3 mask, Round Mode */
45 #define FCSR0_RM    8            /* Round Mode bit num on fcsr0 */
46 
47 FIELD(FCSR0, ENABLES, 0, 5)
48 FIELD(FCSR0, RM, 8, 2)
49 FIELD(FCSR0, FLAGS, 16, 5)
50 FIELD(FCSR0, CAUSE, 24, 5)
51 
52 #define GET_FP_CAUSE(REG)      FIELD_EX32(REG, FCSR0, CAUSE)
53 #define SET_FP_CAUSE(REG, V) \
54     do { \
55         (REG) = FIELD_DP32(REG, FCSR0, CAUSE, V); \
56     } while (0)
57 #define UPDATE_FP_CAUSE(REG, V) \
58     do { \
59         (REG) |= FIELD_DP32(0, FCSR0, CAUSE, V); \
60     } while (0)
61 
62 #define GET_FP_ENABLES(REG)    FIELD_EX32(REG, FCSR0, ENABLES)
63 #define SET_FP_ENABLES(REG, V) \
64     do { \
65         (REG) = FIELD_DP32(REG, FCSR0, ENABLES, V); \
66     } while (0)
67 
68 #define GET_FP_FLAGS(REG)      FIELD_EX32(REG, FCSR0, FLAGS)
69 #define SET_FP_FLAGS(REG, V) \
70     do { \
71         (REG) = FIELD_DP32(REG, FCSR0, FLAGS, V); \
72     } while (0)
73 
74 #define UPDATE_FP_FLAGS(REG, V) \
75     do { \
76         (REG) |= FIELD_DP32(0, FCSR0, FLAGS, V); \
77     } while (0)
78 
79 #define FP_INEXACT        1
80 #define FP_UNDERFLOW      2
81 #define FP_OVERFLOW       4
82 #define FP_DIV0           8
83 #define FP_INVALID        16
84 
85 #define EXCODE(code, subcode) ( ((subcode) << 6) | (code) )
86 #define EXCODE_MCODE(code)    ( (code) & 0x3f )
87 #define EXCODE_SUBCODE(code)  ( (code) >> 6 )
88 
89 #define  EXCCODE_EXTERNAL_INT        64   /* plus external interrupt number */
90 #define  EXCCODE_INT                 EXCODE(0, 0)
91 #define  EXCCODE_PIL                 EXCODE(1, 0)
92 #define  EXCCODE_PIS                 EXCODE(2, 0)
93 #define  EXCCODE_PIF                 EXCODE(3, 0)
94 #define  EXCCODE_PME                 EXCODE(4, 0)
95 #define  EXCCODE_PNR                 EXCODE(5, 0)
96 #define  EXCCODE_PNX                 EXCODE(6, 0)
97 #define  EXCCODE_PPI                 EXCODE(7, 0)
98 #define  EXCCODE_ADEF                EXCODE(8, 0) /* Different exception subcode */
99 #define  EXCCODE_ADEM                EXCODE(8, 1)
100 #define  EXCCODE_ALE                 EXCODE(9, 0)
101 #define  EXCCODE_BCE                 EXCODE(10, 0)
102 #define  EXCCODE_SYS                 EXCODE(11, 0)
103 #define  EXCCODE_BRK                 EXCODE(12, 0)
104 #define  EXCCODE_INE                 EXCODE(13, 0)
105 #define  EXCCODE_IPE                 EXCODE(14, 0)
106 #define  EXCCODE_FPD                 EXCODE(15, 0)
107 #define  EXCCODE_SXD                 EXCODE(16, 0)
108 #define  EXCCODE_ASXD                EXCODE(17, 0)
109 #define  EXCCODE_FPE                 EXCODE(18, 0) /* Different exception subcode */
110 #define  EXCCODE_VFPE                EXCODE(18, 1)
111 #define  EXCCODE_WPEF                EXCODE(19, 0) /* Different exception subcode */
112 #define  EXCCODE_WPEM                EXCODE(19, 1)
113 #define  EXCCODE_BTD                 EXCODE(20, 0)
114 #define  EXCCODE_BTE                 EXCODE(21, 0)
115 #define  EXCCODE_DBP                 EXCODE(26, 0) /* Reserved subcode used for debug */
116 
117 /* cpucfg[0] bits */
118 FIELD(CPUCFG0, PRID, 0, 32)
119 
120 /* cpucfg[1] bits */
121 FIELD(CPUCFG1, ARCH, 0, 2)
122 FIELD(CPUCFG1, PGMMU, 2, 1)
123 FIELD(CPUCFG1, IOCSR, 3, 1)
124 FIELD(CPUCFG1, PALEN, 4, 8)
125 FIELD(CPUCFG1, VALEN, 12, 8)
126 FIELD(CPUCFG1, UAL, 20, 1)
127 FIELD(CPUCFG1, RI, 21, 1)
128 FIELD(CPUCFG1, EP, 22, 1)
129 FIELD(CPUCFG1, RPLV, 23, 1)
130 FIELD(CPUCFG1, HP, 24, 1)
131 FIELD(CPUCFG1, IOCSR_BRD, 25, 1)
132 FIELD(CPUCFG1, MSG_INT, 26, 1)
133 
134 /* cpucfg[1].arch */
135 #define CPUCFG1_ARCH_LA32R       0
136 #define CPUCFG1_ARCH_LA32        1
137 #define CPUCFG1_ARCH_LA64        2
138 
139 /* cpucfg[2] bits */
140 FIELD(CPUCFG2, FP, 0, 1)
141 FIELD(CPUCFG2, FP_SP, 1, 1)
142 FIELD(CPUCFG2, FP_DP, 2, 1)
143 FIELD(CPUCFG2, FP_VER, 3, 3)
144 FIELD(CPUCFG2, LSX, 6, 1)
145 FIELD(CPUCFG2, LASX, 7, 1)
146 FIELD(CPUCFG2, COMPLEX, 8, 1)
147 FIELD(CPUCFG2, CRYPTO, 9, 1)
148 FIELD(CPUCFG2, LVZ, 10, 1)
149 FIELD(CPUCFG2, LVZ_VER, 11, 3)
150 FIELD(CPUCFG2, LLFTP, 14, 1)
151 FIELD(CPUCFG2, LLFTP_VER, 15, 3)
152 FIELD(CPUCFG2, LBT_X86, 18, 1)
153 FIELD(CPUCFG2, LBT_ARM, 19, 1)
154 FIELD(CPUCFG2, LBT_MIPS, 20, 1)
155 FIELD(CPUCFG2, LSPW, 21, 1)
156 FIELD(CPUCFG2, LAM, 22, 1)
157 
158 /* cpucfg[3] bits */
159 FIELD(CPUCFG3, CCDMA, 0, 1)
160 FIELD(CPUCFG3, SFB, 1, 1)
161 FIELD(CPUCFG3, UCACC, 2, 1)
162 FIELD(CPUCFG3, LLEXC, 3, 1)
163 FIELD(CPUCFG3, SCDLY, 4, 1)
164 FIELD(CPUCFG3, LLDBAR, 5, 1)
165 FIELD(CPUCFG3, ITLBHMC, 6, 1)
166 FIELD(CPUCFG3, ICHMC, 7, 1)
167 FIELD(CPUCFG3, SPW_LVL, 8, 3)
168 FIELD(CPUCFG3, SPW_HP_HF, 11, 1)
169 FIELD(CPUCFG3, RVA, 12, 1)
170 FIELD(CPUCFG3, RVAMAX, 13, 4)
171 
172 /* cpucfg[4] bits */
173 FIELD(CPUCFG4, CC_FREQ, 0, 32)
174 
175 /* cpucfg[5] bits */
176 FIELD(CPUCFG5, CC_MUL, 0, 16)
177 FIELD(CPUCFG5, CC_DIV, 16, 16)
178 
179 /* cpucfg[6] bits */
180 FIELD(CPUCFG6, PMP, 0, 1)
181 FIELD(CPUCFG6, PMVER, 1, 3)
182 FIELD(CPUCFG6, PMNUM, 4, 4)
183 FIELD(CPUCFG6, PMBITS, 8, 6)
184 FIELD(CPUCFG6, UPM, 14, 1)
185 
186 /* cpucfg[16] bits */
187 FIELD(CPUCFG16, L1_IUPRE, 0, 1)
188 FIELD(CPUCFG16, L1_IUUNIFY, 1, 1)
189 FIELD(CPUCFG16, L1_DPRE, 2, 1)
190 FIELD(CPUCFG16, L2_IUPRE, 3, 1)
191 FIELD(CPUCFG16, L2_IUUNIFY, 4, 1)
192 FIELD(CPUCFG16, L2_IUPRIV, 5, 1)
193 FIELD(CPUCFG16, L2_IUINCL, 6, 1)
194 FIELD(CPUCFG16, L2_DPRE, 7, 1)
195 FIELD(CPUCFG16, L2_DPRIV, 8, 1)
196 FIELD(CPUCFG16, L2_DINCL, 9, 1)
197 FIELD(CPUCFG16, L3_IUPRE, 10, 1)
198 FIELD(CPUCFG16, L3_IUUNIFY, 11, 1)
199 FIELD(CPUCFG16, L3_IUPRIV, 12, 1)
200 FIELD(CPUCFG16, L3_IUINCL, 13, 1)
201 FIELD(CPUCFG16, L3_DPRE, 14, 1)
202 FIELD(CPUCFG16, L3_DPRIV, 15, 1)
203 FIELD(CPUCFG16, L3_DINCL, 16, 1)
204 
205 /* cpucfg[17] bits */
206 FIELD(CPUCFG17, L1IU_WAYS, 0, 16)
207 FIELD(CPUCFG17, L1IU_SETS, 16, 8)
208 FIELD(CPUCFG17, L1IU_SIZE, 24, 7)
209 
210 /* cpucfg[18] bits */
211 FIELD(CPUCFG18, L1D_WAYS, 0, 16)
212 FIELD(CPUCFG18, L1D_SETS, 16, 8)
213 FIELD(CPUCFG18, L1D_SIZE, 24, 7)
214 
215 /* cpucfg[19] bits */
216 FIELD(CPUCFG19, L2IU_WAYS, 0, 16)
217 FIELD(CPUCFG19, L2IU_SETS, 16, 8)
218 FIELD(CPUCFG19, L2IU_SIZE, 24, 7)
219 
220 /* cpucfg[20] bits */
221 FIELD(CPUCFG20, L3IU_WAYS, 0, 16)
222 FIELD(CPUCFG20, L3IU_SETS, 16, 8)
223 FIELD(CPUCFG20, L3IU_SIZE, 24, 7)
224 
225 /*CSR_CRMD */
226 FIELD(CSR_CRMD, PLV, 0, 2)
227 FIELD(CSR_CRMD, IE, 2, 1)
228 FIELD(CSR_CRMD, DA, 3, 1)
229 FIELD(CSR_CRMD, PG, 4, 1)
230 FIELD(CSR_CRMD, DATF, 5, 2)
231 FIELD(CSR_CRMD, DATM, 7, 2)
232 FIELD(CSR_CRMD, WE, 9, 1)
233 
234 extern const char * const regnames[32];
235 extern const char * const fregnames[32];
236 
237 #define N_IRQS      13
238 #define IRQ_TIMER   11
239 #define IRQ_IPI     12
240 
241 #define LOONGARCH_STLB         2048 /* 2048 STLB */
242 #define LOONGARCH_MTLB         64   /* 64 MTLB */
243 #define LOONGARCH_TLB_MAX      (LOONGARCH_STLB + LOONGARCH_MTLB)
244 
245 /*
246  * define the ASID PS E VPPN field of TLB
247  */
248 FIELD(TLB_MISC, E, 0, 1)
249 FIELD(TLB_MISC, ASID, 1, 10)
250 FIELD(TLB_MISC, VPPN, 13, 35)
251 FIELD(TLB_MISC, PS, 48, 6)
252 
253 #define LSX_LEN    (128)
254 #define LASX_LEN   (256)
255 
256 typedef union VReg {
257     int8_t   B[LASX_LEN / 8];
258     int16_t  H[LASX_LEN / 16];
259     int32_t  W[LASX_LEN / 32];
260     int64_t  D[LASX_LEN / 64];
261     uint8_t  UB[LASX_LEN / 8];
262     uint16_t UH[LASX_LEN / 16];
263     uint32_t UW[LASX_LEN / 32];
264     uint64_t UD[LASX_LEN / 64];
265     Int128   Q[LASX_LEN / 128];
266 } VReg;
267 
268 typedef union fpr_t fpr_t;
269 union fpr_t {
270     VReg  vreg;
271 };
272 
273 #ifdef CONFIG_TCG
274 struct LoongArchTLB {
275     uint64_t tlb_misc;
276     /* Fields corresponding to CSR_TLBELO0/1 */
277     uint64_t tlb_entry0;
278     uint64_t tlb_entry1;
279 };
280 typedef struct LoongArchTLB LoongArchTLB;
281 #endif
282 
283 typedef struct CPUArchState {
284     uint64_t gpr[32];
285     uint64_t pc;
286 
287     fpr_t fpr[32];
288     bool cf[8];
289     uint32_t fcsr0;
290 
291     uint32_t cpucfg[21];
292 
293     /* LoongArch CSRs */
294     uint64_t CSR_CRMD;
295     uint64_t CSR_PRMD;
296     uint64_t CSR_EUEN;
297     uint64_t CSR_MISC;
298     uint64_t CSR_ECFG;
299     uint64_t CSR_ESTAT;
300     uint64_t CSR_ERA;
301     uint64_t CSR_BADV;
302     uint64_t CSR_BADI;
303     uint64_t CSR_EENTRY;
304     uint64_t CSR_TLBIDX;
305     uint64_t CSR_TLBEHI;
306     uint64_t CSR_TLBELO0;
307     uint64_t CSR_TLBELO1;
308     uint64_t CSR_ASID;
309     uint64_t CSR_PGDL;
310     uint64_t CSR_PGDH;
311     uint64_t CSR_PGD;
312     uint64_t CSR_PWCL;
313     uint64_t CSR_PWCH;
314     uint64_t CSR_STLBPS;
315     uint64_t CSR_RVACFG;
316     uint64_t CSR_CPUID;
317     uint64_t CSR_PRCFG1;
318     uint64_t CSR_PRCFG2;
319     uint64_t CSR_PRCFG3;
320     uint64_t CSR_SAVE[16];
321     uint64_t CSR_TID;
322     uint64_t CSR_TCFG;
323     uint64_t CSR_TVAL;
324     uint64_t CSR_CNTC;
325     uint64_t CSR_TICLR;
326     uint64_t CSR_LLBCTL;
327     uint64_t CSR_IMPCTL1;
328     uint64_t CSR_IMPCTL2;
329     uint64_t CSR_TLBRENTRY;
330     uint64_t CSR_TLBRBADV;
331     uint64_t CSR_TLBRERA;
332     uint64_t CSR_TLBRSAVE;
333     uint64_t CSR_TLBRELO0;
334     uint64_t CSR_TLBRELO1;
335     uint64_t CSR_TLBREHI;
336     uint64_t CSR_TLBRPRMD;
337     uint64_t CSR_MERRCTL;
338     uint64_t CSR_MERRINFO1;
339     uint64_t CSR_MERRINFO2;
340     uint64_t CSR_MERRENTRY;
341     uint64_t CSR_MERRERA;
342     uint64_t CSR_MERRSAVE;
343     uint64_t CSR_CTAG;
344     uint64_t CSR_DMW[4];
345     uint64_t CSR_DBG;
346     uint64_t CSR_DERA;
347     uint64_t CSR_DSAVE;
348 
349 #ifdef CONFIG_TCG
350     float_status fp_status;
351     uint32_t fcsr0_mask;
352     uint64_t lladdr; /* LL virtual address compared against SC */
353     uint64_t llval;
354 #endif
355 #ifndef CONFIG_USER_ONLY
356 #ifdef CONFIG_TCG
357     LoongArchTLB  tlb[LOONGARCH_TLB_MAX];
358 #endif
359 
360     AddressSpace *address_space_iocsr;
361     bool load_elf;
362     uint64_t elf_address;
363     uint32_t mp_state;
364     /* Store ipistate to access from this struct */
365     DeviceState *ipistate;
366 
367     struct loongarch_boot_info *boot_info;
368 #endif
369 } CPULoongArchState;
370 
371 /**
372  * LoongArchCPU:
373  * @env: #CPULoongArchState
374  *
375  * A LoongArch CPU.
376  */
377 struct ArchCPU {
378     CPUState parent_obj;
379 
380     CPULoongArchState env;
381     QEMUTimer timer;
382     uint32_t  phy_id;
383 
384     /* 'compatible' string for this CPU for Linux device trees */
385     const char *dtb_compatible;
386     /* used by KVM_REG_LOONGARCH_COUNTER ioctl to access guest time counters */
387     uint64_t kvm_state_counter;
388 };
389 
390 /**
391  * LoongArchCPUClass:
392  * @parent_realize: The parent class' realize handler.
393  * @parent_phases: The parent class' reset phase handlers.
394  *
395  * A LoongArch CPU model.
396  */
397 struct LoongArchCPUClass {
398     CPUClass parent_class;
399 
400     DeviceRealize parent_realize;
401     ResettablePhases parent_phases;
402 };
403 
404 /*
405  * LoongArch CPUs has 4 privilege levels.
406  * 0 for kernel mode, 3 for user mode.
407  * Define an extra index for DA(direct addressing) mode.
408  */
409 #define MMU_PLV_KERNEL   0
410 #define MMU_PLV_USER     3
411 #define MMU_KERNEL_IDX   MMU_PLV_KERNEL
412 #define MMU_USER_IDX     MMU_PLV_USER
413 #define MMU_DA_IDX       4
414 
415 static inline bool is_la64(CPULoongArchState *env)
416 {
417     return FIELD_EX32(env->cpucfg[1], CPUCFG1, ARCH) == CPUCFG1_ARCH_LA64;
418 }
419 
420 static inline bool is_va32(CPULoongArchState *env)
421 {
422     /* VA32 if !LA64 or VA32L[1-3] */
423     bool va32 = !is_la64(env);
424     uint64_t plv = FIELD_EX64(env->CSR_CRMD, CSR_CRMD, PLV);
425     if (plv >= 1 && (FIELD_EX64(env->CSR_MISC, CSR_MISC, VA32) & (1 << plv))) {
426         va32 = true;
427     }
428     return va32;
429 }
430 
431 static inline void set_pc(CPULoongArchState *env, uint64_t value)
432 {
433     if (is_va32(env)) {
434         env->pc = (uint32_t)value;
435     } else {
436         env->pc = value;
437     }
438 }
439 
440 /*
441  * LoongArch CPUs hardware flags.
442  */
443 #define HW_FLAGS_PLV_MASK   R_CSR_CRMD_PLV_MASK  /* 0x03 */
444 #define HW_FLAGS_EUEN_FPE   0x04
445 #define HW_FLAGS_EUEN_SXE   0x08
446 #define HW_FLAGS_CRMD_PG    R_CSR_CRMD_PG_MASK   /* 0x10 */
447 #define HW_FLAGS_VA32       0x20
448 #define HW_FLAGS_EUEN_ASXE  0x40
449 
450 static inline void cpu_get_tb_cpu_state(CPULoongArchState *env, vaddr *pc,
451                                         uint64_t *cs_base, uint32_t *flags)
452 {
453     *pc = env->pc;
454     *cs_base = 0;
455     *flags = env->CSR_CRMD & (R_CSR_CRMD_PLV_MASK | R_CSR_CRMD_PG_MASK);
456     *flags |= FIELD_EX64(env->CSR_EUEN, CSR_EUEN, FPE) * HW_FLAGS_EUEN_FPE;
457     *flags |= FIELD_EX64(env->CSR_EUEN, CSR_EUEN, SXE) * HW_FLAGS_EUEN_SXE;
458     *flags |= FIELD_EX64(env->CSR_EUEN, CSR_EUEN, ASXE) * HW_FLAGS_EUEN_ASXE;
459     *flags |= is_va32(env) * HW_FLAGS_VA32;
460 }
461 
462 #include "exec/cpu-all.h"
463 
464 #define CPU_RESOLVING_TYPE TYPE_LOONGARCH_CPU
465 
466 void loongarch_cpu_post_init(Object *obj);
467 
468 #endif /* LOONGARCH_CPU_H */
469