2 * MIPS emulation for qemu: CPU initialisation routines.
4 * Copyright (c) 2004-2005 Jocelyn Mayer
5 * Copyright (c) 2007 Herve Poussineau
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 /* CPU / CPU family specific config register values. */
24 /* Have config1, is MIPS32R1, uses TLB, no virtual icache,
26 #define MIPS_CONFIG0 \
27 ((1 << CP0C0_M) | (0x0 << CP0C0_K23) | (0x0 << CP0C0_KU) | \
28 (0x0 << CP0C0_AT) | (0x0 << CP0C0_AR) | (0x1 << CP0C0_MT) | \
31 /* Have config2, no coprocessor2 attached, no MDMX support attached,
32 no performance counters, watch registers present,
33 no code compression, EJTAG present, no FPU */
34 #define MIPS_CONFIG1 \
36 (0 << CP0C1_C2) | (0 << CP0C1_MD) | (0 << CP0C1_PC) | \
37 (1 << CP0C1_WR) | (0 << CP0C1_CA) | (1 << CP0C1_EP) | \
40 /* Have config3, no tertiary/secondary caches implemented */
41 #define MIPS_CONFIG2 \
44 /* No config4, no DSP ASE, no large physaddr,
45 no external interrupt controller, no vectored interupts,
46 no 1kb pages, no SmartMIPS ASE, no trace logic */
47 #define MIPS_CONFIG3 \
48 ((0 << CP0C3_M) | (0 << CP0C3_DSPP) | (0 << CP0C3_LPA) | \
49 (0 << CP0C3_VEIC) | (0 << CP0C3_VInt) | (0 << CP0C3_SP) | \
50 (0 << CP0C3_SM) | (0 << CP0C3_TL))
52 /* Define a implementation number of 1.
53 Define a major version 1, minor version 0. */
54 #define MIPS_FCR0 ((0 << FCR0_S) | (0x1 << FCR0_PRID) | (0x10 << FCR0_REV))
57 const unsigned char *name;
67 int32_t CP0_Status_rw_bitmask;
68 int32_t CP0_TCStatus_rw_bitmask;
72 int32_t CP0_SRSConf0_rw_bitmask;
74 int32_t CP0_SRSConf1_rw_bitmask;
76 int32_t CP0_SRSConf2_rw_bitmask;
78 int32_t CP0_SRSConf3_rw_bitmask;
80 int32_t CP0_SRSConf4_rw_bitmask;
85 /*****************************************************************************/
86 /* MIPS CPU definitions */
87 static mips_def_t mips_defs[] =
91 .CP0_PRid = 0x00018000,
92 .CP0_Config0 = MIPS_CONFIG0,
93 .CP0_Config1 = MIPS_CONFIG1 | (15 << CP0C1_MMU) |
94 (0 << CP0C1_IS) | (3 << CP0C1_IL) | (1 << CP0C1_IA) |
95 (0 << CP0C1_DS) | (3 << CP0C1_DL) | (1 << CP0C1_DA),
96 .CP0_Config2 = MIPS_CONFIG2,
97 .CP0_Config3 = MIPS_CONFIG3,
100 .CP0_Status_rw_bitmask = 0x1278FF17,
101 .insn_flags = CPU_MIPS32 | ASE_MIPS16,
105 .CP0_PRid = 0x00018400,
106 .CP0_Config0 = MIPS_CONFIG0,
107 .CP0_Config1 = MIPS_CONFIG1 | (15 << CP0C1_MMU) |
108 (0 << CP0C1_IS) | (3 << CP0C1_IL) | (1 << CP0C1_IA) |
109 (0 << CP0C1_DS) | (3 << CP0C1_DL) | (1 << CP0C1_DA),
110 .CP0_Config2 = MIPS_CONFIG2,
111 .CP0_Config3 = MIPS_CONFIG3,
114 .CP0_Status_rw_bitmask = 0x1278FF17,
115 .insn_flags = CPU_MIPS32 | ASE_MIPS16,
119 .CP0_PRid = 0x00019000,
120 .CP0_Config0 = MIPS_CONFIG0 | (0x1 << CP0C0_AR),
121 .CP0_Config1 = MIPS_CONFIG1 | (15 << CP0C1_MMU) |
122 (0 << CP0C1_IS) | (3 << CP0C1_IL) | (1 << CP0C1_IA) |
123 (0 << CP0C1_DS) | (3 << CP0C1_DL) | (1 << CP0C1_DA),
124 .CP0_Config2 = MIPS_CONFIG2,
125 .CP0_Config3 = MIPS_CONFIG3 | (0 << CP0C3_VInt),
128 .CP0_Status_rw_bitmask = 0x1278FF17,
129 .insn_flags = CPU_MIPS32R2 | ASE_MIPS16,
133 .CP0_PRid = 0x00019100,
134 /* Config1 implemented, MIPS32R2, fixed mapping MMU,
135 no virtual icache, uncached coherency. */
136 .CP0_Config0 = (1 << CP0C0_M) | (0x1 << CP0C0_AR) |
137 (0x3 << CP0C0_MT) | (0x2 << CP0C0_K0),
138 .CP0_Config1 = MIPS_CONFIG1 |
139 (0 << CP0C1_IS) | (3 << CP0C1_IL) | (1 << CP0C1_IA) |
140 (0 << CP0C1_DS) | (3 << CP0C1_DL) | (1 << CP0C1_DA),
141 .CP0_Config2 = MIPS_CONFIG2,
142 .CP0_Config3 = MIPS_CONFIG3,
145 .CP0_Status_rw_bitmask = 0x1258FF17,
146 .insn_flags = CPU_MIPS32R2 | ASE_MIPS16,
150 .CP0_PRid = 0x00019300,
151 .CP0_Config0 = MIPS_CONFIG0 | (0x1 << CP0C0_AR),
152 .CP0_Config1 = MIPS_CONFIG1 | (15 << CP0C1_MMU) |
153 (0 << CP0C1_IS) | (3 << CP0C1_IL) | (1 << CP0C1_IA) |
154 (0 << CP0C1_DS) | (3 << CP0C1_DL) | (1 << CP0C1_DA),
155 .CP0_Config2 = MIPS_CONFIG2,
156 .CP0_Config3 = MIPS_CONFIG3 | (0 << CP0C3_VInt),
159 /* No DSP implemented. */
160 .CP0_Status_rw_bitmask = 0x1278FF1F,
161 .insn_flags = CPU_MIPS32R2 | ASE_MIPS16,
165 .CP0_PRid = 0x00019300,
166 .CP0_Config0 = MIPS_CONFIG0 | (0x1 << CP0C0_AR),
167 .CP0_Config1 = MIPS_CONFIG1 | (1 << CP0C1_FP) | (15 << CP0C1_MMU) |
168 (0 << CP0C1_IS) | (3 << CP0C1_IL) | (1 << CP0C1_IA) |
169 (0 << CP0C1_DS) | (3 << CP0C1_DL) | (1 << CP0C1_DA),
170 .CP0_Config2 = MIPS_CONFIG2,
171 .CP0_Config3 = MIPS_CONFIG3 | (0 << CP0C3_VInt),
174 /* No DSP implemented. */
175 .CP0_Status_rw_bitmask = 0x3678FF1F,
176 .CP1_fcr0 = (1 << FCR0_F64) | (1 << FCR0_L) | (1 << FCR0_W) |
177 (1 << FCR0_D) | (1 << FCR0_S) | (0x93 << FCR0_PRID),
178 .insn_flags = CPU_MIPS32R2 | ASE_MIPS16,
182 .CP0_PRid = 0x00019500,
183 .CP0_Config0 = MIPS_CONFIG0 | (0x1 << CP0C0_AR),
184 .CP0_Config1 = MIPS_CONFIG1 | (1 << CP0C1_FP) | (15 << CP0C1_MMU) |
185 (0 << CP0C1_IS) | (3 << CP0C1_IL) | (1 << CP0C1_IA) |
186 (0 << CP0C1_DS) | (3 << CP0C1_DL) | (1 << CP0C1_DA),
187 .CP0_Config2 = MIPS_CONFIG2,
188 .CP0_Config3 = MIPS_CONFIG3 | (0 << CP0C3_VInt) | (1 << CP0C3_MT),
191 /* No DSP implemented. */
192 .CP0_Status_rw_bitmask = 0x3678FF1F,
193 /* No DSP implemented. */
194 .CP0_TCStatus_rw_bitmask = (0 << CP0TCSt_TCU3) | (0 << CP0TCSt_TCU2) |
195 (1 << CP0TCSt_TCU1) | (1 << CP0TCSt_TCU0) |
196 (0 << CP0TCSt_TMX) | (1 << CP0TCSt_DT) |
197 (1 << CP0TCSt_DA) | (1 << CP0TCSt_A) |
198 (0x3 << CP0TCSt_TKSU) | (1 << CP0TCSt_IXMT) |
199 (0xff << CP0TCSt_TASID),
200 .CP1_fcr0 = (1 << FCR0_F64) | (1 << FCR0_L) | (1 << FCR0_W) |
201 (1 << FCR0_D) | (1 << FCR0_S) | (0x95 << FCR0_PRID),
202 .CP0_SRSCtl = (0xf << CP0SRSCtl_HSS),
203 .CP0_SRSConf0_rw_bitmask = 0x3fffffff,
204 .CP0_SRSConf0 = (1 << CP0SRSC0_M) | (0x3fe << CP0SRSC0_SRS3) |
205 (0x3fe << CP0SRSC0_SRS2) | (0x3fe << CP0SRSC0_SRS1),
206 .CP0_SRSConf1_rw_bitmask = 0x3fffffff,
207 .CP0_SRSConf1 = (1 << CP0SRSC1_M) | (0x3fe << CP0SRSC1_SRS6) |
208 (0x3fe << CP0SRSC1_SRS5) | (0x3fe << CP0SRSC1_SRS4),
209 .CP0_SRSConf2_rw_bitmask = 0x3fffffff,
210 .CP0_SRSConf2 = (1 << CP0SRSC2_M) | (0x3fe << CP0SRSC2_SRS9) |
211 (0x3fe << CP0SRSC2_SRS8) | (0x3fe << CP0SRSC2_SRS7),
212 .CP0_SRSConf3_rw_bitmask = 0x3fffffff,
213 .CP0_SRSConf3 = (1 << CP0SRSC3_M) | (0x3fe << CP0SRSC3_SRS12) |
214 (0x3fe << CP0SRSC3_SRS11) | (0x3fe << CP0SRSC3_SRS10),
215 .CP0_SRSConf4_rw_bitmask = 0x3fffffff,
216 .CP0_SRSConf4 = (0x3fe << CP0SRSC4_SRS15) |
217 (0x3fe << CP0SRSC4_SRS14) | (0x3fe << CP0SRSC4_SRS13),
218 .insn_flags = CPU_MIPS32R2 | ASE_MIPS16 | ASE_DSP | ASE_MT,
220 #if defined(TARGET_MIPS64)
223 .CP0_PRid = 0x00000400,
224 .CP0_Config0 = MIPS_CONFIG0 | (0x2 << CP0C0_AT),
225 .CP0_Config1 = MIPS_CONFIG1 | (1 << CP0C1_FP) | (47 << CP0C1_MMU) |
226 (0 << CP0C1_IS) | (3 << CP0C1_IL) | (1 << CP0C1_IA) |
227 (0 << CP0C1_DS) | (3 << CP0C1_DL) | (1 << CP0C1_DA),
228 .CP0_Config2 = MIPS_CONFIG2,
229 .CP0_Config3 = MIPS_CONFIG3,
232 .CP0_Status_rw_bitmask = 0x3678FFFF,
233 /* The R4000 has a full 64bit FPU doesn't use the fcr0 bits. */
234 .CP1_fcr0 = (0x5 << FCR0_PRID) | (0x0 << FCR0_REV),
236 .insn_flags = CPU_MIPS3,
240 .CP0_PRid = 0x00018100,
241 .CP0_Config0 = MIPS_CONFIG0 | (0x2 << CP0C0_AT),
242 .CP0_Config1 = MIPS_CONFIG1 | (31 << CP0C1_MMU) |
243 (1 << CP0C1_IS) | (4 << CP0C1_IL) | (1 << CP0C1_IA) |
244 (1 << CP0C1_DS) | (4 << CP0C1_DL) | (1 << CP0C1_DA) |
245 (1 << CP0C1_PC) | (1 << CP0C1_WR) | (1 << CP0C1_EP),
246 .CP0_Config2 = MIPS_CONFIG2,
247 .CP0_Config3 = MIPS_CONFIG3,
250 .CP0_Status_rw_bitmask = 0x32F8FFFF,
252 .insn_flags = CPU_MIPS64,
256 .CP0_PRid = 0x00018100,
257 .CP0_Config0 = MIPS_CONFIG0 | (0x2 << CP0C0_AT),
258 .CP0_Config1 = MIPS_CONFIG1 | (1 << CP0C1_FP) | (31 << CP0C1_MMU) |
259 (1 << CP0C1_IS) | (4 << CP0C1_IL) | (1 << CP0C1_IA) |
260 (1 << CP0C1_DS) | (4 << CP0C1_DL) | (1 << CP0C1_DA) |
261 (1 << CP0C1_PC) | (1 << CP0C1_WR) | (1 << CP0C1_EP),
262 .CP0_Config2 = MIPS_CONFIG2,
263 .CP0_Config3 = MIPS_CONFIG3,
266 .CP0_Status_rw_bitmask = 0x36F8FFFF,
267 /* The 5Kf has F64 / L / W but doesn't use the fcr0 bits. */
268 .CP1_fcr0 = (1 << FCR0_D) | (1 << FCR0_S) |
269 (0x81 << FCR0_PRID) | (0x0 << FCR0_REV),
271 .insn_flags = CPU_MIPS64,
275 /* We emulate a later version of the 20Kc, earlier ones had a broken
277 .CP0_PRid = 0x000182a0,
278 .CP0_Config0 = MIPS_CONFIG0 | (0x2 << CP0C0_AT) | (1 << CP0C0_VI),
279 .CP0_Config1 = MIPS_CONFIG1 | (1 << CP0C1_FP) | (47 << CP0C1_MMU) |
280 (2 << CP0C1_IS) | (4 << CP0C1_IL) | (3 << CP0C1_IA) |
281 (2 << CP0C1_DS) | (4 << CP0C1_DL) | (3 << CP0C1_DA) |
282 (1 << CP0C1_PC) | (1 << CP0C1_WR) | (1 << CP0C1_EP),
283 .CP0_Config2 = MIPS_CONFIG2,
284 .CP0_Config3 = MIPS_CONFIG3,
287 .CP0_Status_rw_bitmask = 0x36FBFFFF,
288 /* The 20Kc has F64 / L / W but doesn't use the fcr0 bits. */
289 .CP1_fcr0 = (1 << FCR0_3D) | (1 << FCR0_PS) |
290 (1 << FCR0_D) | (1 << FCR0_S) |
291 (0x82 << FCR0_PRID) | (0x0 << FCR0_REV),
293 .insn_flags = CPU_MIPS64 | ASE_MIPS3D,
296 /* A generic CPU providing MIPS64 Release 2 features.
297 FIXME: Eventually this should be replaced by a real CPU model. */
298 .name = "MIPS64R2-generic",
299 .CP0_PRid = 0x00000000,
300 .CP0_Config0 = MIPS_CONFIG0 | (0x2 << CP0C0_AT) | (0x1 << CP0C0_AR),
301 .CP0_Config1 = MIPS_CONFIG1 | (1 << CP0C1_FP) | (63 << CP0C1_MMU) |
302 (2 << CP0C1_IS) | (4 << CP0C1_IL) | (3 << CP0C1_IA) |
303 (2 << CP0C1_DS) | (4 << CP0C1_DL) | (3 << CP0C1_DA) |
304 (1 << CP0C1_PC) | (1 << CP0C1_WR) | (1 << CP0C1_EP),
305 .CP0_Config2 = MIPS_CONFIG2,
306 .CP0_Config3 = MIPS_CONFIG3,
309 .CP0_Status_rw_bitmask = 0x36FBFFFF,
310 .CP1_fcr0 = (1 << FCR0_3D) | (1 << FCR0_PS) | (1 << FCR0_L) |
311 (1 << FCR0_W) | (1 << FCR0_D) | (1 << FCR0_S) |
312 (0x00 << FCR0_PRID) | (0x0 << FCR0_REV),
314 .insn_flags = CPU_MIPS64R2 | ASE_MIPS3D,
319 static const mips_def_t *cpu_mips_find_by_name (const unsigned char *name)
323 for (i = 0; i < sizeof(mips_defs) / sizeof(mips_defs[0]); i++) {
324 if (strcasecmp(name, mips_defs[i].name) == 0) {
325 return &mips_defs[i];
331 void mips_cpu_list (FILE *f, int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
335 for (i = 0; i < sizeof(mips_defs) / sizeof(mips_defs[0]); i++) {
336 (*cpu_fprintf)(f, "MIPS '%s'\n",
341 #ifndef CONFIG_USER_ONLY
342 static void no_mmu_init (CPUMIPSState *env, const mips_def_t *def)
344 env->tlb->nb_tlb = 1;
345 env->tlb->map_address = &no_mmu_map_address;
348 static void fixed_mmu_init (CPUMIPSState *env, const mips_def_t *def)
350 env->tlb->nb_tlb = 1;
351 env->tlb->map_address = &fixed_mmu_map_address;
354 static void r4k_mmu_init (CPUMIPSState *env, const mips_def_t *def)
356 env->tlb->nb_tlb = 1 + ((def->CP0_Config1 >> CP0C1_MMU) & 63);
357 env->tlb->map_address = &r4k_map_address;
358 env->tlb->do_tlbwi = r4k_do_tlbwi;
359 env->tlb->do_tlbwr = r4k_do_tlbwr;
360 env->tlb->do_tlbp = r4k_do_tlbp;
361 env->tlb->do_tlbr = r4k_do_tlbr;
364 static void mmu_init (CPUMIPSState *env, const mips_def_t *def)
366 env->tlb = qemu_mallocz(sizeof(CPUMIPSTLBContext));
368 /* There are more full-featured MMU variants in older MIPS CPUs,
369 R3000, R6000 and R8000 come to mind. If we ever support them,
370 this check will need to look up a different place than those
371 newfangled config registers. */
372 switch ((env->CP0_Config0 >> CP0C0_MT) & 3) {
374 no_mmu_init(env, def);
377 r4k_mmu_init(env, def);
380 fixed_mmu_init(env, def);
383 cpu_abort(env, "MMU type not supported\n");
385 env->CP0_Random = env->tlb->nb_tlb - 1;
386 env->tlb->tlb_in_use = env->tlb->nb_tlb;
388 #endif /* CONFIG_USER_ONLY */
390 static void fpu_init (CPUMIPSState *env, const mips_def_t *def)
392 env->fpu = qemu_mallocz(sizeof(CPUMIPSFPUContext));
394 env->fpu->fcr0 = def->CP1_fcr0;
395 #ifdef CONFIG_USER_ONLY
396 if (env->CP0_Config1 & (1 << CP0C1_FP))
397 env->hflags |= MIPS_HFLAG_FPU;
398 if (env->fpu->fcr0 & (1 << FCR0_F64))
399 env->hflags |= MIPS_HFLAG_F64;
403 static void mvp_init (CPUMIPSState *env, const mips_def_t *def)
405 env->mvp = qemu_mallocz(sizeof(CPUMIPSMVPContext));
407 /* MVPConf1 implemented, TLB sharable, no gating storage support,
408 programmable cache partitioning implemented, number of allocatable
409 and sharable TLB entries, MVP has allocatable TCs, 2 VPEs
410 implemented, 5 TCs implemented. */
411 env->mvp->CP0_MVPConf0 = (1 << CP0MVPC0_M) | (1 << CP0MVPC0_TLBS) |
412 (0 << CP0MVPC0_GS) | (1 << CP0MVPC0_PCP) |
413 #ifndef CONFIG_USER_ONLY
414 /* Usermode has no TLB support */
415 (env->tlb->nb_tlb << CP0MVPC0_PTLBE) |
417 // TODO: actually do 2 VPEs.
418 // (1 << CP0MVPC0_TCA) | (0x1 << CP0MVPC0_PVPE) |
419 // (0x04 << CP0MVPC0_PTC);
420 (1 << CP0MVPC0_TCA) | (0x0 << CP0MVPC0_PVPE) |
421 (0x04 << CP0MVPC0_PTC);
422 /* Allocatable CP1 have media extensions, allocatable CP1 have FP support,
423 no UDI implemented, no CP2 implemented, 1 CP1 implemented. */
424 env->mvp->CP0_MVPConf1 = (1 << CP0MVPC1_CIM) | (1 << CP0MVPC1_CIF) |
425 (0x0 << CP0MVPC1_PCX) | (0x0 << CP0MVPC1_PCP2) |
426 (0x1 << CP0MVPC1_PCP1);
429 static int cpu_mips_register (CPUMIPSState *env, const mips_def_t *def)
431 env->CP0_PRid = def->CP0_PRid;
432 env->CP0_Config0 = def->CP0_Config0;
433 #ifdef TARGET_WORDS_BIGENDIAN
434 env->CP0_Config0 |= (1 << CP0C0_BE);
436 env->CP0_Config1 = def->CP0_Config1;
437 env->CP0_Config2 = def->CP0_Config2;
438 env->CP0_Config3 = def->CP0_Config3;
439 env->CP0_Config6 = def->CP0_Config6;
440 env->CP0_Config7 = def->CP0_Config7;
441 env->SYNCI_Step = def->SYNCI_Step;
442 env->CCRes = def->CCRes;
443 env->CP0_Status_rw_bitmask = def->CP0_Status_rw_bitmask;
444 env->CP0_TCStatus_rw_bitmask = def->CP0_TCStatus_rw_bitmask;
445 env->CP0_SRSCtl = def->CP0_SRSCtl;
446 #if defined(TARGET_MIPS64)
447 if (def->insn_flags & ISA_MIPS3)
449 env->hflags |= MIPS_HFLAG_64;
450 env->SEGBITS = def->SEGBITS;
451 env->SEGMask = (3ULL << 62) | ((1ULL << def->SEGBITS) - 1);
454 env->SEGMask = 0xFFFFFFFF;
457 env->CP0_SRSConf0_rw_bitmask = def->CP0_SRSConf0_rw_bitmask;
458 env->CP0_SRSConf0 = def->CP0_SRSConf0;
459 env->CP0_SRSConf1_rw_bitmask = def->CP0_SRSConf1_rw_bitmask;
460 env->CP0_SRSConf1 = def->CP0_SRSConf1;
461 env->CP0_SRSConf2_rw_bitmask = def->CP0_SRSConf2_rw_bitmask;
462 env->CP0_SRSConf2 = def->CP0_SRSConf2;
463 env->CP0_SRSConf3_rw_bitmask = def->CP0_SRSConf3_rw_bitmask;
464 env->CP0_SRSConf3 = def->CP0_SRSConf3;
465 env->CP0_SRSConf4_rw_bitmask = def->CP0_SRSConf4_rw_bitmask;
466 env->CP0_SRSConf4 = def->CP0_SRSConf4;
467 env->insn_flags = def->insn_flags;
469 #ifndef CONFIG_USER_ONLY