vhost: use avail event idx on vhost_svq_kick
[qemu/kevin.git] / target / nios2 / cpu.c
blob9a5351bc81dcfd54f845595dfdeaece68744f259
1 /*
2 * QEMU Nios II CPU
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"
24 #include "cpu.h"
25 #include "exec/log.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;
34 env->pc = value;
37 static vaddr nios2_cpu_get_pc(CPUState *cs)
39 Nios2CPU *cpu = NIOS2_CPU(cs);
40 CPUNios2State *env = &cpu->env;
42 return env->pc;
45 static void nios2_restore_state_to_opc(CPUState *cs,
46 const TranslationBlock *tb,
47 const uint64_t *data)
49 Nios2CPU *cpu = NIOS2_CPU(cs);
50 CPUNios2State *env = &cpu->env;
52 env->pc = data[0];
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));
76 #else
77 env->ctrl[CR_STATUS] = CR_STATUS_RSIE;
78 nios2_update_crs(env);
79 memset(env->shadow_regs, 0, sizeof(env->shadow_regs));
80 #endif
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);
89 if (level) {
90 cpu_interrupt(cs, CPU_INTERRUPT_HARD);
91 } else {
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);
106 } else {
107 cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
110 #endif
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)
119 mmu_init(&cpu->env);
120 #endif
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);
146 WR_REG(CR_ESTATUS);
147 WR_REG(CR_BSTATUS);
148 RO_REG(CR_CPUID);
149 RO_REG(CR_EXCEPTION);
150 WR_REG(CR_BADADDR);
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);
159 } else {
160 RO_FIELD(CR_STATUS, RSIE);
161 WR_REG(CR_IENABLE);
162 RO_REG(CR_IPENDING);
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);
181 WR_REG(CR_TLBACC);
185 * TODO: ECC (config, eccinj) and MPU (config, mpubase, mpuacc) are
186 * unimplemented, so their corresponding control regs remain reserved.
189 #undef WR_REG
190 #undef RO_REG
191 #undef WR_FIELD
192 #undef RO_FIELD
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);
205 } else {
206 qdev_init_gpio_in_named(DEVICE(cpu), iic_set_irq, "IRQ", 32);
208 #endif
210 cpu_exec_realizefn(cs, &local_err);
211 if (local_err != NULL) {
212 error_propagate(errp, local_err);
213 return;
216 realize_cr_status(cs);
217 qemu_init_vcpu(cs);
218 cpu_reset(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];
232 if (cpu->rnmi) {
233 return !(status & CR_STATUS_NMI);
235 if (!(status & CR_STATUS_PIE)) {
236 return false;
238 if (cpu->ril <= FIELD_EX32(status, CR_STATUS, IL)) {
239 return false;
241 if (cpu->rrs != FIELD_EX32(status, CR_STATUS, CRS)) {
242 return true;
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)) {
252 return false;
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) {
262 if (cpu->eic_present
263 ? eic_take_interrupt(cpu)
264 : iic_take_interrupt(cpu)) {
265 cs->exception_index = EXCP_IRQ;
266 nios2_cpu_do_interrupt(cs);
267 return true;
270 return false;
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;
285 uint32_t val;
287 if (n < 32) { /* GP regs */
288 val = env->regs[n];
289 } else if (n == 32) { /* PC */
290 val = env->pc;
291 } else if (n < 49) { /* Status regs */
292 unsigned cr = n - 33;
293 if (nios2_cr_reserved(&cpu->cr_state[cr])) {
294 val = 0;
295 } else {
296 val = env->ctrl[n - 33];
298 } else {
299 /* Invalid regs */
300 return 0;
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;
311 uint32_t val;
313 if (n > cc->gdb_num_core_regs) {
314 return 0;
316 val = ldl_p(mem_buf);
318 if (n < 32) { /* GP regs */
319 env->regs[n] = val;
320 } else if (n == 32) { /* PC */
321 env->pc = val;
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];
327 env->ctrl[cr] = val;
328 } else {
329 g_assert_not_reached();
332 return 4;
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,
353 #endif
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;
388 #endif
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,
397 .parent = TYPE_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)