]> git.proxmox.com Git - qemu.git/blob - target-xtensa/cpu.h
target-xtensa: implement CACHEATTR SR
[qemu.git] / target-xtensa / cpu.h
1 /*
2 * Copyright (c) 2011, Max Filippov, Open Source and Linux Lab.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the Open Source and Linux Lab nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #ifndef CPU_XTENSA_H
29 #define CPU_XTENSA_H
30
31 #define TARGET_LONG_BITS 32
32 #define ELF_MACHINE EM_XTENSA
33
34 #define CPUArchState struct CPUXtensaState
35
36 #include "config.h"
37 #include "qemu-common.h"
38 #include "cpu-defs.h"
39 #include "fpu/softfloat.h"
40
41 #define TARGET_HAS_ICE 1
42
43 #define NB_MMU_MODES 4
44
45 #define TARGET_PHYS_ADDR_SPACE_BITS 32
46 #define TARGET_VIRT_ADDR_SPACE_BITS 32
47 #define TARGET_PAGE_BITS 12
48
49 enum {
50 /* Additional instructions */
51 XTENSA_OPTION_CODE_DENSITY,
52 XTENSA_OPTION_LOOP,
53 XTENSA_OPTION_EXTENDED_L32R,
54 XTENSA_OPTION_16_BIT_IMUL,
55 XTENSA_OPTION_32_BIT_IMUL,
56 XTENSA_OPTION_32_BIT_IMUL_HIGH,
57 XTENSA_OPTION_32_BIT_IDIV,
58 XTENSA_OPTION_MAC16,
59 XTENSA_OPTION_MISC_OP_NSA,
60 XTENSA_OPTION_MISC_OP_MINMAX,
61 XTENSA_OPTION_MISC_OP_SEXT,
62 XTENSA_OPTION_MISC_OP_CLAMPS,
63 XTENSA_OPTION_COPROCESSOR,
64 XTENSA_OPTION_BOOLEAN,
65 XTENSA_OPTION_FP_COPROCESSOR,
66 XTENSA_OPTION_MP_SYNCHRO,
67 XTENSA_OPTION_CONDITIONAL_STORE,
68 XTENSA_OPTION_ATOMCTL,
69
70 /* Interrupts and exceptions */
71 XTENSA_OPTION_EXCEPTION,
72 XTENSA_OPTION_RELOCATABLE_VECTOR,
73 XTENSA_OPTION_UNALIGNED_EXCEPTION,
74 XTENSA_OPTION_INTERRUPT,
75 XTENSA_OPTION_HIGH_PRIORITY_INTERRUPT,
76 XTENSA_OPTION_TIMER_INTERRUPT,
77
78 /* Local memory */
79 XTENSA_OPTION_ICACHE,
80 XTENSA_OPTION_ICACHE_TEST,
81 XTENSA_OPTION_ICACHE_INDEX_LOCK,
82 XTENSA_OPTION_DCACHE,
83 XTENSA_OPTION_DCACHE_TEST,
84 XTENSA_OPTION_DCACHE_INDEX_LOCK,
85 XTENSA_OPTION_IRAM,
86 XTENSA_OPTION_IROM,
87 XTENSA_OPTION_DRAM,
88 XTENSA_OPTION_DROM,
89 XTENSA_OPTION_XLMI,
90 XTENSA_OPTION_HW_ALIGNMENT,
91 XTENSA_OPTION_MEMORY_ECC_PARITY,
92
93 /* Memory protection and translation */
94 XTENSA_OPTION_REGION_PROTECTION,
95 XTENSA_OPTION_REGION_TRANSLATION,
96 XTENSA_OPTION_MMU,
97 XTENSA_OPTION_CACHEATTR,
98
99 /* Other */
100 XTENSA_OPTION_WINDOWED_REGISTER,
101 XTENSA_OPTION_PROCESSOR_INTERFACE,
102 XTENSA_OPTION_MISC_SR,
103 XTENSA_OPTION_THREAD_POINTER,
104 XTENSA_OPTION_PROCESSOR_ID,
105 XTENSA_OPTION_DEBUG,
106 XTENSA_OPTION_TRACE_PORT,
107 };
108
109 enum {
110 THREADPTR = 231,
111 FCR = 232,
112 FSR = 233,
113 };
114
115 enum {
116 LBEG = 0,
117 LEND = 1,
118 LCOUNT = 2,
119 SAR = 3,
120 BR = 4,
121 LITBASE = 5,
122 SCOMPARE1 = 12,
123 ACCLO = 16,
124 ACCHI = 17,
125 MR = 32,
126 WINDOW_BASE = 72,
127 WINDOW_START = 73,
128 PTEVADDR = 83,
129 RASID = 90,
130 ITLBCFG = 91,
131 DTLBCFG = 92,
132 IBREAKENABLE = 96,
133 CACHEATTR = 98,
134 ATOMCTL = 99,
135 IBREAKA = 128,
136 DBREAKA = 144,
137 DBREAKC = 160,
138 EPC1 = 177,
139 DEPC = 192,
140 EPS2 = 194,
141 EXCSAVE1 = 209,
142 CPENABLE = 224,
143 INTSET = 226,
144 INTCLEAR = 227,
145 INTENABLE = 228,
146 PS = 230,
147 VECBASE = 231,
148 EXCCAUSE = 232,
149 DEBUGCAUSE = 233,
150 CCOUNT = 234,
151 PRID = 235,
152 ICOUNT = 236,
153 ICOUNTLEVEL = 237,
154 EXCVADDR = 238,
155 CCOMPARE = 240,
156 };
157
158 #define PS_INTLEVEL 0xf
159 #define PS_INTLEVEL_SHIFT 0
160
161 #define PS_EXCM 0x10
162 #define PS_UM 0x20
163
164 #define PS_RING 0xc0
165 #define PS_RING_SHIFT 6
166
167 #define PS_OWB 0xf00
168 #define PS_OWB_SHIFT 8
169
170 #define PS_CALLINC 0x30000
171 #define PS_CALLINC_SHIFT 16
172 #define PS_CALLINC_LEN 2
173
174 #define PS_WOE 0x40000
175
176 #define DEBUGCAUSE_IC 0x1
177 #define DEBUGCAUSE_IB 0x2
178 #define DEBUGCAUSE_DB 0x4
179 #define DEBUGCAUSE_BI 0x8
180 #define DEBUGCAUSE_BN 0x10
181 #define DEBUGCAUSE_DI 0x20
182 #define DEBUGCAUSE_DBNUM 0xf00
183 #define DEBUGCAUSE_DBNUM_SHIFT 8
184
185 #define DBREAKC_SB 0x80000000
186 #define DBREAKC_LB 0x40000000
187 #define DBREAKC_SB_LB (DBREAKC_SB | DBREAKC_LB)
188 #define DBREAKC_MASK 0x3f
189
190 #define MAX_NAREG 64
191 #define MAX_NINTERRUPT 32
192 #define MAX_NLEVEL 6
193 #define MAX_NNMI 1
194 #define MAX_NCCOMPARE 3
195 #define MAX_TLB_WAY_SIZE 8
196 #define MAX_NDBREAK 2
197
198 #define REGION_PAGE_MASK 0xe0000000
199
200 #define PAGE_CACHE_MASK 0x700
201 #define PAGE_CACHE_SHIFT 8
202 #define PAGE_CACHE_INVALID 0x000
203 #define PAGE_CACHE_BYPASS 0x100
204 #define PAGE_CACHE_WT 0x200
205 #define PAGE_CACHE_WB 0x400
206 #define PAGE_CACHE_ISOLATE 0x600
207
208 enum {
209 /* Static vectors */
210 EXC_RESET,
211 EXC_MEMORY_ERROR,
212
213 /* Dynamic vectors */
214 EXC_WINDOW_OVERFLOW4,
215 EXC_WINDOW_UNDERFLOW4,
216 EXC_WINDOW_OVERFLOW8,
217 EXC_WINDOW_UNDERFLOW8,
218 EXC_WINDOW_OVERFLOW12,
219 EXC_WINDOW_UNDERFLOW12,
220 EXC_IRQ,
221 EXC_KERNEL,
222 EXC_USER,
223 EXC_DOUBLE,
224 EXC_DEBUG,
225 EXC_MAX
226 };
227
228 enum {
229 ILLEGAL_INSTRUCTION_CAUSE = 0,
230 SYSCALL_CAUSE,
231 INSTRUCTION_FETCH_ERROR_CAUSE,
232 LOAD_STORE_ERROR_CAUSE,
233 LEVEL1_INTERRUPT_CAUSE,
234 ALLOCA_CAUSE,
235 INTEGER_DIVIDE_BY_ZERO_CAUSE,
236 PRIVILEGED_CAUSE = 8,
237 LOAD_STORE_ALIGNMENT_CAUSE,
238
239 INSTR_PIF_DATA_ERROR_CAUSE = 12,
240 LOAD_STORE_PIF_DATA_ERROR_CAUSE,
241 INSTR_PIF_ADDR_ERROR_CAUSE,
242 LOAD_STORE_PIF_ADDR_ERROR_CAUSE,
243
244 INST_TLB_MISS_CAUSE,
245 INST_TLB_MULTI_HIT_CAUSE,
246 INST_FETCH_PRIVILEGE_CAUSE,
247 INST_FETCH_PROHIBITED_CAUSE = 20,
248 LOAD_STORE_TLB_MISS_CAUSE = 24,
249 LOAD_STORE_TLB_MULTI_HIT_CAUSE,
250 LOAD_STORE_PRIVILEGE_CAUSE,
251 LOAD_PROHIBITED_CAUSE = 28,
252 STORE_PROHIBITED_CAUSE,
253
254 COPROCESSOR0_DISABLED = 32,
255 };
256
257 typedef enum {
258 INTTYPE_LEVEL,
259 INTTYPE_EDGE,
260 INTTYPE_NMI,
261 INTTYPE_SOFTWARE,
262 INTTYPE_TIMER,
263 INTTYPE_DEBUG,
264 INTTYPE_WRITE_ERR,
265 INTTYPE_MAX
266 } interrupt_type;
267
268 typedef struct xtensa_tlb_entry {
269 uint32_t vaddr;
270 uint32_t paddr;
271 uint8_t asid;
272 uint8_t attr;
273 bool variable;
274 } xtensa_tlb_entry;
275
276 typedef struct xtensa_tlb {
277 unsigned nways;
278 const unsigned way_size[10];
279 bool varway56;
280 unsigned nrefillentries;
281 } xtensa_tlb;
282
283 typedef struct XtensaGdbReg {
284 int targno;
285 int type;
286 int group;
287 } XtensaGdbReg;
288
289 typedef struct XtensaGdbRegmap {
290 int num_regs;
291 int num_core_regs;
292 /* PC + a + ar + sr + ur */
293 XtensaGdbReg reg[1 + 16 + 64 + 256 + 256];
294 } XtensaGdbRegmap;
295
296 typedef struct XtensaConfig {
297 const char *name;
298 uint64_t options;
299 XtensaGdbRegmap gdb_regmap;
300 unsigned nareg;
301 int excm_level;
302 int ndepc;
303 uint32_t vecbase;
304 uint32_t exception_vector[EXC_MAX];
305 unsigned ninterrupt;
306 unsigned nlevel;
307 uint32_t interrupt_vector[MAX_NLEVEL + MAX_NNMI + 1];
308 uint32_t level_mask[MAX_NLEVEL + MAX_NNMI + 1];
309 uint32_t inttype_mask[INTTYPE_MAX];
310 struct {
311 uint32_t level;
312 interrupt_type inttype;
313 } interrupt[MAX_NINTERRUPT];
314 unsigned nccompare;
315 uint32_t timerint[MAX_NCCOMPARE];
316 unsigned nextint;
317 unsigned extint[MAX_NINTERRUPT];
318
319 unsigned debug_level;
320 unsigned nibreak;
321 unsigned ndbreak;
322
323 uint32_t clock_freq_khz;
324
325 xtensa_tlb itlb;
326 xtensa_tlb dtlb;
327 } XtensaConfig;
328
329 typedef struct XtensaConfigList {
330 const XtensaConfig *config;
331 struct XtensaConfigList *next;
332 } XtensaConfigList;
333
334 typedef struct CPUXtensaState {
335 const XtensaConfig *config;
336 uint32_t regs[16];
337 uint32_t pc;
338 uint32_t sregs[256];
339 uint32_t uregs[256];
340 uint32_t phys_regs[MAX_NAREG];
341 float32 fregs[16];
342 float_status fp_status;
343
344 xtensa_tlb_entry itlb[7][MAX_TLB_WAY_SIZE];
345 xtensa_tlb_entry dtlb[10][MAX_TLB_WAY_SIZE];
346 unsigned autorefill_idx;
347
348 int pending_irq_level; /* level of last raised IRQ */
349 void **irq_inputs;
350 QEMUTimer *ccompare_timer;
351 uint32_t wake_ccount;
352 int64_t halt_clock;
353
354 int exception_taken;
355
356 /* Watchpoints for DBREAK registers */
357 CPUWatchpoint *cpu_watchpoint[MAX_NDBREAK];
358
359 CPU_COMMON
360 } CPUXtensaState;
361
362 #include "cpu-qom.h"
363
364 #define cpu_exec cpu_xtensa_exec
365 #define cpu_gen_code cpu_xtensa_gen_code
366 #define cpu_signal_handler cpu_xtensa_signal_handler
367 #define cpu_list xtensa_cpu_list
368
369 #ifdef TARGET_WORDS_BIGENDIAN
370 #define XTENSA_DEFAULT_CPU_MODEL "fsf"
371 #else
372 #define XTENSA_DEFAULT_CPU_MODEL "dc232b"
373 #endif
374
375 XtensaCPU *cpu_xtensa_init(const char *cpu_model);
376
377 static inline CPUXtensaState *cpu_init(const char *cpu_model)
378 {
379 XtensaCPU *cpu = cpu_xtensa_init(cpu_model);
380 if (cpu == NULL) {
381 return NULL;
382 }
383 return &cpu->env;
384 }
385
386 void xtensa_translate_init(void);
387 int cpu_xtensa_exec(CPUXtensaState *s);
388 void xtensa_register_core(XtensaConfigList *node);
389 void do_interrupt(CPUXtensaState *s);
390 void check_interrupts(CPUXtensaState *s);
391 void xtensa_irq_init(CPUXtensaState *env);
392 void *xtensa_get_extint(CPUXtensaState *env, unsigned extint);
393 void xtensa_advance_ccount(CPUXtensaState *env, uint32_t d);
394 void xtensa_timer_irq(CPUXtensaState *env, uint32_t id, uint32_t active);
395 void xtensa_rearm_ccompare_timer(CPUXtensaState *env);
396 int cpu_xtensa_signal_handler(int host_signum, void *pinfo, void *puc);
397 void xtensa_cpu_list(FILE *f, fprintf_function cpu_fprintf);
398 void xtensa_sync_window_from_phys(CPUXtensaState *env);
399 void xtensa_sync_phys_from_window(CPUXtensaState *env);
400 uint32_t xtensa_tlb_get_addr_mask(const CPUXtensaState *env, bool dtlb, uint32_t way);
401 void split_tlb_entry_spec_way(const CPUXtensaState *env, uint32_t v, bool dtlb,
402 uint32_t *vpn, uint32_t wi, uint32_t *ei);
403 int xtensa_tlb_lookup(const CPUXtensaState *env, uint32_t addr, bool dtlb,
404 uint32_t *pwi, uint32_t *pei, uint8_t *pring);
405 void xtensa_tlb_set_entry_mmu(const CPUXtensaState *env,
406 xtensa_tlb_entry *entry, bool dtlb,
407 unsigned wi, unsigned ei, uint32_t vpn, uint32_t pte);
408 void xtensa_tlb_set_entry(CPUXtensaState *env, bool dtlb,
409 unsigned wi, unsigned ei, uint32_t vpn, uint32_t pte);
410 int xtensa_get_physical_addr(CPUXtensaState *env, bool update_tlb,
411 uint32_t vaddr, int is_write, int mmu_idx,
412 uint32_t *paddr, uint32_t *page_size, unsigned *access);
413 void reset_mmu(CPUXtensaState *env);
414 void dump_mmu(FILE *f, fprintf_function cpu_fprintf, CPUXtensaState *env);
415 void debug_exception_env(CPUXtensaState *new_env, uint32_t cause);
416
417
418 #define XTENSA_OPTION_BIT(opt) (((uint64_t)1) << (opt))
419
420 static inline bool xtensa_option_bits_enabled(const XtensaConfig *config,
421 uint64_t opt)
422 {
423 return (config->options & opt) != 0;
424 }
425
426 static inline bool xtensa_option_enabled(const XtensaConfig *config, int opt)
427 {
428 return xtensa_option_bits_enabled(config, XTENSA_OPTION_BIT(opt));
429 }
430
431 static inline int xtensa_get_cintlevel(const CPUXtensaState *env)
432 {
433 int level = (env->sregs[PS] & PS_INTLEVEL) >> PS_INTLEVEL_SHIFT;
434 if ((env->sregs[PS] & PS_EXCM) && env->config->excm_level > level) {
435 level = env->config->excm_level;
436 }
437 return level;
438 }
439
440 static inline int xtensa_get_ring(const CPUXtensaState *env)
441 {
442 if (xtensa_option_enabled(env->config, XTENSA_OPTION_MMU)) {
443 return (env->sregs[PS] & PS_RING) >> PS_RING_SHIFT;
444 } else {
445 return 0;
446 }
447 }
448
449 static inline int xtensa_get_cring(const CPUXtensaState *env)
450 {
451 if (xtensa_option_enabled(env->config, XTENSA_OPTION_MMU) &&
452 (env->sregs[PS] & PS_EXCM) == 0) {
453 return (env->sregs[PS] & PS_RING) >> PS_RING_SHIFT;
454 } else {
455 return 0;
456 }
457 }
458
459 static inline xtensa_tlb_entry *xtensa_tlb_get_entry(CPUXtensaState *env,
460 bool dtlb, unsigned wi, unsigned ei)
461 {
462 return dtlb ?
463 env->dtlb[wi] + ei :
464 env->itlb[wi] + ei;
465 }
466
467 /* MMU modes definitions */
468 #define MMU_MODE0_SUFFIX _ring0
469 #define MMU_MODE1_SUFFIX _ring1
470 #define MMU_MODE2_SUFFIX _ring2
471 #define MMU_MODE3_SUFFIX _ring3
472
473 static inline int cpu_mmu_index(CPUXtensaState *env)
474 {
475 return xtensa_get_cring(env);
476 }
477
478 #define XTENSA_TBFLAG_RING_MASK 0x3
479 #define XTENSA_TBFLAG_EXCM 0x4
480 #define XTENSA_TBFLAG_LITBASE 0x8
481 #define XTENSA_TBFLAG_DEBUG 0x10
482 #define XTENSA_TBFLAG_ICOUNT 0x20
483 #define XTENSA_TBFLAG_CPENABLE_MASK 0x3fc0
484 #define XTENSA_TBFLAG_CPENABLE_SHIFT 6
485
486 static inline void cpu_get_tb_cpu_state(CPUXtensaState *env, target_ulong *pc,
487 target_ulong *cs_base, int *flags)
488 {
489 *pc = env->pc;
490 *cs_base = 0;
491 *flags = 0;
492 *flags |= xtensa_get_ring(env);
493 if (env->sregs[PS] & PS_EXCM) {
494 *flags |= XTENSA_TBFLAG_EXCM;
495 }
496 if (xtensa_option_enabled(env->config, XTENSA_OPTION_EXTENDED_L32R) &&
497 (env->sregs[LITBASE] & 1)) {
498 *flags |= XTENSA_TBFLAG_LITBASE;
499 }
500 if (xtensa_option_enabled(env->config, XTENSA_OPTION_DEBUG)) {
501 if (xtensa_get_cintlevel(env) < env->config->debug_level) {
502 *flags |= XTENSA_TBFLAG_DEBUG;
503 }
504 if (xtensa_get_cintlevel(env) < env->sregs[ICOUNTLEVEL]) {
505 *flags |= XTENSA_TBFLAG_ICOUNT;
506 }
507 }
508 if (xtensa_option_enabled(env->config, XTENSA_OPTION_COPROCESSOR)) {
509 *flags |= env->sregs[CPENABLE] << XTENSA_TBFLAG_CPENABLE_SHIFT;
510 }
511 }
512
513 #include "cpu-all.h"
514 #include "exec-all.h"
515
516 static inline int cpu_has_work(CPUState *cpu)
517 {
518 CPUXtensaState *env = &XTENSA_CPU(cpu)->env;
519
520 return env->pending_irq_level;
521 }
522
523 static inline void cpu_pc_from_tb(CPUXtensaState *env, TranslationBlock *tb)
524 {
525 env->pc = tb->pc;
526 }
527
528 #endif