xref: /openbmc/qemu/hw/arm/armv7m.c (revision 3110cdbd)
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 
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     object_property_set_bool(OBJECT(s->cpu), true, "realized", &err);
167     if (err != NULL) {
168         error_propagate(errp, err);
169         return;
170     }
171 
172     /* Note that we must realize the NVIC after the CPU */
173     object_property_set_bool(OBJECT(&s->nvic), true, "realized", &err);
174     if (err != NULL) {
175         error_propagate(errp, err);
176         return;
177     }
178 
179     /* Alias the NVIC's input and output GPIOs as our own so the board
180      * code can wire them up. (We do this in realize because the
181      * NVIC doesn't create the input GPIO array until realize.)
182      */
183     qdev_pass_gpios(DEVICE(&s->nvic), dev, NULL);
184     qdev_pass_gpios(DEVICE(&s->nvic), dev, "SYSRESETREQ");
185 
186     /* Wire the NVIC up to the CPU */
187     sbd = SYS_BUS_DEVICE(&s->nvic);
188     sysbus_connect_irq(sbd, 0,
189                        qdev_get_gpio_in(DEVICE(s->cpu), ARM_CPU_IRQ));
190     s->cpu->env.nvic = &s->nvic;
191 
192     memory_region_add_subregion(&s->container, 0xe000e000,
193                                 sysbus_mmio_get_region(sbd, 0));
194 
195     for (i = 0; i < ARRAY_SIZE(s->bitband); i++) {
196         Object *obj = OBJECT(&s->bitband[i]);
197         SysBusDevice *sbd = SYS_BUS_DEVICE(&s->bitband[i]);
198 
199         object_property_set_int(obj, bitband_input_addr[i], "base", &err);
200         if (err != NULL) {
201             error_propagate(errp, err);
202             return;
203         }
204         object_property_set_link(obj, OBJECT(s->board_memory),
205                                  "source-memory", &error_abort);
206         object_property_set_bool(obj, true, "realized", &err);
207         if (err != NULL) {
208             error_propagate(errp, err);
209             return;
210         }
211 
212         memory_region_add_subregion(&s->container, bitband_output_addr[i],
213                                     sysbus_mmio_get_region(sbd, 0));
214     }
215 }
216 
217 static Property armv7m_properties[] = {
218     DEFINE_PROP_STRING("cpu-type", ARMv7MState, cpu_type),
219     DEFINE_PROP_LINK("memory", ARMv7MState, board_memory, TYPE_MEMORY_REGION,
220                      MemoryRegion *),
221     DEFINE_PROP_END_OF_LIST(),
222 };
223 
224 static void armv7m_class_init(ObjectClass *klass, void *data)
225 {
226     DeviceClass *dc = DEVICE_CLASS(klass);
227 
228     dc->realize = armv7m_realize;
229     dc->props = armv7m_properties;
230 }
231 
232 static const TypeInfo armv7m_info = {
233     .name = TYPE_ARMV7M,
234     .parent = TYPE_SYS_BUS_DEVICE,
235     .instance_size = sizeof(ARMv7MState),
236     .instance_init = armv7m_instance_init,
237     .class_init = armv7m_class_init,
238 };
239 
240 static void armv7m_reset(void *opaque)
241 {
242     ARMCPU *cpu = opaque;
243 
244     cpu_reset(CPU(cpu));
245 }
246 
247 /* Init CPU and memory for a v7-M based board.
248    mem_size is in bytes.
249    Returns the ARMv7M device.  */
250 
251 DeviceState *armv7m_init(MemoryRegion *system_memory, int mem_size, int num_irq,
252                          const char *kernel_filename, const char *cpu_type)
253 {
254     DeviceState *armv7m;
255 
256     armv7m = qdev_create(NULL, TYPE_ARMV7M);
257     qdev_prop_set_uint32(armv7m, "num-irq", num_irq);
258     qdev_prop_set_string(armv7m, "cpu-type", cpu_type);
259     object_property_set_link(OBJECT(armv7m), OBJECT(get_system_memory()),
260                                      "memory", &error_abort);
261     /* This will exit with an error if the user passed us a bad cpu_type */
262     qdev_init_nofail(armv7m);
263 
264     armv7m_load_kernel(ARM_CPU(first_cpu), kernel_filename, mem_size);
265     return armv7m;
266 }
267 
268 void armv7m_load_kernel(ARMCPU *cpu, const char *kernel_filename, int mem_size)
269 {
270     int image_size;
271     uint64_t entry;
272     uint64_t lowaddr;
273     int big_endian;
274 
275 #ifdef TARGET_WORDS_BIGENDIAN
276     big_endian = 1;
277 #else
278     big_endian = 0;
279 #endif
280 
281     if (!kernel_filename && !qtest_enabled()) {
282         fprintf(stderr, "Guest image must be specified (using -kernel)\n");
283         exit(1);
284     }
285 
286     if (kernel_filename) {
287         image_size = load_elf(kernel_filename, NULL, NULL, &entry, &lowaddr,
288                               NULL, big_endian, EM_ARM, 1, 0);
289         if (image_size < 0) {
290             image_size = load_image_targphys(kernel_filename, 0, mem_size);
291             lowaddr = 0;
292         }
293         if (image_size < 0) {
294             error_report("Could not load kernel '%s'", kernel_filename);
295             exit(1);
296         }
297     }
298 
299     /* CPU objects (unlike devices) are not automatically reset on system
300      * reset, so we must always register a handler to do so. Unlike
301      * A-profile CPUs, we don't need to do anything special in the
302      * handler to arrange that it starts correctly.
303      * This is arguably the wrong place to do this, but it matches the
304      * way A-profile does it. Note that this means that every M profile
305      * board must call this function!
306      */
307     qemu_register_reset(armv7m_reset, cpu);
308 }
309 
310 static Property bitband_properties[] = {
311     DEFINE_PROP_UINT32("base", BitBandState, base, 0),
312     DEFINE_PROP_LINK("source-memory", BitBandState, source_memory,
313                      TYPE_MEMORY_REGION, MemoryRegion *),
314     DEFINE_PROP_END_OF_LIST(),
315 };
316 
317 static void bitband_class_init(ObjectClass *klass, void *data)
318 {
319     DeviceClass *dc = DEVICE_CLASS(klass);
320 
321     dc->realize = bitband_realize;
322     dc->props = bitband_properties;
323 }
324 
325 static const TypeInfo bitband_info = {
326     .name          = TYPE_BITBAND,
327     .parent        = TYPE_SYS_BUS_DEVICE,
328     .instance_size = sizeof(BitBandState),
329     .instance_init = bitband_init,
330     .class_init    = bitband_class_init,
331 };
332 
333 static void armv7m_register_types(void)
334 {
335     type_register_static(&bitband_info);
336     type_register_static(&armv7m_info);
337 }
338 
339 type_init(armv7m_register_types)
340