xref: /openbmc/qemu/target/mips/internal.h (revision 1ab3a0de)
1 /*
2  * MIPS internal definitions and helpers
3  *
4  * This work is licensed under the terms of the GNU GPL, version 2 or later.
5  * See the COPYING file in the top-level directory.
6  */
7 
8 #ifndef MIPS_INTERNAL_H
9 #define MIPS_INTERNAL_H
10 
11 #include "exec/memattrs.h"
12 #include "fpu/softfloat-helpers.h"
13 
14 /*
15  * MMU types, the first four entries have the same layout as the
16  * CP0C0_MT field.
17  */
18 enum mips_mmu_types {
19     MMU_TYPE_NONE       = 0,
20     MMU_TYPE_R4000      = 1,    /* Standard TLB */
21     MMU_TYPE_BAT        = 2,    /* Block Address Translation */
22     MMU_TYPE_FMT        = 3,    /* Fixed Mapping */
23     MMU_TYPE_DVF        = 4,    /* Dual VTLB and FTLB */
24     MMU_TYPE_R3000,
25     MMU_TYPE_R6000,
26     MMU_TYPE_R8000
27 };
28 
29 struct mips_def_t {
30     const char *name;
31     int32_t CP0_PRid;
32     int32_t CP0_Config0;
33     int32_t CP0_Config1;
34     int32_t CP0_Config2;
35     int32_t CP0_Config3;
36     int32_t CP0_Config4;
37     int32_t CP0_Config4_rw_bitmask;
38     int32_t CP0_Config5;
39     int32_t CP0_Config5_rw_bitmask;
40     int32_t CP0_Config6;
41     int32_t CP0_Config6_rw_bitmask;
42     int32_t CP0_Config7;
43     int32_t CP0_Config7_rw_bitmask;
44     target_ulong CP0_LLAddr_rw_bitmask;
45     int CP0_LLAddr_shift;
46     int32_t SYNCI_Step;
47     int32_t CCRes;
48     int32_t CP0_Status_rw_bitmask;
49     int32_t CP0_TCStatus_rw_bitmask;
50     int32_t CP0_SRSCtl;
51     int32_t CP1_fcr0;
52     int32_t CP1_fcr31_rw_bitmask;
53     int32_t CP1_fcr31;
54     int32_t MSAIR;
55     int32_t SEGBITS;
56     int32_t PABITS;
57     int32_t CP0_SRSConf0_rw_bitmask;
58     int32_t CP0_SRSConf0;
59     int32_t CP0_SRSConf1_rw_bitmask;
60     int32_t CP0_SRSConf1;
61     int32_t CP0_SRSConf2_rw_bitmask;
62     int32_t CP0_SRSConf2;
63     int32_t CP0_SRSConf3_rw_bitmask;
64     int32_t CP0_SRSConf3;
65     int32_t CP0_SRSConf4_rw_bitmask;
66     int32_t CP0_SRSConf4;
67     int32_t CP0_PageGrain_rw_bitmask;
68     int32_t CP0_PageGrain;
69     target_ulong CP0_EBaseWG_rw_bitmask;
70     uint64_t insn_flags;
71     enum mips_mmu_types mmu_type;
72     int32_t SAARP;
73 };
74 
75 extern const struct mips_def_t mips_defs[];
76 extern const int mips_defs_number;
77 
78 enum CPUMIPSMSADataFormat {
79     DF_BYTE = 0,
80     DF_HALF,
81     DF_WORD,
82     DF_DOUBLE
83 };
84 
85 void mips_cpu_do_interrupt(CPUState *cpu);
86 bool mips_cpu_exec_interrupt(CPUState *cpu, int int_req);
87 void mips_cpu_dump_state(CPUState *cpu, FILE *f, int flags);
88 hwaddr mips_cpu_get_phys_page_debug(CPUState *cpu, vaddr addr);
89 int mips_cpu_gdb_read_register(CPUState *cpu, GByteArray *buf, int reg);
90 int mips_cpu_gdb_write_register(CPUState *cpu, uint8_t *buf, int reg);
91 void mips_cpu_do_unaligned_access(CPUState *cpu, vaddr addr,
92                                   MMUAccessType access_type,
93                                   int mmu_idx, uintptr_t retaddr);
94 
95 #if !defined(CONFIG_USER_ONLY)
96 
97 typedef struct r4k_tlb_t r4k_tlb_t;
98 struct r4k_tlb_t {
99     target_ulong VPN;
100     uint32_t PageMask;
101     uint16_t ASID;
102     uint32_t MMID;
103     unsigned int G:1;
104     unsigned int C0:3;
105     unsigned int C1:3;
106     unsigned int V0:1;
107     unsigned int V1:1;
108     unsigned int D0:1;
109     unsigned int D1:1;
110     unsigned int XI0:1;
111     unsigned int XI1:1;
112     unsigned int RI0:1;
113     unsigned int RI1:1;
114     unsigned int EHINV:1;
115     uint64_t PFN[2];
116 };
117 
118 struct CPUMIPSTLBContext {
119     uint32_t nb_tlb;
120     uint32_t tlb_in_use;
121     int (*map_address)(struct CPUMIPSState *env, hwaddr *physical, int *prot,
122                        target_ulong address, int rw, int access_type);
123     void (*helper_tlbwi)(struct CPUMIPSState *env);
124     void (*helper_tlbwr)(struct CPUMIPSState *env);
125     void (*helper_tlbp)(struct CPUMIPSState *env);
126     void (*helper_tlbr)(struct CPUMIPSState *env);
127     void (*helper_tlbinv)(struct CPUMIPSState *env);
128     void (*helper_tlbinvf)(struct CPUMIPSState *env);
129     union {
130         struct {
131             r4k_tlb_t tlb[MIPS_TLB_MAX];
132         } r4k;
133     } mmu;
134 };
135 
136 int no_mmu_map_address(CPUMIPSState *env, hwaddr *physical, int *prot,
137                        target_ulong address, int rw, int access_type);
138 int fixed_mmu_map_address(CPUMIPSState *env, hwaddr *physical, int *prot,
139                           target_ulong address, int rw, int access_type);
140 int r4k_map_address(CPUMIPSState *env, hwaddr *physical, int *prot,
141                     target_ulong address, int rw, int access_type);
142 void r4k_helper_tlbwi(CPUMIPSState *env);
143 void r4k_helper_tlbwr(CPUMIPSState *env);
144 void r4k_helper_tlbp(CPUMIPSState *env);
145 void r4k_helper_tlbr(CPUMIPSState *env);
146 void r4k_helper_tlbinv(CPUMIPSState *env);
147 void r4k_helper_tlbinvf(CPUMIPSState *env);
148 void r4k_invalidate_tlb(CPUMIPSState *env, int idx, int use_extra);
149 uint32_t cpu_mips_get_random(CPUMIPSState *env);
150 
151 void mips_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr,
152                                     vaddr addr, unsigned size,
153                                     MMUAccessType access_type,
154                                     int mmu_idx, MemTxAttrs attrs,
155                                     MemTxResult response, uintptr_t retaddr);
156 hwaddr cpu_mips_translate_address(CPUMIPSState *env, target_ulong address,
157                                   int rw);
158 #endif
159 
160 #define cpu_signal_handler cpu_mips_signal_handler
161 
162 #ifndef CONFIG_USER_ONLY
163 extern const VMStateDescription vmstate_mips_cpu;
164 #endif
165 
166 static inline bool cpu_mips_hw_interrupts_enabled(CPUMIPSState *env)
167 {
168     return (env->CP0_Status & (1 << CP0St_IE)) &&
169         !(env->CP0_Status & (1 << CP0St_EXL)) &&
170         !(env->CP0_Status & (1 << CP0St_ERL)) &&
171         !(env->hflags & MIPS_HFLAG_DM) &&
172         /*
173          * Note that the TCStatus IXMT field is initialized to zero,
174          * and only MT capable cores can set it to one. So we don't
175          * need to check for MT capabilities here.
176          */
177         !(env->active_tc.CP0_TCStatus & (1 << CP0TCSt_IXMT));
178 }
179 
180 /* Check if there is pending and not masked out interrupt */
181 static inline bool cpu_mips_hw_interrupts_pending(CPUMIPSState *env)
182 {
183     int32_t pending;
184     int32_t status;
185     bool r;
186 
187     pending = env->CP0_Cause & CP0Ca_IP_mask;
188     status = env->CP0_Status & CP0Ca_IP_mask;
189 
190     if (env->CP0_Config3 & (1 << CP0C3_VEIC)) {
191         /*
192          * A MIPS configured with a vectorizing external interrupt controller
193          * will feed a vector into the Cause pending lines. The core treats
194          * the status lines as a vector level, not as individual masks.
195          */
196         r = pending > status;
197     } else {
198         /*
199          * A MIPS configured with compatibility or VInt (Vectored Interrupts)
200          * treats the pending lines as individual interrupt lines, the status
201          * lines are individual masks.
202          */
203         r = (pending & status) != 0;
204     }
205     return r;
206 }
207 
208 void mips_tcg_init(void);
209 
210 /* TODO QOM'ify CPU reset and remove */
211 void cpu_state_reset(CPUMIPSState *s);
212 void cpu_mips_realize_env(CPUMIPSState *env);
213 
214 /* cp0_timer.c */
215 uint32_t cpu_mips_get_count(CPUMIPSState *env);
216 void cpu_mips_store_count(CPUMIPSState *env, uint32_t value);
217 void cpu_mips_store_compare(CPUMIPSState *env, uint32_t value);
218 void cpu_mips_start_count(CPUMIPSState *env);
219 void cpu_mips_stop_count(CPUMIPSState *env);
220 
221 /* helper.c */
222 bool mips_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
223                        MMUAccessType access_type, int mmu_idx,
224                        bool probe, uintptr_t retaddr);
225 
226 /* op_helper.c */
227 uint32_t float_class_s(uint32_t arg, float_status *fst);
228 uint64_t float_class_d(uint64_t arg, float_status *fst);
229 
230 extern unsigned int ieee_rm[];
231 void update_pagemask(CPUMIPSState *env, target_ulong arg1, int32_t *pagemask);
232 
233 static inline void restore_rounding_mode(CPUMIPSState *env)
234 {
235     set_float_rounding_mode(ieee_rm[env->active_fpu.fcr31 & 3],
236                             &env->active_fpu.fp_status);
237 }
238 
239 static inline void restore_flush_mode(CPUMIPSState *env)
240 {
241     set_flush_to_zero((env->active_fpu.fcr31 & (1 << FCR31_FS)) != 0,
242                       &env->active_fpu.fp_status);
243 }
244 
245 static inline void restore_snan_bit_mode(CPUMIPSState *env)
246 {
247     set_snan_bit_is_one((env->active_fpu.fcr31 & (1 << FCR31_NAN2008)) == 0,
248                         &env->active_fpu.fp_status);
249 }
250 
251 static inline void restore_fp_status(CPUMIPSState *env)
252 {
253     restore_rounding_mode(env);
254     restore_flush_mode(env);
255     restore_snan_bit_mode(env);
256 }
257 
258 static inline void restore_msa_fp_status(CPUMIPSState *env)
259 {
260     float_status *status = &env->active_tc.msa_fp_status;
261     int rounding_mode = (env->active_tc.msacsr & MSACSR_RM_MASK) >> MSACSR_RM;
262     bool flush_to_zero = (env->active_tc.msacsr & MSACSR_FS_MASK) != 0;
263 
264     set_float_rounding_mode(ieee_rm[rounding_mode], status);
265     set_flush_to_zero(flush_to_zero, status);
266     set_flush_inputs_to_zero(flush_to_zero, status);
267 }
268 
269 static inline void restore_pamask(CPUMIPSState *env)
270 {
271     if (env->hflags & MIPS_HFLAG_ELPA) {
272         env->PAMask = (1ULL << env->PABITS) - 1;
273     } else {
274         env->PAMask = PAMASK_BASE;
275     }
276 }
277 
278 static inline int mips_vpe_active(CPUMIPSState *env)
279 {
280     int active = 1;
281 
282     /* Check that the VPE is enabled.  */
283     if (!(env->mvp->CP0_MVPControl & (1 << CP0MVPCo_EVP))) {
284         active = 0;
285     }
286     /* Check that the VPE is activated.  */
287     if (!(env->CP0_VPEConf0 & (1 << CP0VPEC0_VPA))) {
288         active = 0;
289     }
290 
291     /*
292      * Now verify that there are active thread contexts in the VPE.
293      *
294      * This assumes the CPU model will internally reschedule threads
295      * if the active one goes to sleep. If there are no threads available
296      * the active one will be in a sleeping state, and we can turn off
297      * the entire VPE.
298      */
299     if (!(env->active_tc.CP0_TCStatus & (1 << CP0TCSt_A))) {
300         /* TC is not activated.  */
301         active = 0;
302     }
303     if (env->active_tc.CP0_TCHalt & 1) {
304         /* TC is in halt state.  */
305         active = 0;
306     }
307 
308     return active;
309 }
310 
311 static inline int mips_vp_active(CPUMIPSState *env)
312 {
313     CPUState *other_cs = first_cpu;
314 
315     /* Check if the VP disabled other VPs (which means the VP is enabled) */
316     if ((env->CP0_VPControl >> CP0VPCtl_DIS) & 1) {
317         return 1;
318     }
319 
320     /* Check if the virtual processor is disabled due to a DVP */
321     CPU_FOREACH(other_cs) {
322         MIPSCPU *other_cpu = MIPS_CPU(other_cs);
323         if ((&other_cpu->env != env) &&
324             ((other_cpu->env.CP0_VPControl >> CP0VPCtl_DIS) & 1)) {
325             return 0;
326         }
327     }
328     return 1;
329 }
330 
331 static inline void compute_hflags(CPUMIPSState *env)
332 {
333     env->hflags &= ~(MIPS_HFLAG_COP1X | MIPS_HFLAG_64 | MIPS_HFLAG_CP0 |
334                      MIPS_HFLAG_F64 | MIPS_HFLAG_FPU | MIPS_HFLAG_KSU |
335                      MIPS_HFLAG_AWRAP | MIPS_HFLAG_DSP | MIPS_HFLAG_DSP_R2 |
336                      MIPS_HFLAG_DSP_R3 | MIPS_HFLAG_SBRI | MIPS_HFLAG_MSA |
337                      MIPS_HFLAG_FRE | MIPS_HFLAG_ELPA | MIPS_HFLAG_ERL);
338     if (env->CP0_Status & (1 << CP0St_ERL)) {
339         env->hflags |= MIPS_HFLAG_ERL;
340     }
341     if (!(env->CP0_Status & (1 << CP0St_EXL)) &&
342         !(env->CP0_Status & (1 << CP0St_ERL)) &&
343         !(env->hflags & MIPS_HFLAG_DM)) {
344         env->hflags |= (env->CP0_Status >> CP0St_KSU) &
345                        MIPS_HFLAG_KSU;
346     }
347 #if defined(TARGET_MIPS64)
348     if ((env->insn_flags & ISA_MIPS3) &&
349         (((env->hflags & MIPS_HFLAG_KSU) != MIPS_HFLAG_UM) ||
350          (env->CP0_Status & (1 << CP0St_PX)) ||
351          (env->CP0_Status & (1 << CP0St_UX)))) {
352         env->hflags |= MIPS_HFLAG_64;
353     }
354 
355     if (!(env->insn_flags & ISA_MIPS3)) {
356         env->hflags |= MIPS_HFLAG_AWRAP;
357     } else if (((env->hflags & MIPS_HFLAG_KSU) == MIPS_HFLAG_UM) &&
358                !(env->CP0_Status & (1 << CP0St_UX))) {
359         env->hflags |= MIPS_HFLAG_AWRAP;
360     } else if (env->insn_flags & ISA_MIPS64R6) {
361         /* Address wrapping for Supervisor and Kernel is specified in R6 */
362         if ((((env->hflags & MIPS_HFLAG_KSU) == MIPS_HFLAG_SM) &&
363              !(env->CP0_Status & (1 << CP0St_SX))) ||
364             (((env->hflags & MIPS_HFLAG_KSU) == MIPS_HFLAG_KM) &&
365              !(env->CP0_Status & (1 << CP0St_KX)))) {
366             env->hflags |= MIPS_HFLAG_AWRAP;
367         }
368     }
369 #endif
370     if (((env->CP0_Status & (1 << CP0St_CU0)) &&
371          !(env->insn_flags & ISA_MIPS32R6)) ||
372         !(env->hflags & MIPS_HFLAG_KSU)) {
373         env->hflags |= MIPS_HFLAG_CP0;
374     }
375     if (env->CP0_Status & (1 << CP0St_CU1)) {
376         env->hflags |= MIPS_HFLAG_FPU;
377     }
378     if (env->CP0_Status & (1 << CP0St_FR)) {
379         env->hflags |= MIPS_HFLAG_F64;
380     }
381     if (((env->hflags & MIPS_HFLAG_KSU) != MIPS_HFLAG_KM) &&
382         (env->CP0_Config5 & (1 << CP0C5_SBRI))) {
383         env->hflags |= MIPS_HFLAG_SBRI;
384     }
385     if (env->insn_flags & ASE_DSP_R3) {
386         /*
387          * Our cpu supports DSP R3 ASE, so enable
388          * access to DSP R3 resources.
389          */
390         if (env->CP0_Status & (1 << CP0St_MX)) {
391             env->hflags |= MIPS_HFLAG_DSP | MIPS_HFLAG_DSP_R2 |
392                            MIPS_HFLAG_DSP_R3;
393         }
394     } else if (env->insn_flags & ASE_DSP_R2) {
395         /*
396          * Our cpu supports DSP R2 ASE, so enable
397          * access to DSP R2 resources.
398          */
399         if (env->CP0_Status & (1 << CP0St_MX)) {
400             env->hflags |= MIPS_HFLAG_DSP | MIPS_HFLAG_DSP_R2;
401         }
402 
403     } else if (env->insn_flags & ASE_DSP) {
404         /*
405          * Our cpu supports DSP ASE, so enable
406          * access to DSP resources.
407          */
408         if (env->CP0_Status & (1 << CP0St_MX)) {
409             env->hflags |= MIPS_HFLAG_DSP;
410         }
411 
412     }
413     if (env->insn_flags & ISA_MIPS32R2) {
414         if (env->active_fpu.fcr0 & (1 << FCR0_F64)) {
415             env->hflags |= MIPS_HFLAG_COP1X;
416         }
417     } else if (env->insn_flags & ISA_MIPS32) {
418         if (env->hflags & MIPS_HFLAG_64) {
419             env->hflags |= MIPS_HFLAG_COP1X;
420         }
421     } else if (env->insn_flags & ISA_MIPS4) {
422         /*
423          * All supported MIPS IV CPUs use the XX (CU3) to enable
424          * and disable the MIPS IV extensions to the MIPS III ISA.
425          * Some other MIPS IV CPUs ignore the bit, so the check here
426          * would be too restrictive for them.
427          */
428         if (env->CP0_Status & (1U << CP0St_CU3)) {
429             env->hflags |= MIPS_HFLAG_COP1X;
430         }
431     }
432     if (env->insn_flags & ASE_MSA) {
433         if (env->CP0_Config5 & (1 << CP0C5_MSAEn)) {
434             env->hflags |= MIPS_HFLAG_MSA;
435         }
436     }
437     if (env->active_fpu.fcr0 & (1 << FCR0_FREP)) {
438         if (env->CP0_Config5 & (1 << CP0C5_FRE)) {
439             env->hflags |= MIPS_HFLAG_FRE;
440         }
441     }
442     if (env->CP0_Config3 & (1 << CP0C3_LPA)) {
443         if (env->CP0_PageGrain & (1 << CP0PG_ELPA)) {
444             env->hflags |= MIPS_HFLAG_ELPA;
445         }
446     }
447 }
448 
449 void cpu_mips_tlb_flush(CPUMIPSState *env);
450 void sync_c0_status(CPUMIPSState *env, CPUMIPSState *cpu, int tc);
451 void cpu_mips_store_status(CPUMIPSState *env, target_ulong val);
452 void cpu_mips_store_cause(CPUMIPSState *env, target_ulong val);
453 
454 void QEMU_NORETURN do_raise_exception_err(CPUMIPSState *env, uint32_t exception,
455                                           int error_code, uintptr_t pc);
456 
457 static inline void QEMU_NORETURN do_raise_exception(CPUMIPSState *env,
458                                                     uint32_t exception,
459                                                     uintptr_t pc)
460 {
461     do_raise_exception_err(env, exception, 0, pc);
462 }
463 
464 #endif
465