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" 2527a4a30eSYoshinori Sato #include "hw/loader.h" 2627a4a30eSYoshinori Sato #include "fpu/softfloat.h" 27fafe0021SRichard Henderson #include "tcg/debug-assert.h" 2827a4a30eSYoshinori Sato 2927a4a30eSYoshinori Sato static void rx_cpu_set_pc(CPUState *cs, vaddr value) 3027a4a30eSYoshinori Sato { 3138688fdbSEduardo Habkost RXCPU *cpu = RX_CPU(cs); 3227a4a30eSYoshinori Sato 3327a4a30eSYoshinori Sato cpu->env.pc = value; 3427a4a30eSYoshinori Sato } 3527a4a30eSYoshinori Sato 36e4fdf9dfSRichard Henderson static vaddr rx_cpu_get_pc(CPUState *cs) 37e4fdf9dfSRichard Henderson { 38e4fdf9dfSRichard Henderson RXCPU *cpu = RX_CPU(cs); 39e4fdf9dfSRichard Henderson 40e4fdf9dfSRichard Henderson return cpu->env.pc; 41e4fdf9dfSRichard Henderson } 42e4fdf9dfSRichard Henderson 4304a37d4cSRichard Henderson static void rx_cpu_synchronize_from_tb(CPUState *cs, 4404a37d4cSRichard Henderson const TranslationBlock *tb) 4527a4a30eSYoshinori Sato { 4638688fdbSEduardo Habkost RXCPU *cpu = RX_CPU(cs); 4727a4a30eSYoshinori Sato 488023d1abSAnton Johansson tcg_debug_assert(!(cs->tcg_cflags & CF_PCREL)); 498023d1abSAnton Johansson cpu->env.pc = tb->pc; 5027a4a30eSYoshinori Sato } 5127a4a30eSYoshinori Sato 525439d7a6SRichard Henderson static void rx_restore_state_to_opc(CPUState *cs, 535439d7a6SRichard Henderson const TranslationBlock *tb, 545439d7a6SRichard Henderson const uint64_t *data) 555439d7a6SRichard Henderson { 565439d7a6SRichard Henderson RXCPU *cpu = RX_CPU(cs); 575439d7a6SRichard Henderson 585439d7a6SRichard Henderson cpu->env.pc = data[0]; 595439d7a6SRichard Henderson } 605439d7a6SRichard Henderson 6127a4a30eSYoshinori Sato static bool rx_cpu_has_work(CPUState *cs) 6227a4a30eSYoshinori Sato { 6327a4a30eSYoshinori Sato return cs->interrupt_request & 6427a4a30eSYoshinori Sato (CPU_INTERRUPT_HARD | CPU_INTERRUPT_FIR); 6527a4a30eSYoshinori Sato } 6627a4a30eSYoshinori Sato 6788c41e40SPeter Maydell static void rx_cpu_reset_hold(Object *obj) 6827a4a30eSYoshinori Sato { 6988c41e40SPeter Maydell RXCPU *cpu = RX_CPU(obj); 7038688fdbSEduardo Habkost RXCPUClass *rcc = RX_CPU_GET_CLASS(cpu); 7127a4a30eSYoshinori Sato CPURXState *env = &cpu->env; 7227a4a30eSYoshinori Sato uint32_t *resetvec; 7327a4a30eSYoshinori Sato 7488c41e40SPeter Maydell if (rcc->parent_phases.hold) { 7588c41e40SPeter Maydell rcc->parent_phases.hold(obj); 7688c41e40SPeter Maydell } 7727a4a30eSYoshinori Sato 7827a4a30eSYoshinori Sato memset(env, 0, offsetof(CPURXState, end_reset_fields)); 7927a4a30eSYoshinori Sato 8027a4a30eSYoshinori Sato resetvec = rom_ptr(0xfffffffc, 4); 8127a4a30eSYoshinori Sato if (resetvec) { 8227a4a30eSYoshinori Sato /* In the case of kernel, it is ignored because it is not set. */ 8327a4a30eSYoshinori Sato env->pc = ldl_p(resetvec); 8427a4a30eSYoshinori Sato } 8527a4a30eSYoshinori Sato rx_cpu_unpack_psw(env, 0, 1); 8627a4a30eSYoshinori Sato env->regs[0] = env->isp = env->usp = 0; 8727a4a30eSYoshinori Sato env->fpsw = 0; 8827a4a30eSYoshinori Sato set_flush_to_zero(1, &env->fp_status); 8927a4a30eSYoshinori Sato set_flush_inputs_to_zero(1, &env->fp_status); 9027a4a30eSYoshinori Sato } 9127a4a30eSYoshinori Sato 9227a4a30eSYoshinori Sato static ObjectClass *rx_cpu_class_by_name(const char *cpu_model) 9327a4a30eSYoshinori Sato { 9427a4a30eSYoshinori Sato ObjectClass *oc; 9527a4a30eSYoshinori Sato char *typename; 9627a4a30eSYoshinori Sato 9727a4a30eSYoshinori Sato oc = object_class_by_name(cpu_model); 983a9d0d7bSPhilippe Mathieu-Daudé if (oc != NULL && object_class_dynamic_cast(oc, TYPE_RX_CPU) != NULL) { 9927a4a30eSYoshinori Sato return oc; 10027a4a30eSYoshinori Sato } 10127a4a30eSYoshinori Sato typename = g_strdup_printf(RX_CPU_TYPE_NAME("%s"), cpu_model); 10227a4a30eSYoshinori Sato oc = object_class_by_name(typename); 10327a4a30eSYoshinori Sato g_free(typename); 10427a4a30eSYoshinori Sato 10527a4a30eSYoshinori Sato return oc; 10627a4a30eSYoshinori Sato } 10727a4a30eSYoshinori Sato 10827a4a30eSYoshinori Sato static void rx_cpu_realize(DeviceState *dev, Error **errp) 10927a4a30eSYoshinori Sato { 11027a4a30eSYoshinori Sato CPUState *cs = CPU(dev); 11138688fdbSEduardo Habkost RXCPUClass *rcc = RX_CPU_GET_CLASS(dev); 11227a4a30eSYoshinori Sato Error *local_err = NULL; 11327a4a30eSYoshinori Sato 11427a4a30eSYoshinori Sato cpu_exec_realizefn(cs, &local_err); 11527a4a30eSYoshinori Sato if (local_err != NULL) { 11627a4a30eSYoshinori Sato error_propagate(errp, local_err); 11727a4a30eSYoshinori Sato return; 11827a4a30eSYoshinori Sato } 11927a4a30eSYoshinori Sato 12027a4a30eSYoshinori Sato qemu_init_vcpu(cs); 12127a4a30eSYoshinori Sato cpu_reset(cs); 12227a4a30eSYoshinori Sato 12327a4a30eSYoshinori Sato rcc->parent_realize(dev, errp); 12427a4a30eSYoshinori Sato } 12527a4a30eSYoshinori Sato 12627a4a30eSYoshinori Sato static void rx_cpu_set_irq(void *opaque, int no, int request) 12727a4a30eSYoshinori Sato { 12827a4a30eSYoshinori Sato RXCPU *cpu = opaque; 12927a4a30eSYoshinori Sato CPUState *cs = CPU(cpu); 13027a4a30eSYoshinori Sato int irq = request & 0xff; 13127a4a30eSYoshinori Sato 13227a4a30eSYoshinori Sato static const int mask[] = { 13327a4a30eSYoshinori Sato [RX_CPU_IRQ] = CPU_INTERRUPT_HARD, 13427a4a30eSYoshinori Sato [RX_CPU_FIR] = CPU_INTERRUPT_FIR, 13527a4a30eSYoshinori Sato }; 13627a4a30eSYoshinori Sato if (irq) { 13727a4a30eSYoshinori Sato cpu->env.req_irq = irq; 13827a4a30eSYoshinori Sato cpu->env.req_ipl = (request >> 8) & 0x0f; 13927a4a30eSYoshinori Sato cpu_interrupt(cs, mask[no]); 14027a4a30eSYoshinori Sato } else { 14127a4a30eSYoshinori Sato cpu_reset_interrupt(cs, mask[no]); 14227a4a30eSYoshinori Sato } 14327a4a30eSYoshinori Sato } 14427a4a30eSYoshinori Sato 14527a4a30eSYoshinori Sato static void rx_cpu_disas_set_info(CPUState *cpu, disassemble_info *info) 14627a4a30eSYoshinori Sato { 14727a4a30eSYoshinori Sato info->mach = bfd_mach_rx; 14827a4a30eSYoshinori Sato info->print_insn = print_insn_rx; 14927a4a30eSYoshinori Sato } 15027a4a30eSYoshinori Sato 15127a4a30eSYoshinori Sato static bool rx_cpu_tlb_fill(CPUState *cs, vaddr addr, int size, 15227a4a30eSYoshinori Sato MMUAccessType access_type, int mmu_idx, 15327a4a30eSYoshinori Sato bool probe, uintptr_t retaddr) 15427a4a30eSYoshinori Sato { 15527a4a30eSYoshinori Sato uint32_t address, physical, prot; 15627a4a30eSYoshinori Sato 15727a4a30eSYoshinori Sato /* Linear mapping */ 15827a4a30eSYoshinori Sato address = physical = addr & TARGET_PAGE_MASK; 15927a4a30eSYoshinori Sato prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 16027a4a30eSYoshinori Sato tlb_set_page(cs, address, physical, prot, mmu_idx, TARGET_PAGE_SIZE); 16127a4a30eSYoshinori Sato return true; 16227a4a30eSYoshinori Sato } 16327a4a30eSYoshinori Sato 16427a4a30eSYoshinori Sato static void rx_cpu_init(Object *obj) 16527a4a30eSYoshinori Sato { 16638688fdbSEduardo Habkost RXCPU *cpu = RX_CPU(obj); 16727a4a30eSYoshinori Sato 16827a4a30eSYoshinori Sato qdev_init_gpio_in(DEVICE(cpu), rx_cpu_set_irq, 2); 16927a4a30eSYoshinori Sato } 17027a4a30eSYoshinori Sato 1718b80bd28SPhilippe Mathieu-Daudé #ifndef CONFIG_USER_ONLY 1728b80bd28SPhilippe Mathieu-Daudé #include "hw/core/sysemu-cpu-ops.h" 1738b80bd28SPhilippe Mathieu-Daudé 1748b80bd28SPhilippe Mathieu-Daudé static const struct SysemuCPUOps rx_sysemu_ops = { 17508928c6dSPhilippe Mathieu-Daudé .get_phys_page_debug = rx_cpu_get_phys_page_debug, 1768b80bd28SPhilippe Mathieu-Daudé }; 1778b80bd28SPhilippe Mathieu-Daudé #endif 1788b80bd28SPhilippe Mathieu-Daudé 17978271684SClaudio Fontana #include "hw/core/tcg-cpu-ops.h" 18078271684SClaudio Fontana 181*1764ad70SRichard Henderson static const TCGCPUOps rx_tcg_ops = { 18278271684SClaudio Fontana .initialize = rx_translate_init, 18378271684SClaudio Fontana .synchronize_from_tb = rx_cpu_synchronize_from_tb, 1845439d7a6SRichard Henderson .restore_state_to_opc = rx_restore_state_to_opc, 18578271684SClaudio Fontana .tlb_fill = rx_cpu_tlb_fill, 18678271684SClaudio Fontana 18778271684SClaudio Fontana #ifndef CONFIG_USER_ONLY 18865c575b6SPhilippe Mathieu-Daudé .cpu_exec_interrupt = rx_cpu_exec_interrupt, 18978271684SClaudio Fontana .do_interrupt = rx_cpu_do_interrupt, 19078271684SClaudio Fontana #endif /* !CONFIG_USER_ONLY */ 19178271684SClaudio Fontana }; 19278271684SClaudio Fontana 19327a4a30eSYoshinori Sato static void rx_cpu_class_init(ObjectClass *klass, void *data) 19427a4a30eSYoshinori Sato { 19527a4a30eSYoshinori Sato DeviceClass *dc = DEVICE_CLASS(klass); 19627a4a30eSYoshinori Sato CPUClass *cc = CPU_CLASS(klass); 19738688fdbSEduardo Habkost RXCPUClass *rcc = RX_CPU_CLASS(klass); 19888c41e40SPeter Maydell ResettableClass *rc = RESETTABLE_CLASS(klass); 19927a4a30eSYoshinori Sato 20027a4a30eSYoshinori Sato device_class_set_parent_realize(dc, rx_cpu_realize, 20127a4a30eSYoshinori Sato &rcc->parent_realize); 20288c41e40SPeter Maydell resettable_class_set_parent_phases(rc, NULL, rx_cpu_reset_hold, NULL, 20388c41e40SPeter Maydell &rcc->parent_phases); 20427a4a30eSYoshinori Sato 20527a4a30eSYoshinori Sato cc->class_by_name = rx_cpu_class_by_name; 20627a4a30eSYoshinori Sato cc->has_work = rx_cpu_has_work; 20727a4a30eSYoshinori Sato cc->dump_state = rx_cpu_dump_state; 20827a4a30eSYoshinori Sato cc->set_pc = rx_cpu_set_pc; 209e4fdf9dfSRichard Henderson cc->get_pc = rx_cpu_get_pc; 21078271684SClaudio Fontana 2118b80bd28SPhilippe Mathieu-Daudé #ifndef CONFIG_USER_ONLY 2128b80bd28SPhilippe Mathieu-Daudé cc->sysemu_ops = &rx_sysemu_ops; 2138b80bd28SPhilippe Mathieu-Daudé #endif 21427a4a30eSYoshinori Sato cc->gdb_read_register = rx_cpu_gdb_read_register; 21527a4a30eSYoshinori Sato cc->gdb_write_register = rx_cpu_gdb_write_register; 21627a4a30eSYoshinori Sato cc->disas_set_info = rx_cpu_disas_set_info; 21727a4a30eSYoshinori Sato 21827a4a30eSYoshinori Sato cc->gdb_num_core_regs = 26; 21927a4a30eSYoshinori Sato cc->gdb_core_xml_file = "rx-core.xml"; 22078271684SClaudio Fontana cc->tcg_ops = &rx_tcg_ops; 22127a4a30eSYoshinori Sato } 22227a4a30eSYoshinori Sato 22327a4a30eSYoshinori Sato static const TypeInfo rx_cpu_info = { 22427a4a30eSYoshinori Sato .name = TYPE_RX_CPU, 22527a4a30eSYoshinori Sato .parent = TYPE_CPU, 22627a4a30eSYoshinori Sato .instance_size = sizeof(RXCPU), 227f669c992SRichard Henderson .instance_align = __alignof(RXCPU), 22827a4a30eSYoshinori Sato .instance_init = rx_cpu_init, 22927a4a30eSYoshinori Sato .abstract = true, 23027a4a30eSYoshinori Sato .class_size = sizeof(RXCPUClass), 23127a4a30eSYoshinori Sato .class_init = rx_cpu_class_init, 23227a4a30eSYoshinori Sato }; 23327a4a30eSYoshinori Sato 23427a4a30eSYoshinori Sato static const TypeInfo rx62n_rx_cpu_info = { 23527a4a30eSYoshinori Sato .name = TYPE_RX62N_CPU, 23627a4a30eSYoshinori Sato .parent = TYPE_RX_CPU, 23727a4a30eSYoshinori Sato }; 23827a4a30eSYoshinori Sato 23927a4a30eSYoshinori Sato static void rx_cpu_register_types(void) 24027a4a30eSYoshinori Sato { 24127a4a30eSYoshinori Sato type_register_static(&rx_cpu_info); 24227a4a30eSYoshinori Sato type_register_static(&rx62n_rx_cpu_info); 24327a4a30eSYoshinori Sato } 24427a4a30eSYoshinori Sato 24527a4a30eSYoshinori Sato type_init(rx_cpu_register_types) 246