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