xref: /openbmc/qemu/target/tricore/cpu.c (revision a9f2ffa0f5c7bdb43981015573b375f070b70287)
1fcf5ef2aSThomas Huth /*
2fcf5ef2aSThomas Huth  *  TriCore emulation for qemu: main translation routines.
3fcf5ef2aSThomas Huth  *
4fcf5ef2aSThomas Huth  *  Copyright (c) 2012-2014 Bastian Koppelmann C-Lab/University Paderborn
5fcf5ef2aSThomas Huth  *
6fcf5ef2aSThomas Huth  * This library is free software; you can redistribute it and/or
7fcf5ef2aSThomas Huth  * modify it under the terms of the GNU Lesser General Public
8fcf5ef2aSThomas Huth  * License as published by the Free Software Foundation; either
902754acdSThomas Huth  * version 2.1 of the License, or (at your option) any later version.
10fcf5ef2aSThomas Huth  *
11fcf5ef2aSThomas Huth  * This library is distributed in the hope that it will be useful,
12fcf5ef2aSThomas Huth  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13fcf5ef2aSThomas Huth  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14fcf5ef2aSThomas Huth  * Lesser General Public License for more details.
15fcf5ef2aSThomas Huth  *
16fcf5ef2aSThomas Huth  * You should have received a copy of the GNU Lesser General Public
17fcf5ef2aSThomas Huth  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18fcf5ef2aSThomas Huth  */
19fcf5ef2aSThomas Huth 
20fcf5ef2aSThomas Huth #include "qemu/osdep.h"
21fcf5ef2aSThomas Huth #include "qapi/error.h"
22fcf5ef2aSThomas Huth #include "cpu.h"
23fcf5ef2aSThomas Huth #include "exec/exec-all.h"
24b190f477SEduardo Otubo #include "qemu/error-report.h"
25fafe0021SRichard Henderson #include "tcg/debug-assert.h"
26b190f477SEduardo Otubo 
set_feature(CPUTriCoreState * env,int feature)27fcf5ef2aSThomas Huth static inline void set_feature(CPUTriCoreState *env, int feature)
28fcf5ef2aSThomas Huth {
29fcf5ef2aSThomas Huth     env->features |= 1ULL << feature;
30fcf5ef2aSThomas Huth }
31fcf5ef2aSThomas Huth 
tricore_gdb_arch_name(CPUState * cs)32a6506838SAkihiko Odaki static const gchar *tricore_gdb_arch_name(CPUState *cs)
33d127de3bSBastian Koppelmann {
34a6506838SAkihiko Odaki     return "tricore";
35d127de3bSBastian Koppelmann }
36d127de3bSBastian Koppelmann 
tricore_cpu_set_pc(CPUState * cs,vaddr value)37fcf5ef2aSThomas Huth static void tricore_cpu_set_pc(CPUState *cs, vaddr value)
38fcf5ef2aSThomas Huth {
3939ac0bacSPhilippe Mathieu-Daudé     cpu_env(cs)->PC = value & ~(target_ulong)1;
40fcf5ef2aSThomas Huth }
41fcf5ef2aSThomas Huth 
tricore_cpu_get_pc(CPUState * cs)42e4fdf9dfSRichard Henderson static vaddr tricore_cpu_get_pc(CPUState *cs)
43e4fdf9dfSRichard Henderson {
4439ac0bacSPhilippe Mathieu-Daudé     return cpu_env(cs)->PC;
45e4fdf9dfSRichard Henderson }
46e4fdf9dfSRichard Henderson 
tricore_cpu_synchronize_from_tb(CPUState * cs,const TranslationBlock * tb)47fcf5ef2aSThomas Huth static void tricore_cpu_synchronize_from_tb(CPUState *cs,
4804a37d4cSRichard Henderson                                             const TranslationBlock *tb)
49fcf5ef2aSThomas Huth {
50b254c342SPhilippe Mathieu-Daudé     tcg_debug_assert(!tcg_cflags_has(cs, CF_PCREL));
5139ac0bacSPhilippe Mathieu-Daudé     cpu_env(cs)->PC = tb->pc;
52fcf5ef2aSThomas Huth }
53fcf5ef2aSThomas Huth 
tricore_restore_state_to_opc(CPUState * cs,const TranslationBlock * tb,const uint64_t * data)54b765e427SRichard Henderson static void tricore_restore_state_to_opc(CPUState *cs,
55b765e427SRichard Henderson                                          const TranslationBlock *tb,
56b765e427SRichard Henderson                                          const uint64_t *data)
57b765e427SRichard Henderson {
5839ac0bacSPhilippe Mathieu-Daudé     cpu_env(cs)->PC = data[0];
59b765e427SRichard Henderson }
60b765e427SRichard Henderson 
tricore_cpu_reset_hold(Object * obj,ResetType type)61ad80e367SPeter Maydell static void tricore_cpu_reset_hold(Object *obj, ResetType type)
62fcf5ef2aSThomas Huth {
63348802b5SPhilippe Mathieu-Daudé     CPUState *cs = CPU(obj);
64348802b5SPhilippe Mathieu-Daudé     TriCoreCPUClass *tcc = TRICORE_CPU_GET_CLASS(obj);
65fcf5ef2aSThomas Huth 
66efcc1068SPeter Maydell     if (tcc->parent_phases.hold) {
67ad80e367SPeter Maydell         tcc->parent_phases.hold(obj, type);
68efcc1068SPeter Maydell     }
69fcf5ef2aSThomas Huth 
7039ac0bacSPhilippe Mathieu-Daudé     cpu_state_reset(cpu_env(cs));
71fcf5ef2aSThomas Huth }
72fcf5ef2aSThomas Huth 
tricore_cpu_has_work(CPUState * cs)73fcf5ef2aSThomas Huth static bool tricore_cpu_has_work(CPUState *cs)
74fcf5ef2aSThomas Huth {
75fcf5ef2aSThomas Huth     return true;
76fcf5ef2aSThomas Huth }
77fcf5ef2aSThomas Huth 
tricore_cpu_mmu_index(CPUState * cs,bool ifetch)78eafa0f68SRichard Henderson static int tricore_cpu_mmu_index(CPUState *cs, bool ifetch)
79eafa0f68SRichard Henderson {
80eafa0f68SRichard Henderson     return 0;
81eafa0f68SRichard Henderson }
82eafa0f68SRichard Henderson 
tricore_cpu_realizefn(DeviceState * dev,Error ** errp)83fcf5ef2aSThomas Huth static void tricore_cpu_realizefn(DeviceState *dev, Error **errp)
84fcf5ef2aSThomas Huth {
85fcf5ef2aSThomas Huth     CPUState *cs = CPU(dev);
86fcf5ef2aSThomas Huth     TriCoreCPU *cpu = TRICORE_CPU(dev);
87fcf5ef2aSThomas Huth     TriCoreCPUClass *tcc = TRICORE_CPU_GET_CLASS(dev);
88fcf5ef2aSThomas Huth     CPUTriCoreState *env = &cpu->env;
89fcf5ef2aSThomas Huth     Error *local_err = NULL;
90fcf5ef2aSThomas Huth 
91fcf5ef2aSThomas Huth     cpu_exec_realizefn(cs, &local_err);
92fcf5ef2aSThomas Huth     if (local_err != NULL) {
93fcf5ef2aSThomas Huth         error_propagate(errp, local_err);
94fcf5ef2aSThomas Huth         return;
95fcf5ef2aSThomas Huth     }
96fcf5ef2aSThomas Huth 
97fcf5ef2aSThomas Huth     /* Some features automatically imply others */
98f8cfdd20SBastian Koppelmann     if (tricore_has_feature(env, TRICORE_FEATURE_162)) {
994d2b2e76SBastian Koppelmann         set_feature(env, TRICORE_FEATURE_161);
1004d2b2e76SBastian Koppelmann     }
1014d2b2e76SBastian Koppelmann 
102f8cfdd20SBastian Koppelmann     if (tricore_has_feature(env, TRICORE_FEATURE_161)) {
103fcf5ef2aSThomas Huth         set_feature(env, TRICORE_FEATURE_16);
104fcf5ef2aSThomas Huth     }
105fcf5ef2aSThomas Huth 
106f8cfdd20SBastian Koppelmann     if (tricore_has_feature(env, TRICORE_FEATURE_16)) {
107fcf5ef2aSThomas Huth         set_feature(env, TRICORE_FEATURE_131);
108fcf5ef2aSThomas Huth     }
109f8cfdd20SBastian Koppelmann     if (tricore_has_feature(env, TRICORE_FEATURE_131)) {
110fcf5ef2aSThomas Huth         set_feature(env, TRICORE_FEATURE_13);
111fcf5ef2aSThomas Huth     }
112fcf5ef2aSThomas Huth     cpu_reset(cs);
113fcf5ef2aSThomas Huth     qemu_init_vcpu(cs);
114fcf5ef2aSThomas Huth 
115fcf5ef2aSThomas Huth     tcc->parent_realize(dev, errp);
116fcf5ef2aSThomas Huth }
117fcf5ef2aSThomas Huth 
tricore_cpu_class_by_name(const char * cpu_model)118fcf5ef2aSThomas Huth static ObjectClass *tricore_cpu_class_by_name(const char *cpu_model)
119fcf5ef2aSThomas Huth {
120fcf5ef2aSThomas Huth     ObjectClass *oc;
121fcf5ef2aSThomas Huth     char *typename;
122fcf5ef2aSThomas Huth 
123b9ad9d5bSIgor Mammedov     typename = g_strdup_printf(TRICORE_CPU_TYPE_NAME("%s"), cpu_model);
124fcf5ef2aSThomas Huth     oc = object_class_by_name(typename);
125fcf5ef2aSThomas Huth     g_free(typename);
126d5be19f5SPhilippe Mathieu-Daudé 
127fcf5ef2aSThomas Huth     return oc;
128fcf5ef2aSThomas Huth }
129fcf5ef2aSThomas Huth 
tc1796_initfn(Object * obj)130fcf5ef2aSThomas Huth static void tc1796_initfn(Object *obj)
131fcf5ef2aSThomas Huth {
132fcf5ef2aSThomas Huth     TriCoreCPU *cpu = TRICORE_CPU(obj);
133fcf5ef2aSThomas Huth 
134fcf5ef2aSThomas Huth     set_feature(&cpu->env, TRICORE_FEATURE_13);
135fcf5ef2aSThomas Huth }
136fcf5ef2aSThomas Huth 
tc1797_initfn(Object * obj)137fcf5ef2aSThomas Huth static void tc1797_initfn(Object *obj)
138fcf5ef2aSThomas Huth {
139fcf5ef2aSThomas Huth     TriCoreCPU *cpu = TRICORE_CPU(obj);
140fcf5ef2aSThomas Huth 
141fcf5ef2aSThomas Huth     set_feature(&cpu->env, TRICORE_FEATURE_131);
142fcf5ef2aSThomas Huth }
143fcf5ef2aSThomas Huth 
tc27x_initfn(Object * obj)144fcf5ef2aSThomas Huth static void tc27x_initfn(Object *obj)
145fcf5ef2aSThomas Huth {
146fcf5ef2aSThomas Huth     TriCoreCPU *cpu = TRICORE_CPU(obj);
147fcf5ef2aSThomas Huth 
148fcf5ef2aSThomas Huth     set_feature(&cpu->env, TRICORE_FEATURE_161);
149fcf5ef2aSThomas Huth }
150fcf5ef2aSThomas Huth 
tc37x_initfn(Object * obj)1514d2b2e76SBastian Koppelmann static void tc37x_initfn(Object *obj)
1524d2b2e76SBastian Koppelmann {
1534d2b2e76SBastian Koppelmann     TriCoreCPU *cpu = TRICORE_CPU(obj);
1544d2b2e76SBastian Koppelmann 
1554d2b2e76SBastian Koppelmann     set_feature(&cpu->env, TRICORE_FEATURE_162);
1564d2b2e76SBastian Koppelmann }
1574d2b2e76SBastian Koppelmann 
tricore_cpu_exec_interrupt(CPUState * cs,int interrupt_request)158*de680286SPeter Maydell static bool tricore_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
159*de680286SPeter Maydell {
160*de680286SPeter Maydell     /* Interrupts are not implemented */
161*de680286SPeter Maydell     return false;
162*de680286SPeter Maydell }
1634d2b2e76SBastian Koppelmann 
1648b80bd28SPhilippe Mathieu-Daudé #include "hw/core/sysemu-cpu-ops.h"
1658b80bd28SPhilippe Mathieu-Daudé 
1668b80bd28SPhilippe Mathieu-Daudé static const struct SysemuCPUOps tricore_sysemu_ops = {
16708928c6dSPhilippe Mathieu-Daudé     .get_phys_page_debug = tricore_cpu_get_phys_page_debug,
1688b80bd28SPhilippe Mathieu-Daudé };
1698b80bd28SPhilippe Mathieu-Daudé 
17078271684SClaudio Fontana #include "hw/core/tcg-cpu-ops.h"
17178271684SClaudio Fontana 
1721764ad70SRichard Henderson static const TCGCPUOps tricore_tcg_ops = {
17378271684SClaudio Fontana     .initialize = tricore_tcg_init,
17478271684SClaudio Fontana     .synchronize_from_tb = tricore_cpu_synchronize_from_tb,
175b765e427SRichard Henderson     .restore_state_to_opc = tricore_restore_state_to_opc,
17678271684SClaudio Fontana     .tlb_fill = tricore_cpu_tlb_fill,
177*de680286SPeter Maydell     .cpu_exec_interrupt = tricore_cpu_exec_interrupt,
1784f7b1ecbSPeter Maydell     .cpu_exec_halt = tricore_cpu_has_work,
17978271684SClaudio Fontana };
18078271684SClaudio Fontana 
tricore_cpu_class_init(ObjectClass * c,void * data)181fcf5ef2aSThomas Huth static void tricore_cpu_class_init(ObjectClass *c, void *data)
182fcf5ef2aSThomas Huth {
183fcf5ef2aSThomas Huth     TriCoreCPUClass *mcc = TRICORE_CPU_CLASS(c);
184fcf5ef2aSThomas Huth     CPUClass *cc = CPU_CLASS(c);
185fcf5ef2aSThomas Huth     DeviceClass *dc = DEVICE_CLASS(c);
186efcc1068SPeter Maydell     ResettableClass *rc = RESETTABLE_CLASS(c);
187fcf5ef2aSThomas Huth 
188bf853881SPhilippe Mathieu-Daudé     device_class_set_parent_realize(dc, tricore_cpu_realizefn,
189bf853881SPhilippe Mathieu-Daudé                                     &mcc->parent_realize);
190fcf5ef2aSThomas Huth 
191efcc1068SPeter Maydell     resettable_class_set_parent_phases(rc, NULL, tricore_cpu_reset_hold, NULL,
192efcc1068SPeter Maydell                                        &mcc->parent_phases);
193fcf5ef2aSThomas Huth     cc->class_by_name = tricore_cpu_class_by_name;
194fcf5ef2aSThomas Huth     cc->has_work = tricore_cpu_has_work;
195eafa0f68SRichard Henderson     cc->mmu_index = tricore_cpu_mmu_index;
196fcf5ef2aSThomas Huth 
197d127de3bSBastian Koppelmann     cc->gdb_read_register = tricore_cpu_gdb_read_register;
198d127de3bSBastian Koppelmann     cc->gdb_write_register = tricore_cpu_gdb_write_register;
199d127de3bSBastian Koppelmann     cc->gdb_num_core_regs = 44;
200d127de3bSBastian Koppelmann     cc->gdb_arch_name = tricore_gdb_arch_name;
201d127de3bSBastian Koppelmann 
202fcf5ef2aSThomas Huth     cc->dump_state = tricore_cpu_dump_state;
203fcf5ef2aSThomas Huth     cc->set_pc = tricore_cpu_set_pc;
204e4fdf9dfSRichard Henderson     cc->get_pc = tricore_cpu_get_pc;
2058b80bd28SPhilippe Mathieu-Daudé     cc->sysemu_ops = &tricore_sysemu_ops;
20678271684SClaudio Fontana     cc->tcg_ops = &tricore_tcg_ops;
207fcf5ef2aSThomas Huth }
208fcf5ef2aSThomas Huth 
209b9ad9d5bSIgor Mammedov #define DEFINE_TRICORE_CPU_TYPE(cpu_model, initfn) \
210b9ad9d5bSIgor Mammedov     {                                              \
211b9ad9d5bSIgor Mammedov         .parent = TYPE_TRICORE_CPU,                \
212b9ad9d5bSIgor Mammedov         .instance_init = initfn,                   \
213b9ad9d5bSIgor Mammedov         .name = TRICORE_CPU_TYPE_NAME(cpu_model),  \
214fcf5ef2aSThomas Huth     }
215fcf5ef2aSThomas Huth 
216b9ad9d5bSIgor Mammedov static const TypeInfo tricore_cpu_type_infos[] = {
217b9ad9d5bSIgor Mammedov     {
218fcf5ef2aSThomas Huth         .name = TYPE_TRICORE_CPU,
219fcf5ef2aSThomas Huth         .parent = TYPE_CPU,
220fcf5ef2aSThomas Huth         .instance_size = sizeof(TriCoreCPU),
221f669c992SRichard Henderson         .instance_align = __alignof(TriCoreCPU),
222fcf5ef2aSThomas Huth         .abstract = true,
223fcf5ef2aSThomas Huth         .class_size = sizeof(TriCoreCPUClass),
224fcf5ef2aSThomas Huth         .class_init = tricore_cpu_class_init,
225b9ad9d5bSIgor Mammedov     },
226b9ad9d5bSIgor Mammedov     DEFINE_TRICORE_CPU_TYPE("tc1796", tc1796_initfn),
227b9ad9d5bSIgor Mammedov     DEFINE_TRICORE_CPU_TYPE("tc1797", tc1797_initfn),
228b9ad9d5bSIgor Mammedov     DEFINE_TRICORE_CPU_TYPE("tc27x", tc27x_initfn),
2294d2b2e76SBastian Koppelmann     DEFINE_TRICORE_CPU_TYPE("tc37x", tc37x_initfn),
230fcf5ef2aSThomas Huth };
231fcf5ef2aSThomas Huth 
232b9ad9d5bSIgor Mammedov DEFINE_TYPES(tricore_cpu_type_infos)
233