2 * Xilinx Zynq MPSoC emulation
4 * Copyright (C) 2015 Xilinx Inc
5 * Written by Peter Crosthwaite <peter.crosthwaite@xilinx.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 #include "qemu/osdep.h"
19 #include "qapi/error.h"
20 #include "qemu-common.h"
22 #include "hw/arm/xlnx-zynqmp.h"
23 #include "hw/intc/arm_gic_common.h"
24 #include "exec/address-spaces.h"
25 #include "sysemu/kvm.h"
28 #define GIC_NUM_SPI_INTR 160
30 #define ARM_PHYS_TIMER_PPI 30
31 #define ARM_VIRT_TIMER_PPI 27
33 #define GEM_REVISION 0x40070106
35 #define GIC_BASE_ADDR 0xf9000000
36 #define GIC_DIST_ADDR 0xf9010000
37 #define GIC_CPU_ADDR 0xf9020000
40 #define SATA_ADDR 0xFD0C0000
41 #define SATA_NUM_PORTS 2
43 #define DP_ADDR 0xfd4a0000
46 #define DPDMA_ADDR 0xfd4c0000
49 static const uint64_t gem_addr
[XLNX_ZYNQMP_NUM_GEMS
] = {
50 0xFF0B0000, 0xFF0C0000, 0xFF0D0000, 0xFF0E0000,
53 static const int gem_intr
[XLNX_ZYNQMP_NUM_GEMS
] = {
57 static const uint64_t uart_addr
[XLNX_ZYNQMP_NUM_UARTS
] = {
58 0xFF000000, 0xFF010000,
61 static const int uart_intr
[XLNX_ZYNQMP_NUM_UARTS
] = {
65 static const uint64_t sdhci_addr
[XLNX_ZYNQMP_NUM_SDHCI
] = {
66 0xFF160000, 0xFF170000,
69 static const int sdhci_intr
[XLNX_ZYNQMP_NUM_SDHCI
] = {
73 static const uint64_t spi_addr
[XLNX_ZYNQMP_NUM_SPIS
] = {
74 0xFF040000, 0xFF050000,
77 static const int spi_intr
[XLNX_ZYNQMP_NUM_SPIS
] = {
81 typedef struct XlnxZynqMPGICRegion
{
84 } XlnxZynqMPGICRegion
;
86 static const XlnxZynqMPGICRegion xlnx_zynqmp_gic_regions
[] = {
87 { .region_index
= 0, .address
= GIC_DIST_ADDR
, },
88 { .region_index
= 1, .address
= GIC_CPU_ADDR
, },
91 static inline int arm_gic_ppi_index(int cpu_nr
, int ppi_index
)
93 return GIC_NUM_SPI_INTR
+ cpu_nr
* GIC_INTERNAL
+ ppi_index
;
96 static void xlnx_zynqmp_create_rpu(XlnxZynqMPState
*s
, const char *boot_cpu
,
102 for (i
= 0; i
< XLNX_ZYNQMP_NUM_RPU_CPUS
; i
++) {
105 object_initialize(&s
->rpu_cpu
[i
], sizeof(s
->rpu_cpu
[i
]),
106 "cortex-r5-" TYPE_ARM_CPU
);
107 object_property_add_child(OBJECT(s
), "rpu-cpu[*]",
108 OBJECT(&s
->rpu_cpu
[i
]), &error_abort
);
110 name
= object_get_canonical_path_component(OBJECT(&s
->rpu_cpu
[i
]));
111 if (strcmp(name
, boot_cpu
)) {
112 /* Secondary CPUs start in PSCI powered-down state */
113 object_property_set_bool(OBJECT(&s
->rpu_cpu
[i
]), true,
114 "start-powered-off", &error_abort
);
116 s
->boot_cpu_ptr
= &s
->rpu_cpu
[i
];
120 object_property_set_bool(OBJECT(&s
->rpu_cpu
[i
]), true, "reset-hivecs",
122 object_property_set_bool(OBJECT(&s
->rpu_cpu
[i
]), true, "realized",
125 error_propagate(errp
, err
);
131 static void xlnx_zynqmp_init(Object
*obj
)
133 XlnxZynqMPState
*s
= XLNX_ZYNQMP(obj
);
136 for (i
= 0; i
< XLNX_ZYNQMP_NUM_APU_CPUS
; i
++) {
137 object_initialize(&s
->apu_cpu
[i
], sizeof(s
->apu_cpu
[i
]),
138 "cortex-a53-" TYPE_ARM_CPU
);
139 object_property_add_child(obj
, "apu-cpu[*]", OBJECT(&s
->apu_cpu
[i
]),
143 object_initialize(&s
->gic
, sizeof(s
->gic
), gic_class_name());
144 qdev_set_parent_bus(DEVICE(&s
->gic
), sysbus_get_default());
146 for (i
= 0; i
< XLNX_ZYNQMP_NUM_GEMS
; i
++) {
147 object_initialize(&s
->gem
[i
], sizeof(s
->gem
[i
]), TYPE_CADENCE_GEM
);
148 qdev_set_parent_bus(DEVICE(&s
->gem
[i
]), sysbus_get_default());
151 for (i
= 0; i
< XLNX_ZYNQMP_NUM_UARTS
; i
++) {
152 object_initialize(&s
->uart
[i
], sizeof(s
->uart
[i
]), TYPE_CADENCE_UART
);
153 qdev_set_parent_bus(DEVICE(&s
->uart
[i
]), sysbus_get_default());
156 object_initialize(&s
->sata
, sizeof(s
->sata
), TYPE_SYSBUS_AHCI
);
157 qdev_set_parent_bus(DEVICE(&s
->sata
), sysbus_get_default());
159 for (i
= 0; i
< XLNX_ZYNQMP_NUM_SDHCI
; i
++) {
160 object_initialize(&s
->sdhci
[i
], sizeof(s
->sdhci
[i
]),
162 qdev_set_parent_bus(DEVICE(&s
->sdhci
[i
]),
163 sysbus_get_default());
166 for (i
= 0; i
< XLNX_ZYNQMP_NUM_SPIS
; i
++) {
167 object_initialize(&s
->spi
[i
], sizeof(s
->spi
[i
]),
169 qdev_set_parent_bus(DEVICE(&s
->spi
[i
]), sysbus_get_default());
172 object_initialize(&s
->dp
, sizeof(s
->dp
), TYPE_XLNX_DP
);
173 qdev_set_parent_bus(DEVICE(&s
->dp
), sysbus_get_default());
175 object_initialize(&s
->dpdma
, sizeof(s
->dpdma
), TYPE_XLNX_DPDMA
);
176 qdev_set_parent_bus(DEVICE(&s
->dpdma
), sysbus_get_default());
179 static void xlnx_zynqmp_realize(DeviceState
*dev
, Error
**errp
)
181 XlnxZynqMPState
*s
= XLNX_ZYNQMP(dev
);
182 MemoryRegion
*system_memory
= get_system_memory();
185 const char *boot_cpu
= s
->boot_cpu
? s
->boot_cpu
: "apu-cpu[0]";
186 ram_addr_t ddr_low_size
, ddr_high_size
;
187 qemu_irq gic_spi
[GIC_NUM_SPI_INTR
];
190 ram_size
= memory_region_size(s
->ddr_ram
);
192 /* Create the DDR Memory Regions. User friendly checks should happen at
195 if (ram_size
> XLNX_ZYNQMP_MAX_LOW_RAM_SIZE
) {
196 /* The RAM size is above the maximum available for the low DDR.
197 * Create the high DDR memory region as well.
199 assert(ram_size
<= XLNX_ZYNQMP_MAX_RAM_SIZE
);
200 ddr_low_size
= XLNX_ZYNQMP_MAX_LOW_RAM_SIZE
;
201 ddr_high_size
= ram_size
- XLNX_ZYNQMP_MAX_LOW_RAM_SIZE
;
203 memory_region_init_alias(&s
->ddr_ram_high
, NULL
,
204 "ddr-ram-high", s
->ddr_ram
,
205 ddr_low_size
, ddr_high_size
);
206 memory_region_add_subregion(get_system_memory(),
207 XLNX_ZYNQMP_HIGH_RAM_START
,
210 /* RAM must be non-zero */
212 ddr_low_size
= ram_size
;
215 memory_region_init_alias(&s
->ddr_ram_low
, NULL
,
216 "ddr-ram-low", s
->ddr_ram
,
218 memory_region_add_subregion(get_system_memory(), 0, &s
->ddr_ram_low
);
220 /* Create the four OCM banks */
221 for (i
= 0; i
< XLNX_ZYNQMP_NUM_OCM_BANKS
; i
++) {
222 char *ocm_name
= g_strdup_printf("zynqmp.ocm_ram_bank_%d", i
);
224 memory_region_init_ram(&s
->ocm_ram
[i
], NULL
, ocm_name
,
225 XLNX_ZYNQMP_OCM_RAM_SIZE
, &error_fatal
);
226 memory_region_add_subregion(get_system_memory(),
227 XLNX_ZYNQMP_OCM_RAM_0_ADDRESS
+
228 i
* XLNX_ZYNQMP_OCM_RAM_SIZE
,
234 qdev_prop_set_uint32(DEVICE(&s
->gic
), "num-irq", GIC_NUM_SPI_INTR
+ 32);
235 qdev_prop_set_uint32(DEVICE(&s
->gic
), "revision", 2);
236 qdev_prop_set_uint32(DEVICE(&s
->gic
), "num-cpu", XLNX_ZYNQMP_NUM_APU_CPUS
);
238 /* Realize APUs before realizing the GIC. KVM requires this. */
239 for (i
= 0; i
< XLNX_ZYNQMP_NUM_APU_CPUS
; i
++) {
242 object_property_set_int(OBJECT(&s
->apu_cpu
[i
]), QEMU_PSCI_CONDUIT_SMC
,
243 "psci-conduit", &error_abort
);
245 name
= object_get_canonical_path_component(OBJECT(&s
->apu_cpu
[i
]));
246 if (strcmp(name
, boot_cpu
)) {
247 /* Secondary CPUs start in PSCI powered-down state */
248 object_property_set_bool(OBJECT(&s
->apu_cpu
[i
]), true,
249 "start-powered-off", &error_abort
);
251 s
->boot_cpu_ptr
= &s
->apu_cpu
[i
];
255 object_property_set_bool(OBJECT(&s
->apu_cpu
[i
]),
256 s
->secure
, "has_el3", NULL
);
257 object_property_set_bool(OBJECT(&s
->apu_cpu
[i
]),
258 s
->virt
, "has_el2", NULL
);
259 object_property_set_int(OBJECT(&s
->apu_cpu
[i
]), GIC_BASE_ADDR
,
260 "reset-cbar", &error_abort
);
261 object_property_set_bool(OBJECT(&s
->apu_cpu
[i
]), true, "realized",
264 error_propagate(errp
, err
);
269 object_property_set_bool(OBJECT(&s
->gic
), true, "realized", &err
);
271 error_propagate(errp
, err
);
275 assert(ARRAY_SIZE(xlnx_zynqmp_gic_regions
) == XLNX_ZYNQMP_GIC_REGIONS
);
276 for (i
= 0; i
< XLNX_ZYNQMP_GIC_REGIONS
; i
++) {
277 SysBusDevice
*gic
= SYS_BUS_DEVICE(&s
->gic
);
278 const XlnxZynqMPGICRegion
*r
= &xlnx_zynqmp_gic_regions
[i
];
279 MemoryRegion
*mr
= sysbus_mmio_get_region(gic
, r
->region_index
);
280 uint32_t addr
= r
->address
;
283 sysbus_mmio_map(gic
, r
->region_index
, addr
);
285 for (j
= 0; j
< XLNX_ZYNQMP_GIC_ALIASES
; j
++) {
286 MemoryRegion
*alias
= &s
->gic_mr
[i
][j
];
288 addr
+= XLNX_ZYNQMP_GIC_REGION_SIZE
;
289 memory_region_init_alias(alias
, OBJECT(s
), "zynqmp-gic-alias", mr
,
290 0, XLNX_ZYNQMP_GIC_REGION_SIZE
);
291 memory_region_add_subregion(system_memory
, addr
, alias
);
295 for (i
= 0; i
< XLNX_ZYNQMP_NUM_APU_CPUS
; i
++) {
298 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->gic
), i
,
299 qdev_get_gpio_in(DEVICE(&s
->apu_cpu
[i
]),
301 irq
= qdev_get_gpio_in(DEVICE(&s
->gic
),
302 arm_gic_ppi_index(i
, ARM_PHYS_TIMER_PPI
));
303 qdev_connect_gpio_out(DEVICE(&s
->apu_cpu
[i
]), 0, irq
);
304 irq
= qdev_get_gpio_in(DEVICE(&s
->gic
),
305 arm_gic_ppi_index(i
, ARM_VIRT_TIMER_PPI
));
306 qdev_connect_gpio_out(DEVICE(&s
->apu_cpu
[i
]), 1, irq
);
310 xlnx_zynqmp_create_rpu(s
, boot_cpu
, &err
);
312 error_propagate(errp
, err
);
317 if (!s
->boot_cpu_ptr
) {
318 error_setg(errp
, "ZynqMP Boot cpu %s not found", boot_cpu
);
322 for (i
= 0; i
< GIC_NUM_SPI_INTR
; i
++) {
323 gic_spi
[i
] = qdev_get_gpio_in(DEVICE(&s
->gic
), i
);
326 for (i
= 0; i
< XLNX_ZYNQMP_NUM_GEMS
; i
++) {
327 NICInfo
*nd
= &nd_table
[i
];
330 qemu_check_nic_model(nd
, TYPE_CADENCE_GEM
);
331 qdev_set_nic_properties(DEVICE(&s
->gem
[i
]), nd
);
333 object_property_set_int(OBJECT(&s
->gem
[i
]), GEM_REVISION
, "revision",
335 object_property_set_int(OBJECT(&s
->gem
[i
]), 2, "num-priority-queues",
337 object_property_set_bool(OBJECT(&s
->gem
[i
]), true, "realized", &err
);
339 error_propagate(errp
, err
);
342 sysbus_mmio_map(SYS_BUS_DEVICE(&s
->gem
[i
]), 0, gem_addr
[i
]);
343 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->gem
[i
]), 0,
344 gic_spi
[gem_intr
[i
]]);
347 for (i
= 0; i
< XLNX_ZYNQMP_NUM_UARTS
; i
++) {
348 qdev_prop_set_chr(DEVICE(&s
->uart
[i
]), "chardev", serial_hds
[i
]);
349 object_property_set_bool(OBJECT(&s
->uart
[i
]), true, "realized", &err
);
351 error_propagate(errp
, err
);
354 sysbus_mmio_map(SYS_BUS_DEVICE(&s
->uart
[i
]), 0, uart_addr
[i
]);
355 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->uart
[i
]), 0,
356 gic_spi
[uart_intr
[i
]]);
359 object_property_set_int(OBJECT(&s
->sata
), SATA_NUM_PORTS
, "num-ports",
361 object_property_set_bool(OBJECT(&s
->sata
), true, "realized", &err
);
363 error_propagate(errp
, err
);
367 sysbus_mmio_map(SYS_BUS_DEVICE(&s
->sata
), 0, SATA_ADDR
);
368 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->sata
), 0, gic_spi
[SATA_INTR
]);
370 for (i
= 0; i
< XLNX_ZYNQMP_NUM_SDHCI
; i
++) {
373 object_property_set_bool(OBJECT(&s
->sdhci
[i
]), true,
376 error_propagate(errp
, err
);
379 sysbus_mmio_map(SYS_BUS_DEVICE(&s
->sdhci
[i
]), 0,
381 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->sdhci
[i
]), 0,
382 gic_spi
[sdhci_intr
[i
]]);
383 /* Alias controller SD bus to the SoC itself */
384 bus_name
= g_strdup_printf("sd-bus%d", i
);
385 object_property_add_alias(OBJECT(s
), bus_name
,
386 OBJECT(&s
->sdhci
[i
]), "sd-bus",
391 for (i
= 0; i
< XLNX_ZYNQMP_NUM_SPIS
; i
++) {
394 object_property_set_bool(OBJECT(&s
->spi
[i
]), true, "realized", &err
);
396 sysbus_mmio_map(SYS_BUS_DEVICE(&s
->spi
[i
]), 0, spi_addr
[i
]);
397 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->spi
[i
]), 0,
398 gic_spi
[spi_intr
[i
]]);
400 /* Alias controller SPI bus to the SoC itself */
401 bus_name
= g_strdup_printf("spi%d", i
);
402 object_property_add_alias(OBJECT(s
), bus_name
,
403 OBJECT(&s
->spi
[i
]), "spi0",
408 object_property_set_bool(OBJECT(&s
->dp
), true, "realized", &err
);
410 error_propagate(errp
, err
);
413 sysbus_mmio_map(SYS_BUS_DEVICE(&s
->dp
), 0, DP_ADDR
);
414 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->dp
), 0, gic_spi
[DP_IRQ
]);
416 object_property_set_bool(OBJECT(&s
->dpdma
), true, "realized", &err
);
418 error_propagate(errp
, err
);
421 object_property_set_link(OBJECT(&s
->dp
), OBJECT(&s
->dpdma
), "dpdma",
423 sysbus_mmio_map(SYS_BUS_DEVICE(&s
->dpdma
), 0, DPDMA_ADDR
);
424 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->dpdma
), 0, gic_spi
[DPDMA_IRQ
]);
427 static Property xlnx_zynqmp_props
[] = {
428 DEFINE_PROP_STRING("boot-cpu", XlnxZynqMPState
, boot_cpu
),
429 DEFINE_PROP_BOOL("secure", XlnxZynqMPState
, secure
, false),
430 DEFINE_PROP_BOOL("virtualization", XlnxZynqMPState
, virt
, false),
431 DEFINE_PROP_BOOL("has_rpu", XlnxZynqMPState
, has_rpu
, false),
432 DEFINE_PROP_LINK("ddr-ram", XlnxZynqMPState
, ddr_ram
, TYPE_MEMORY_REGION
,
434 DEFINE_PROP_END_OF_LIST()
437 static void xlnx_zynqmp_class_init(ObjectClass
*oc
, void *data
)
439 DeviceClass
*dc
= DEVICE_CLASS(oc
);
441 dc
->props
= xlnx_zynqmp_props
;
442 dc
->realize
= xlnx_zynqmp_realize
;
445 static const TypeInfo xlnx_zynqmp_type_info
= {
446 .name
= TYPE_XLNX_ZYNQMP
,
447 .parent
= TYPE_DEVICE
,
448 .instance_size
= sizeof(XlnxZynqMPState
),
449 .instance_init
= xlnx_zynqmp_init
,
450 .class_init
= xlnx_zynqmp_class_init
,
453 static void xlnx_zynqmp_register_types(void)
455 type_register_static(&xlnx_zynqmp_type_info
);
458 type_init(xlnx_zynqmp_register_types
)