1
2
3
4
5
6
7
8
9
10#include "qemu/osdep.h"
11#include "hw/arm/armv7m.h"
12#include "qapi/error.h"
13#include "qemu-common.h"
14#include "cpu.h"
15#include "hw/sysbus.h"
16#include "hw/arm/arm.h"
17#include "hw/loader.h"
18#include "elf.h"
19#include "sysemu/qtest.h"
20#include "qemu/error-report.h"
21#include "exec/address-spaces.h"
22#include "target/arm/idau.h"
23
24
25
26
27static inline hwaddr bitband_addr(BitBandState *s, hwaddr offset)
28{
29 return s->base | (offset & 0x1ffffff) >> 5;
30}
31
32static MemTxResult bitband_read(void *opaque, hwaddr offset,
33 uint64_t *data, unsigned size, MemTxAttrs attrs)
34{
35 BitBandState *s = opaque;
36 uint8_t buf[4];
37 MemTxResult res;
38 int bitpos, bit;
39 hwaddr addr;
40
41 assert(size <= 4);
42
43
44 addr = bitband_addr(s, offset) & (-size);
45 res = address_space_read(&s->source_as, addr, attrs, buf, size);
46 if (res) {
47 return res;
48 }
49
50 bitpos = (offset >> 2) & ((size * 8) - 1);
51
52 bit = (buf[bitpos >> 3] >> (bitpos & 7)) & 1;
53 *data = bit;
54 return MEMTX_OK;
55}
56
57static MemTxResult bitband_write(void *opaque, hwaddr offset, uint64_t value,
58 unsigned size, MemTxAttrs attrs)
59{
60 BitBandState *s = opaque;
61 uint8_t buf[4];
62 MemTxResult res;
63 int bitpos, bit;
64 hwaddr addr;
65
66 assert(size <= 4);
67
68
69 addr = bitband_addr(s, offset) & (-size);
70 res = address_space_read(&s->source_as, addr, attrs, buf, size);
71 if (res) {
72 return res;
73 }
74
75 bitpos = (offset >> 2) & ((size * 8) - 1);
76
77 bit = 1 << (bitpos & 7);
78 if (value & 1) {
79 buf[bitpos >> 3] |= bit;
80 } else {
81 buf[bitpos >> 3] &= ~bit;
82 }
83 return address_space_write(&s->source_as, addr, attrs, buf, size);
84}
85
86static const MemoryRegionOps bitband_ops = {
87 .read_with_attrs = bitband_read,
88 .write_with_attrs = bitband_write,
89 .endianness = DEVICE_NATIVE_ENDIAN,
90 .impl.min_access_size = 1,
91 .impl.max_access_size = 4,
92 .valid.min_access_size = 1,
93 .valid.max_access_size = 4,
94};
95
96static void bitband_init(Object *obj)
97{
98 BitBandState *s = BITBAND(obj);
99 SysBusDevice *dev = SYS_BUS_DEVICE(obj);
100
101 memory_region_init_io(&s->iomem, obj, &bitband_ops, s,
102 "bitband", 0x02000000);
103 sysbus_init_mmio(dev, &s->iomem);
104}
105
106static void bitband_realize(DeviceState *dev, Error **errp)
107{
108 BitBandState *s = BITBAND(dev);
109
110 if (!s->source_memory) {
111 error_setg(errp, "source-memory property not set");
112 return;
113 }
114
115 address_space_init(&s->source_as, s->source_memory, "bitband-source");
116}
117
118
119
120static const hwaddr bitband_input_addr[ARMV7M_NUM_BITBANDS] = {
121 0x20000000, 0x40000000
122};
123
124static const hwaddr bitband_output_addr[ARMV7M_NUM_BITBANDS] = {
125 0x22000000, 0x42000000
126};
127
128static void armv7m_instance_init(Object *obj)
129{
130 ARMv7MState *s = ARMV7M(obj);
131 int i;
132
133
134
135 memory_region_init(&s->container, obj, "armv7m-container", UINT64_MAX);
136
137 object_initialize(&s->nvic, sizeof(s->nvic), TYPE_NVIC);
138 qdev_set_parent_bus(DEVICE(&s->nvic), sysbus_get_default());
139 object_property_add_alias(obj, "num-irq",
140 OBJECT(&s->nvic), "num-irq", &error_abort);
141
142 for (i = 0; i < ARRAY_SIZE(s->bitband); i++) {
143 object_initialize(&s->bitband[i], sizeof(s->bitband[i]), TYPE_BITBAND);
144 qdev_set_parent_bus(DEVICE(&s->bitband[i]), sysbus_get_default());
145 }
146}
147
148static void armv7m_realize(DeviceState *dev, Error **errp)
149{
150 ARMv7MState *s = ARMV7M(dev);
151 SysBusDevice *sbd;
152 Error *err = NULL;
153 int i;
154
155 if (!s->board_memory) {
156 error_setg(errp, "memory property was not set");
157 return;
158 }
159
160 memory_region_add_subregion_overlap(&s->container, 0, s->board_memory, -1);
161
162 s->cpu = ARM_CPU(object_new(s->cpu_type));
163
164 object_property_set_link(OBJECT(s->cpu), OBJECT(&s->container), "memory",
165 &error_abort);
166 if (object_property_find(OBJECT(s->cpu), "idau", NULL)) {
167 object_property_set_link(OBJECT(s->cpu), s->idau, "idau", &err);
168 if (err != NULL) {
169 error_propagate(errp, err);
170 return;
171 }
172 }
173 if (object_property_find(OBJECT(s->cpu), "init-svtor", NULL)) {
174 object_property_set_uint(OBJECT(s->cpu), s->init_svtor,
175 "init-svtor", &err);
176 if (err != NULL) {
177 error_propagate(errp, err);
178 return;
179 }
180 }
181 object_property_set_bool(OBJECT(s->cpu), true, "realized", &err);
182 if (err != NULL) {
183 error_propagate(errp, err);
184 return;
185 }
186
187
188 object_property_set_bool(OBJECT(&s->nvic), true, "realized", &err);
189 if (err != NULL) {
190 error_propagate(errp, err);
191 return;
192 }
193
194
195
196
197
198 qdev_pass_gpios(DEVICE(&s->nvic), dev, NULL);
199 qdev_pass_gpios(DEVICE(&s->nvic), dev, "SYSRESETREQ");
200
201
202 sbd = SYS_BUS_DEVICE(&s->nvic);
203 sysbus_connect_irq(sbd, 0,
204 qdev_get_gpio_in(DEVICE(s->cpu), ARM_CPU_IRQ));
205 s->cpu->env.nvic = &s->nvic;
206
207 memory_region_add_subregion(&s->container, 0xe000e000,
208 sysbus_mmio_get_region(sbd, 0));
209
210 for (i = 0; i < ARRAY_SIZE(s->bitband); i++) {
211 Object *obj = OBJECT(&s->bitband[i]);
212 SysBusDevice *sbd = SYS_BUS_DEVICE(&s->bitband[i]);
213
214 object_property_set_int(obj, bitband_input_addr[i], "base", &err);
215 if (err != NULL) {
216 error_propagate(errp, err);
217 return;
218 }
219 object_property_set_link(obj, OBJECT(s->board_memory),
220 "source-memory", &error_abort);
221 object_property_set_bool(obj, true, "realized", &err);
222 if (err != NULL) {
223 error_propagate(errp, err);
224 return;
225 }
226
227 memory_region_add_subregion(&s->container, bitband_output_addr[i],
228 sysbus_mmio_get_region(sbd, 0));
229 }
230}
231
232static Property armv7m_properties[] = {
233 DEFINE_PROP_STRING("cpu-type", ARMv7MState, cpu_type),
234 DEFINE_PROP_LINK("memory", ARMv7MState, board_memory, TYPE_MEMORY_REGION,
235 MemoryRegion *),
236 DEFINE_PROP_LINK("idau", ARMv7MState, idau, TYPE_IDAU_INTERFACE, Object *),
237 DEFINE_PROP_UINT32("init-svtor", ARMv7MState, init_svtor, 0),
238 DEFINE_PROP_END_OF_LIST(),
239};
240
241static void armv7m_class_init(ObjectClass *klass, void *data)
242{
243 DeviceClass *dc = DEVICE_CLASS(klass);
244
245 dc->realize = armv7m_realize;
246 dc->props = armv7m_properties;
247}
248
249static const TypeInfo armv7m_info = {
250 .name = TYPE_ARMV7M,
251 .parent = TYPE_SYS_BUS_DEVICE,
252 .instance_size = sizeof(ARMv7MState),
253 .instance_init = armv7m_instance_init,
254 .class_init = armv7m_class_init,
255};
256
257static void armv7m_reset(void *opaque)
258{
259 ARMCPU *cpu = opaque;
260
261 cpu_reset(CPU(cpu));
262}
263
264
265
266
267
268DeviceState *armv7m_init(MemoryRegion *system_memory, int mem_size, int num_irq,
269 const char *kernel_filename, const char *cpu_type)
270{
271 DeviceState *armv7m;
272
273 armv7m = qdev_create(NULL, TYPE_ARMV7M);
274 qdev_prop_set_uint32(armv7m, "num-irq", num_irq);
275 qdev_prop_set_string(armv7m, "cpu-type", cpu_type);
276 object_property_set_link(OBJECT(armv7m), OBJECT(get_system_memory()),
277 "memory", &error_abort);
278
279 qdev_init_nofail(armv7m);
280
281 armv7m_load_kernel(ARM_CPU(first_cpu), kernel_filename, mem_size);
282 return armv7m;
283}
284
285void armv7m_load_kernel(ARMCPU *cpu, const char *kernel_filename, int mem_size)
286{
287 int image_size;
288 uint64_t entry;
289 uint64_t lowaddr;
290 int big_endian;
291 AddressSpace *as;
292 int asidx;
293 CPUState *cs = CPU(cpu);
294
295#ifdef TARGET_WORDS_BIGENDIAN
296 big_endian = 1;
297#else
298 big_endian = 0;
299#endif
300
301 if (!kernel_filename && !qtest_enabled()) {
302 error_report("Guest image must be specified (using -kernel)");
303 exit(1);
304 }
305
306 if (arm_feature(&cpu->env, ARM_FEATURE_EL3)) {
307 asidx = ARMASIdx_S;
308 } else {
309 asidx = ARMASIdx_NS;
310 }
311 as = cpu_get_address_space(cs, asidx);
312
313 if (kernel_filename) {
314 image_size = load_elf_as(kernel_filename, NULL, NULL, &entry, &lowaddr,
315 NULL, big_endian, EM_ARM, 1, 0, as);
316 if (image_size < 0) {
317 image_size = load_image_targphys_as(kernel_filename, 0,
318 mem_size, as);
319 lowaddr = 0;
320 }
321 if (image_size < 0) {
322 error_report("Could not load kernel '%s'", kernel_filename);
323 exit(1);
324 }
325 }
326
327
328
329
330
331
332
333
334
335 qemu_register_reset(armv7m_reset, cpu);
336}
337
338static Property bitband_properties[] = {
339 DEFINE_PROP_UINT32("base", BitBandState, base, 0),
340 DEFINE_PROP_LINK("source-memory", BitBandState, source_memory,
341 TYPE_MEMORY_REGION, MemoryRegion *),
342 DEFINE_PROP_END_OF_LIST(),
343};
344
345static void bitband_class_init(ObjectClass *klass, void *data)
346{
347 DeviceClass *dc = DEVICE_CLASS(klass);
348
349 dc->realize = bitband_realize;
350 dc->props = bitband_properties;
351}
352
353static const TypeInfo bitband_info = {
354 .name = TYPE_BITBAND,
355 .parent = TYPE_SYS_BUS_DEVICE,
356 .instance_size = sizeof(BitBandState),
357 .instance_init = bitband_init,
358 .class_init = bitband_class_init,
359};
360
361static void armv7m_register_types(void)
362{
363 type_register_static(&bitband_info);
364 type_register_static(&armv7m_info);
365}
366
367type_init(armv7m_register_types)
368