Allow emulation of 32bit targets in the MIPS64 capable qemu version.
[qemu] / target-mips / translate_init.c
1 /*
2  *  MIPS emulation for qemu: CPU initialisation routines.
3  *
4  *  Copyright (c) 2004-2005 Jocelyn Mayer
5  *  Copyright (c) 2007 Herve Poussineau
6  *
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.
11  *
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.
16  *
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
20  */
21
22 /* CPU / CPU family specific config register values. */
23
24 /* Have config1, is MIPS32R1, uses TLB, no virtual icache,
25    uncached coherency */
26 #define MIPS_CONFIG0                                              \
27   ((1 << CP0C0_M) | (0x0 << CP0C0_K23) | (0x0 << CP0C0_KU) |      \
28    (0x0 << CP0C0_AT) | (0x0 << CP0C0_AR) | (0x1 << CP0C0_MT) |    \
29    (0x2 << CP0C0_K0))
30
31 /* Have config2, 64 sets Icache, 16 bytes Icache line,
32    2-way Icache, 64 sets Dcache, 16 bytes Dcache line, 2-way Dcache,
33    no coprocessor2 attached, no MDMX support attached,
34    no performance counters, watch registers present,
35    no code compression, EJTAG present, no FPU */
36 #define MIPS_CONFIG1                                              \
37 ((1 << CP0C1_M) |                                                 \
38  (0x0 << CP0C1_IS) | (0x3 << CP0C1_IL) | (0x1 << CP0C1_IA) |      \
39  (0x0 << CP0C1_DS) | (0x3 << CP0C1_DL) | (0x1 << CP0C1_DA) |      \
40  (0 << CP0C1_C2) | (0 << CP0C1_MD) | (0 << CP0C1_PC) |            \
41  (1 << CP0C1_WR) | (0 << CP0C1_CA) | (1 << CP0C1_EP) |            \
42  (0 << CP0C1_FP))
43
44 /* Have config3, no tertiary/secondary caches implemented */
45 #define MIPS_CONFIG2                                              \
46 ((1 << CP0C2_M))
47
48 /* No config4, no DSP ASE, no large physaddr,
49    no external interrupt controller, no vectored interupts,
50    no 1kb pages, no MT ASE, no SmartMIPS ASE, no trace logic */
51 #define MIPS_CONFIG3                                              \
52 ((0 << CP0C3_M) | (0 << CP0C3_DSPP) | (0 << CP0C3_LPA) |          \
53  (0 << CP0C3_VEIC) | (0 << CP0C3_VInt) | (0 << CP0C3_SP) |        \
54  (0 << CP0C3_MT) | (0 << CP0C3_SM) | (0 << CP0C3_TL))
55
56 /* Define a implementation number of 1.
57    Define a major version 1, minor version 0. */
58 #define MIPS_FCR0 ((0 << FCR0_S) | (0x1 << FCR0_PRID) | (0x10 << FCR0_REV))
59
60
61 struct mips_def_t {
62     const unsigned char *name;
63     int32_t CP0_PRid;
64     int32_t CP0_Config0;
65     int32_t CP0_Config1;
66     int32_t CP0_Config2;
67     int32_t CP0_Config3;
68     int32_t CP0_Config6;
69     int32_t CP0_Config7;
70     int32_t SYNCI_Step;
71     int32_t CCRes;
72     int32_t Status_rw_bitmask;
73     int32_t CP1_fcr0;
74 };
75
76 /*****************************************************************************/
77 /* MIPS CPU definitions */
78 static mips_def_t mips_defs[] =
79 {
80     {
81         .name = "4Kc",
82         .CP0_PRid = 0x00018000,
83         .CP0_Config0 = MIPS_CONFIG0,
84         .CP0_Config1 = MIPS_CONFIG1 | (15 << CP0C1_MMU),
85         .CP0_Config2 = MIPS_CONFIG2,
86         .CP0_Config3 = MIPS_CONFIG3,
87         .SYNCI_Step = 32,
88         .CCRes = 2,
89         .Status_rw_bitmask = 0x3278FF17,
90     },
91     {
92         .name = "4KEcR1",
93         .CP0_PRid = 0x00018400,
94         .CP0_Config0 = MIPS_CONFIG0,
95         .CP0_Config1 = MIPS_CONFIG1 | (15 << CP0C1_MMU),
96         .CP0_Config2 = MIPS_CONFIG2,
97         .CP0_Config3 = MIPS_CONFIG3,
98         .SYNCI_Step = 32,
99         .CCRes = 2,
100         .Status_rw_bitmask = 0x3278FF17,
101     },
102     {
103         .name = "4KEc",
104         .CP0_PRid = 0x00019000,
105         .CP0_Config0 = MIPS_CONFIG0 | (0x1 << CP0C0_AR),
106         .CP0_Config1 = MIPS_CONFIG1 | (15 << CP0C1_MMU),
107         .CP0_Config2 = MIPS_CONFIG2,
108         .CP0_Config3 = MIPS_CONFIG3,
109         .SYNCI_Step = 32,
110         .CCRes = 2,
111         .Status_rw_bitmask = 0x3278FF17,
112     },
113     {
114         .name = "24Kc",
115         .CP0_PRid = 0x00019300,
116         .CP0_Config0 = MIPS_CONFIG0 | (0x1 << CP0C0_AR),
117         .CP0_Config1 = MIPS_CONFIG1 | (15 << CP0C1_MMU),
118         .CP0_Config2 = MIPS_CONFIG2,
119         .CP0_Config3 = MIPS_CONFIG3,
120         .SYNCI_Step = 32,
121         .CCRes = 2,
122         .Status_rw_bitmask = 0x3278FF17,
123     },
124     {
125         .name = "24Kf",
126         .CP0_PRid = 0x00019300,
127         .CP0_Config0 = MIPS_CONFIG0 | (0x1 << CP0C0_AR),
128         .CP0_Config1 = MIPS_CONFIG1 | (1 << CP0C1_FP) | (15 << CP0C1_MMU),
129         .CP0_Config2 = MIPS_CONFIG2,
130         .CP0_Config3 = MIPS_CONFIG3,
131         .SYNCI_Step = 32,
132         .CCRes = 2,
133         .Status_rw_bitmask = 0x3678FF17,
134         .CP1_fcr0 = (1 << FCR0_F64) | (1 << FCR0_L) | (1 << FCR0_W) |
135                     (1 << FCR0_D) | (1 << FCR0_S) | (0x93 << FCR0_PRID),
136     },
137 #ifdef TARGET_MIPS64
138     {
139         .name = "R4000",
140         .CP0_PRid = 0x00000400,
141         .CP0_Config0 = MIPS_CONFIG0 | (0x2 << CP0C0_AT),
142         .CP0_Config1 = MIPS_CONFIG1 | (1 << CP0C1_FP) | (47 << CP0C1_MMU),
143         .CP0_Config2 = MIPS_CONFIG2,
144         .CP0_Config3 = MIPS_CONFIG3,
145         .SYNCI_Step = 16,
146         .CCRes = 2,
147         .Status_rw_bitmask = 0x3678FFFF,
148         /* The R4000 has a full 64bit FPU doesn't use the fcr0 bits. */
149         .CP1_fcr0 = (0x5 << FCR0_PRID) | (0x0 << FCR0_REV),
150     },
151     {
152         .name = "5Kc",
153         .CP0_PRid = 0x00018100,
154         .CP0_Config0 = MIPS_CONFIG0 | (0x2 << CP0C0_AT),
155         .CP0_Config1 = MIPS_CONFIG1 | (31 << CP0C1_MMU) |
156                     (1 << CP0C1_IS) | (4 << CP0C1_IL) | (1 << CP0C1_IA) |
157                     (1 << CP0C1_DS) | (4 << CP0C1_DL) | (1 << CP0C1_DA) |
158                     (1 << CP0C1_PC) | (1 << CP0C1_WR) | (1 << CP0C1_EP),
159         .CP0_Config2 = MIPS_CONFIG2,
160         .CP0_Config3 = MIPS_CONFIG3,
161         .SYNCI_Step = 32,
162         .CCRes = 2,
163         .Status_rw_bitmask = 0x32F8FFFF,
164     },
165     {
166         .name = "5Kf",
167         .CP0_PRid = 0x00018100,
168         .CP0_Config0 = MIPS_CONFIG0 | (0x2 << CP0C0_AT),
169         .CP0_Config1 = MIPS_CONFIG1 | (1 << CP0C1_FP) | (31 << CP0C1_MMU) |
170                     (1 << CP0C1_IS) | (4 << CP0C1_IL) | (1 << CP0C1_IA) |
171                     (1 << CP0C1_DS) | (4 << CP0C1_DL) | (1 << CP0C1_DA) |
172                     (1 << CP0C1_PC) | (1 << CP0C1_WR) | (1 << CP0C1_EP),
173         .CP0_Config2 = MIPS_CONFIG2,
174         .CP0_Config3 = MIPS_CONFIG3,
175         .SYNCI_Step = 32,
176         .CCRes = 2,
177         .Status_rw_bitmask = 0x36F8FFFF,
178         /* The 5Kf has F64 / L / W but doesn't use the fcr0 bits. */
179         .CP1_fcr0 = (1 << FCR0_D) | (1 << FCR0_S) |
180                     (0x81 << FCR0_PRID) | (0x0 << FCR0_REV),
181     },
182     {
183         .name = "20Kc",
184         /* We emulate a later version of the 20Kc, earlier ones had a broken
185            WAIT instruction. */
186         .CP0_PRid = 0x000182a0,
187         .CP0_Config0 = MIPS_CONFIG0 | (0x2 << CP0C0_AT) | (1 << CP0C0_VI),
188         .CP0_Config1 = MIPS_CONFIG1 | (1 << CP0C1_FP) | (47 << CP0C1_MMU) |
189                     (2 << CP0C1_IS) | (4 << CP0C1_IL) | (3 << CP0C1_IA) |
190                     (2 << CP0C1_DS) | (4 << CP0C1_DL) | (3 << CP0C1_DA) |
191                     (1 << CP0C1_PC) | (1 << CP0C1_WR) | (1 << CP0C1_EP),
192         .CP0_Config2 = MIPS_CONFIG2,
193         .CP0_Config3 = MIPS_CONFIG3,
194         .SYNCI_Step = 32,
195         .CCRes = 2,
196         .Status_rw_bitmask = 0x36FBFFFF,
197         /* The 20Kc has F64 / L / W but doesn't use the fcr0 bits. */
198         .CP1_fcr0 = (1 << FCR0_3D) | (1 << FCR0_PS) |
199                     (1 << FCR0_D) | (1 << FCR0_S) |
200                     (0x82 << FCR0_PRID) | (0x0 << FCR0_REV),
201     },
202 #endif
203 };
204
205 int mips_find_by_name (const unsigned char *name, mips_def_t **def)
206 {
207     int i, ret;
208
209     ret = -1;
210     *def = NULL;
211     for (i = 0; i < sizeof(mips_defs) / sizeof(mips_defs[0]); i++) {
212         if (strcasecmp(name, mips_defs[i].name) == 0) {
213             *def = &mips_defs[i];
214             ret = 0;
215             break;
216         }
217     }
218
219     return ret;
220 }
221
222 void mips_cpu_list (FILE *f, int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
223 {
224     int i;
225
226     for (i = 0; i < sizeof(mips_defs) / sizeof(mips_defs[0]); i++) {
227         (*cpu_fprintf)(f, "MIPS '%s'\n",
228                        mips_defs[i].name);
229     }
230 }
231
232 #ifndef CONFIG_USER_ONLY
233 static void no_mmu_init (CPUMIPSState *env, mips_def_t *def)
234 {
235     env->nb_tlb = 1;
236     env->map_address = &no_mmu_map_address;
237 }
238
239 static void fixed_mmu_init (CPUMIPSState *env, mips_def_t *def)
240 {
241     env->nb_tlb = 1;
242     env->map_address = &fixed_mmu_map_address;
243 }
244
245 static void r4k_mmu_init (CPUMIPSState *env, mips_def_t *def)
246 {
247     env->nb_tlb = 1 + ((def->CP0_Config1 >> CP0C1_MMU) & 63);
248     env->map_address = &r4k_map_address;
249     env->do_tlbwi = r4k_do_tlbwi;
250     env->do_tlbwr = r4k_do_tlbwr;
251     env->do_tlbp = r4k_do_tlbp;
252     env->do_tlbr = r4k_do_tlbr;
253 }
254 #endif /* CONFIG_USER_ONLY */
255
256 int cpu_mips_register (CPUMIPSState *env, mips_def_t *def)
257 {
258     if (!def)
259         def = env->cpu_model;
260     if (!def)
261         cpu_abort(env, "Unable to find MIPS CPU definition\n");
262     env->cpu_model = def;
263     env->CP0_PRid = def->CP0_PRid;
264     env->CP0_Config0 = def->CP0_Config0;
265 #ifdef TARGET_WORDS_BIGENDIAN
266     env->CP0_Config0 |= (1 << CP0C0_BE);
267 #endif
268     env->CP0_Config1 = def->CP0_Config1;
269     env->CP0_Config2 = def->CP0_Config2;
270     env->CP0_Config3 = def->CP0_Config3;
271     env->CP0_Config6 = def->CP0_Config6;
272     env->CP0_Config7 = def->CP0_Config7;
273     env->SYNCI_Step = def->SYNCI_Step;
274     env->CCRes = def->CCRes;
275     env->Status_rw_bitmask = def->Status_rw_bitmask;
276     env->fcr0 = def->CP1_fcr0;
277 #ifdef CONFIG_USER_ONLY
278     if (env->CP0_Config1 & (1 << CP0C1_FP))
279         env->hflags |= MIPS_HFLAG_FPU;
280     if (env->fcr0 & (1 << FCR0_F64))
281         env->hflags |= MIPS_HFLAG_F64;
282 #else
283     /* There are more full-featured MMU variants in older MIPS CPUs,
284        R3000, R6000 and R8000 come to mind. If we ever support them,
285        this check will need to look up a different place than those
286        newfangled config registers. */
287     switch ((env->CP0_Config0 >> CP0C0_MT) & 3) {
288         case 0:
289             no_mmu_init(env, def);
290             break;
291         case 1:
292             r4k_mmu_init(env, def);
293             break;
294         case 3:
295             fixed_mmu_init(env, def);
296             break;
297         default:
298             cpu_abort(env, "MMU type not supported\n");
299     }
300     env->CP0_Random = env->nb_tlb - 1;
301     env->tlb_in_use = env->nb_tlb;
302 #endif /* CONFIG_USER_ONLY */
303     return 0;
304 }