xref: /openbmc/qemu/hw/arm/armv7m.c (revision 9d81b2d2)
1 /*
2  * ARMV7M System emulation.
3  *
4  * Copyright (c) 2006-2007 CodeSourcery.
5  * Written by Paul Brook
6  *
7  * This code is licensed under the GPL.
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 
23 /* Bitbanded IO.  Each word corresponds to a single bit.  */
24 
25 /* Get the byte address of the real memory for a bitband access.  */
26 static inline hwaddr bitband_addr(BitBandState *s, hwaddr offset)
27 {
28     return s->base | (offset & 0x1ffffff) >> 5;
29 }
30 
31 static MemTxResult bitband_read(void *opaque, hwaddr offset,
32                                 uint64_t *data, unsigned size, MemTxAttrs attrs)
33 {
34     BitBandState *s = opaque;
35     uint8_t buf[4];
36     MemTxResult res;
37     int bitpos, bit;
38     hwaddr addr;
39 
40     assert(size <= 4);
41 
42     /* Find address in underlying memory and round down to multiple of size */
43     addr = bitband_addr(s, offset) & (-size);
44     res = address_space_read(s->source_as, addr, attrs, buf, size);
45     if (res) {
46         return res;
47     }
48     /* Bit position in the N bytes read... */
49     bitpos = (offset >> 2) & ((size * 8) - 1);
50     /* ...converted to byte in buffer and bit in byte */
51     bit = (buf[bitpos >> 3] >> (bitpos & 7)) & 1;
52     *data = bit;
53     return MEMTX_OK;
54 }
55 
56 static MemTxResult bitband_write(void *opaque, hwaddr offset, uint64_t value,
57                                  unsigned size, MemTxAttrs attrs)
58 {
59     BitBandState *s = opaque;
60     uint8_t buf[4];
61     MemTxResult res;
62     int bitpos, bit;
63     hwaddr addr;
64 
65     assert(size <= 4);
66 
67     /* Find address in underlying memory and round down to multiple of size */
68     addr = bitband_addr(s, offset) & (-size);
69     res = address_space_read(s->source_as, addr, attrs, buf, size);
70     if (res) {
71         return res;
72     }
73     /* Bit position in the N bytes read... */
74     bitpos = (offset >> 2) & ((size * 8) - 1);
75     /* ...converted to byte in buffer and bit in byte */
76     bit = 1 << (bitpos & 7);
77     if (value & 1) {
78         buf[bitpos >> 3] |= bit;
79     } else {
80         buf[bitpos >> 3] &= ~bit;
81     }
82     return address_space_write(s->source_as, addr, attrs, buf, size);
83 }
84 
85 static const MemoryRegionOps bitband_ops = {
86     .read_with_attrs = bitband_read,
87     .write_with_attrs = bitband_write,
88     .endianness = DEVICE_NATIVE_ENDIAN,
89     .impl.min_access_size = 1,
90     .impl.max_access_size = 4,
91     .valid.min_access_size = 1,
92     .valid.max_access_size = 4,
93 };
94 
95 static void bitband_init(Object *obj)
96 {
97     BitBandState *s = BITBAND(obj);
98     SysBusDevice *dev = SYS_BUS_DEVICE(obj);
99 
100     memory_region_init_io(&s->iomem, obj, &bitband_ops, s,
101                           "bitband", 0x02000000);
102     sysbus_init_mmio(dev, &s->iomem);
103 }
104 
105 static void bitband_realize(DeviceState *dev, Error **errp)
106 {
107     BitBandState *s = BITBAND(dev);
108 
109     if (!s->source_memory) {
110         error_setg(errp, "source-memory property not set");
111         return;
112     }
113 
114     s->source_as = address_space_init_shareable(s->source_memory,
115                                                 "bitband-source");
116 }
117 
118 /* Board init.  */
119 
120 static const hwaddr bitband_input_addr[ARMV7M_NUM_BITBANDS] = {
121     0x20000000, 0x40000000
122 };
123 
124 static const hwaddr bitband_output_addr[ARMV7M_NUM_BITBANDS] = {
125     0x22000000, 0x42000000
126 };
127 
128 static void armv7m_instance_init(Object *obj)
129 {
130     ARMv7MState *s = ARMV7M(obj);
131     int i;
132 
133     /* Can't init the cpu here, we don't yet know which model to use */
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 
148 static void armv7m_realize(DeviceState *dev, Error **errp)
149 {
150     ARMv7MState *s = ARMV7M(dev);
151     SysBusDevice *sbd;
152     Error *err = NULL;
153     int i;
154     char **cpustr;
155     ObjectClass *oc;
156     const char *typename;
157     CPUClass *cc;
158 
159     if (!s->board_memory) {
160         error_setg(errp, "memory property was not set");
161         return;
162     }
163 
164     memory_region_add_subregion_overlap(&s->container, 0, s->board_memory, -1);
165 
166     cpustr = g_strsplit(s->cpu_model, ",", 2);
167 
168     oc = cpu_class_by_name(TYPE_ARM_CPU, cpustr[0]);
169     if (!oc) {
170         error_setg(errp, "Unknown CPU model %s", cpustr[0]);
171         g_strfreev(cpustr);
172         return;
173     }
174 
175     cc = CPU_CLASS(oc);
176     typename = object_class_get_name(oc);
177     cc->parse_features(typename, cpustr[1], &err);
178     g_strfreev(cpustr);
179     if (err) {
180         error_propagate(errp, err);
181         return;
182     }
183 
184     s->cpu = ARM_CPU(object_new(typename));
185     if (!s->cpu) {
186         error_setg(errp, "Unknown CPU model %s", s->cpu_model);
187         return;
188     }
189 
190     object_property_set_link(OBJECT(s->cpu), OBJECT(&s->container), "memory",
191                              &error_abort);
192     object_property_set_bool(OBJECT(s->cpu), true, "realized", &err);
193     if (err != NULL) {
194         error_propagate(errp, err);
195         return;
196     }
197 
198     /* Note that we must realize the NVIC after the CPU */
199     object_property_set_bool(OBJECT(&s->nvic), true, "realized", &err);
200     if (err != NULL) {
201         error_propagate(errp, err);
202         return;
203     }
204 
205     /* Alias the NVIC's input and output GPIOs as our own so the board
206      * code can wire them up. (We do this in realize because the
207      * NVIC doesn't create the input GPIO array until realize.)
208      */
209     qdev_pass_gpios(DEVICE(&s->nvic), dev, NULL);
210     qdev_pass_gpios(DEVICE(&s->nvic), dev, "SYSRESETREQ");
211 
212     /* Wire the NVIC up to the CPU */
213     sbd = SYS_BUS_DEVICE(&s->nvic);
214     sysbus_connect_irq(sbd, 0,
215                        qdev_get_gpio_in(DEVICE(s->cpu), ARM_CPU_IRQ));
216     s->cpu->env.nvic = &s->nvic;
217 
218     memory_region_add_subregion(&s->container, 0xe000e000,
219                                 sysbus_mmio_get_region(sbd, 0));
220 
221     for (i = 0; i < ARRAY_SIZE(s->bitband); i++) {
222         Object *obj = OBJECT(&s->bitband[i]);
223         SysBusDevice *sbd = SYS_BUS_DEVICE(&s->bitband[i]);
224 
225         object_property_set_int(obj, bitband_input_addr[i], "base", &err);
226         if (err != NULL) {
227             error_propagate(errp, err);
228             return;
229         }
230         object_property_set_link(obj, OBJECT(s->board_memory),
231                                  "source-memory", &error_abort);
232         object_property_set_bool(obj, true, "realized", &err);
233         if (err != NULL) {
234             error_propagate(errp, err);
235             return;
236         }
237 
238         memory_region_add_subregion(&s->container, bitband_output_addr[i],
239                                     sysbus_mmio_get_region(sbd, 0));
240     }
241 }
242 
243 static Property armv7m_properties[] = {
244     DEFINE_PROP_STRING("cpu-model", ARMv7MState, cpu_model),
245     DEFINE_PROP_LINK("memory", ARMv7MState, board_memory, TYPE_MEMORY_REGION,
246                      MemoryRegion *),
247     DEFINE_PROP_END_OF_LIST(),
248 };
249 
250 static void armv7m_class_init(ObjectClass *klass, void *data)
251 {
252     DeviceClass *dc = DEVICE_CLASS(klass);
253 
254     dc->realize = armv7m_realize;
255     dc->props = armv7m_properties;
256 }
257 
258 static const TypeInfo armv7m_info = {
259     .name = TYPE_ARMV7M,
260     .parent = TYPE_SYS_BUS_DEVICE,
261     .instance_size = sizeof(ARMv7MState),
262     .instance_init = armv7m_instance_init,
263     .class_init = armv7m_class_init,
264 };
265 
266 static void armv7m_reset(void *opaque)
267 {
268     ARMCPU *cpu = opaque;
269 
270     cpu_reset(CPU(cpu));
271 }
272 
273 /* Init CPU and memory for a v7-M based board.
274    mem_size is in bytes.
275    Returns the ARMv7M device.  */
276 
277 DeviceState *armv7m_init(MemoryRegion *system_memory, int mem_size, int num_irq,
278                       const char *kernel_filename, const char *cpu_model)
279 {
280     DeviceState *armv7m;
281 
282     if (cpu_model == NULL) {
283         cpu_model = "cortex-m3";
284     }
285 
286     armv7m = qdev_create(NULL, TYPE_ARMV7M);
287     qdev_prop_set_uint32(armv7m, "num-irq", num_irq);
288     qdev_prop_set_string(armv7m, "cpu-model", cpu_model);
289     object_property_set_link(OBJECT(armv7m), OBJECT(get_system_memory()),
290                                      "memory", &error_abort);
291     /* This will exit with an error if the user passed us a bad cpu_model */
292     qdev_init_nofail(armv7m);
293 
294     armv7m_load_kernel(ARM_CPU(first_cpu), kernel_filename, mem_size);
295     return armv7m;
296 }
297 
298 void armv7m_load_kernel(ARMCPU *cpu, const char *kernel_filename, int mem_size)
299 {
300     int image_size;
301     uint64_t entry;
302     uint64_t lowaddr;
303     int big_endian;
304 
305 #ifdef TARGET_WORDS_BIGENDIAN
306     big_endian = 1;
307 #else
308     big_endian = 0;
309 #endif
310 
311     if (!kernel_filename && !qtest_enabled()) {
312         fprintf(stderr, "Guest image must be specified (using -kernel)\n");
313         exit(1);
314     }
315 
316     if (kernel_filename) {
317         image_size = load_elf(kernel_filename, NULL, NULL, &entry, &lowaddr,
318                               NULL, big_endian, EM_ARM, 1, 0);
319         if (image_size < 0) {
320             image_size = load_image_targphys(kernel_filename, 0, mem_size);
321             lowaddr = 0;
322         }
323         if (image_size < 0) {
324             error_report("Could not load kernel '%s'", kernel_filename);
325             exit(1);
326         }
327     }
328 
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      */
337     qemu_register_reset(armv7m_reset, cpu);
338 }
339 
340 static Property bitband_properties[] = {
341     DEFINE_PROP_UINT32("base", BitBandState, base, 0),
342     DEFINE_PROP_LINK("source-memory", BitBandState, source_memory,
343                      TYPE_MEMORY_REGION, MemoryRegion *),
344     DEFINE_PROP_END_OF_LIST(),
345 };
346 
347 static void bitband_class_init(ObjectClass *klass, void *data)
348 {
349     DeviceClass *dc = DEVICE_CLASS(klass);
350 
351     dc->realize = bitband_realize;
352     dc->props = bitband_properties;
353 }
354 
355 static const TypeInfo bitband_info = {
356     .name          = TYPE_BITBAND,
357     .parent        = TYPE_SYS_BUS_DEVICE,
358     .instance_size = sizeof(BitBandState),
359     .instance_init = bitband_init,
360     .class_init    = bitband_class_init,
361 };
362 
363 static void armv7m_register_types(void)
364 {
365     type_register_static(&bitband_info);
366     type_register_static(&armv7m_info);
367 }
368 
369 type_init(armv7m_register_types)
370