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 "hw/arm/xlnx-zynqmp.h"
19 #include "hw/intc/arm_gic_common.h"
20 #include "exec/address-spaces.h"
22 #define GIC_NUM_SPI_INTR 160
24 #define ARM_PHYS_TIMER_PPI 30
25 #define ARM_VIRT_TIMER_PPI 27
27 #define GIC_BASE_ADDR 0xf9000000
28 #define GIC_DIST_ADDR 0xf9010000
29 #define GIC_CPU_ADDR 0xf9020000
31 static const uint64_t gem_addr
[XLNX_ZYNQMP_NUM_GEMS
] = {
32 0xFF0B0000, 0xFF0C0000, 0xFF0D0000, 0xFF0E0000,
35 static const int gem_intr
[XLNX_ZYNQMP_NUM_GEMS
] = {
39 static const uint64_t uart_addr
[XLNX_ZYNQMP_NUM_UARTS
] = {
40 0xFF000000, 0xFF010000,
43 static const int uart_intr
[XLNX_ZYNQMP_NUM_UARTS
] = {
47 typedef struct XlnxZynqMPGICRegion
{
50 } XlnxZynqMPGICRegion
;
52 static const XlnxZynqMPGICRegion xlnx_zynqmp_gic_regions
[] = {
53 { .region_index
= 0, .address
= GIC_DIST_ADDR
, },
54 { .region_index
= 1, .address
= GIC_CPU_ADDR
, },
57 static inline int arm_gic_ppi_index(int cpu_nr
, int ppi_index
)
59 return GIC_NUM_SPI_INTR
+ cpu_nr
* GIC_INTERNAL
+ ppi_index
;
62 static void xlnx_zynqmp_init(Object
*obj
)
64 XlnxZynqMPState
*s
= XLNX_ZYNQMP(obj
);
67 for (i
= 0; i
< XLNX_ZYNQMP_NUM_APU_CPUS
; i
++) {
68 object_initialize(&s
->apu_cpu
[i
], sizeof(s
->apu_cpu
[i
]),
69 "cortex-a53-" TYPE_ARM_CPU
);
70 object_property_add_child(obj
, "apu-cpu[*]", OBJECT(&s
->apu_cpu
[i
]),
74 object_initialize(&s
->gic
, sizeof(s
->gic
), TYPE_ARM_GIC
);
75 qdev_set_parent_bus(DEVICE(&s
->gic
), sysbus_get_default());
77 for (i
= 0; i
< XLNX_ZYNQMP_NUM_GEMS
; i
++) {
78 object_initialize(&s
->gem
[i
], sizeof(s
->gem
[i
]), TYPE_CADENCE_GEM
);
79 qdev_set_parent_bus(DEVICE(&s
->gem
[i
]), sysbus_get_default());
82 for (i
= 0; i
< XLNX_ZYNQMP_NUM_UARTS
; i
++) {
83 object_initialize(&s
->uart
[i
], sizeof(s
->uart
[i
]), TYPE_CADENCE_UART
);
84 qdev_set_parent_bus(DEVICE(&s
->uart
[i
]), sysbus_get_default());
88 static void xlnx_zynqmp_realize(DeviceState
*dev
, Error
**errp
)
90 XlnxZynqMPState
*s
= XLNX_ZYNQMP(dev
);
91 MemoryRegion
*system_memory
= get_system_memory();
93 const char *boot_cpu
= s
->boot_cpu
? s
->boot_cpu
: "apu-cpu[0]";
94 qemu_irq gic_spi
[GIC_NUM_SPI_INTR
];
97 qdev_prop_set_uint32(DEVICE(&s
->gic
), "num-irq", GIC_NUM_SPI_INTR
+ 32);
98 qdev_prop_set_uint32(DEVICE(&s
->gic
), "revision", 2);
99 qdev_prop_set_uint32(DEVICE(&s
->gic
), "num-cpu", XLNX_ZYNQMP_NUM_APU_CPUS
);
100 object_property_set_bool(OBJECT(&s
->gic
), true, "realized", &err
);
102 error_propagate((errp
), (err
));
105 assert(ARRAY_SIZE(xlnx_zynqmp_gic_regions
) == XLNX_ZYNQMP_GIC_REGIONS
);
106 for (i
= 0; i
< XLNX_ZYNQMP_GIC_REGIONS
; i
++) {
107 SysBusDevice
*gic
= SYS_BUS_DEVICE(&s
->gic
);
108 const XlnxZynqMPGICRegion
*r
= &xlnx_zynqmp_gic_regions
[i
];
109 MemoryRegion
*mr
= sysbus_mmio_get_region(gic
, r
->region_index
);
110 uint32_t addr
= r
->address
;
113 sysbus_mmio_map(gic
, r
->region_index
, addr
);
115 for (j
= 0; j
< XLNX_ZYNQMP_GIC_ALIASES
; j
++) {
116 MemoryRegion
*alias
= &s
->gic_mr
[i
][j
];
118 addr
+= XLNX_ZYNQMP_GIC_REGION_SIZE
;
119 memory_region_init_alias(alias
, OBJECT(s
), "zynqmp-gic-alias", mr
,
120 0, XLNX_ZYNQMP_GIC_REGION_SIZE
);
121 memory_region_add_subregion(system_memory
, addr
, alias
);
125 for (i
= 0; i
< XLNX_ZYNQMP_NUM_APU_CPUS
; i
++) {
129 object_property_set_int(OBJECT(&s
->apu_cpu
[i
]), QEMU_PSCI_CONDUIT_SMC
,
130 "psci-conduit", &error_abort
);
132 name
= object_get_canonical_path_component(OBJECT(&s
->apu_cpu
[i
]));
133 if (strcmp(name
, boot_cpu
)) {
134 /* Secondary CPUs start in PSCI powered-down state */
135 object_property_set_bool(OBJECT(&s
->apu_cpu
[i
]), true,
136 "start-powered-off", &error_abort
);
138 s
->boot_cpu_ptr
= &s
->apu_cpu
[i
];
141 object_property_set_int(OBJECT(&s
->apu_cpu
[i
]), GIC_BASE_ADDR
,
144 error_propagate((errp
), (err
));
148 object_property_set_bool(OBJECT(&s
->apu_cpu
[i
]), true, "realized",
151 error_propagate((errp
), (err
));
155 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->gic
), i
,
156 qdev_get_gpio_in(DEVICE(&s
->apu_cpu
[i
]),
158 irq
= qdev_get_gpio_in(DEVICE(&s
->gic
),
159 arm_gic_ppi_index(i
, ARM_PHYS_TIMER_PPI
));
160 qdev_connect_gpio_out(DEVICE(&s
->apu_cpu
[i
]), 0, irq
);
161 irq
= qdev_get_gpio_in(DEVICE(&s
->gic
),
162 arm_gic_ppi_index(i
, ARM_VIRT_TIMER_PPI
));
163 qdev_connect_gpio_out(DEVICE(&s
->apu_cpu
[i
]), 1, irq
);
166 if (!s
->boot_cpu_ptr
) {
167 error_setg(errp
, "ZynqMP Boot cpu %s not found\n", boot_cpu
);
171 for (i
= 0; i
< GIC_NUM_SPI_INTR
; i
++) {
172 gic_spi
[i
] = qdev_get_gpio_in(DEVICE(&s
->gic
), i
);
175 for (i
= 0; i
< XLNX_ZYNQMP_NUM_GEMS
; i
++) {
176 NICInfo
*nd
= &nd_table
[i
];
179 qemu_check_nic_model(nd
, TYPE_CADENCE_GEM
);
180 qdev_set_nic_properties(DEVICE(&s
->gem
[i
]), nd
);
182 object_property_set_bool(OBJECT(&s
->gem
[i
]), true, "realized", &err
);
184 error_propagate((errp
), (err
));
187 sysbus_mmio_map(SYS_BUS_DEVICE(&s
->gem
[i
]), 0, gem_addr
[i
]);
188 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->gem
[i
]), 0,
189 gic_spi
[gem_intr
[i
]]);
192 for (i
= 0; i
< XLNX_ZYNQMP_NUM_UARTS
; i
++) {
193 object_property_set_bool(OBJECT(&s
->uart
[i
]), true, "realized", &err
);
195 error_propagate((errp
), (err
));
198 sysbus_mmio_map(SYS_BUS_DEVICE(&s
->uart
[i
]), 0, uart_addr
[i
]);
199 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->uart
[i
]), 0,
200 gic_spi
[uart_intr
[i
]]);
204 static Property xlnx_zynqmp_props
[] = {
205 DEFINE_PROP_STRING("boot-cpu", XlnxZynqMPState
, boot_cpu
),
206 DEFINE_PROP_END_OF_LIST()
209 static void xlnx_zynqmp_class_init(ObjectClass
*oc
, void *data
)
211 DeviceClass
*dc
= DEVICE_CLASS(oc
);
213 dc
->props
= xlnx_zynqmp_props
;
214 dc
->realize
= xlnx_zynqmp_realize
;
217 static const TypeInfo xlnx_zynqmp_type_info
= {
218 .name
= TYPE_XLNX_ZYNQMP
,
219 .parent
= TYPE_DEVICE
,
220 .instance_size
= sizeof(XlnxZynqMPState
),
221 .instance_init
= xlnx_zynqmp_init
,
222 .class_init
= xlnx_zynqmp_class_init
,
225 static void xlnx_zynqmp_register_types(void)
227 type_register_static(&xlnx_zynqmp_type_info
);
230 type_init(xlnx_zynqmp_register_types
)