cpu: Factor out cpu_generic_init()
[qemu.git] / target-moxie / cpu.c
1 /*
2 * QEMU Moxie CPU
3 *
4 * Copyright (c) 2013 Anthony Green
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "cpu.h"
21 #include "qemu-common.h"
22 #include "migration/vmstate.h"
23 #include "machine.h"
24
25 static void moxie_cpu_set_pc(CPUState *cs, vaddr value)
26 {
27 MoxieCPU *cpu = MOXIE_CPU(cs);
28
29 cpu->env.pc = value;
30 }
31
32 static bool moxie_cpu_has_work(CPUState *cs)
33 {
34 return cs->interrupt_request & CPU_INTERRUPT_HARD;
35 }
36
37 static void moxie_cpu_reset(CPUState *s)
38 {
39 MoxieCPU *cpu = MOXIE_CPU(s);
40 MoxieCPUClass *mcc = MOXIE_CPU_GET_CLASS(cpu);
41 CPUMoxieState *env = &cpu->env;
42
43 mcc->parent_reset(s);
44
45 memset(env, 0, offsetof(CPUMoxieState, breakpoints));
46 env->pc = 0x1000;
47
48 tlb_flush(env, 1);
49 }
50
51 static void moxie_cpu_realizefn(DeviceState *dev, Error **errp)
52 {
53 CPUState *cs = CPU(dev);
54 MoxieCPUClass *mcc = MOXIE_CPU_GET_CLASS(dev);
55
56 qemu_init_vcpu(cs);
57 cpu_reset(cs);
58
59 mcc->parent_realize(dev, errp);
60 }
61
62 static void moxie_cpu_initfn(Object *obj)
63 {
64 CPUState *cs = CPU(obj);
65 MoxieCPU *cpu = MOXIE_CPU(obj);
66 static int inited;
67
68 cs->env_ptr = &cpu->env;
69 cpu_exec_init(&cpu->env);
70
71 if (tcg_enabled() && !inited) {
72 inited = 1;
73 moxie_translate_init();
74 }
75 }
76
77 static ObjectClass *moxie_cpu_class_by_name(const char *cpu_model)
78 {
79 ObjectClass *oc;
80
81 if (cpu_model == NULL) {
82 return NULL;
83 }
84
85 oc = object_class_by_name(cpu_model);
86 if (oc != NULL && (!object_class_dynamic_cast(oc, TYPE_MOXIE_CPU) ||
87 object_class_is_abstract(oc))) {
88 return NULL;
89 }
90 return oc;
91 }
92
93 static void moxie_cpu_class_init(ObjectClass *oc, void *data)
94 {
95 DeviceClass *dc = DEVICE_CLASS(oc);
96 CPUClass *cc = CPU_CLASS(oc);
97 MoxieCPUClass *mcc = MOXIE_CPU_CLASS(oc);
98
99 mcc->parent_realize = dc->realize;
100 dc->realize = moxie_cpu_realizefn;
101
102 mcc->parent_reset = cc->reset;
103 cc->reset = moxie_cpu_reset;
104
105 cc->class_by_name = moxie_cpu_class_by_name;
106
107 cc->has_work = moxie_cpu_has_work;
108 cc->do_interrupt = moxie_cpu_do_interrupt;
109 cc->dump_state = moxie_cpu_dump_state;
110 cc->set_pc = moxie_cpu_set_pc;
111 #ifndef CONFIG_USER_ONLY
112 cc->get_phys_page_debug = moxie_cpu_get_phys_page_debug;
113 cc->vmsd = &vmstate_moxie_cpu;
114 #endif
115 }
116
117 static void moxielite_initfn(Object *obj)
118 {
119 /* Set cpu feature flags */
120 }
121
122 static void moxie_any_initfn(Object *obj)
123 {
124 /* Set cpu feature flags */
125 }
126
127 typedef struct MoxieCPUInfo {
128 const char *name;
129 void (*initfn)(Object *obj);
130 } MoxieCPUInfo;
131
132 static const MoxieCPUInfo moxie_cpus[] = {
133 { .name = "MoxieLite", .initfn = moxielite_initfn },
134 { .name = "any", .initfn = moxie_any_initfn },
135 };
136
137 MoxieCPU *cpu_moxie_init(const char *cpu_model)
138 {
139 return MOXIE_CPU(cpu_generic_init(TYPE_MOXIE_CPU, cpu_model));
140 }
141
142 static void cpu_register(const MoxieCPUInfo *info)
143 {
144 TypeInfo type_info = {
145 .parent = TYPE_MOXIE_CPU,
146 .instance_size = sizeof(MoxieCPU),
147 .instance_init = info->initfn,
148 .class_size = sizeof(MoxieCPUClass),
149 };
150
151 type_info.name = g_strdup_printf("%s-" TYPE_MOXIE_CPU, info->name);
152 type_register(&type_info);
153 g_free((void *)type_info.name);
154 }
155
156 static const TypeInfo moxie_cpu_type_info = {
157 .name = TYPE_MOXIE_CPU,
158 .parent = TYPE_CPU,
159 .instance_size = sizeof(MoxieCPU),
160 .instance_init = moxie_cpu_initfn,
161 .class_size = sizeof(MoxieCPUClass),
162 .class_init = moxie_cpu_class_init,
163 };
164
165 static void moxie_cpu_register_types(void)
166 {
167 int i;
168 type_register_static(&moxie_cpu_type_info);
169 for (i = 0; i < ARRAY_SIZE(moxie_cpus); i++) {
170 cpu_register(&moxie_cpus[i]);
171 }
172 }
173
174 type_init(moxie_cpu_register_types)