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 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 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 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 49*b254c342SPhilippe Mathieu-Daudé tcg_debug_assert(!tcg_cflags_has(cs, CF_PCREL)); 508023d1abSAnton Johansson cpu->env.pc = tb->pc; 5127a4a30eSYoshinori Sato } 5227a4a30eSYoshinori Sato 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 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 68ef5cc166SRichard Henderson static int riscv_cpu_mmu_index(CPUState *cs, bool ifunc) 69ef5cc166SRichard Henderson { 70ef5cc166SRichard Henderson return 0; 71ef5cc166SRichard Henderson } 72ef5cc166SRichard Henderson 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); 9627a4a30eSYoshinori Sato } 9727a4a30eSYoshinori Sato 9827a4a30eSYoshinori Sato static ObjectClass *rx_cpu_class_by_name(const char *cpu_model) 9927a4a30eSYoshinori Sato { 10027a4a30eSYoshinori Sato ObjectClass *oc; 10127a4a30eSYoshinori Sato char *typename; 10227a4a30eSYoshinori Sato 10327a4a30eSYoshinori Sato oc = object_class_by_name(cpu_model); 1043a9d0d7bSPhilippe Mathieu-Daudé if (oc != NULL && object_class_dynamic_cast(oc, TYPE_RX_CPU) != NULL) { 10527a4a30eSYoshinori Sato return oc; 10627a4a30eSYoshinori Sato } 10727a4a30eSYoshinori Sato typename = g_strdup_printf(RX_CPU_TYPE_NAME("%s"), cpu_model); 10827a4a30eSYoshinori Sato oc = object_class_by_name(typename); 10927a4a30eSYoshinori Sato g_free(typename); 11027a4a30eSYoshinori Sato 11127a4a30eSYoshinori Sato return oc; 11227a4a30eSYoshinori Sato } 11327a4a30eSYoshinori Sato 11427a4a30eSYoshinori Sato static void rx_cpu_realize(DeviceState *dev, Error **errp) 11527a4a30eSYoshinori Sato { 11627a4a30eSYoshinori Sato CPUState *cs = CPU(dev); 11738688fdbSEduardo Habkost RXCPUClass *rcc = RX_CPU_GET_CLASS(dev); 11827a4a30eSYoshinori Sato Error *local_err = NULL; 11927a4a30eSYoshinori Sato 12027a4a30eSYoshinori Sato cpu_exec_realizefn(cs, &local_err); 12127a4a30eSYoshinori Sato if (local_err != NULL) { 12227a4a30eSYoshinori Sato error_propagate(errp, local_err); 12327a4a30eSYoshinori Sato return; 12427a4a30eSYoshinori Sato } 12527a4a30eSYoshinori Sato 12627a4a30eSYoshinori Sato qemu_init_vcpu(cs); 12727a4a30eSYoshinori Sato cpu_reset(cs); 12827a4a30eSYoshinori Sato 12927a4a30eSYoshinori Sato rcc->parent_realize(dev, errp); 13027a4a30eSYoshinori Sato } 13127a4a30eSYoshinori Sato 13227a4a30eSYoshinori Sato static void rx_cpu_set_irq(void *opaque, int no, int request) 13327a4a30eSYoshinori Sato { 13427a4a30eSYoshinori Sato RXCPU *cpu = opaque; 13527a4a30eSYoshinori Sato CPUState *cs = CPU(cpu); 13627a4a30eSYoshinori Sato int irq = request & 0xff; 13727a4a30eSYoshinori Sato 13827a4a30eSYoshinori Sato static const int mask[] = { 13927a4a30eSYoshinori Sato [RX_CPU_IRQ] = CPU_INTERRUPT_HARD, 14027a4a30eSYoshinori Sato [RX_CPU_FIR] = CPU_INTERRUPT_FIR, 14127a4a30eSYoshinori Sato }; 14227a4a30eSYoshinori Sato if (irq) { 14327a4a30eSYoshinori Sato cpu->env.req_irq = irq; 14427a4a30eSYoshinori Sato cpu->env.req_ipl = (request >> 8) & 0x0f; 14527a4a30eSYoshinori Sato cpu_interrupt(cs, mask[no]); 14627a4a30eSYoshinori Sato } else { 14727a4a30eSYoshinori Sato cpu_reset_interrupt(cs, mask[no]); 14827a4a30eSYoshinori Sato } 14927a4a30eSYoshinori Sato } 15027a4a30eSYoshinori Sato 15127a4a30eSYoshinori Sato static void rx_cpu_disas_set_info(CPUState *cpu, disassemble_info *info) 15227a4a30eSYoshinori Sato { 15327a4a30eSYoshinori Sato info->mach = bfd_mach_rx; 15427a4a30eSYoshinori Sato info->print_insn = print_insn_rx; 15527a4a30eSYoshinori Sato } 15627a4a30eSYoshinori Sato 15727a4a30eSYoshinori Sato static bool rx_cpu_tlb_fill(CPUState *cs, vaddr addr, int size, 15827a4a30eSYoshinori Sato MMUAccessType access_type, int mmu_idx, 15927a4a30eSYoshinori Sato bool probe, uintptr_t retaddr) 16027a4a30eSYoshinori Sato { 16127a4a30eSYoshinori Sato uint32_t address, physical, prot; 16227a4a30eSYoshinori Sato 16327a4a30eSYoshinori Sato /* Linear mapping */ 16427a4a30eSYoshinori Sato address = physical = addr & TARGET_PAGE_MASK; 16527a4a30eSYoshinori Sato prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 16627a4a30eSYoshinori Sato tlb_set_page(cs, address, physical, prot, mmu_idx, TARGET_PAGE_SIZE); 16727a4a30eSYoshinori Sato return true; 16827a4a30eSYoshinori Sato } 16927a4a30eSYoshinori Sato 17027a4a30eSYoshinori Sato static void rx_cpu_init(Object *obj) 17127a4a30eSYoshinori Sato { 17238688fdbSEduardo Habkost RXCPU *cpu = RX_CPU(obj); 17327a4a30eSYoshinori Sato 17427a4a30eSYoshinori Sato qdev_init_gpio_in(DEVICE(cpu), rx_cpu_set_irq, 2); 17527a4a30eSYoshinori Sato } 17627a4a30eSYoshinori Sato 1778b80bd28SPhilippe Mathieu-Daudé #ifndef CONFIG_USER_ONLY 1788b80bd28SPhilippe Mathieu-Daudé #include "hw/core/sysemu-cpu-ops.h" 1798b80bd28SPhilippe Mathieu-Daudé 1808b80bd28SPhilippe Mathieu-Daudé static const struct SysemuCPUOps rx_sysemu_ops = { 18108928c6dSPhilippe Mathieu-Daudé .get_phys_page_debug = rx_cpu_get_phys_page_debug, 1828b80bd28SPhilippe Mathieu-Daudé }; 1838b80bd28SPhilippe Mathieu-Daudé #endif 1848b80bd28SPhilippe Mathieu-Daudé 18578271684SClaudio Fontana #include "hw/core/tcg-cpu-ops.h" 18678271684SClaudio Fontana 1871764ad70SRichard Henderson static const TCGCPUOps rx_tcg_ops = { 18878271684SClaudio Fontana .initialize = rx_translate_init, 18978271684SClaudio Fontana .synchronize_from_tb = rx_cpu_synchronize_from_tb, 1905439d7a6SRichard Henderson .restore_state_to_opc = rx_restore_state_to_opc, 19178271684SClaudio Fontana .tlb_fill = rx_cpu_tlb_fill, 19278271684SClaudio Fontana 19378271684SClaudio Fontana #ifndef CONFIG_USER_ONLY 19465c575b6SPhilippe Mathieu-Daudé .cpu_exec_interrupt = rx_cpu_exec_interrupt, 19578271684SClaudio Fontana .do_interrupt = rx_cpu_do_interrupt, 19678271684SClaudio Fontana #endif /* !CONFIG_USER_ONLY */ 19778271684SClaudio Fontana }; 19878271684SClaudio Fontana 19927a4a30eSYoshinori Sato static void rx_cpu_class_init(ObjectClass *klass, void *data) 20027a4a30eSYoshinori Sato { 20127a4a30eSYoshinori Sato DeviceClass *dc = DEVICE_CLASS(klass); 20227a4a30eSYoshinori Sato CPUClass *cc = CPU_CLASS(klass); 20338688fdbSEduardo Habkost RXCPUClass *rcc = RX_CPU_CLASS(klass); 20488c41e40SPeter Maydell ResettableClass *rc = RESETTABLE_CLASS(klass); 20527a4a30eSYoshinori Sato 20627a4a30eSYoshinori Sato device_class_set_parent_realize(dc, rx_cpu_realize, 20727a4a30eSYoshinori Sato &rcc->parent_realize); 20888c41e40SPeter Maydell resettable_class_set_parent_phases(rc, NULL, rx_cpu_reset_hold, NULL, 20988c41e40SPeter Maydell &rcc->parent_phases); 21027a4a30eSYoshinori Sato 21127a4a30eSYoshinori Sato cc->class_by_name = rx_cpu_class_by_name; 21227a4a30eSYoshinori Sato cc->has_work = rx_cpu_has_work; 213ef5cc166SRichard Henderson cc->mmu_index = riscv_cpu_mmu_index; 21427a4a30eSYoshinori Sato cc->dump_state = rx_cpu_dump_state; 21527a4a30eSYoshinori Sato cc->set_pc = rx_cpu_set_pc; 216e4fdf9dfSRichard Henderson cc->get_pc = rx_cpu_get_pc; 21778271684SClaudio Fontana 2188b80bd28SPhilippe Mathieu-Daudé #ifndef CONFIG_USER_ONLY 2198b80bd28SPhilippe Mathieu-Daudé cc->sysemu_ops = &rx_sysemu_ops; 2208b80bd28SPhilippe Mathieu-Daudé #endif 22127a4a30eSYoshinori Sato cc->gdb_read_register = rx_cpu_gdb_read_register; 22227a4a30eSYoshinori Sato cc->gdb_write_register = rx_cpu_gdb_write_register; 22327a4a30eSYoshinori Sato cc->disas_set_info = rx_cpu_disas_set_info; 22427a4a30eSYoshinori Sato 22527a4a30eSYoshinori Sato cc->gdb_core_xml_file = "rx-core.xml"; 22678271684SClaudio Fontana cc->tcg_ops = &rx_tcg_ops; 22727a4a30eSYoshinori Sato } 22827a4a30eSYoshinori Sato 22927a4a30eSYoshinori Sato static const TypeInfo rx_cpu_info = { 23027a4a30eSYoshinori Sato .name = TYPE_RX_CPU, 23127a4a30eSYoshinori Sato .parent = TYPE_CPU, 23227a4a30eSYoshinori Sato .instance_size = sizeof(RXCPU), 233f669c992SRichard Henderson .instance_align = __alignof(RXCPU), 23427a4a30eSYoshinori Sato .instance_init = rx_cpu_init, 23527a4a30eSYoshinori Sato .abstract = true, 23627a4a30eSYoshinori Sato .class_size = sizeof(RXCPUClass), 23727a4a30eSYoshinori Sato .class_init = rx_cpu_class_init, 23827a4a30eSYoshinori Sato }; 23927a4a30eSYoshinori Sato 24027a4a30eSYoshinori Sato static const TypeInfo rx62n_rx_cpu_info = { 24127a4a30eSYoshinori Sato .name = TYPE_RX62N_CPU, 24227a4a30eSYoshinori Sato .parent = TYPE_RX_CPU, 24327a4a30eSYoshinori Sato }; 24427a4a30eSYoshinori Sato 24527a4a30eSYoshinori Sato static void rx_cpu_register_types(void) 24627a4a30eSYoshinori Sato { 24727a4a30eSYoshinori Sato type_register_static(&rx_cpu_info); 24827a4a30eSYoshinori Sato type_register_static(&rx62n_rx_cpu_info); 24927a4a30eSYoshinori Sato } 25027a4a30eSYoshinori Sato 25127a4a30eSYoshinori Sato type_init(rx_cpu_register_types) 252