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 static void xen_pvh_init(MachineState *ms) 126 { 127 XenPVHMachineState *s = XEN_PVH_MACHINE(ms); 128 XenPVHMachineClass *xpc = XEN_PVH_MACHINE_GET_CLASS(s); 129 MemoryRegion *sysmem = get_system_memory(); 130 131 if (ms->ram_size == 0) { 132 warn_report("%s: ram size not specified. QEMU machine started" 133 " without IOREQ (no emulated devices including virtio)", 134 MACHINE_CLASS(object_get_class(OBJECT(ms)))->desc); 135 return; 136 } 137 138 xen_pvh_init_ram(s, sysmem); 139 xen_register_ioreq(&s->ioreq, ms->smp.max_cpus, &xen_memory_listener); 140 141 if (s->cfg.virtio_mmio_num) { 142 xen_create_virtio_mmio_devices(s); 143 } 144 145 #ifdef CONFIG_TPM 146 if (xpc->has_tpm) { 147 if (s->cfg.tpm.base) { 148 xen_enable_tpm(s); 149 } else { 150 warn_report("tpm-base-addr is not set. TPM will not be enabled"); 151 } 152 } 153 #endif 154 155 /* Call the implementation specific init. */ 156 if (xpc->init) { 157 xpc->init(ms); 158 } 159 } 160 161 #define XEN_PVH_PROP_MEMMAP_SETTER(n, f) \ 162 static void xen_pvh_set_ ## n ## _ ## f(Object *obj, Visitor *v, \ 163 const char *name, void *opaque, \ 164 Error **errp) \ 165 { \ 166 XenPVHMachineState *xp = XEN_PVH_MACHINE(obj); \ 167 uint64_t value; \ 168 \ 169 if (!visit_type_size(v, name, &value, errp)) { \ 170 return; \ 171 } \ 172 xp->cfg.n.f = value; \ 173 } 174 175 #define XEN_PVH_PROP_MEMMAP_GETTER(n, f) \ 176 static void xen_pvh_get_ ## n ## _ ## f(Object *obj, Visitor *v, \ 177 const char *name, void *opaque, \ 178 Error **errp) \ 179 { \ 180 XenPVHMachineState *xp = XEN_PVH_MACHINE(obj); \ 181 uint64_t value = xp->cfg.n.f; \ 182 \ 183 visit_type_uint64(v, name, &value, errp); \ 184 } 185 186 #define XEN_PVH_PROP_MEMMAP_BASE(n) \ 187 XEN_PVH_PROP_MEMMAP_SETTER(n, base) \ 188 XEN_PVH_PROP_MEMMAP_GETTER(n, base) \ 189 190 #define XEN_PVH_PROP_MEMMAP_SIZE(n) \ 191 XEN_PVH_PROP_MEMMAP_SETTER(n, size) \ 192 XEN_PVH_PROP_MEMMAP_GETTER(n, size) 193 194 #define XEN_PVH_PROP_MEMMAP(n) \ 195 XEN_PVH_PROP_MEMMAP_BASE(n) \ 196 XEN_PVH_PROP_MEMMAP_SIZE(n) 197 198 XEN_PVH_PROP_MEMMAP(ram_low) 199 XEN_PVH_PROP_MEMMAP(ram_high) 200 /* TPM only has a base-addr option. */ 201 XEN_PVH_PROP_MEMMAP_BASE(tpm) 202 XEN_PVH_PROP_MEMMAP(virtio_mmio) 203 204 void xen_pvh_class_setup_common_props(XenPVHMachineClass *xpc) 205 { 206 ObjectClass *oc = OBJECT_CLASS(xpc); 207 MachineClass *mc = MACHINE_CLASS(xpc); 208 209 #define OC_MEMMAP_PROP_BASE(c, prop_name, name) \ 210 do { \ 211 object_class_property_add(c, prop_name "-base", "uint64_t", \ 212 xen_pvh_get_ ## name ## _base, \ 213 xen_pvh_set_ ## name ## _base, NULL, NULL); \ 214 object_class_property_set_description(oc, prop_name "-base", \ 215 "Set base address for " prop_name); \ 216 } while (0) 217 218 #define OC_MEMMAP_PROP_SIZE(c, prop_name, name) \ 219 do { \ 220 object_class_property_add(c, prop_name "-size", "uint64_t", \ 221 xen_pvh_get_ ## name ## _size, \ 222 xen_pvh_set_ ## name ## _size, NULL, NULL); \ 223 object_class_property_set_description(oc, prop_name "-size", \ 224 "Set memory range size for " prop_name); \ 225 } while (0) 226 227 #define OC_MEMMAP_PROP(c, prop_name, name) \ 228 do { \ 229 OC_MEMMAP_PROP_BASE(c, prop_name, name); \ 230 OC_MEMMAP_PROP_SIZE(c, prop_name, name); \ 231 } while (0) 232 233 /* 234 * We provide memmap properties to allow Xen to move things to other 235 * addresses for example when users need to accomodate the memory-map 236 * for 1:1 mapped devices/memory. 237 */ 238 OC_MEMMAP_PROP(oc, "ram-low", ram_low); 239 OC_MEMMAP_PROP(oc, "ram-high", ram_high); 240 241 if (xpc->has_virtio_mmio) { 242 OC_MEMMAP_PROP(oc, "virtio-mmio", virtio_mmio); 243 } 244 245 #ifdef CONFIG_TPM 246 if (xpc->has_tpm) { 247 object_class_property_add(oc, "tpm-base-addr", "uint64_t", 248 xen_pvh_get_tpm_base, 249 xen_pvh_set_tpm_base, 250 NULL, NULL); 251 object_class_property_set_description(oc, "tpm-base-addr", 252 "Set Base address for TPM device."); 253 254 machine_class_allow_dynamic_sysbus_dev(mc, TYPE_TPM_TIS_SYSBUS); 255 } 256 #endif 257 } 258 259 static void xen_pvh_class_init(ObjectClass *oc, void *data) 260 { 261 MachineClass *mc = MACHINE_CLASS(oc); 262 263 mc->init = xen_pvh_init; 264 265 mc->desc = "Xen PVH machine"; 266 mc->max_cpus = 1; 267 mc->default_machine_opts = "accel=xen"; 268 /* Set to zero to make sure that the real ram size is passed. */ 269 mc->default_ram_size = 0; 270 } 271 272 static const TypeInfo xen_pvh_info = { 273 .name = TYPE_XEN_PVH_MACHINE, 274 .parent = TYPE_MACHINE, 275 .abstract = true, 276 .instance_size = sizeof(XenPVHMachineState), 277 .class_size = sizeof(XenPVHMachineClass), 278 .class_init = xen_pvh_class_init, 279 }; 280 281 static void xen_pvh_register_types(void) 282 { 283 type_register_static(&xen_pvh_info); 284 } 285 286 type_init(xen_pvh_register_types); 287