1 /* 2 * QEMU Xen PVH machine - common code. 3 * 4 * Copyright (c) 2024 Advanced Micro Devices, Inc. 5 * 6 * SPDX-License-Identifier: GPL-2.0-or-later 7 */ 8 9 #include "qemu/osdep.h" 10 #include "qemu/error-report.h" 11 #include "qapi/error.h" 12 #include "qapi/visitor.h" 13 #include "hw/boards.h" 14 #include "hw/irq.h" 15 #include "hw/sysbus.h" 16 #include "sysemu/sysemu.h" 17 #include "sysemu/tpm.h" 18 #include "sysemu/tpm_backend.h" 19 #include "hw/xen/xen-pvh-common.h" 20 #include "trace.h" 21 22 static const MemoryListener xen_memory_listener = { 23 .region_add = xen_region_add, 24 .region_del = xen_region_del, 25 .log_start = NULL, 26 .log_stop = NULL, 27 .log_sync = NULL, 28 .log_global_start = NULL, 29 .log_global_stop = NULL, 30 .priority = MEMORY_LISTENER_PRIORITY_ACCEL, 31 }; 32 33 static void xen_pvh_init_ram(XenPVHMachineState *s, 34 MemoryRegion *sysmem) 35 { 36 MachineState *ms = MACHINE(s); 37 ram_addr_t block_len, ram_size[2]; 38 39 if (ms->ram_size <= s->cfg.ram_low.size) { 40 ram_size[0] = ms->ram_size; 41 ram_size[1] = 0; 42 block_len = s->cfg.ram_low.base + ram_size[0]; 43 } else { 44 ram_size[0] = s->cfg.ram_low.size; 45 ram_size[1] = ms->ram_size - s->cfg.ram_low.size; 46 block_len = s->cfg.ram_high.base + ram_size[1]; 47 } 48 49 memory_region_init_ram(&xen_memory, NULL, "xen.ram", block_len, 50 &error_fatal); 51 52 memory_region_init_alias(&s->ram.low, NULL, "xen.ram.lo", &xen_memory, 53 s->cfg.ram_low.base, ram_size[0]); 54 memory_region_add_subregion(sysmem, s->cfg.ram_low.base, &s->ram.low); 55 if (ram_size[1] > 0) { 56 memory_region_init_alias(&s->ram.high, NULL, "xen.ram.hi", &xen_memory, 57 s->cfg.ram_high.base, ram_size[1]); 58 memory_region_add_subregion(sysmem, s->cfg.ram_high.base, &s->ram.high); 59 } 60 61 /* Setup support for grants. */ 62 memory_region_init_ram(&xen_grants, NULL, "xen.grants", block_len, 63 &error_fatal); 64 memory_region_add_subregion(sysmem, XEN_GRANT_ADDR_OFF, &xen_grants); 65 } 66 67 static void xen_set_irq(void *opaque, int irq, int level) 68 { 69 if (xendevicemodel_set_irq_level(xen_dmod, xen_domid, irq, level)) { 70 error_report("xendevicemodel_set_irq_level failed"); 71 } 72 } 73 74 static void xen_create_virtio_mmio_devices(XenPVHMachineState *s) 75 { 76 int i; 77 78 /* 79 * We create the transports in reverse order. Since qbus_realize() 80 * prepends (not appends) new child buses, the decrementing loop below will 81 * create a list of virtio-mmio buses with increasing base addresses. 82 * 83 * When a -device option is processed from the command line, 84 * qbus_find_recursive() picks the next free virtio-mmio bus in forwards 85 * order. 86 * 87 * This is what the Xen tools expect. 88 */ 89 for (i = s->cfg.virtio_mmio_num - 1; i >= 0; i--) { 90 hwaddr base = s->cfg.virtio_mmio.base + i * s->cfg.virtio_mmio.size; 91 qemu_irq irq = qemu_allocate_irq(xen_set_irq, NULL, 92 s->cfg.virtio_mmio_irq_base + i); 93 94 sysbus_create_simple("virtio-mmio", base, irq); 95 96 trace_xen_create_virtio_mmio_devices(i, 97 s->cfg.virtio_mmio_irq_base + i, 98 base); 99 } 100 } 101 102 #ifdef CONFIG_TPM 103 static void xen_enable_tpm(XenPVHMachineState *s) 104 { 105 Error *errp = NULL; 106 DeviceState *dev; 107 SysBusDevice *busdev; 108 109 TPMBackend *be = qemu_find_tpm_be("tpm0"); 110 if (be == NULL) { 111 error_report("Couldn't find tmp0 backend"); 112 return; 113 } 114 dev = qdev_new(TYPE_TPM_TIS_SYSBUS); 115 object_property_set_link(OBJECT(dev), "tpmdev", OBJECT(be), &errp); 116 object_property_set_str(OBJECT(dev), "tpmdev", be->id, &errp); 117 busdev = SYS_BUS_DEVICE(dev); 118 sysbus_realize_and_unref(busdev, &error_fatal); 119 sysbus_mmio_map(busdev, 0, s->cfg.tpm.base); 120 121 trace_xen_enable_tpm(s->cfg.tpm.base); 122 } 123 #endif 124 125 /* 126 * We use the GPEX PCIe controller with its internal INTX PCI interrupt 127 * swizzling. This swizzling is emulated in QEMU and routes all INTX 128 * interrupts from endpoints down to only 4 INTX interrupts. 129 * See include/hw/pci/pci.h : pci_swizzle() 130 */ 131 static inline void xenpvh_gpex_init(XenPVHMachineState *s, 132 XenPVHMachineClass *xpc, 133 MemoryRegion *sysmem) 134 { 135 MemoryRegion *ecam_reg; 136 MemoryRegion *mmio_reg; 137 DeviceState *dev; 138 int i; 139 140 object_initialize_child(OBJECT(s), "gpex", &s->pci.gpex, 141 TYPE_GPEX_HOST); 142 dev = DEVICE(&s->pci.gpex); 143 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal); 144 145 ecam_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0); 146 memory_region_add_subregion(sysmem, s->cfg.pci_ecam.base, ecam_reg); 147 148 mmio_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 1); 149 150 if (s->cfg.pci_mmio.size) { 151 memory_region_init_alias(&s->pci.mmio_alias, OBJECT(dev), "pcie-mmio", 152 mmio_reg, 153 s->cfg.pci_mmio.base, s->cfg.pci_mmio.size); 154 memory_region_add_subregion(sysmem, s->cfg.pci_mmio.base, 155 &s->pci.mmio_alias); 156 } 157 158 if (s->cfg.pci_mmio_high.size) { 159 memory_region_init_alias(&s->pci.mmio_high_alias, OBJECT(dev), 160 "pcie-mmio-high", 161 mmio_reg, s->cfg.pci_mmio_high.base, s->cfg.pci_mmio_high.size); 162 memory_region_add_subregion(sysmem, s->cfg.pci_mmio_high.base, 163 &s->pci.mmio_high_alias); 164 } 165 166 /* 167 * PVH implementations with PCI enabled must provide set_pci_intx_irq() 168 * and optionally an implementation of set_pci_link_route(). 169 */ 170 assert(xpc->set_pci_intx_irq); 171 172 for (i = 0; i < GPEX_NUM_IRQS; i++) { 173 qemu_irq irq = qemu_allocate_irq(xpc->set_pci_intx_irq, s, i); 174 175 sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, irq); 176 gpex_set_irq_num(GPEX_HOST(dev), i, s->cfg.pci_intx_irq_base + i); 177 if (xpc->set_pci_link_route) { 178 xpc->set_pci_link_route(i, s->cfg.pci_intx_irq_base + i); 179 } 180 } 181 } 182 183 static void xen_pvh_init(MachineState *ms) 184 { 185 XenPVHMachineState *s = XEN_PVH_MACHINE(ms); 186 XenPVHMachineClass *xpc = XEN_PVH_MACHINE_GET_CLASS(s); 187 MemoryRegion *sysmem = get_system_memory(); 188 189 if (ms->ram_size == 0) { 190 warn_report("%s: ram size not specified. QEMU machine started" 191 " without IOREQ (no emulated devices including virtio)", 192 MACHINE_CLASS(object_get_class(OBJECT(ms)))->desc); 193 return; 194 } 195 196 xen_pvh_init_ram(s, sysmem); 197 xen_register_ioreq(&s->ioreq, ms->smp.max_cpus, &xen_memory_listener); 198 199 if (s->cfg.virtio_mmio_num) { 200 xen_create_virtio_mmio_devices(s); 201 } 202 203 #ifdef CONFIG_TPM 204 if (xpc->has_tpm) { 205 if (s->cfg.tpm.base) { 206 xen_enable_tpm(s); 207 } else { 208 warn_report("tpm-base-addr is not set. TPM will not be enabled"); 209 } 210 } 211 #endif 212 213 /* Non-zero pci-ecam-size enables PCI. */ 214 if (s->cfg.pci_ecam.size) { 215 if (s->cfg.pci_ecam.size != 256 * MiB) { 216 error_report("pci-ecam-size only supports values 0 or 0x10000000"); 217 exit(EXIT_FAILURE); 218 } 219 xenpvh_gpex_init(s, xpc, sysmem); 220 } 221 222 /* Call the implementation specific init. */ 223 if (xpc->init) { 224 xpc->init(ms); 225 } 226 } 227 228 #define XEN_PVH_PROP_MEMMAP_SETTER(n, f) \ 229 static void xen_pvh_set_ ## n ## _ ## f(Object *obj, Visitor *v, \ 230 const char *name, void *opaque, \ 231 Error **errp) \ 232 { \ 233 XenPVHMachineState *xp = XEN_PVH_MACHINE(obj); \ 234 uint64_t value; \ 235 \ 236 if (!visit_type_size(v, name, &value, errp)) { \ 237 return; \ 238 } \ 239 xp->cfg.n.f = value; \ 240 } 241 242 #define XEN_PVH_PROP_MEMMAP_GETTER(n, f) \ 243 static void xen_pvh_get_ ## n ## _ ## f(Object *obj, Visitor *v, \ 244 const char *name, void *opaque, \ 245 Error **errp) \ 246 { \ 247 XenPVHMachineState *xp = XEN_PVH_MACHINE(obj); \ 248 uint64_t value = xp->cfg.n.f; \ 249 \ 250 visit_type_uint64(v, name, &value, errp); \ 251 } 252 253 #define XEN_PVH_PROP_MEMMAP_BASE(n) \ 254 XEN_PVH_PROP_MEMMAP_SETTER(n, base) \ 255 XEN_PVH_PROP_MEMMAP_GETTER(n, base) \ 256 257 #define XEN_PVH_PROP_MEMMAP_SIZE(n) \ 258 XEN_PVH_PROP_MEMMAP_SETTER(n, size) \ 259 XEN_PVH_PROP_MEMMAP_GETTER(n, size) 260 261 #define XEN_PVH_PROP_MEMMAP(n) \ 262 XEN_PVH_PROP_MEMMAP_BASE(n) \ 263 XEN_PVH_PROP_MEMMAP_SIZE(n) 264 265 XEN_PVH_PROP_MEMMAP(ram_low) 266 XEN_PVH_PROP_MEMMAP(ram_high) 267 /* TPM only has a base-addr option. */ 268 XEN_PVH_PROP_MEMMAP_BASE(tpm) 269 XEN_PVH_PROP_MEMMAP(virtio_mmio) 270 XEN_PVH_PROP_MEMMAP(pci_ecam) 271 XEN_PVH_PROP_MEMMAP(pci_mmio) 272 XEN_PVH_PROP_MEMMAP(pci_mmio_high) 273 274 void xen_pvh_class_setup_common_props(XenPVHMachineClass *xpc) 275 { 276 ObjectClass *oc = OBJECT_CLASS(xpc); 277 MachineClass *mc = MACHINE_CLASS(xpc); 278 279 #define OC_MEMMAP_PROP_BASE(c, prop_name, name) \ 280 do { \ 281 object_class_property_add(c, prop_name "-base", "uint64_t", \ 282 xen_pvh_get_ ## name ## _base, \ 283 xen_pvh_set_ ## name ## _base, NULL, NULL); \ 284 object_class_property_set_description(oc, prop_name "-base", \ 285 "Set base address for " prop_name); \ 286 } while (0) 287 288 #define OC_MEMMAP_PROP_SIZE(c, prop_name, name) \ 289 do { \ 290 object_class_property_add(c, prop_name "-size", "uint64_t", \ 291 xen_pvh_get_ ## name ## _size, \ 292 xen_pvh_set_ ## name ## _size, NULL, NULL); \ 293 object_class_property_set_description(oc, prop_name "-size", \ 294 "Set memory range size for " prop_name); \ 295 } while (0) 296 297 #define OC_MEMMAP_PROP(c, prop_name, name) \ 298 do { \ 299 OC_MEMMAP_PROP_BASE(c, prop_name, name); \ 300 OC_MEMMAP_PROP_SIZE(c, prop_name, name); \ 301 } while (0) 302 303 /* 304 * We provide memmap properties to allow Xen to move things to other 305 * addresses for example when users need to accomodate the memory-map 306 * for 1:1 mapped devices/memory. 307 */ 308 OC_MEMMAP_PROP(oc, "ram-low", ram_low); 309 OC_MEMMAP_PROP(oc, "ram-high", ram_high); 310 311 if (xpc->has_virtio_mmio) { 312 OC_MEMMAP_PROP(oc, "virtio-mmio", virtio_mmio); 313 } 314 315 if (xpc->has_pci) { 316 OC_MEMMAP_PROP(oc, "pci-ecam", pci_ecam); 317 OC_MEMMAP_PROP(oc, "pci-mmio", pci_mmio); 318 OC_MEMMAP_PROP(oc, "pci-mmio-high", pci_mmio_high); 319 } 320 321 #ifdef CONFIG_TPM 322 if (xpc->has_tpm) { 323 object_class_property_add(oc, "tpm-base-addr", "uint64_t", 324 xen_pvh_get_tpm_base, 325 xen_pvh_set_tpm_base, 326 NULL, NULL); 327 object_class_property_set_description(oc, "tpm-base-addr", 328 "Set Base address for TPM device."); 329 330 machine_class_allow_dynamic_sysbus_dev(mc, TYPE_TPM_TIS_SYSBUS); 331 } 332 #endif 333 } 334 335 static void xen_pvh_class_init(ObjectClass *oc, void *data) 336 { 337 MachineClass *mc = MACHINE_CLASS(oc); 338 339 mc->init = xen_pvh_init; 340 341 mc->desc = "Xen PVH machine"; 342 mc->max_cpus = 1; 343 mc->default_machine_opts = "accel=xen"; 344 /* Set to zero to make sure that the real ram size is passed. */ 345 mc->default_ram_size = 0; 346 } 347 348 static const TypeInfo xen_pvh_info = { 349 .name = TYPE_XEN_PVH_MACHINE, 350 .parent = TYPE_MACHINE, 351 .abstract = true, 352 .instance_size = sizeof(XenPVHMachineState), 353 .class_size = sizeof(XenPVHMachineClass), 354 .class_init = xen_pvh_class_init, 355 }; 356 357 static void xen_pvh_register_types(void) 358 { 359 type_register_static(&xen_pvh_info); 360 } 361 362 type_init(xen_pvh_register_types); 363