4 * Copyright (c) 2012 Chris Wulff <crwulff@gmail.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see
18 * <http://www.gnu.org/licenses/lgpl-2.1.html>
21 #include "qemu/osdep.h"
22 #include "qemu/module.h"
23 #include "qapi/error.h"
26 #include "exec/gdbstub.h"
27 #include "hw/qdev-properties.h"
29 static void nios2_cpu_set_pc(CPUState
*cs
, vaddr value
)
31 Nios2CPU
*cpu
= NIOS2_CPU(cs
);
32 CPUNios2State
*env
= &cpu
->env
;
37 static vaddr
nios2_cpu_get_pc(CPUState
*cs
)
39 Nios2CPU
*cpu
= NIOS2_CPU(cs
);
40 CPUNios2State
*env
= &cpu
->env
;
45 static void nios2_restore_state_to_opc(CPUState
*cs
,
46 const TranslationBlock
*tb
,
49 Nios2CPU
*cpu
= NIOS2_CPU(cs
);
50 CPUNios2State
*env
= &cpu
->env
;
55 static bool nios2_cpu_has_work(CPUState
*cs
)
57 return cs
->interrupt_request
& CPU_INTERRUPT_HARD
;
60 static void nios2_cpu_reset(DeviceState
*dev
)
62 CPUState
*cs
= CPU(dev
);
63 Nios2CPU
*cpu
= NIOS2_CPU(cs
);
64 Nios2CPUClass
*ncc
= NIOS2_CPU_GET_CLASS(cpu
);
65 CPUNios2State
*env
= &cpu
->env
;
67 ncc
->parent_reset(dev
);
69 memset(env
->ctrl
, 0, sizeof(env
->ctrl
));
70 env
->pc
= cpu
->reset_addr
;
72 #if defined(CONFIG_USER_ONLY)
73 /* Start in user mode with interrupts enabled. */
74 env
->ctrl
[CR_STATUS
] = CR_STATUS_RSIE
| CR_STATUS_U
| CR_STATUS_PIE
;
75 memset(env
->regs
, 0, sizeof(env
->regs
));
77 env
->ctrl
[CR_STATUS
] = CR_STATUS_RSIE
;
78 nios2_update_crs(env
);
79 memset(env
->shadow_regs
, 0, sizeof(env
->shadow_regs
));
83 #ifndef CONFIG_USER_ONLY
84 static void eic_set_irq(void *opaque
, int irq
, int level
)
86 Nios2CPU
*cpu
= opaque
;
87 CPUState
*cs
= CPU(cpu
);
90 cpu_interrupt(cs
, CPU_INTERRUPT_HARD
);
92 cpu_reset_interrupt(cs
, CPU_INTERRUPT_HARD
);
96 static void iic_set_irq(void *opaque
, int irq
, int level
)
98 Nios2CPU
*cpu
= opaque
;
99 CPUNios2State
*env
= &cpu
->env
;
100 CPUState
*cs
= CPU(cpu
);
102 env
->ctrl
[CR_IPENDING
] = deposit32(env
->ctrl
[CR_IPENDING
], irq
, 1, !!level
);
104 if (env
->ctrl
[CR_IPENDING
]) {
105 cpu_interrupt(cs
, CPU_INTERRUPT_HARD
);
107 cpu_reset_interrupt(cs
, CPU_INTERRUPT_HARD
);
112 static void nios2_cpu_initfn(Object
*obj
)
114 Nios2CPU
*cpu
= NIOS2_CPU(obj
);
116 cpu_set_cpustate_pointers(cpu
);
118 #if !defined(CONFIG_USER_ONLY)
123 static ObjectClass
*nios2_cpu_class_by_name(const char *cpu_model
)
125 return object_class_by_name(TYPE_NIOS2_CPU
);
128 static void realize_cr_status(CPUState
*cs
)
130 Nios2CPU
*cpu
= NIOS2_CPU(cs
);
132 /* Begin with all fields of all registers are reserved. */
133 memset(cpu
->cr_state
, 0, sizeof(cpu
->cr_state
));
136 * The combination of writable and readonly is the set of all
137 * non-reserved fields. We apply writable as a mask to bits,
138 * and merge in existing readonly bits, before storing.
140 #define WR_REG(C) cpu->cr_state[C].writable = -1
141 #define RO_REG(C) cpu->cr_state[C].readonly = -1
142 #define WR_FIELD(C, F) cpu->cr_state[C].writable |= R_##C##_##F##_MASK
143 #define RO_FIELD(C, F) cpu->cr_state[C].readonly |= R_##C##_##F##_MASK
145 WR_FIELD(CR_STATUS
, PIE
);
149 RO_REG(CR_EXCEPTION
);
152 if (cpu
->eic_present
) {
153 WR_FIELD(CR_STATUS
, RSIE
);
154 RO_FIELD(CR_STATUS
, NMI
);
155 WR_FIELD(CR_STATUS
, PRS
);
156 RO_FIELD(CR_STATUS
, CRS
);
157 WR_FIELD(CR_STATUS
, IL
);
158 WR_FIELD(CR_STATUS
, IH
);
160 RO_FIELD(CR_STATUS
, RSIE
);
165 if (cpu
->mmu_present
) {
166 WR_FIELD(CR_STATUS
, U
);
167 WR_FIELD(CR_STATUS
, EH
);
169 WR_FIELD(CR_PTEADDR
, VPN
);
170 WR_FIELD(CR_PTEADDR
, PTBASE
);
172 RO_FIELD(CR_TLBMISC
, D
);
173 RO_FIELD(CR_TLBMISC
, PERM
);
174 RO_FIELD(CR_TLBMISC
, BAD
);
175 RO_FIELD(CR_TLBMISC
, DBL
);
176 WR_FIELD(CR_TLBMISC
, PID
);
177 WR_FIELD(CR_TLBMISC
, WE
);
178 WR_FIELD(CR_TLBMISC
, RD
);
179 WR_FIELD(CR_TLBMISC
, WAY
);
185 * TODO: ECC (config, eccinj) and MPU (config, mpubase, mpuacc) are
186 * unimplemented, so their corresponding control regs remain reserved.
195 static void nios2_cpu_realizefn(DeviceState
*dev
, Error
**errp
)
197 CPUState
*cs
= CPU(dev
);
198 Nios2CPU
*cpu
= NIOS2_CPU(cs
);
199 Nios2CPUClass
*ncc
= NIOS2_CPU_GET_CLASS(dev
);
200 Error
*local_err
= NULL
;
202 #ifndef CONFIG_USER_ONLY
203 if (cpu
->eic_present
) {
204 qdev_init_gpio_in_named(DEVICE(cpu
), eic_set_irq
, "EIC", 1);
206 qdev_init_gpio_in_named(DEVICE(cpu
), iic_set_irq
, "IRQ", 32);
210 cpu_exec_realizefn(cs
, &local_err
);
211 if (local_err
!= NULL
) {
212 error_propagate(errp
, local_err
);
216 realize_cr_status(cs
);
220 /* We have reserved storage for cpuid; might as well use it. */
221 cpu
->env
.ctrl
[CR_CPUID
] = cs
->cpu_index
;
223 ncc
->parent_realize(dev
, errp
);
226 #ifndef CONFIG_USER_ONLY
227 static bool eic_take_interrupt(Nios2CPU
*cpu
)
229 CPUNios2State
*env
= &cpu
->env
;
230 const uint32_t status
= env
->ctrl
[CR_STATUS
];
233 return !(status
& CR_STATUS_NMI
);
235 if (!(status
& CR_STATUS_PIE
)) {
238 if (cpu
->ril
<= FIELD_EX32(status
, CR_STATUS
, IL
)) {
241 if (cpu
->rrs
!= FIELD_EX32(status
, CR_STATUS
, CRS
)) {
244 return status
& CR_STATUS_RSIE
;
247 static bool iic_take_interrupt(Nios2CPU
*cpu
)
249 CPUNios2State
*env
= &cpu
->env
;
251 if (!(env
->ctrl
[CR_STATUS
] & CR_STATUS_PIE
)) {
254 return env
->ctrl
[CR_IPENDING
] & env
->ctrl
[CR_IENABLE
];
257 static bool nios2_cpu_exec_interrupt(CPUState
*cs
, int interrupt_request
)
259 Nios2CPU
*cpu
= NIOS2_CPU(cs
);
261 if (interrupt_request
& CPU_INTERRUPT_HARD
) {
263 ? eic_take_interrupt(cpu
)
264 : iic_take_interrupt(cpu
)) {
265 cs
->exception_index
= EXCP_IRQ
;
266 nios2_cpu_do_interrupt(cs
);
272 #endif /* !CONFIG_USER_ONLY */
274 static void nios2_cpu_disas_set_info(CPUState
*cpu
, disassemble_info
*info
)
276 /* NOTE: NiosII R2 is not supported yet. */
277 info
->mach
= bfd_arch_nios2
;
278 info
->print_insn
= print_insn_nios2
;
281 static int nios2_cpu_gdb_read_register(CPUState
*cs
, GByteArray
*mem_buf
, int n
)
283 Nios2CPU
*cpu
= NIOS2_CPU(cs
);
284 CPUNios2State
*env
= &cpu
->env
;
287 if (n
< 32) { /* GP regs */
289 } else if (n
== 32) { /* PC */
291 } else if (n
< 49) { /* Status regs */
292 unsigned cr
= n
- 33;
293 if (nios2_cr_reserved(&cpu
->cr_state
[cr
])) {
296 val
= env
->ctrl
[n
- 33];
303 return gdb_get_reg32(mem_buf
, val
);
306 static int nios2_cpu_gdb_write_register(CPUState
*cs
, uint8_t *mem_buf
, int n
)
308 Nios2CPU
*cpu
= NIOS2_CPU(cs
);
309 CPUClass
*cc
= CPU_GET_CLASS(cs
);
310 CPUNios2State
*env
= &cpu
->env
;
313 if (n
> cc
->gdb_num_core_regs
) {
316 val
= ldl_p(mem_buf
);
318 if (n
< 32) { /* GP regs */
320 } else if (n
== 32) { /* PC */
322 } else if (n
< 49) { /* Status regs */
323 unsigned cr
= n
- 33;
324 /* ??? Maybe allow the debugger to write to readonly fields. */
325 val
&= cpu
->cr_state
[cr
].writable
;
326 val
|= cpu
->cr_state
[cr
].readonly
& env
->ctrl
[cr
];
329 g_assert_not_reached();
335 static Property nios2_properties
[] = {
336 DEFINE_PROP_BOOL("diverr_present", Nios2CPU
, diverr_present
, true),
337 DEFINE_PROP_BOOL("mmu_present", Nios2CPU
, mmu_present
, true),
338 /* ALTR,pid-num-bits */
339 DEFINE_PROP_UINT32("mmu_pid_num_bits", Nios2CPU
, pid_num_bits
, 8),
340 /* ALTR,tlb-num-ways */
341 DEFINE_PROP_UINT32("mmu_tlb_num_ways", Nios2CPU
, tlb_num_ways
, 16),
342 /* ALTR,tlb-num-entries */
343 DEFINE_PROP_UINT32("mmu_pid_num_entries", Nios2CPU
, tlb_num_entries
, 256),
344 DEFINE_PROP_END_OF_LIST(),
347 #ifndef CONFIG_USER_ONLY
348 #include "hw/core/sysemu-cpu-ops.h"
350 static const struct SysemuCPUOps nios2_sysemu_ops
= {
351 .get_phys_page_debug
= nios2_cpu_get_phys_page_debug
,
355 #include "hw/core/tcg-cpu-ops.h"
357 static const struct TCGCPUOps nios2_tcg_ops
= {
358 .initialize
= nios2_tcg_init
,
359 .restore_state_to_opc
= nios2_restore_state_to_opc
,
361 #ifndef CONFIG_USER_ONLY
362 .tlb_fill
= nios2_cpu_tlb_fill
,
363 .cpu_exec_interrupt
= nios2_cpu_exec_interrupt
,
364 .do_interrupt
= nios2_cpu_do_interrupt
,
365 .do_unaligned_access
= nios2_cpu_do_unaligned_access
,
366 #endif /* !CONFIG_USER_ONLY */
369 static void nios2_cpu_class_init(ObjectClass
*oc
, void *data
)
371 DeviceClass
*dc
= DEVICE_CLASS(oc
);
372 CPUClass
*cc
= CPU_CLASS(oc
);
373 Nios2CPUClass
*ncc
= NIOS2_CPU_CLASS(oc
);
375 device_class_set_parent_realize(dc
, nios2_cpu_realizefn
,
376 &ncc
->parent_realize
);
377 device_class_set_props(dc
, nios2_properties
);
378 device_class_set_parent_reset(dc
, nios2_cpu_reset
, &ncc
->parent_reset
);
380 cc
->class_by_name
= nios2_cpu_class_by_name
;
381 cc
->has_work
= nios2_cpu_has_work
;
382 cc
->dump_state
= nios2_cpu_dump_state
;
383 cc
->set_pc
= nios2_cpu_set_pc
;
384 cc
->get_pc
= nios2_cpu_get_pc
;
385 cc
->disas_set_info
= nios2_cpu_disas_set_info
;
386 #ifndef CONFIG_USER_ONLY
387 cc
->sysemu_ops
= &nios2_sysemu_ops
;
389 cc
->gdb_read_register
= nios2_cpu_gdb_read_register
;
390 cc
->gdb_write_register
= nios2_cpu_gdb_write_register
;
391 cc
->gdb_num_core_regs
= 49;
392 cc
->tcg_ops
= &nios2_tcg_ops
;
395 static const TypeInfo nios2_cpu_type_info
= {
396 .name
= TYPE_NIOS2_CPU
,
398 .instance_size
= sizeof(Nios2CPU
),
399 .instance_init
= nios2_cpu_initfn
,
400 .class_size
= sizeof(Nios2CPUClass
),
401 .class_init
= nios2_cpu_class_init
,
404 static void nios2_cpu_register_types(void)
406 type_register_static(&nios2_cpu_type_info
);
409 type_init(nios2_cpu_register_types
)