hw/arm/mps2-tz: Get armv7m_load_kernel() size argument from RAMInfo
[qemu/ar7.git] / hw / arm / mps2-tz.c
blob4dfd9a3aa52ca18f6c5b3fc1bffbc4a86a58976a
1 /*
2 * ARM V2M MPS2 board emulation, trustzone aware FPGA images
4 * Copyright (c) 2017 Linaro Limited
5 * Written by Peter Maydell
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 or
9 * (at your option) any later version.
12 /* The MPS2 and MPS2+ dev boards are FPGA based (the 2+ has a bigger
13 * FPGA but is otherwise the same as the 2). Since the CPU itself
14 * and most of the devices are in the FPGA, the details of the board
15 * as seen by the guest depend significantly on the FPGA image.
16 * This source file covers the following FPGA images, for TrustZone cores:
17 * "mps2-an505" -- Cortex-M33 as documented in ARM Application Note AN505
18 * "mps2-an521" -- Dual Cortex-M33 as documented in Application Note AN521
20 * Links to the TRM for the board itself and to the various Application
21 * Notes which document the FPGA images can be found here:
22 * https://developer.arm.com/products/system-design/development-boards/fpga-prototyping-boards/mps2
24 * Board TRM:
25 * http://infocenter.arm.com/help/topic/com.arm.doc.100112_0200_06_en/versatile_express_cortex_m_prototyping_systems_v2m_mps2_and_v2m_mps2plus_technical_reference_100112_0200_06_en.pdf
26 * Application Note AN505:
27 * http://infocenter.arm.com/help/topic/com.arm.doc.dai0505b/index.html
28 * Application Note AN521:
29 * http://infocenter.arm.com/help/topic/com.arm.doc.dai0521c/index.html
31 * The AN505 defers to the Cortex-M33 processor ARMv8M IoT Kit FVP User Guide
32 * (ARM ECM0601256) for the details of some of the device layout:
33 * http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ecm0601256/index.html
34 * Similarly, the AN521 uses the SSE-200, and the SSE-200 TRM defines
35 * most of the device layout:
36 * http://infocenter.arm.com/help/topic/com.arm.doc.101104_0100_00_en/corelink_sse200_subsystem_for_embedded_technical_reference_manual_101104_0100_00_en.pdf
40 #include "qemu/osdep.h"
41 #include "qemu/units.h"
42 #include "qemu/cutils.h"
43 #include "qapi/error.h"
44 #include "qemu/error-report.h"
45 #include "hw/arm/boot.h"
46 #include "hw/arm/armv7m.h"
47 #include "hw/or-irq.h"
48 #include "hw/boards.h"
49 #include "exec/address-spaces.h"
50 #include "sysemu/sysemu.h"
51 #include "hw/misc/unimp.h"
52 #include "hw/char/cmsdk-apb-uart.h"
53 #include "hw/timer/cmsdk-apb-timer.h"
54 #include "hw/misc/mps2-scc.h"
55 #include "hw/misc/mps2-fpgaio.h"
56 #include "hw/misc/tz-mpc.h"
57 #include "hw/misc/tz-msc.h"
58 #include "hw/arm/armsse.h"
59 #include "hw/dma/pl080.h"
60 #include "hw/ssi/pl022.h"
61 #include "hw/i2c/arm_sbcon_i2c.h"
62 #include "hw/net/lan9118.h"
63 #include "net/net.h"
64 #include "hw/core/split-irq.h"
65 #include "hw/qdev-clock.h"
66 #include "qom/object.h"
68 #define MPS2TZ_NUMIRQ_MAX 92
69 #define MPS2TZ_RAM_MAX 4
71 typedef enum MPS2TZFPGAType {
72 FPGA_AN505,
73 FPGA_AN521,
74 } MPS2TZFPGAType;
77 * Define the layout of RAM in a board, including which parts are
78 * behind which MPCs.
79 * mrindex specifies the index into mms->ram[] to use for the backing RAM;
80 * -1 means "use the system RAM".
82 typedef struct RAMInfo {
83 const char *name;
84 uint32_t base;
85 uint32_t size;
86 int mpc; /* MPC number, -1 for "not behind an MPC" */
87 int mrindex;
88 int flags;
89 } RAMInfo;
92 * Flag values:
93 * IS_ALIAS: this RAM area is an alias to the upstream end of the
94 * MPC specified by its .mpc value
95 * IS_ROM: this RAM area is read-only
97 #define IS_ALIAS 1
98 #define IS_ROM 2
100 struct MPS2TZMachineClass {
101 MachineClass parent;
102 MPS2TZFPGAType fpga_type;
103 uint32_t scc_id;
104 uint32_t sysclk_frq; /* Main SYSCLK frequency in Hz */
105 uint32_t len_oscclk;
106 const uint32_t *oscclk;
107 uint32_t fpgaio_num_leds; /* Number of LEDs in FPGAIO LED0 register */
108 bool fpgaio_has_switches; /* Does FPGAIO have SWITCH register? */
109 int numirq; /* Number of external interrupts */
110 const RAMInfo *raminfo;
111 const char *armsse_type;
114 struct MPS2TZMachineState {
115 MachineState parent;
117 ARMSSE iotkit;
118 MemoryRegion ram[MPS2TZ_RAM_MAX];
119 MPS2SCC scc;
120 MPS2FPGAIO fpgaio;
121 TZPPC ppc[5];
122 TZMPC mpc[3];
123 PL022State spi[5];
124 ArmSbconI2CState i2c[4];
125 UnimplementedDeviceState i2s_audio;
126 UnimplementedDeviceState gpio[4];
127 UnimplementedDeviceState gfx;
128 PL080State dma[4];
129 TZMSC msc[4];
130 CMSDKAPBUART uart[5];
131 SplitIRQ sec_resp_splitter;
132 qemu_or_irq uart_irq_orgate;
133 DeviceState *lan9118;
134 SplitIRQ cpu_irq_splitter[MPS2TZ_NUMIRQ_MAX];
135 Clock *sysclk;
136 Clock *s32kclk;
139 #define TYPE_MPS2TZ_MACHINE "mps2tz"
140 #define TYPE_MPS2TZ_AN505_MACHINE MACHINE_TYPE_NAME("mps2-an505")
141 #define TYPE_MPS2TZ_AN521_MACHINE MACHINE_TYPE_NAME("mps2-an521")
143 OBJECT_DECLARE_TYPE(MPS2TZMachineState, MPS2TZMachineClass, MPS2TZ_MACHINE)
145 /* Slow 32Khz S32KCLK frequency in Hz */
146 #define S32KCLK_FRQ (32 * 1000)
148 static const uint32_t an505_oscclk[] = {
149 40000000,
150 24580000,
151 25000000,
154 static const RAMInfo an505_raminfo[] = { {
155 .name = "ssram-0",
156 .base = 0x00000000,
157 .size = 0x00400000,
158 .mpc = 0,
159 .mrindex = 0,
160 }, {
161 .name = "ssram-1",
162 .base = 0x28000000,
163 .size = 0x00200000,
164 .mpc = 1,
165 .mrindex = 1,
166 }, {
167 .name = "ssram-2",
168 .base = 0x28200000,
169 .size = 0x00200000,
170 .mpc = 2,
171 .mrindex = 2,
172 }, {
173 .name = "ssram-0-alias",
174 .base = 0x00400000,
175 .size = 0x00400000,
176 .mpc = 0,
177 .mrindex = 3,
178 .flags = IS_ALIAS,
179 }, {
180 /* Use the largest bit of contiguous RAM as our "system memory" */
181 .name = "mps.ram",
182 .base = 0x80000000,
183 .size = 16 * MiB,
184 .mpc = -1,
185 .mrindex = -1,
186 }, {
187 .name = NULL,
191 static const RAMInfo *find_raminfo_for_mpc(MPS2TZMachineState *mms, int mpc)
193 MPS2TZMachineClass *mmc = MPS2TZ_MACHINE_GET_CLASS(mms);
194 const RAMInfo *p;
196 for (p = mmc->raminfo; p->name; p++) {
197 if (p->mpc == mpc && !(p->flags & IS_ALIAS)) {
198 return p;
201 /* if raminfo array doesn't have an entry for each MPC this is a bug */
202 g_assert_not_reached();
205 static MemoryRegion *mr_for_raminfo(MPS2TZMachineState *mms,
206 const RAMInfo *raminfo)
208 /* Return an initialized MemoryRegion for the RAMInfo. */
209 MemoryRegion *ram;
211 if (raminfo->mrindex < 0) {
212 /* Means this RAMInfo is for QEMU's "system memory" */
213 MachineState *machine = MACHINE(mms);
214 assert(!(raminfo->flags & IS_ROM));
215 return machine->ram;
218 assert(raminfo->mrindex < MPS2TZ_RAM_MAX);
219 ram = &mms->ram[raminfo->mrindex];
221 memory_region_init_ram(ram, NULL, raminfo->name,
222 raminfo->size, &error_fatal);
223 if (raminfo->flags & IS_ROM) {
224 memory_region_set_readonly(ram, true);
226 return ram;
229 /* Create an alias of an entire original MemoryRegion @orig
230 * located at @base in the memory map.
232 static void make_ram_alias(MemoryRegion *mr, const char *name,
233 MemoryRegion *orig, hwaddr base)
235 memory_region_init_alias(mr, NULL, name, orig, 0,
236 memory_region_size(orig));
237 memory_region_add_subregion(get_system_memory(), base, mr);
240 static qemu_irq get_sse_irq_in(MPS2TZMachineState *mms, int irqno)
243 * Return a qemu_irq which will signal IRQ n to all CPUs in the
244 * SSE. The irqno should be as the CPU sees it, so the first
245 * external-to-the-SSE interrupt is 32.
247 MachineClass *mc = MACHINE_GET_CLASS(mms);
248 MPS2TZMachineClass *mmc = MPS2TZ_MACHINE_GET_CLASS(mms);
250 assert(irqno >= 32 && irqno < (mmc->numirq + 32));
253 * Convert from "CPU irq number" (as listed in the FPGA image
254 * documentation) to the SSE external-interrupt number.
256 irqno -= 32;
258 if (mc->max_cpus > 1) {
259 return qdev_get_gpio_in(DEVICE(&mms->cpu_irq_splitter[irqno]), 0);
260 } else {
261 return qdev_get_gpio_in_named(DEVICE(&mms->iotkit), "EXP_IRQ", irqno);
265 /* Most of the devices in the AN505 FPGA image sit behind
266 * Peripheral Protection Controllers. These data structures
267 * define the layout of which devices sit behind which PPCs.
268 * The devfn for each port is a function which creates, configures
269 * and initializes the device, returning the MemoryRegion which
270 * needs to be plugged into the downstream end of the PPC port.
272 typedef MemoryRegion *MakeDevFn(MPS2TZMachineState *mms, void *opaque,
273 const char *name, hwaddr size,
274 const int *irqs);
276 typedef struct PPCPortInfo {
277 const char *name;
278 MakeDevFn *devfn;
279 void *opaque;
280 hwaddr addr;
281 hwaddr size;
282 int irqs[3]; /* currently no device needs more IRQ lines than this */
283 } PPCPortInfo;
285 typedef struct PPCInfo {
286 const char *name;
287 PPCPortInfo ports[TZ_NUM_PORTS];
288 } PPCInfo;
290 static MemoryRegion *make_unimp_dev(MPS2TZMachineState *mms,
291 void *opaque,
292 const char *name, hwaddr size,
293 const int *irqs)
295 /* Initialize, configure and realize a TYPE_UNIMPLEMENTED_DEVICE,
296 * and return a pointer to its MemoryRegion.
298 UnimplementedDeviceState *uds = opaque;
300 object_initialize_child(OBJECT(mms), name, uds, TYPE_UNIMPLEMENTED_DEVICE);
301 qdev_prop_set_string(DEVICE(uds), "name", name);
302 qdev_prop_set_uint64(DEVICE(uds), "size", size);
303 sysbus_realize(SYS_BUS_DEVICE(uds), &error_fatal);
304 return sysbus_mmio_get_region(SYS_BUS_DEVICE(uds), 0);
307 static MemoryRegion *make_uart(MPS2TZMachineState *mms, void *opaque,
308 const char *name, hwaddr size,
309 const int *irqs)
311 /* The irq[] array is tx, rx, combined, in that order */
312 MPS2TZMachineClass *mmc = MPS2TZ_MACHINE_GET_CLASS(mms);
313 CMSDKAPBUART *uart = opaque;
314 int i = uart - &mms->uart[0];
315 SysBusDevice *s;
316 DeviceState *orgate_dev = DEVICE(&mms->uart_irq_orgate);
318 object_initialize_child(OBJECT(mms), name, uart, TYPE_CMSDK_APB_UART);
319 qdev_prop_set_chr(DEVICE(uart), "chardev", serial_hd(i));
320 qdev_prop_set_uint32(DEVICE(uart), "pclk-frq", mmc->sysclk_frq);
321 sysbus_realize(SYS_BUS_DEVICE(uart), &error_fatal);
322 s = SYS_BUS_DEVICE(uart);
323 sysbus_connect_irq(s, 0, get_sse_irq_in(mms, irqs[0]));
324 sysbus_connect_irq(s, 1, get_sse_irq_in(mms, irqs[1]));
325 sysbus_connect_irq(s, 2, qdev_get_gpio_in(orgate_dev, i * 2));
326 sysbus_connect_irq(s, 3, qdev_get_gpio_in(orgate_dev, i * 2 + 1));
327 sysbus_connect_irq(s, 4, get_sse_irq_in(mms, irqs[2]));
328 return sysbus_mmio_get_region(SYS_BUS_DEVICE(uart), 0);
331 static MemoryRegion *make_scc(MPS2TZMachineState *mms, void *opaque,
332 const char *name, hwaddr size,
333 const int *irqs)
335 MPS2SCC *scc = opaque;
336 DeviceState *sccdev;
337 MPS2TZMachineClass *mmc = MPS2TZ_MACHINE_GET_CLASS(mms);
338 uint32_t i;
340 object_initialize_child(OBJECT(mms), "scc", scc, TYPE_MPS2_SCC);
341 sccdev = DEVICE(scc);
342 qdev_prop_set_uint32(sccdev, "scc-cfg4", 0x2);
343 qdev_prop_set_uint32(sccdev, "scc-aid", 0x00200008);
344 qdev_prop_set_uint32(sccdev, "scc-id", mmc->scc_id);
345 qdev_prop_set_uint32(sccdev, "len-oscclk", mmc->len_oscclk);
346 for (i = 0; i < mmc->len_oscclk; i++) {
347 g_autofree char *propname = g_strdup_printf("oscclk[%u]", i);
348 qdev_prop_set_uint32(sccdev, propname, mmc->oscclk[i]);
350 sysbus_realize(SYS_BUS_DEVICE(scc), &error_fatal);
351 return sysbus_mmio_get_region(SYS_BUS_DEVICE(sccdev), 0);
354 static MemoryRegion *make_fpgaio(MPS2TZMachineState *mms, void *opaque,
355 const char *name, hwaddr size,
356 const int *irqs)
358 MPS2FPGAIO *fpgaio = opaque;
359 MPS2TZMachineClass *mmc = MPS2TZ_MACHINE_GET_CLASS(mms);
361 object_initialize_child(OBJECT(mms), "fpgaio", fpgaio, TYPE_MPS2_FPGAIO);
362 qdev_prop_set_uint32(DEVICE(fpgaio), "num-leds", mmc->fpgaio_num_leds);
363 qdev_prop_set_bit(DEVICE(fpgaio), "has-switches", mmc->fpgaio_has_switches);
364 sysbus_realize(SYS_BUS_DEVICE(fpgaio), &error_fatal);
365 return sysbus_mmio_get_region(SYS_BUS_DEVICE(fpgaio), 0);
368 static MemoryRegion *make_eth_dev(MPS2TZMachineState *mms, void *opaque,
369 const char *name, hwaddr size,
370 const int *irqs)
372 SysBusDevice *s;
373 NICInfo *nd = &nd_table[0];
375 /* In hardware this is a LAN9220; the LAN9118 is software compatible
376 * except that it doesn't support the checksum-offload feature.
378 qemu_check_nic_model(nd, "lan9118");
379 mms->lan9118 = qdev_new(TYPE_LAN9118);
380 qdev_set_nic_properties(mms->lan9118, nd);
382 s = SYS_BUS_DEVICE(mms->lan9118);
383 sysbus_realize_and_unref(s, &error_fatal);
384 sysbus_connect_irq(s, 0, get_sse_irq_in(mms, irqs[0]));
385 return sysbus_mmio_get_region(s, 0);
388 static MemoryRegion *make_mpc(MPS2TZMachineState *mms, void *opaque,
389 const char *name, hwaddr size,
390 const int *irqs)
392 TZMPC *mpc = opaque;
393 int i = mpc - &mms->mpc[0];
394 MemoryRegion *upstream;
395 const RAMInfo *raminfo = find_raminfo_for_mpc(mms, i);
396 MemoryRegion *ram = mr_for_raminfo(mms, raminfo);
398 object_initialize_child(OBJECT(mms), name, mpc, TYPE_TZ_MPC);
399 object_property_set_link(OBJECT(mpc), "downstream", OBJECT(ram),
400 &error_fatal);
401 sysbus_realize(SYS_BUS_DEVICE(mpc), &error_fatal);
402 /* Map the upstream end of the MPC into system memory */
403 upstream = sysbus_mmio_get_region(SYS_BUS_DEVICE(mpc), 1);
404 memory_region_add_subregion(get_system_memory(), raminfo->base, upstream);
405 /* and connect its interrupt to the IoTKit */
406 qdev_connect_gpio_out_named(DEVICE(mpc), "irq", 0,
407 qdev_get_gpio_in_named(DEVICE(&mms->iotkit),
408 "mpcexp_status", i));
410 /* Return the register interface MR for our caller to map behind the PPC */
411 return sysbus_mmio_get_region(SYS_BUS_DEVICE(mpc), 0);
414 static MemoryRegion *make_dma(MPS2TZMachineState *mms, void *opaque,
415 const char *name, hwaddr size,
416 const int *irqs)
418 /* The irq[] array is DMACINTR, DMACINTERR, DMACINTTC, in that order */
419 PL080State *dma = opaque;
420 int i = dma - &mms->dma[0];
421 SysBusDevice *s;
422 char *mscname = g_strdup_printf("%s-msc", name);
423 TZMSC *msc = &mms->msc[i];
424 DeviceState *iotkitdev = DEVICE(&mms->iotkit);
425 MemoryRegion *msc_upstream;
426 MemoryRegion *msc_downstream;
429 * Each DMA device is a PL081 whose transaction master interface
430 * is guarded by a Master Security Controller. The downstream end of
431 * the MSC connects to the IoTKit AHB Slave Expansion port, so the
432 * DMA devices can see all devices and memory that the CPU does.
434 object_initialize_child(OBJECT(mms), mscname, msc, TYPE_TZ_MSC);
435 msc_downstream = sysbus_mmio_get_region(SYS_BUS_DEVICE(&mms->iotkit), 0);
436 object_property_set_link(OBJECT(msc), "downstream",
437 OBJECT(msc_downstream), &error_fatal);
438 object_property_set_link(OBJECT(msc), "idau", OBJECT(mms), &error_fatal);
439 sysbus_realize(SYS_BUS_DEVICE(msc), &error_fatal);
441 qdev_connect_gpio_out_named(DEVICE(msc), "irq", 0,
442 qdev_get_gpio_in_named(iotkitdev,
443 "mscexp_status", i));
444 qdev_connect_gpio_out_named(iotkitdev, "mscexp_clear", i,
445 qdev_get_gpio_in_named(DEVICE(msc),
446 "irq_clear", 0));
447 qdev_connect_gpio_out_named(iotkitdev, "mscexp_ns", i,
448 qdev_get_gpio_in_named(DEVICE(msc),
449 "cfg_nonsec", 0));
450 qdev_connect_gpio_out(DEVICE(&mms->sec_resp_splitter),
451 ARRAY_SIZE(mms->ppc) + i,
452 qdev_get_gpio_in_named(DEVICE(msc),
453 "cfg_sec_resp", 0));
454 msc_upstream = sysbus_mmio_get_region(SYS_BUS_DEVICE(msc), 0);
456 object_initialize_child(OBJECT(mms), name, dma, TYPE_PL081);
457 object_property_set_link(OBJECT(dma), "downstream", OBJECT(msc_upstream),
458 &error_fatal);
459 sysbus_realize(SYS_BUS_DEVICE(dma), &error_fatal);
461 s = SYS_BUS_DEVICE(dma);
462 /* Wire up DMACINTR, DMACINTERR, DMACINTTC */
463 sysbus_connect_irq(s, 0, get_sse_irq_in(mms, irqs[0]));
464 sysbus_connect_irq(s, 1, get_sse_irq_in(mms, irqs[1]));
465 sysbus_connect_irq(s, 2, get_sse_irq_in(mms, irqs[2]));
467 g_free(mscname);
468 return sysbus_mmio_get_region(s, 0);
471 static MemoryRegion *make_spi(MPS2TZMachineState *mms, void *opaque,
472 const char *name, hwaddr size,
473 const int *irqs)
476 * The AN505 has five PL022 SPI controllers.
477 * One of these should have the LCD controller behind it; the others
478 * are connected only to the FPGA's "general purpose SPI connector"
479 * or "shield" expansion connectors.
480 * Note that if we do implement devices behind SPI, the chip select
481 * lines are set via the "MISC" register in the MPS2 FPGAIO device.
483 PL022State *spi = opaque;
484 SysBusDevice *s;
486 object_initialize_child(OBJECT(mms), name, spi, TYPE_PL022);
487 sysbus_realize(SYS_BUS_DEVICE(spi), &error_fatal);
488 s = SYS_BUS_DEVICE(spi);
489 sysbus_connect_irq(s, 0, get_sse_irq_in(mms, irqs[0]));
490 return sysbus_mmio_get_region(s, 0);
493 static MemoryRegion *make_i2c(MPS2TZMachineState *mms, void *opaque,
494 const char *name, hwaddr size,
495 const int *irqs)
497 ArmSbconI2CState *i2c = opaque;
498 SysBusDevice *s;
500 object_initialize_child(OBJECT(mms), name, i2c, TYPE_ARM_SBCON_I2C);
501 s = SYS_BUS_DEVICE(i2c);
502 sysbus_realize(s, &error_fatal);
503 return sysbus_mmio_get_region(s, 0);
506 static void create_non_mpc_ram(MPS2TZMachineState *mms)
509 * Handle the RAMs which are either not behind MPCs or which are
510 * aliases to another MPC.
512 const RAMInfo *p;
513 MPS2TZMachineClass *mmc = MPS2TZ_MACHINE_GET_CLASS(mms);
515 for (p = mmc->raminfo; p->name; p++) {
516 if (p->flags & IS_ALIAS) {
517 SysBusDevice *mpc_sbd = SYS_BUS_DEVICE(&mms->mpc[p->mpc]);
518 MemoryRegion *upstream = sysbus_mmio_get_region(mpc_sbd, 1);
519 make_ram_alias(&mms->ram[p->mrindex], p->name, upstream, p->base);
520 } else if (p->mpc == -1) {
521 /* RAM not behind an MPC */
522 MemoryRegion *mr = mr_for_raminfo(mms, p);
523 memory_region_add_subregion(get_system_memory(), p->base, mr);
528 static uint32_t boot_ram_size(MPS2TZMachineState *mms)
530 /* Return the size of the RAM block at guest address zero */
531 const RAMInfo *p;
532 MPS2TZMachineClass *mmc = MPS2TZ_MACHINE_GET_CLASS(mms);
534 for (p = mmc->raminfo; p->name; p++) {
535 if (p->base == 0) {
536 return p->size;
539 g_assert_not_reached();
542 static void mps2tz_common_init(MachineState *machine)
544 MPS2TZMachineState *mms = MPS2TZ_MACHINE(machine);
545 MPS2TZMachineClass *mmc = MPS2TZ_MACHINE_GET_CLASS(mms);
546 MachineClass *mc = MACHINE_GET_CLASS(machine);
547 MemoryRegion *system_memory = get_system_memory();
548 DeviceState *iotkitdev;
549 DeviceState *dev_splitter;
550 const PPCInfo *ppcs;
551 int num_ppcs;
552 int i;
554 if (strcmp(machine->cpu_type, mc->default_cpu_type) != 0) {
555 error_report("This board can only be used with CPU %s",
556 mc->default_cpu_type);
557 exit(1);
560 if (machine->ram_size != mc->default_ram_size) {
561 char *sz = size_to_str(mc->default_ram_size);
562 error_report("Invalid RAM size, should be %s", sz);
563 g_free(sz);
564 exit(EXIT_FAILURE);
567 /* These clocks don't need migration because they are fixed-frequency */
568 mms->sysclk = clock_new(OBJECT(machine), "SYSCLK");
569 clock_set_hz(mms->sysclk, mmc->sysclk_frq);
570 mms->s32kclk = clock_new(OBJECT(machine), "S32KCLK");
571 clock_set_hz(mms->s32kclk, S32KCLK_FRQ);
573 object_initialize_child(OBJECT(machine), TYPE_IOTKIT, &mms->iotkit,
574 mmc->armsse_type);
575 iotkitdev = DEVICE(&mms->iotkit);
576 object_property_set_link(OBJECT(&mms->iotkit), "memory",
577 OBJECT(system_memory), &error_abort);
578 qdev_prop_set_uint32(iotkitdev, "EXP_NUMIRQ", mmc->numirq);
579 qdev_connect_clock_in(iotkitdev, "MAINCLK", mms->sysclk);
580 qdev_connect_clock_in(iotkitdev, "S32KCLK", mms->s32kclk);
581 sysbus_realize(SYS_BUS_DEVICE(&mms->iotkit), &error_fatal);
584 * If this board has more than one CPU, then we need to create splitters
585 * to feed the IRQ inputs for each CPU in the SSE from each device in the
586 * board. If there is only one CPU, we can just wire the device IRQ
587 * directly to the SSE's IRQ input.
589 assert(mmc->numirq <= MPS2TZ_NUMIRQ_MAX);
590 if (mc->max_cpus > 1) {
591 for (i = 0; i < mmc->numirq; i++) {
592 char *name = g_strdup_printf("mps2-irq-splitter%d", i);
593 SplitIRQ *splitter = &mms->cpu_irq_splitter[i];
595 object_initialize_child_with_props(OBJECT(machine), name,
596 splitter, sizeof(*splitter),
597 TYPE_SPLIT_IRQ, &error_fatal,
598 NULL);
599 g_free(name);
601 object_property_set_int(OBJECT(splitter), "num-lines", 2,
602 &error_fatal);
603 qdev_realize(DEVICE(splitter), NULL, &error_fatal);
604 qdev_connect_gpio_out(DEVICE(splitter), 0,
605 qdev_get_gpio_in_named(DEVICE(&mms->iotkit),
606 "EXP_IRQ", i));
607 qdev_connect_gpio_out(DEVICE(splitter), 1,
608 qdev_get_gpio_in_named(DEVICE(&mms->iotkit),
609 "EXP_CPU1_IRQ", i));
613 /* The sec_resp_cfg output from the IoTKit must be split into multiple
614 * lines, one for each of the PPCs we create here, plus one per MSC.
616 object_initialize_child(OBJECT(machine), "sec-resp-splitter",
617 &mms->sec_resp_splitter, TYPE_SPLIT_IRQ);
618 object_property_set_int(OBJECT(&mms->sec_resp_splitter), "num-lines",
619 ARRAY_SIZE(mms->ppc) + ARRAY_SIZE(mms->msc),
620 &error_fatal);
621 qdev_realize(DEVICE(&mms->sec_resp_splitter), NULL, &error_fatal);
622 dev_splitter = DEVICE(&mms->sec_resp_splitter);
623 qdev_connect_gpio_out_named(iotkitdev, "sec_resp_cfg", 0,
624 qdev_get_gpio_in(dev_splitter, 0));
627 * The IoTKit sets up much of the memory layout, including
628 * the aliases between secure and non-secure regions in the
629 * address space, and also most of the devices in the system.
630 * The FPGA itself contains various RAMs and some additional devices.
631 * The FPGA images have an odd combination of different RAMs,
632 * because in hardware they are different implementations and
633 * connected to different buses, giving varying performance/size
634 * tradeoffs. For QEMU they're all just RAM, though. We arbitrarily
635 * call the largest lump our "system memory".
639 * The overflow IRQs for all UARTs are ORed together.
640 * Tx, Rx and "combined" IRQs are sent to the NVIC separately.
641 * Create the OR gate for this: it has one input for the TX overflow
642 * and one for the RX overflow for each UART we might have.
643 * (If the board has fewer than the maximum possible number of UARTs
644 * those inputs are never wired up and are treated as always-zero.)
646 object_initialize_child(OBJECT(mms), "uart-irq-orgate",
647 &mms->uart_irq_orgate, TYPE_OR_IRQ);
648 object_property_set_int(OBJECT(&mms->uart_irq_orgate), "num-lines",
649 2 * ARRAY_SIZE(mms->uart),
650 &error_fatal);
651 qdev_realize(DEVICE(&mms->uart_irq_orgate), NULL, &error_fatal);
652 qdev_connect_gpio_out(DEVICE(&mms->uart_irq_orgate), 0,
653 get_sse_irq_in(mms, 47));
655 /* Most of the devices in the FPGA are behind Peripheral Protection
656 * Controllers. The required order for initializing things is:
657 * + initialize the PPC
658 * + initialize, configure and realize downstream devices
659 * + connect downstream device MemoryRegions to the PPC
660 * + realize the PPC
661 * + map the PPC's MemoryRegions to the places in the address map
662 * where the downstream devices should appear
663 * + wire up the PPC's control lines to the IoTKit object
666 const PPCInfo an505_ppcs[] = { {
667 .name = "apb_ppcexp0",
668 .ports = {
669 { "ssram-0-mpc", make_mpc, &mms->mpc[0], 0x58007000, 0x1000 },
670 { "ssram-1-mpc", make_mpc, &mms->mpc[1], 0x58008000, 0x1000 },
671 { "ssram-2-mpc", make_mpc, &mms->mpc[2], 0x58009000, 0x1000 },
673 }, {
674 .name = "apb_ppcexp1",
675 .ports = {
676 { "spi0", make_spi, &mms->spi[0], 0x40205000, 0x1000, { 51 } },
677 { "spi1", make_spi, &mms->spi[1], 0x40206000, 0x1000, { 52 } },
678 { "spi2", make_spi, &mms->spi[2], 0x40209000, 0x1000, { 53 } },
679 { "spi3", make_spi, &mms->spi[3], 0x4020a000, 0x1000, { 54 } },
680 { "spi4", make_spi, &mms->spi[4], 0x4020b000, 0x1000, { 55 } },
681 { "uart0", make_uart, &mms->uart[0], 0x40200000, 0x1000, { 32, 33, 42 } },
682 { "uart1", make_uart, &mms->uart[1], 0x40201000, 0x1000, { 34, 35, 43 } },
683 { "uart2", make_uart, &mms->uart[2], 0x40202000, 0x1000, { 36, 37, 44 } },
684 { "uart3", make_uart, &mms->uart[3], 0x40203000, 0x1000, { 38, 39, 45 } },
685 { "uart4", make_uart, &mms->uart[4], 0x40204000, 0x1000, { 40, 41, 46 } },
686 { "i2c0", make_i2c, &mms->i2c[0], 0x40207000, 0x1000 },
687 { "i2c1", make_i2c, &mms->i2c[1], 0x40208000, 0x1000 },
688 { "i2c2", make_i2c, &mms->i2c[2], 0x4020c000, 0x1000 },
689 { "i2c3", make_i2c, &mms->i2c[3], 0x4020d000, 0x1000 },
691 }, {
692 .name = "apb_ppcexp2",
693 .ports = {
694 { "scc", make_scc, &mms->scc, 0x40300000, 0x1000 },
695 { "i2s-audio", make_unimp_dev, &mms->i2s_audio,
696 0x40301000, 0x1000 },
697 { "fpgaio", make_fpgaio, &mms->fpgaio, 0x40302000, 0x1000 },
699 }, {
700 .name = "ahb_ppcexp0",
701 .ports = {
702 { "gfx", make_unimp_dev, &mms->gfx, 0x41000000, 0x140000 },
703 { "gpio0", make_unimp_dev, &mms->gpio[0], 0x40100000, 0x1000 },
704 { "gpio1", make_unimp_dev, &mms->gpio[1], 0x40101000, 0x1000 },
705 { "gpio2", make_unimp_dev, &mms->gpio[2], 0x40102000, 0x1000 },
706 { "gpio3", make_unimp_dev, &mms->gpio[3], 0x40103000, 0x1000 },
707 { "eth", make_eth_dev, NULL, 0x42000000, 0x100000, { 48 } },
709 }, {
710 .name = "ahb_ppcexp1",
711 .ports = {
712 { "dma0", make_dma, &mms->dma[0], 0x40110000, 0x1000, { 58, 56, 57 } },
713 { "dma1", make_dma, &mms->dma[1], 0x40111000, 0x1000, { 61, 59, 60 } },
714 { "dma2", make_dma, &mms->dma[2], 0x40112000, 0x1000, { 64, 62, 63 } },
715 { "dma3", make_dma, &mms->dma[3], 0x40113000, 0x1000, { 67, 65, 66 } },
720 switch (mmc->fpga_type) {
721 case FPGA_AN505:
722 case FPGA_AN521:
723 ppcs = an505_ppcs;
724 num_ppcs = ARRAY_SIZE(an505_ppcs);
725 break;
726 default:
727 g_assert_not_reached();
730 for (i = 0; i < num_ppcs; i++) {
731 const PPCInfo *ppcinfo = &ppcs[i];
732 TZPPC *ppc = &mms->ppc[i];
733 DeviceState *ppcdev;
734 int port;
735 char *gpioname;
737 object_initialize_child(OBJECT(machine), ppcinfo->name, ppc,
738 TYPE_TZ_PPC);
739 ppcdev = DEVICE(ppc);
741 for (port = 0; port < TZ_NUM_PORTS; port++) {
742 const PPCPortInfo *pinfo = &ppcinfo->ports[port];
743 MemoryRegion *mr;
744 char *portname;
746 if (!pinfo->devfn) {
747 continue;
750 mr = pinfo->devfn(mms, pinfo->opaque, pinfo->name, pinfo->size,
751 pinfo->irqs);
752 portname = g_strdup_printf("port[%d]", port);
753 object_property_set_link(OBJECT(ppc), portname, OBJECT(mr),
754 &error_fatal);
755 g_free(portname);
758 sysbus_realize(SYS_BUS_DEVICE(ppc), &error_fatal);
760 for (port = 0; port < TZ_NUM_PORTS; port++) {
761 const PPCPortInfo *pinfo = &ppcinfo->ports[port];
763 if (!pinfo->devfn) {
764 continue;
766 sysbus_mmio_map(SYS_BUS_DEVICE(ppc), port, pinfo->addr);
768 gpioname = g_strdup_printf("%s_nonsec", ppcinfo->name);
769 qdev_connect_gpio_out_named(iotkitdev, gpioname, port,
770 qdev_get_gpio_in_named(ppcdev,
771 "cfg_nonsec",
772 port));
773 g_free(gpioname);
774 gpioname = g_strdup_printf("%s_ap", ppcinfo->name);
775 qdev_connect_gpio_out_named(iotkitdev, gpioname, port,
776 qdev_get_gpio_in_named(ppcdev,
777 "cfg_ap", port));
778 g_free(gpioname);
781 gpioname = g_strdup_printf("%s_irq_enable", ppcinfo->name);
782 qdev_connect_gpio_out_named(iotkitdev, gpioname, 0,
783 qdev_get_gpio_in_named(ppcdev,
784 "irq_enable", 0));
785 g_free(gpioname);
786 gpioname = g_strdup_printf("%s_irq_clear", ppcinfo->name);
787 qdev_connect_gpio_out_named(iotkitdev, gpioname, 0,
788 qdev_get_gpio_in_named(ppcdev,
789 "irq_clear", 0));
790 g_free(gpioname);
791 gpioname = g_strdup_printf("%s_irq_status", ppcinfo->name);
792 qdev_connect_gpio_out_named(ppcdev, "irq", 0,
793 qdev_get_gpio_in_named(iotkitdev,
794 gpioname, 0));
795 g_free(gpioname);
797 qdev_connect_gpio_out(dev_splitter, i,
798 qdev_get_gpio_in_named(ppcdev,
799 "cfg_sec_resp", 0));
802 create_unimplemented_device("FPGA NS PC", 0x48007000, 0x1000);
804 create_non_mpc_ram(mms);
806 armv7m_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename,
807 boot_ram_size(mms));
810 static void mps2_tz_idau_check(IDAUInterface *ii, uint32_t address,
811 int *iregion, bool *exempt, bool *ns, bool *nsc)
814 * The MPS2 TZ FPGA images have IDAUs in them which are connected to
815 * the Master Security Controllers. Thes have the same logic as
816 * is used by the IoTKit for the IDAU connected to the CPU, except
817 * that MSCs don't care about the NSC attribute.
819 int region = extract32(address, 28, 4);
821 *ns = !(region & 1);
822 *nsc = false;
823 /* 0xe0000000..0xe00fffff and 0xf0000000..0xf00fffff are exempt */
824 *exempt = (address & 0xeff00000) == 0xe0000000;
825 *iregion = region;
828 static void mps2tz_class_init(ObjectClass *oc, void *data)
830 MachineClass *mc = MACHINE_CLASS(oc);
831 IDAUInterfaceClass *iic = IDAU_INTERFACE_CLASS(oc);
833 mc->init = mps2tz_common_init;
834 iic->check = mps2_tz_idau_check;
837 static void mps2tz_set_default_ram_info(MPS2TZMachineClass *mmc)
840 * Set mc->default_ram_size and default_ram_id from the
841 * information in mmc->raminfo.
843 MachineClass *mc = MACHINE_CLASS(mmc);
844 const RAMInfo *p;
846 for (p = mmc->raminfo; p->name; p++) {
847 if (p->mrindex < 0) {
848 /* Found the entry for "system memory" */
849 mc->default_ram_size = p->size;
850 mc->default_ram_id = p->name;
851 return;
854 g_assert_not_reached();
857 static void mps2tz_an505_class_init(ObjectClass *oc, void *data)
859 MachineClass *mc = MACHINE_CLASS(oc);
860 MPS2TZMachineClass *mmc = MPS2TZ_MACHINE_CLASS(oc);
862 mc->desc = "ARM MPS2 with AN505 FPGA image for Cortex-M33";
863 mc->default_cpus = 1;
864 mc->min_cpus = mc->default_cpus;
865 mc->max_cpus = mc->default_cpus;
866 mmc->fpga_type = FPGA_AN505;
867 mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-m33");
868 mmc->scc_id = 0x41045050;
869 mmc->sysclk_frq = 20 * 1000 * 1000; /* 20MHz */
870 mmc->oscclk = an505_oscclk;
871 mmc->len_oscclk = ARRAY_SIZE(an505_oscclk);
872 mmc->fpgaio_num_leds = 2;
873 mmc->fpgaio_has_switches = false;
874 mmc->numirq = 92;
875 mmc->raminfo = an505_raminfo;
876 mmc->armsse_type = TYPE_IOTKIT;
877 mps2tz_set_default_ram_info(mmc);
880 static void mps2tz_an521_class_init(ObjectClass *oc, void *data)
882 MachineClass *mc = MACHINE_CLASS(oc);
883 MPS2TZMachineClass *mmc = MPS2TZ_MACHINE_CLASS(oc);
885 mc->desc = "ARM MPS2 with AN521 FPGA image for dual Cortex-M33";
886 mc->default_cpus = 2;
887 mc->min_cpus = mc->default_cpus;
888 mc->max_cpus = mc->default_cpus;
889 mmc->fpga_type = FPGA_AN521;
890 mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-m33");
891 mmc->scc_id = 0x41045210;
892 mmc->sysclk_frq = 20 * 1000 * 1000; /* 20MHz */
893 mmc->oscclk = an505_oscclk; /* AN521 is the same as AN505 here */
894 mmc->len_oscclk = ARRAY_SIZE(an505_oscclk);
895 mmc->fpgaio_num_leds = 2;
896 mmc->fpgaio_has_switches = false;
897 mmc->numirq = 92;
898 mmc->raminfo = an505_raminfo; /* AN521 is the same as AN505 here */
899 mmc->armsse_type = TYPE_SSE200;
900 mps2tz_set_default_ram_info(mmc);
903 static const TypeInfo mps2tz_info = {
904 .name = TYPE_MPS2TZ_MACHINE,
905 .parent = TYPE_MACHINE,
906 .abstract = true,
907 .instance_size = sizeof(MPS2TZMachineState),
908 .class_size = sizeof(MPS2TZMachineClass),
909 .class_init = mps2tz_class_init,
910 .interfaces = (InterfaceInfo[]) {
911 { TYPE_IDAU_INTERFACE },
916 static const TypeInfo mps2tz_an505_info = {
917 .name = TYPE_MPS2TZ_AN505_MACHINE,
918 .parent = TYPE_MPS2TZ_MACHINE,
919 .class_init = mps2tz_an505_class_init,
922 static const TypeInfo mps2tz_an521_info = {
923 .name = TYPE_MPS2TZ_AN521_MACHINE,
924 .parent = TYPE_MPS2TZ_MACHINE,
925 .class_init = mps2tz_an521_class_init,
928 static void mps2tz_machine_init(void)
930 type_register_static(&mps2tz_info);
931 type_register_static(&mps2tz_an505_info);
932 type_register_static(&mps2tz_an521_info);
935 type_init(mps2tz_machine_init);