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 void rx_cpu_list_entry(gpointer data, gpointer user_data) 9327a4a30eSYoshinori Sato { 9427a4a30eSYoshinori Sato ObjectClass *oc = data; 9527a4a30eSYoshinori Sato 9627a4a30eSYoshinori Sato qemu_printf(" %s\n", object_class_get_name(oc)); 9727a4a30eSYoshinori Sato } 9827a4a30eSYoshinori Sato 9927a4a30eSYoshinori Sato void rx_cpu_list(void) 10027a4a30eSYoshinori Sato { 10127a4a30eSYoshinori Sato GSList *list; 10227a4a30eSYoshinori Sato list = object_class_get_list_sorted(TYPE_RX_CPU, false); 10327a4a30eSYoshinori Sato qemu_printf("Available CPUs:\n"); 10427a4a30eSYoshinori Sato g_slist_foreach(list, rx_cpu_list_entry, NULL); 10527a4a30eSYoshinori Sato g_slist_free(list); 10627a4a30eSYoshinori Sato } 10727a4a30eSYoshinori Sato 10827a4a30eSYoshinori Sato static ObjectClass *rx_cpu_class_by_name(const char *cpu_model) 10927a4a30eSYoshinori Sato { 11027a4a30eSYoshinori Sato ObjectClass *oc; 11127a4a30eSYoshinori Sato char *typename; 11227a4a30eSYoshinori Sato 11327a4a30eSYoshinori Sato oc = object_class_by_name(cpu_model); 114*3a9d0d7bSPhilippe Mathieu-Daudé if (oc != NULL && object_class_dynamic_cast(oc, TYPE_RX_CPU) != NULL) { 11527a4a30eSYoshinori Sato return oc; 11627a4a30eSYoshinori Sato } 11727a4a30eSYoshinori Sato typename = g_strdup_printf(RX_CPU_TYPE_NAME("%s"), cpu_model); 11827a4a30eSYoshinori Sato oc = object_class_by_name(typename); 11927a4a30eSYoshinori Sato g_free(typename); 12027a4a30eSYoshinori Sato 12127a4a30eSYoshinori Sato return oc; 12227a4a30eSYoshinori Sato } 12327a4a30eSYoshinori Sato 12427a4a30eSYoshinori Sato static void rx_cpu_realize(DeviceState *dev, Error **errp) 12527a4a30eSYoshinori Sato { 12627a4a30eSYoshinori Sato CPUState *cs = CPU(dev); 12738688fdbSEduardo Habkost RXCPUClass *rcc = RX_CPU_GET_CLASS(dev); 12827a4a30eSYoshinori Sato Error *local_err = NULL; 12927a4a30eSYoshinori Sato 13027a4a30eSYoshinori Sato cpu_exec_realizefn(cs, &local_err); 13127a4a30eSYoshinori Sato if (local_err != NULL) { 13227a4a30eSYoshinori Sato error_propagate(errp, local_err); 13327a4a30eSYoshinori Sato return; 13427a4a30eSYoshinori Sato } 13527a4a30eSYoshinori Sato 13627a4a30eSYoshinori Sato qemu_init_vcpu(cs); 13727a4a30eSYoshinori Sato cpu_reset(cs); 13827a4a30eSYoshinori Sato 13927a4a30eSYoshinori Sato rcc->parent_realize(dev, errp); 14027a4a30eSYoshinori Sato } 14127a4a30eSYoshinori Sato 14227a4a30eSYoshinori Sato static void rx_cpu_set_irq(void *opaque, int no, int request) 14327a4a30eSYoshinori Sato { 14427a4a30eSYoshinori Sato RXCPU *cpu = opaque; 14527a4a30eSYoshinori Sato CPUState *cs = CPU(cpu); 14627a4a30eSYoshinori Sato int irq = request & 0xff; 14727a4a30eSYoshinori Sato 14827a4a30eSYoshinori Sato static const int mask[] = { 14927a4a30eSYoshinori Sato [RX_CPU_IRQ] = CPU_INTERRUPT_HARD, 15027a4a30eSYoshinori Sato [RX_CPU_FIR] = CPU_INTERRUPT_FIR, 15127a4a30eSYoshinori Sato }; 15227a4a30eSYoshinori Sato if (irq) { 15327a4a30eSYoshinori Sato cpu->env.req_irq = irq; 15427a4a30eSYoshinori Sato cpu->env.req_ipl = (request >> 8) & 0x0f; 15527a4a30eSYoshinori Sato cpu_interrupt(cs, mask[no]); 15627a4a30eSYoshinori Sato } else { 15727a4a30eSYoshinori Sato cpu_reset_interrupt(cs, mask[no]); 15827a4a30eSYoshinori Sato } 15927a4a30eSYoshinori Sato } 16027a4a30eSYoshinori Sato 16127a4a30eSYoshinori Sato static void rx_cpu_disas_set_info(CPUState *cpu, disassemble_info *info) 16227a4a30eSYoshinori Sato { 16327a4a30eSYoshinori Sato info->mach = bfd_mach_rx; 16427a4a30eSYoshinori Sato info->print_insn = print_insn_rx; 16527a4a30eSYoshinori Sato } 16627a4a30eSYoshinori Sato 16727a4a30eSYoshinori Sato static bool rx_cpu_tlb_fill(CPUState *cs, vaddr addr, int size, 16827a4a30eSYoshinori Sato MMUAccessType access_type, int mmu_idx, 16927a4a30eSYoshinori Sato bool probe, uintptr_t retaddr) 17027a4a30eSYoshinori Sato { 17127a4a30eSYoshinori Sato uint32_t address, physical, prot; 17227a4a30eSYoshinori Sato 17327a4a30eSYoshinori Sato /* Linear mapping */ 17427a4a30eSYoshinori Sato address = physical = addr & TARGET_PAGE_MASK; 17527a4a30eSYoshinori Sato prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 17627a4a30eSYoshinori Sato tlb_set_page(cs, address, physical, prot, mmu_idx, TARGET_PAGE_SIZE); 17727a4a30eSYoshinori Sato return true; 17827a4a30eSYoshinori Sato } 17927a4a30eSYoshinori Sato 18027a4a30eSYoshinori Sato static void rx_cpu_init(Object *obj) 18127a4a30eSYoshinori Sato { 18238688fdbSEduardo Habkost RXCPU *cpu = RX_CPU(obj); 18327a4a30eSYoshinori Sato 18427a4a30eSYoshinori Sato qdev_init_gpio_in(DEVICE(cpu), rx_cpu_set_irq, 2); 18527a4a30eSYoshinori Sato } 18627a4a30eSYoshinori Sato 1878b80bd28SPhilippe Mathieu-Daudé #ifndef CONFIG_USER_ONLY 1888b80bd28SPhilippe Mathieu-Daudé #include "hw/core/sysemu-cpu-ops.h" 1898b80bd28SPhilippe Mathieu-Daudé 1908b80bd28SPhilippe Mathieu-Daudé static const struct SysemuCPUOps rx_sysemu_ops = { 19108928c6dSPhilippe Mathieu-Daudé .get_phys_page_debug = rx_cpu_get_phys_page_debug, 1928b80bd28SPhilippe Mathieu-Daudé }; 1938b80bd28SPhilippe Mathieu-Daudé #endif 1948b80bd28SPhilippe Mathieu-Daudé 19578271684SClaudio Fontana #include "hw/core/tcg-cpu-ops.h" 19678271684SClaudio Fontana 19711906557SRichard Henderson static const struct TCGCPUOps rx_tcg_ops = { 19878271684SClaudio Fontana .initialize = rx_translate_init, 19978271684SClaudio Fontana .synchronize_from_tb = rx_cpu_synchronize_from_tb, 2005439d7a6SRichard Henderson .restore_state_to_opc = rx_restore_state_to_opc, 20178271684SClaudio Fontana .tlb_fill = rx_cpu_tlb_fill, 20278271684SClaudio Fontana 20378271684SClaudio Fontana #ifndef CONFIG_USER_ONLY 20465c575b6SPhilippe Mathieu-Daudé .cpu_exec_interrupt = rx_cpu_exec_interrupt, 20578271684SClaudio Fontana .do_interrupt = rx_cpu_do_interrupt, 20678271684SClaudio Fontana #endif /* !CONFIG_USER_ONLY */ 20778271684SClaudio Fontana }; 20878271684SClaudio Fontana 20927a4a30eSYoshinori Sato static void rx_cpu_class_init(ObjectClass *klass, void *data) 21027a4a30eSYoshinori Sato { 21127a4a30eSYoshinori Sato DeviceClass *dc = DEVICE_CLASS(klass); 21227a4a30eSYoshinori Sato CPUClass *cc = CPU_CLASS(klass); 21338688fdbSEduardo Habkost RXCPUClass *rcc = RX_CPU_CLASS(klass); 21488c41e40SPeter Maydell ResettableClass *rc = RESETTABLE_CLASS(klass); 21527a4a30eSYoshinori Sato 21627a4a30eSYoshinori Sato device_class_set_parent_realize(dc, rx_cpu_realize, 21727a4a30eSYoshinori Sato &rcc->parent_realize); 21888c41e40SPeter Maydell resettable_class_set_parent_phases(rc, NULL, rx_cpu_reset_hold, NULL, 21988c41e40SPeter Maydell &rcc->parent_phases); 22027a4a30eSYoshinori Sato 22127a4a30eSYoshinori Sato cc->class_by_name = rx_cpu_class_by_name; 22227a4a30eSYoshinori Sato cc->has_work = rx_cpu_has_work; 22327a4a30eSYoshinori Sato cc->dump_state = rx_cpu_dump_state; 22427a4a30eSYoshinori Sato cc->set_pc = rx_cpu_set_pc; 225e4fdf9dfSRichard Henderson cc->get_pc = rx_cpu_get_pc; 22678271684SClaudio Fontana 2278b80bd28SPhilippe Mathieu-Daudé #ifndef CONFIG_USER_ONLY 2288b80bd28SPhilippe Mathieu-Daudé cc->sysemu_ops = &rx_sysemu_ops; 2298b80bd28SPhilippe Mathieu-Daudé #endif 23027a4a30eSYoshinori Sato cc->gdb_read_register = rx_cpu_gdb_read_register; 23127a4a30eSYoshinori Sato cc->gdb_write_register = rx_cpu_gdb_write_register; 23227a4a30eSYoshinori Sato cc->disas_set_info = rx_cpu_disas_set_info; 23327a4a30eSYoshinori Sato 23427a4a30eSYoshinori Sato cc->gdb_num_core_regs = 26; 23527a4a30eSYoshinori Sato cc->gdb_core_xml_file = "rx-core.xml"; 23678271684SClaudio Fontana cc->tcg_ops = &rx_tcg_ops; 23727a4a30eSYoshinori Sato } 23827a4a30eSYoshinori Sato 23927a4a30eSYoshinori Sato static const TypeInfo rx_cpu_info = { 24027a4a30eSYoshinori Sato .name = TYPE_RX_CPU, 24127a4a30eSYoshinori Sato .parent = TYPE_CPU, 24227a4a30eSYoshinori Sato .instance_size = sizeof(RXCPU), 243f669c992SRichard Henderson .instance_align = __alignof(RXCPU), 24427a4a30eSYoshinori Sato .instance_init = rx_cpu_init, 24527a4a30eSYoshinori Sato .abstract = true, 24627a4a30eSYoshinori Sato .class_size = sizeof(RXCPUClass), 24727a4a30eSYoshinori Sato .class_init = rx_cpu_class_init, 24827a4a30eSYoshinori Sato }; 24927a4a30eSYoshinori Sato 25027a4a30eSYoshinori Sato static const TypeInfo rx62n_rx_cpu_info = { 25127a4a30eSYoshinori Sato .name = TYPE_RX62N_CPU, 25227a4a30eSYoshinori Sato .parent = TYPE_RX_CPU, 25327a4a30eSYoshinori Sato }; 25427a4a30eSYoshinori Sato 25527a4a30eSYoshinori Sato static void rx_cpu_register_types(void) 25627a4a30eSYoshinori Sato { 25727a4a30eSYoshinori Sato type_register_static(&rx_cpu_info); 25827a4a30eSYoshinori Sato type_register_static(&rx62n_rx_cpu_info); 25927a4a30eSYoshinori Sato } 26027a4a30eSYoshinori Sato 26127a4a30eSYoshinori Sato type_init(rx_cpu_register_types) 262