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
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"
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
{
77 * Define the layout of RAM in a board, including which parts are
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
{
86 int mpc
; /* MPC number, -1 for "not behind an MPC" */
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
100 struct MPS2TZMachineClass
{
102 MPS2TZFPGAType fpga_type
;
104 uint32_t sysclk_frq
; /* Main SYSCLK frequency in Hz */
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
{
118 MemoryRegion ram
[MPS2TZ_RAM_MAX
];
124 ArmSbconI2CState i2c
[4];
125 UnimplementedDeviceState i2s_audio
;
126 UnimplementedDeviceState gpio
[4];
127 UnimplementedDeviceState gfx
;
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
];
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
[] = {
154 static const RAMInfo an505_raminfo
[] = { {
173 .name
= "ssram-0-alias",
180 /* Use the largest bit of contiguous RAM as our "system memory" */
191 static const RAMInfo
*find_raminfo_for_mpc(MPS2TZMachineState
*mms
, int mpc
)
193 MPS2TZMachineClass
*mmc
= MPS2TZ_MACHINE_GET_CLASS(mms
);
196 for (p
= mmc
->raminfo
; p
->name
; p
++) {
197 if (p
->mpc
== mpc
&& !(p
->flags
& IS_ALIAS
)) {
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. */
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
));
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);
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.
258 if (mc
->max_cpus
> 1) {
259 return qdev_get_gpio_in(DEVICE(&mms
->cpu_irq_splitter
[irqno
]), 0);
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
,
276 typedef struct PPCPortInfo
{
282 int irqs
[3]; /* currently no device needs more IRQ lines than this */
285 typedef struct PPCInfo
{
287 PPCPortInfo ports
[TZ_NUM_PORTS
];
290 static MemoryRegion
*make_unimp_dev(MPS2TZMachineState
*mms
,
292 const char *name
, hwaddr size
,
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
,
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];
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
,
335 MPS2SCC
*scc
= opaque
;
337 MPS2TZMachineClass
*mmc
= MPS2TZ_MACHINE_GET_CLASS(mms
);
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
,
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
,
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
,
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
),
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
,
418 /* The irq[] array is DMACINTR, DMACINTERR, DMACINTTC, in that order */
419 PL080State
*dma
= opaque
;
420 int i
= dma
- &mms
->dma
[0];
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
),
447 qdev_connect_gpio_out_named(iotkitdev
, "mscexp_ns", i
,
448 qdev_get_gpio_in_named(DEVICE(msc
),
450 qdev_connect_gpio_out(DEVICE(&mms
->sec_resp_splitter
),
451 ARRAY_SIZE(mms
->ppc
) + i
,
452 qdev_get_gpio_in_named(DEVICE(msc
),
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
),
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]));
468 return sysbus_mmio_get_region(s
, 0);
471 static MemoryRegion
*make_spi(MPS2TZMachineState
*mms
, void *opaque
,
472 const char *name
, hwaddr size
,
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
;
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
,
497 ArmSbconI2CState
*i2c
= opaque
;
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.
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 */
532 MPS2TZMachineClass
*mmc
= MPS2TZ_MACHINE_GET_CLASS(mms
);
534 for (p
= mmc
->raminfo
; p
->name
; p
++) {
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
;
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
);
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
);
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
,
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
,
601 object_property_set_int(OBJECT(splitter
), "num-lines", 2,
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
),
607 qdev_connect_gpio_out(DEVICE(splitter
), 1,
608 qdev_get_gpio_in_named(DEVICE(&mms
->iotkit
),
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
),
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
),
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
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",
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 },
674 .name
= "apb_ppcexp1",
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 },
692 .name
= "apb_ppcexp2",
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 },
700 .name
= "ahb_ppcexp0",
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 } },
710 .name
= "ahb_ppcexp1",
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
) {
724 num_ppcs
= ARRAY_SIZE(an505_ppcs
);
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
];
737 object_initialize_child(OBJECT(machine
), ppcinfo
->name
, ppc
,
739 ppcdev
= DEVICE(ppc
);
741 for (port
= 0; port
< TZ_NUM_PORTS
; port
++) {
742 const PPCPortInfo
*pinfo
= &ppcinfo
->ports
[port
];
750 mr
= pinfo
->devfn(mms
, pinfo
->opaque
, pinfo
->name
, pinfo
->size
,
752 portname
= g_strdup_printf("port[%d]", port
);
753 object_property_set_link(OBJECT(ppc
), portname
, OBJECT(mr
),
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
];
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
,
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
,
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
,
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
,
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
,
797 qdev_connect_gpio_out(dev_splitter
, i
,
798 qdev_get_gpio_in_named(ppcdev
,
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
,
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);
823 /* 0xe0000000..0xe00fffff and 0xf0000000..0xf00fffff are exempt */
824 *exempt
= (address
& 0xeff00000) == 0xe0000000;
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
);
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
;
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;
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;
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
,
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
);