]> git.proxmox.com Git - mirror_qemu.git/blame - hw/arm/armv7m.c
Merge remote-tracking branch 'remotes/vivier2/tags/linux-user-for-5.2-pull-request...
[mirror_qemu.git] / hw / arm / armv7m.c
CommitLineData
9ee6e8bb
PB
1/*
2 * ARMV7M System emulation.
3 *
4 * Copyright (c) 2006-2007 CodeSourcery.
5 * Written by Paul Brook
6 *
2167f7bc 7 * This code is licensed under the GPL.
9ee6e8bb
PB
8 */
9
12b16722 10#include "qemu/osdep.h"
56b7c66f 11#include "hw/arm/armv7m.h"
da34e65c 12#include "qapi/error.h"
4771d756 13#include "cpu.h"
83c9f4ca 14#include "hw/sysbus.h"
12ec8bd5 15#include "hw/arm/boot.h"
83c9f4ca 16#include "hw/loader.h"
a27bd6c7 17#include "hw/qdev-properties.h"
ca20cf32 18#include "elf.h"
5633b90a 19#include "sysemu/qtest.h"
71e8a915 20#include "sysemu/reset.h"
5633b90a 21#include "qemu/error-report.h"
0b8fa32f 22#include "qemu/module.h"
618119c2 23#include "exec/address-spaces.h"
c60c1b0d 24#include "target/arm/idau.h"
9ee6e8bb
PB
25
26/* Bitbanded IO. Each word corresponds to a single bit. */
27
2167f7bc 28/* Get the byte address of the real memory for a bitband access. */
f68d881c 29static inline hwaddr bitband_addr(BitBandState *s, hwaddr offset)
9ee6e8bb 30{
f68d881c 31 return s->base | (offset & 0x1ffffff) >> 5;
9ee6e8bb
PB
32}
33
f68d881c
PM
34static MemTxResult bitband_read(void *opaque, hwaddr offset,
35 uint64_t *data, unsigned size, MemTxAttrs attrs)
9ee6e8bb 36{
f68d881c
PM
37 BitBandState *s = opaque;
38 uint8_t buf[4];
39 MemTxResult res;
40 int bitpos, bit;
41 hwaddr addr;
42
43 assert(size <= 4);
44
45 /* Find address in underlying memory and round down to multiple of size */
46 addr = bitband_addr(s, offset) & (-size);
b516572f 47 res = address_space_read(&s->source_as, addr, attrs, buf, size);
f68d881c
PM
48 if (res) {
49 return res;
50 }
51 /* Bit position in the N bytes read... */
52 bitpos = (offset >> 2) & ((size * 8) - 1);
53 /* ...converted to byte in buffer and bit in byte */
54 bit = (buf[bitpos >> 3] >> (bitpos & 7)) & 1;
55 *data = bit;
56 return MEMTX_OK;
9ee6e8bb
PB
57}
58
f68d881c
PM
59static MemTxResult bitband_write(void *opaque, hwaddr offset, uint64_t value,
60 unsigned size, MemTxAttrs attrs)
9ee6e8bb 61{
f68d881c
PM
62 BitBandState *s = opaque;
63 uint8_t buf[4];
64 MemTxResult res;
65 int bitpos, bit;
66 hwaddr addr;
67
68 assert(size <= 4);
69
70 /* Find address in underlying memory and round down to multiple of size */
71 addr = bitband_addr(s, offset) & (-size);
b516572f 72 res = address_space_read(&s->source_as, addr, attrs, buf, size);
f68d881c
PM
73 if (res) {
74 return res;
75 }
76 /* Bit position in the N bytes read... */
77 bitpos = (offset >> 2) & ((size * 8) - 1);
78 /* ...converted to byte in buffer and bit in byte */
79 bit = 1 << (bitpos & 7);
80 if (value & 1) {
81 buf[bitpos >> 3] |= bit;
82 } else {
83 buf[bitpos >> 3] &= ~bit;
84 }
b516572f 85 return address_space_write(&s->source_as, addr, attrs, buf, size);
9ee6e8bb
PB
86}
87
f69bf9d4 88static const MemoryRegionOps bitband_ops = {
f68d881c
PM
89 .read_with_attrs = bitband_read,
90 .write_with_attrs = bitband_write,
f69bf9d4 91 .endianness = DEVICE_NATIVE_ENDIAN,
f68d881c
PM
92 .impl.min_access_size = 1,
93 .impl.max_access_size = 4,
94 .valid.min_access_size = 1,
95 .valid.max_access_size = 4,
9ee6e8bb
PB
96};
97
3f5ab254 98static void bitband_init(Object *obj)
9ee6e8bb 99{
3f5ab254
XZ
100 BitBandState *s = BITBAND(obj);
101 SysBusDevice *dev = SYS_BUS_DEVICE(obj);
9ee6e8bb 102
f68d881c 103 memory_region_init_io(&s->iomem, obj, &bitband_ops, s,
64bde0f3 104 "bitband", 0x02000000);
750ecd44 105 sysbus_init_mmio(dev, &s->iomem);
40905a6a
PB
106}
107
f68d881c
PM
108static void bitband_realize(DeviceState *dev, Error **errp)
109{
110 BitBandState *s = BITBAND(dev);
111
112 if (!s->source_memory) {
113 error_setg(errp, "source-memory property not set");
114 return;
115 }
116
b516572f 117 address_space_init(&s->source_as, s->source_memory, "bitband-source");
f68d881c
PM
118}
119
9ee6e8bb 120/* Board init. */
983fe826 121
56b7c66f
PM
122static const hwaddr bitband_input_addr[ARMV7M_NUM_BITBANDS] = {
123 0x20000000, 0x40000000
124};
125
126static const hwaddr bitband_output_addr[ARMV7M_NUM_BITBANDS] = {
127 0x22000000, 0x42000000
128};
129
130static void armv7m_instance_init(Object *obj)
131{
132 ARMv7MState *s = ARMV7M(obj);
133 int i;
134
135 /* Can't init the cpu here, we don't yet know which model to use */
136
618119c2
PM
137 memory_region_init(&s->container, obj, "armv7m-container", UINT64_MAX);
138
db873cc5 139 object_initialize_child(obj, "nvnic", &s->nvic, TYPE_NVIC);
56b7c66f 140 object_property_add_alias(obj, "num-irq",
d2623129 141 OBJECT(&s->nvic), "num-irq");
56b7c66f
PM
142
143 for (i = 0; i < ARRAY_SIZE(s->bitband); i++) {
5a147c8c
MA
144 object_initialize_child(obj, "bitband[*]", &s->bitband[i],
145 TYPE_BITBAND);
56b7c66f
PM
146 }
147}
148
149static void armv7m_realize(DeviceState *dev, Error **errp)
150{
151 ARMv7MState *s = ARMV7M(dev);
98957a94 152 SysBusDevice *sbd;
56b7c66f
PM
153 Error *err = NULL;
154 int i;
56b7c66f 155
618119c2
PM
156 if (!s->board_memory) {
157 error_setg(errp, "memory property was not set");
158 return;
159 }
160
161 memory_region_add_subregion_overlap(&s->container, 0, s->board_memory, -1);
162
e4c81e3a
PM
163 s->cpu = ARM_CPU(object_new_with_props(s->cpu_type, OBJECT(s), "cpu",
164 &err, NULL));
165 if (err != NULL) {
166 error_propagate(errp, err);
167 return;
168 }
56b7c66f 169
5325cc34 170 object_property_set_link(OBJECT(s->cpu), "memory", OBJECT(&s->container),
618119c2 171 &error_abort);
c60c1b0d 172 if (object_property_find(OBJECT(s->cpu), "idau", NULL)) {
5325cc34 173 object_property_set_link(OBJECT(s->cpu), "idau", s->idau,
c24d9716 174 &error_abort);
c60c1b0d 175 }
60d75d81 176 if (object_property_find(OBJECT(s->cpu), "init-svtor", NULL)) {
778a2dc5 177 if (!object_property_set_uint(OBJECT(s->cpu), "init-svtor",
668f62ec 178 s->init_svtor, errp)) {
60d75d81
PM
179 return;
180 }
181 }
66647809 182 if (object_property_find(OBJECT(s->cpu), "start-powered-off", NULL)) {
778a2dc5 183 if (!object_property_set_bool(OBJECT(s->cpu), "start-powered-off",
668f62ec 184 s->start_powered_off, errp)) {
66647809
PM
185 return;
186 }
187 }
e0cf7b81 188 if (object_property_find(OBJECT(s->cpu), "vfp", NULL)) {
668f62ec 189 if (!object_property_set_bool(OBJECT(s->cpu), "vfp", s->vfp, errp)) {
e0cf7b81
PM
190 return;
191 }
192 }
193 if (object_property_find(OBJECT(s->cpu), "dsp", NULL)) {
668f62ec 194 if (!object_property_set_bool(OBJECT(s->cpu), "dsp", s->dsp, errp)) {
e0cf7b81
PM
195 return;
196 }
197 }
95f87565 198
3693f217
PM
199 /*
200 * Tell the CPU where the NVIC is; it will fail realize if it doesn't
201 * have one. Similarly, tell the NVIC where its CPU is.
95f87565
PM
202 */
203 s->cpu->env.nvic = &s->nvic;
3693f217 204 s->nvic.cpu = s->cpu;
95f87565 205
668f62ec 206 if (!qdev_realize(DEVICE(s->cpu), NULL, errp)) {
56b7c66f
PM
207 return;
208 }
209
210 /* Note that we must realize the NVIC after the CPU */
668f62ec 211 if (!sysbus_realize(SYS_BUS_DEVICE(&s->nvic), errp)) {
56b7c66f
PM
212 return;
213 }
214
215 /* Alias the NVIC's input and output GPIOs as our own so the board
216 * code can wire them up. (We do this in realize because the
217 * NVIC doesn't create the input GPIO array until realize.)
218 */
219 qdev_pass_gpios(DEVICE(&s->nvic), dev, NULL);
220 qdev_pass_gpios(DEVICE(&s->nvic), dev, "SYSRESETREQ");
514b4f36 221 qdev_pass_gpios(DEVICE(&s->nvic), dev, "NMI");
56b7c66f
PM
222
223 /* Wire the NVIC up to the CPU */
98957a94
PM
224 sbd = SYS_BUS_DEVICE(&s->nvic);
225 sysbus_connect_irq(sbd, 0,
56b7c66f 226 qdev_get_gpio_in(DEVICE(s->cpu), ARM_CPU_IRQ));
56b7c66f 227
98957a94
PM
228 memory_region_add_subregion(&s->container, 0xe000e000,
229 sysbus_mmio_get_region(sbd, 0));
230
210d1867
MA
231 for (i = 0; i < ARRAY_SIZE(s->bitband); i++) {
232 if (s->enable_bitband) {
a1c5a062
SH
233 Object *obj = OBJECT(&s->bitband[i]);
234 SysBusDevice *sbd = SYS_BUS_DEVICE(&s->bitband[i]);
235
778a2dc5 236 if (!object_property_set_int(obj, "base",
668f62ec 237 bitband_input_addr[i], errp)) {
a1c5a062
SH
238 return;
239 }
5325cc34
MA
240 object_property_set_link(obj, "source-memory",
241 OBJECT(s->board_memory), &error_abort);
668f62ec 242 if (!sysbus_realize(SYS_BUS_DEVICE(obj), errp)) {
a1c5a062
SH
243 return;
244 }
245
246 memory_region_add_subregion(&s->container, bitband_output_addr[i],
247 sysbus_mmio_get_region(sbd, 0));
210d1867
MA
248 } else {
249 object_unparent(OBJECT(&s->bitband[i]));
56b7c66f 250 }
56b7c66f
PM
251 }
252}
253
254static Property armv7m_properties[] = {
ba1ba5cc 255 DEFINE_PROP_STRING("cpu-type", ARMv7MState, cpu_type),
e2ff1215
FZ
256 DEFINE_PROP_LINK("memory", ARMv7MState, board_memory, TYPE_MEMORY_REGION,
257 MemoryRegion *),
c60c1b0d 258 DEFINE_PROP_LINK("idau", ARMv7MState, idau, TYPE_IDAU_INTERFACE, Object *),
60d75d81 259 DEFINE_PROP_UINT32("init-svtor", ARMv7MState, init_svtor, 0),
a1c5a062 260 DEFINE_PROP_BOOL("enable-bitband", ARMv7MState, enable_bitband, false),
66647809
PM
261 DEFINE_PROP_BOOL("start-powered-off", ARMv7MState, start_powered_off,
262 false),
e0cf7b81
PM
263 DEFINE_PROP_BOOL("vfp", ARMv7MState, vfp, true),
264 DEFINE_PROP_BOOL("dsp", ARMv7MState, dsp, true),
56b7c66f
PM
265 DEFINE_PROP_END_OF_LIST(),
266};
267
268static void armv7m_class_init(ObjectClass *klass, void *data)
269{
270 DeviceClass *dc = DEVICE_CLASS(klass);
271
272 dc->realize = armv7m_realize;
4f67d30b 273 device_class_set_props(dc, armv7m_properties);
56b7c66f
PM
274}
275
276static const TypeInfo armv7m_info = {
277 .name = TYPE_ARMV7M,
278 .parent = TYPE_SYS_BUS_DEVICE,
279 .instance_size = sizeof(ARMv7MState),
280 .instance_init = armv7m_instance_init,
281 .class_init = armv7m_class_init,
282};
283
983fe826
PB
284static void armv7m_reset(void *opaque)
285{
31363f12
AF
286 ARMCPU *cpu = opaque;
287
288 cpu_reset(CPU(cpu));
983fe826
PB
289}
290
3651c285
PM
291void armv7m_load_kernel(ARMCPU *cpu, const char *kernel_filename, int mem_size)
292{
293 int image_size;
294 uint64_t entry;
295 uint64_t lowaddr;
296 int big_endian;
891f3bc3
PM
297 AddressSpace *as;
298 int asidx;
299 CPUState *cs = CPU(cpu);
9ee6e8bb 300
ca20cf32
BS
301#ifdef TARGET_WORDS_BIGENDIAN
302 big_endian = 1;
303#else
304 big_endian = 0;
305#endif
306
891f3bc3
PM
307 if (arm_feature(&cpu->env, ARM_FEATURE_EL3)) {
308 asidx = ARMASIdx_S;
309 } else {
310 asidx = ARMASIdx_NS;
311 }
312 as = cpu_get_address_space(cs, asidx);
313
5633b90a 314 if (kernel_filename) {
4366e1db 315 image_size = load_elf_as(kernel_filename, NULL, NULL, NULL,
6cdda0ff 316 &entry, &lowaddr, NULL,
891f3bc3 317 NULL, big_endian, EM_ARM, 1, 0, as);
5633b90a 318 if (image_size < 0) {
891f3bc3
PM
319 image_size = load_image_targphys_as(kernel_filename, 0,
320 mem_size, as);
5633b90a
AF
321 lowaddr = 0;
322 }
323 if (image_size < 0) {
324 error_report("Could not load kernel '%s'", kernel_filename);
325 exit(1);
326 }
9ee6e8bb
PB
327 }
328
3651c285
PM
329 /* CPU objects (unlike devices) are not automatically reset on system
330 * reset, so we must always register a handler to do so. Unlike
331 * A-profile CPUs, we don't need to do anything special in the
332 * handler to arrange that it starts correctly.
333 * This is arguably the wrong place to do this, but it matches the
334 * way A-profile does it. Note that this means that every M profile
335 * board must call this function!
336 */
31363f12 337 qemu_register_reset(armv7m_reset, cpu);
9ee6e8bb 338}
40905a6a 339
999e12bb
AL
340static Property bitband_properties[] = {
341 DEFINE_PROP_UINT32("base", BitBandState, base, 0),
5f486f97
FZ
342 DEFINE_PROP_LINK("source-memory", BitBandState, source_memory,
343 TYPE_MEMORY_REGION, MemoryRegion *),
999e12bb
AL
344 DEFINE_PROP_END_OF_LIST(),
345};
346
347static void bitband_class_init(ObjectClass *klass, void *data)
348{
39bffca2 349 DeviceClass *dc = DEVICE_CLASS(klass);
999e12bb 350
f68d881c 351 dc->realize = bitband_realize;
4f67d30b 352 device_class_set_props(dc, bitband_properties);
999e12bb
AL
353}
354
8c43a6f0 355static const TypeInfo bitband_info = {
936230a7 356 .name = TYPE_BITBAND,
39bffca2
AL
357 .parent = TYPE_SYS_BUS_DEVICE,
358 .instance_size = sizeof(BitBandState),
3f5ab254 359 .instance_init = bitband_init,
39bffca2 360 .class_init = bitband_class_init,
ee6847d1
GH
361};
362
83f7d43a 363static void armv7m_register_types(void)
40905a6a 364{
39bffca2 365 type_register_static(&bitband_info);
56b7c66f 366 type_register_static(&armv7m_info);
40905a6a
PB
367}
368
83f7d43a 369type_init(armv7m_register_types)