xref: /qemu/target/rx/cpu.c (revision 68df8c8dba57f539d24f1a92a8699a179d9bb6fb)
1 /*
2  * QEMU RX CPU
3  *
4  * Copyright (c) 2019 Yoshinori Sato
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2 or later, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "qemu/osdep.h"
20 #include "qemu/qemu-print.h"
21 #include "qapi/error.h"
22 #include "cpu.h"
23 #include "migration/vmstate.h"
24 #include "exec/exec-all.h"
25 #include "exec/page-protection.h"
26 #include "exec/translation-block.h"
27 #include "hw/loader.h"
28 #include "fpu/softfloat.h"
29 #include "tcg/debug-assert.h"
30 
31 static void rx_cpu_set_pc(CPUState *cs, vaddr value)
32 {
33     RXCPU *cpu = RX_CPU(cs);
34 
35     cpu->env.pc = value;
36 }
37 
38 static vaddr rx_cpu_get_pc(CPUState *cs)
39 {
40     RXCPU *cpu = RX_CPU(cs);
41 
42     return cpu->env.pc;
43 }
44 
45 static void rx_cpu_synchronize_from_tb(CPUState *cs,
46                                        const TranslationBlock *tb)
47 {
48     RXCPU *cpu = RX_CPU(cs);
49 
50     tcg_debug_assert(!tcg_cflags_has(cs, CF_PCREL));
51     cpu->env.pc = tb->pc;
52 }
53 
54 static void rx_restore_state_to_opc(CPUState *cs,
55                                     const TranslationBlock *tb,
56                                     const uint64_t *data)
57 {
58     RXCPU *cpu = RX_CPU(cs);
59 
60     cpu->env.pc = data[0];
61 }
62 
63 static bool rx_cpu_has_work(CPUState *cs)
64 {
65     return cs->interrupt_request &
66         (CPU_INTERRUPT_HARD | CPU_INTERRUPT_FIR);
67 }
68 
69 static int riscv_cpu_mmu_index(CPUState *cs, bool ifunc)
70 {
71     return 0;
72 }
73 
74 static void rx_cpu_reset_hold(Object *obj, ResetType type)
75 {
76     CPUState *cs = CPU(obj);
77     RXCPUClass *rcc = RX_CPU_GET_CLASS(obj);
78     CPURXState *env = cpu_env(cs);
79     uint32_t *resetvec;
80 
81     if (rcc->parent_phases.hold) {
82         rcc->parent_phases.hold(obj, type);
83     }
84 
85     memset(env, 0, offsetof(CPURXState, end_reset_fields));
86 
87     resetvec = rom_ptr(0xfffffffc, 4);
88     if (resetvec) {
89         /* In the case of kernel, it is ignored because it is not set. */
90         env->pc = ldl_p(resetvec);
91     }
92     rx_cpu_unpack_psw(env, 0, 1);
93     env->regs[0] = env->isp = env->usp = 0;
94     env->fpsw = 0;
95     set_flush_to_zero(1, &env->fp_status);
96     set_flush_inputs_to_zero(1, &env->fp_status);
97     /*
98      * TODO: this is not the correct NaN propagation rule for this
99      * architecture. The "RX Family User's Manual: Software" table 1.6
100      * defines the propagation rules as "prefer SNaN over QNaN;
101      * then prefer dest over source", which is float_2nan_prop_s_ab.
102      */
103     set_float_2nan_prop_rule(float_2nan_prop_x87, &env->fp_status);
104     /* Default NaN value: sign bit clear, set frac msb */
105     set_float_default_nan_pattern(0b01000000, &env->fp_status);
106 }
107 
108 static ObjectClass *rx_cpu_class_by_name(const char *cpu_model)
109 {
110     ObjectClass *oc;
111     char *typename;
112 
113     oc = object_class_by_name(cpu_model);
114     if (oc != NULL && object_class_dynamic_cast(oc, TYPE_RX_CPU) != NULL) {
115         return oc;
116     }
117     typename = g_strdup_printf(RX_CPU_TYPE_NAME("%s"), cpu_model);
118     oc = object_class_by_name(typename);
119     g_free(typename);
120 
121     return oc;
122 }
123 
124 static void rx_cpu_realize(DeviceState *dev, Error **errp)
125 {
126     CPUState *cs = CPU(dev);
127     RXCPUClass *rcc = RX_CPU_GET_CLASS(dev);
128     Error *local_err = NULL;
129 
130     cpu_exec_realizefn(cs, &local_err);
131     if (local_err != NULL) {
132         error_propagate(errp, local_err);
133         return;
134     }
135 
136     qemu_init_vcpu(cs);
137     cpu_reset(cs);
138 
139     rcc->parent_realize(dev, errp);
140 }
141 
142 static void rx_cpu_set_irq(void *opaque, int no, int request)
143 {
144     RXCPU *cpu = opaque;
145     CPUState *cs = CPU(cpu);
146     int irq = request & 0xff;
147 
148     static const int mask[] = {
149         [RX_CPU_IRQ] = CPU_INTERRUPT_HARD,
150         [RX_CPU_FIR] = CPU_INTERRUPT_FIR,
151     };
152     if (irq) {
153         cpu->env.req_irq = irq;
154         cpu->env.req_ipl = (request >> 8) & 0x0f;
155         cpu_interrupt(cs, mask[no]);
156     } else {
157         cpu_reset_interrupt(cs, mask[no]);
158     }
159 }
160 
161 static void rx_cpu_disas_set_info(CPUState *cpu, disassemble_info *info)
162 {
163     info->mach = bfd_mach_rx;
164     info->print_insn = print_insn_rx;
165 }
166 
167 static bool rx_cpu_tlb_fill(CPUState *cs, vaddr addr, int size,
168                             MMUAccessType access_type, int mmu_idx,
169                             bool probe, uintptr_t retaddr)
170 {
171     uint32_t address, physical, prot;
172 
173     /* Linear mapping */
174     address = physical = addr & TARGET_PAGE_MASK;
175     prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
176     tlb_set_page(cs, address, physical, prot, mmu_idx, TARGET_PAGE_SIZE);
177     return true;
178 }
179 
180 static void rx_cpu_init(Object *obj)
181 {
182     RXCPU *cpu = RX_CPU(obj);
183 
184     qdev_init_gpio_in(DEVICE(cpu), rx_cpu_set_irq, 2);
185 }
186 
187 #ifndef CONFIG_USER_ONLY
188 #include "hw/core/sysemu-cpu-ops.h"
189 
190 static const struct SysemuCPUOps rx_sysemu_ops = {
191     .get_phys_page_debug = rx_cpu_get_phys_page_debug,
192 };
193 #endif
194 
195 #include "hw/core/tcg-cpu-ops.h"
196 
197 static const TCGCPUOps rx_tcg_ops = {
198     .initialize = rx_translate_init,
199     .synchronize_from_tb = rx_cpu_synchronize_from_tb,
200     .restore_state_to_opc = rx_restore_state_to_opc,
201     .tlb_fill = rx_cpu_tlb_fill,
202 
203 #ifndef CONFIG_USER_ONLY
204     .cpu_exec_interrupt = rx_cpu_exec_interrupt,
205     .cpu_exec_halt = rx_cpu_has_work,
206     .do_interrupt = rx_cpu_do_interrupt,
207 #endif /* !CONFIG_USER_ONLY */
208 };
209 
210 static void rx_cpu_class_init(ObjectClass *klass, void *data)
211 {
212     DeviceClass *dc = DEVICE_CLASS(klass);
213     CPUClass *cc = CPU_CLASS(klass);
214     RXCPUClass *rcc = RX_CPU_CLASS(klass);
215     ResettableClass *rc = RESETTABLE_CLASS(klass);
216 
217     device_class_set_parent_realize(dc, rx_cpu_realize,
218                                     &rcc->parent_realize);
219     resettable_class_set_parent_phases(rc, NULL, rx_cpu_reset_hold, NULL,
220                                        &rcc->parent_phases);
221 
222     cc->class_by_name = rx_cpu_class_by_name;
223     cc->has_work = rx_cpu_has_work;
224     cc->mmu_index = riscv_cpu_mmu_index;
225     cc->dump_state = rx_cpu_dump_state;
226     cc->set_pc = rx_cpu_set_pc;
227     cc->get_pc = rx_cpu_get_pc;
228 
229 #ifndef CONFIG_USER_ONLY
230     cc->sysemu_ops = &rx_sysemu_ops;
231 #endif
232     cc->gdb_read_register = rx_cpu_gdb_read_register;
233     cc->gdb_write_register = rx_cpu_gdb_write_register;
234     cc->disas_set_info = rx_cpu_disas_set_info;
235 
236     cc->gdb_core_xml_file = "rx-core.xml";
237     cc->tcg_ops = &rx_tcg_ops;
238 }
239 
240 static const TypeInfo rx_cpu_info = {
241     .name = TYPE_RX_CPU,
242     .parent = TYPE_CPU,
243     .instance_size = sizeof(RXCPU),
244     .instance_align = __alignof(RXCPU),
245     .instance_init = rx_cpu_init,
246     .abstract = true,
247     .class_size = sizeof(RXCPUClass),
248     .class_init = rx_cpu_class_init,
249 };
250 
251 static const TypeInfo rx62n_rx_cpu_info = {
252     .name = TYPE_RX62N_CPU,
253     .parent = TYPE_RX_CPU,
254 };
255 
256 static void rx_cpu_register_types(void)
257 {
258     type_register_static(&rx_cpu_info);
259     type_register_static(&rx62n_rx_cpu_info);
260 }
261 
262 type_init(rx_cpu_register_types)
263