127a4a30eSYoshinori Sato /*
227a4a30eSYoshinori Sato * QEMU RX CPU
327a4a30eSYoshinori Sato *
427a4a30eSYoshinori Sato * Copyright (c) 2019 Yoshinori Sato
527a4a30eSYoshinori Sato *
627a4a30eSYoshinori Sato * This program is free software; you can redistribute it and/or modify it
727a4a30eSYoshinori Sato * under the terms and conditions of the GNU General Public License,
827a4a30eSYoshinori Sato * version 2 or later, as published by the Free Software Foundation.
927a4a30eSYoshinori Sato *
1027a4a30eSYoshinori Sato * This program is distributed in the hope it will be useful, but WITHOUT
1127a4a30eSYoshinori Sato * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1227a4a30eSYoshinori Sato * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
1327a4a30eSYoshinori Sato * more details.
1427a4a30eSYoshinori Sato *
1527a4a30eSYoshinori Sato * You should have received a copy of the GNU General Public License along with
1627a4a30eSYoshinori Sato * this program. If not, see <http://www.gnu.org/licenses/>.
1727a4a30eSYoshinori Sato */
1827a4a30eSYoshinori Sato
1927a4a30eSYoshinori Sato #include "qemu/osdep.h"
2027a4a30eSYoshinori Sato #include "qemu/qemu-print.h"
2127a4a30eSYoshinori Sato #include "qapi/error.h"
2227a4a30eSYoshinori Sato #include "cpu.h"
2327a4a30eSYoshinori Sato #include "migration/vmstate.h"
2427a4a30eSYoshinori Sato #include "exec/exec-all.h"
2574781c08SPhilippe Mathieu-Daudé #include "exec/page-protection.h"
2627a4a30eSYoshinori Sato #include "hw/loader.h"
2727a4a30eSYoshinori Sato #include "fpu/softfloat.h"
28fafe0021SRichard Henderson #include "tcg/debug-assert.h"
2927a4a30eSYoshinori Sato
rx_cpu_set_pc(CPUState * cs,vaddr value)3027a4a30eSYoshinori Sato static void rx_cpu_set_pc(CPUState *cs, vaddr value)
3127a4a30eSYoshinori Sato {
3238688fdbSEduardo Habkost RXCPU *cpu = RX_CPU(cs);
3327a4a30eSYoshinori Sato
3427a4a30eSYoshinori Sato cpu->env.pc = value;
3527a4a30eSYoshinori Sato }
3627a4a30eSYoshinori Sato
rx_cpu_get_pc(CPUState * cs)37e4fdf9dfSRichard Henderson static vaddr rx_cpu_get_pc(CPUState *cs)
38e4fdf9dfSRichard Henderson {
39e4fdf9dfSRichard Henderson RXCPU *cpu = RX_CPU(cs);
40e4fdf9dfSRichard Henderson
41e4fdf9dfSRichard Henderson return cpu->env.pc;
42e4fdf9dfSRichard Henderson }
43e4fdf9dfSRichard Henderson
rx_cpu_synchronize_from_tb(CPUState * cs,const TranslationBlock * tb)4404a37d4cSRichard Henderson static void rx_cpu_synchronize_from_tb(CPUState *cs,
4504a37d4cSRichard Henderson const TranslationBlock *tb)
4627a4a30eSYoshinori Sato {
4738688fdbSEduardo Habkost RXCPU *cpu = RX_CPU(cs);
4827a4a30eSYoshinori Sato
49b254c342SPhilippe Mathieu-Daudé tcg_debug_assert(!tcg_cflags_has(cs, CF_PCREL));
508023d1abSAnton Johansson cpu->env.pc = tb->pc;
5127a4a30eSYoshinori Sato }
5227a4a30eSYoshinori Sato
rx_restore_state_to_opc(CPUState * cs,const TranslationBlock * tb,const uint64_t * data)535439d7a6SRichard Henderson static void rx_restore_state_to_opc(CPUState *cs,
545439d7a6SRichard Henderson const TranslationBlock *tb,
555439d7a6SRichard Henderson const uint64_t *data)
565439d7a6SRichard Henderson {
575439d7a6SRichard Henderson RXCPU *cpu = RX_CPU(cs);
585439d7a6SRichard Henderson
595439d7a6SRichard Henderson cpu->env.pc = data[0];
605439d7a6SRichard Henderson }
615439d7a6SRichard Henderson
rx_cpu_has_work(CPUState * cs)6227a4a30eSYoshinori Sato static bool rx_cpu_has_work(CPUState *cs)
6327a4a30eSYoshinori Sato {
6427a4a30eSYoshinori Sato return cs->interrupt_request &
6527a4a30eSYoshinori Sato (CPU_INTERRUPT_HARD | CPU_INTERRUPT_FIR);
6627a4a30eSYoshinori Sato }
6727a4a30eSYoshinori Sato
riscv_cpu_mmu_index(CPUState * cs,bool ifunc)68ef5cc166SRichard Henderson static int riscv_cpu_mmu_index(CPUState *cs, bool ifunc)
69ef5cc166SRichard Henderson {
70ef5cc166SRichard Henderson return 0;
71ef5cc166SRichard Henderson }
72ef5cc166SRichard Henderson
rx_cpu_reset_hold(Object * obj,ResetType type)73ad80e367SPeter Maydell static void rx_cpu_reset_hold(Object *obj, ResetType type)
7427a4a30eSYoshinori Sato {
75f2a4459dSPhilippe Mathieu-Daudé CPUState *cs = CPU(obj);
76348802b5SPhilippe Mathieu-Daudé RXCPUClass *rcc = RX_CPU_GET_CLASS(obj);
77f2a4459dSPhilippe Mathieu-Daudé CPURXState *env = cpu_env(cs);
7827a4a30eSYoshinori Sato uint32_t *resetvec;
7927a4a30eSYoshinori Sato
8088c41e40SPeter Maydell if (rcc->parent_phases.hold) {
81ad80e367SPeter Maydell rcc->parent_phases.hold(obj, type);
8288c41e40SPeter Maydell }
8327a4a30eSYoshinori Sato
8427a4a30eSYoshinori Sato memset(env, 0, offsetof(CPURXState, end_reset_fields));
8527a4a30eSYoshinori Sato
8627a4a30eSYoshinori Sato resetvec = rom_ptr(0xfffffffc, 4);
8727a4a30eSYoshinori Sato if (resetvec) {
8827a4a30eSYoshinori Sato /* In the case of kernel, it is ignored because it is not set. */
8927a4a30eSYoshinori Sato env->pc = ldl_p(resetvec);
9027a4a30eSYoshinori Sato }
9127a4a30eSYoshinori Sato rx_cpu_unpack_psw(env, 0, 1);
9227a4a30eSYoshinori Sato env->regs[0] = env->isp = env->usp = 0;
9327a4a30eSYoshinori Sato env->fpsw = 0;
9427a4a30eSYoshinori Sato set_flush_to_zero(1, &env->fp_status);
9527a4a30eSYoshinori Sato set_flush_inputs_to_zero(1, &env->fp_status);
96*ba655846SPeter Maydell /*
97*ba655846SPeter Maydell * TODO: this is not the correct NaN propagation rule for this
98*ba655846SPeter Maydell * architecture. The "RX Family User's Manual: Software" table 1.6
99*ba655846SPeter Maydell * defines the propagation rules as "prefer SNaN over QNaN;
100*ba655846SPeter Maydell * then prefer dest over source", which is float_2nan_prop_s_ab.
101*ba655846SPeter Maydell */
102*ba655846SPeter Maydell set_float_2nan_prop_rule(float_2nan_prop_x87, &env->fp_status);
10327a4a30eSYoshinori Sato }
10427a4a30eSYoshinori Sato
rx_cpu_class_by_name(const char * cpu_model)10527a4a30eSYoshinori Sato static ObjectClass *rx_cpu_class_by_name(const char *cpu_model)
10627a4a30eSYoshinori Sato {
10727a4a30eSYoshinori Sato ObjectClass *oc;
10827a4a30eSYoshinori Sato char *typename;
10927a4a30eSYoshinori Sato
11027a4a30eSYoshinori Sato oc = object_class_by_name(cpu_model);
1113a9d0d7bSPhilippe Mathieu-Daudé if (oc != NULL && object_class_dynamic_cast(oc, TYPE_RX_CPU) != NULL) {
11227a4a30eSYoshinori Sato return oc;
11327a4a30eSYoshinori Sato }
11427a4a30eSYoshinori Sato typename = g_strdup_printf(RX_CPU_TYPE_NAME("%s"), cpu_model);
11527a4a30eSYoshinori Sato oc = object_class_by_name(typename);
11627a4a30eSYoshinori Sato g_free(typename);
11727a4a30eSYoshinori Sato
11827a4a30eSYoshinori Sato return oc;
11927a4a30eSYoshinori Sato }
12027a4a30eSYoshinori Sato
rx_cpu_realize(DeviceState * dev,Error ** errp)12127a4a30eSYoshinori Sato static void rx_cpu_realize(DeviceState *dev, Error **errp)
12227a4a30eSYoshinori Sato {
12327a4a30eSYoshinori Sato CPUState *cs = CPU(dev);
12438688fdbSEduardo Habkost RXCPUClass *rcc = RX_CPU_GET_CLASS(dev);
12527a4a30eSYoshinori Sato Error *local_err = NULL;
12627a4a30eSYoshinori Sato
12727a4a30eSYoshinori Sato cpu_exec_realizefn(cs, &local_err);
12827a4a30eSYoshinori Sato if (local_err != NULL) {
12927a4a30eSYoshinori Sato error_propagate(errp, local_err);
13027a4a30eSYoshinori Sato return;
13127a4a30eSYoshinori Sato }
13227a4a30eSYoshinori Sato
13327a4a30eSYoshinori Sato qemu_init_vcpu(cs);
13427a4a30eSYoshinori Sato cpu_reset(cs);
13527a4a30eSYoshinori Sato
13627a4a30eSYoshinori Sato rcc->parent_realize(dev, errp);
13727a4a30eSYoshinori Sato }
13827a4a30eSYoshinori Sato
rx_cpu_set_irq(void * opaque,int no,int request)13927a4a30eSYoshinori Sato static void rx_cpu_set_irq(void *opaque, int no, int request)
14027a4a30eSYoshinori Sato {
14127a4a30eSYoshinori Sato RXCPU *cpu = opaque;
14227a4a30eSYoshinori Sato CPUState *cs = CPU(cpu);
14327a4a30eSYoshinori Sato int irq = request & 0xff;
14427a4a30eSYoshinori Sato
14527a4a30eSYoshinori Sato static const int mask[] = {
14627a4a30eSYoshinori Sato [RX_CPU_IRQ] = CPU_INTERRUPT_HARD,
14727a4a30eSYoshinori Sato [RX_CPU_FIR] = CPU_INTERRUPT_FIR,
14827a4a30eSYoshinori Sato };
14927a4a30eSYoshinori Sato if (irq) {
15027a4a30eSYoshinori Sato cpu->env.req_irq = irq;
15127a4a30eSYoshinori Sato cpu->env.req_ipl = (request >> 8) & 0x0f;
15227a4a30eSYoshinori Sato cpu_interrupt(cs, mask[no]);
15327a4a30eSYoshinori Sato } else {
15427a4a30eSYoshinori Sato cpu_reset_interrupt(cs, mask[no]);
15527a4a30eSYoshinori Sato }
15627a4a30eSYoshinori Sato }
15727a4a30eSYoshinori Sato
rx_cpu_disas_set_info(CPUState * cpu,disassemble_info * info)15827a4a30eSYoshinori Sato static void rx_cpu_disas_set_info(CPUState *cpu, disassemble_info *info)
15927a4a30eSYoshinori Sato {
16027a4a30eSYoshinori Sato info->mach = bfd_mach_rx;
16127a4a30eSYoshinori Sato info->print_insn = print_insn_rx;
16227a4a30eSYoshinori Sato }
16327a4a30eSYoshinori Sato
rx_cpu_tlb_fill(CPUState * cs,vaddr addr,int size,MMUAccessType access_type,int mmu_idx,bool probe,uintptr_t retaddr)16427a4a30eSYoshinori Sato static bool rx_cpu_tlb_fill(CPUState *cs, vaddr addr, int size,
16527a4a30eSYoshinori Sato MMUAccessType access_type, int mmu_idx,
16627a4a30eSYoshinori Sato bool probe, uintptr_t retaddr)
16727a4a30eSYoshinori Sato {
16827a4a30eSYoshinori Sato uint32_t address, physical, prot;
16927a4a30eSYoshinori Sato
17027a4a30eSYoshinori Sato /* Linear mapping */
17127a4a30eSYoshinori Sato address = physical = addr & TARGET_PAGE_MASK;
17227a4a30eSYoshinori Sato prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
17327a4a30eSYoshinori Sato tlb_set_page(cs, address, physical, prot, mmu_idx, TARGET_PAGE_SIZE);
17427a4a30eSYoshinori Sato return true;
17527a4a30eSYoshinori Sato }
17627a4a30eSYoshinori Sato
rx_cpu_init(Object * obj)17727a4a30eSYoshinori Sato static void rx_cpu_init(Object *obj)
17827a4a30eSYoshinori Sato {
17938688fdbSEduardo Habkost RXCPU *cpu = RX_CPU(obj);
18027a4a30eSYoshinori Sato
18127a4a30eSYoshinori Sato qdev_init_gpio_in(DEVICE(cpu), rx_cpu_set_irq, 2);
18227a4a30eSYoshinori Sato }
18327a4a30eSYoshinori Sato
1848b80bd28SPhilippe Mathieu-Daudé #ifndef CONFIG_USER_ONLY
1858b80bd28SPhilippe Mathieu-Daudé #include "hw/core/sysemu-cpu-ops.h"
1868b80bd28SPhilippe Mathieu-Daudé
1878b80bd28SPhilippe Mathieu-Daudé static const struct SysemuCPUOps rx_sysemu_ops = {
18808928c6dSPhilippe Mathieu-Daudé .get_phys_page_debug = rx_cpu_get_phys_page_debug,
1898b80bd28SPhilippe Mathieu-Daudé };
1908b80bd28SPhilippe Mathieu-Daudé #endif
1918b80bd28SPhilippe Mathieu-Daudé
19278271684SClaudio Fontana #include "hw/core/tcg-cpu-ops.h"
19378271684SClaudio Fontana
1941764ad70SRichard Henderson static const TCGCPUOps rx_tcg_ops = {
19578271684SClaudio Fontana .initialize = rx_translate_init,
19678271684SClaudio Fontana .synchronize_from_tb = rx_cpu_synchronize_from_tb,
1975439d7a6SRichard Henderson .restore_state_to_opc = rx_restore_state_to_opc,
19878271684SClaudio Fontana .tlb_fill = rx_cpu_tlb_fill,
19978271684SClaudio Fontana
20078271684SClaudio Fontana #ifndef CONFIG_USER_ONLY
20165c575b6SPhilippe Mathieu-Daudé .cpu_exec_interrupt = rx_cpu_exec_interrupt,
2024f7b1ecbSPeter Maydell .cpu_exec_halt = rx_cpu_has_work,
20378271684SClaudio Fontana .do_interrupt = rx_cpu_do_interrupt,
20478271684SClaudio Fontana #endif /* !CONFIG_USER_ONLY */
20578271684SClaudio Fontana };
20678271684SClaudio Fontana
rx_cpu_class_init(ObjectClass * klass,void * data)20727a4a30eSYoshinori Sato static void rx_cpu_class_init(ObjectClass *klass, void *data)
20827a4a30eSYoshinori Sato {
20927a4a30eSYoshinori Sato DeviceClass *dc = DEVICE_CLASS(klass);
21027a4a30eSYoshinori Sato CPUClass *cc = CPU_CLASS(klass);
21138688fdbSEduardo Habkost RXCPUClass *rcc = RX_CPU_CLASS(klass);
21288c41e40SPeter Maydell ResettableClass *rc = RESETTABLE_CLASS(klass);
21327a4a30eSYoshinori Sato
21427a4a30eSYoshinori Sato device_class_set_parent_realize(dc, rx_cpu_realize,
21527a4a30eSYoshinori Sato &rcc->parent_realize);
21688c41e40SPeter Maydell resettable_class_set_parent_phases(rc, NULL, rx_cpu_reset_hold, NULL,
21788c41e40SPeter Maydell &rcc->parent_phases);
21827a4a30eSYoshinori Sato
21927a4a30eSYoshinori Sato cc->class_by_name = rx_cpu_class_by_name;
22027a4a30eSYoshinori Sato cc->has_work = rx_cpu_has_work;
221ef5cc166SRichard Henderson cc->mmu_index = riscv_cpu_mmu_index;
22227a4a30eSYoshinori Sato cc->dump_state = rx_cpu_dump_state;
22327a4a30eSYoshinori Sato cc->set_pc = rx_cpu_set_pc;
224e4fdf9dfSRichard Henderson cc->get_pc = rx_cpu_get_pc;
22578271684SClaudio Fontana
2268b80bd28SPhilippe Mathieu-Daudé #ifndef CONFIG_USER_ONLY
2278b80bd28SPhilippe Mathieu-Daudé cc->sysemu_ops = &rx_sysemu_ops;
2288b80bd28SPhilippe Mathieu-Daudé #endif
22927a4a30eSYoshinori Sato cc->gdb_read_register = rx_cpu_gdb_read_register;
23027a4a30eSYoshinori Sato cc->gdb_write_register = rx_cpu_gdb_write_register;
23127a4a30eSYoshinori Sato cc->disas_set_info = rx_cpu_disas_set_info;
23227a4a30eSYoshinori Sato
23327a4a30eSYoshinori Sato cc->gdb_core_xml_file = "rx-core.xml";
23478271684SClaudio Fontana cc->tcg_ops = &rx_tcg_ops;
23527a4a30eSYoshinori Sato }
23627a4a30eSYoshinori Sato
23727a4a30eSYoshinori Sato static const TypeInfo rx_cpu_info = {
23827a4a30eSYoshinori Sato .name = TYPE_RX_CPU,
23927a4a30eSYoshinori Sato .parent = TYPE_CPU,
24027a4a30eSYoshinori Sato .instance_size = sizeof(RXCPU),
241f669c992SRichard Henderson .instance_align = __alignof(RXCPU),
24227a4a30eSYoshinori Sato .instance_init = rx_cpu_init,
24327a4a30eSYoshinori Sato .abstract = true,
24427a4a30eSYoshinori Sato .class_size = sizeof(RXCPUClass),
24527a4a30eSYoshinori Sato .class_init = rx_cpu_class_init,
24627a4a30eSYoshinori Sato };
24727a4a30eSYoshinori Sato
24827a4a30eSYoshinori Sato static const TypeInfo rx62n_rx_cpu_info = {
24927a4a30eSYoshinori Sato .name = TYPE_RX62N_CPU,
25027a4a30eSYoshinori Sato .parent = TYPE_RX_CPU,
25127a4a30eSYoshinori Sato };
25227a4a30eSYoshinori Sato
rx_cpu_register_types(void)25327a4a30eSYoshinori Sato static void rx_cpu_register_types(void)
25427a4a30eSYoshinori Sato {
25527a4a30eSYoshinori Sato type_register_static(&rx_cpu_info);
25627a4a30eSYoshinori Sato type_register_static(&rx62n_rx_cpu_info);
25727a4a30eSYoshinori Sato }
25827a4a30eSYoshinori Sato
25927a4a30eSYoshinori Sato type_init(rx_cpu_register_types)
260