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 for (i = 0; i < s->cfg.virtio_mmio_num; i++) { 79 hwaddr base = s->cfg.virtio_mmio.base + i * s->cfg.virtio_mmio.size; 80 qemu_irq irq = qemu_allocate_irq(xen_set_irq, NULL, 81 s->cfg.virtio_mmio_irq_base + i); 82 83 sysbus_create_simple("virtio-mmio", base, irq); 84 85 trace_xen_create_virtio_mmio_devices(i, 86 s->cfg.virtio_mmio_irq_base + i, 87 base); 88 } 89 } 90 91 #ifdef CONFIG_TPM 92 static void xen_enable_tpm(XenPVHMachineState *s) 93 { 94 Error *errp = NULL; 95 DeviceState *dev; 96 SysBusDevice *busdev; 97 98 TPMBackend *be = qemu_find_tpm_be("tpm0"); 99 if (be == NULL) { 100 error_report("Couldn't find tmp0 backend"); 101 return; 102 } 103 dev = qdev_new(TYPE_TPM_TIS_SYSBUS); 104 object_property_set_link(OBJECT(dev), "tpmdev", OBJECT(be), &errp); 105 object_property_set_str(OBJECT(dev), "tpmdev", be->id, &errp); 106 busdev = SYS_BUS_DEVICE(dev); 107 sysbus_realize_and_unref(busdev, &error_fatal); 108 sysbus_mmio_map(busdev, 0, s->cfg.tpm.base); 109 110 trace_xen_enable_tpm(s->cfg.tpm.base); 111 } 112 #endif 113 114 static void xen_pvh_init(MachineState *ms) 115 { 116 XenPVHMachineState *s = XEN_PVH_MACHINE(ms); 117 XenPVHMachineClass *xpc = XEN_PVH_MACHINE_GET_CLASS(s); 118 MemoryRegion *sysmem = get_system_memory(); 119 120 if (ms->ram_size == 0) { 121 warn_report("%s: ram size not specified. QEMU machine started" 122 " without IOREQ (no emulated devices including virtio)", 123 MACHINE_CLASS(object_get_class(OBJECT(ms)))->desc); 124 return; 125 } 126 127 xen_pvh_init_ram(s, sysmem); 128 xen_register_ioreq(&s->ioreq, ms->smp.max_cpus, &xen_memory_listener); 129 130 if (s->cfg.virtio_mmio_num) { 131 xen_create_virtio_mmio_devices(s); 132 } 133 134 #ifdef CONFIG_TPM 135 if (xpc->has_tpm) { 136 if (s->cfg.tpm.base) { 137 xen_enable_tpm(s); 138 } else { 139 warn_report("tpm-base-addr is not set. TPM will not be enabled"); 140 } 141 } 142 #endif 143 144 /* Call the implementation specific init. */ 145 if (xpc->init) { 146 xpc->init(ms); 147 } 148 } 149 150 #define XEN_PVH_PROP_MEMMAP_SETTER(n, f) \ 151 static void xen_pvh_set_ ## n ## _ ## f(Object *obj, Visitor *v, \ 152 const char *name, void *opaque, \ 153 Error **errp) \ 154 { \ 155 XenPVHMachineState *xp = XEN_PVH_MACHINE(obj); \ 156 uint64_t value; \ 157 \ 158 if (!visit_type_size(v, name, &value, errp)) { \ 159 return; \ 160 } \ 161 xp->cfg.n.f = value; \ 162 } 163 164 #define XEN_PVH_PROP_MEMMAP_GETTER(n, f) \ 165 static void xen_pvh_get_ ## n ## _ ## f(Object *obj, Visitor *v, \ 166 const char *name, void *opaque, \ 167 Error **errp) \ 168 { \ 169 XenPVHMachineState *xp = XEN_PVH_MACHINE(obj); \ 170 uint64_t value = xp->cfg.n.f; \ 171 \ 172 visit_type_uint64(v, name, &value, errp); \ 173 } 174 175 #define XEN_PVH_PROP_MEMMAP_BASE(n) \ 176 XEN_PVH_PROP_MEMMAP_SETTER(n, base) \ 177 XEN_PVH_PROP_MEMMAP_GETTER(n, base) \ 178 179 #define XEN_PVH_PROP_MEMMAP_SIZE(n) \ 180 XEN_PVH_PROP_MEMMAP_SETTER(n, size) \ 181 XEN_PVH_PROP_MEMMAP_GETTER(n, size) 182 183 #define XEN_PVH_PROP_MEMMAP(n) \ 184 XEN_PVH_PROP_MEMMAP_BASE(n) \ 185 XEN_PVH_PROP_MEMMAP_SIZE(n) 186 187 XEN_PVH_PROP_MEMMAP(ram_low) 188 XEN_PVH_PROP_MEMMAP(ram_high) 189 /* TPM only has a base-addr option. */ 190 XEN_PVH_PROP_MEMMAP_BASE(tpm) 191 XEN_PVH_PROP_MEMMAP(virtio_mmio) 192 193 void xen_pvh_class_setup_common_props(XenPVHMachineClass *xpc) 194 { 195 ObjectClass *oc = OBJECT_CLASS(xpc); 196 MachineClass *mc = MACHINE_CLASS(xpc); 197 198 #define OC_MEMMAP_PROP_BASE(c, prop_name, name) \ 199 do { \ 200 object_class_property_add(c, prop_name "-base", "uint64_t", \ 201 xen_pvh_get_ ## name ## _base, \ 202 xen_pvh_set_ ## name ## _base, NULL, NULL); \ 203 object_class_property_set_description(oc, prop_name "-base", \ 204 "Set base address for " prop_name); \ 205 } while (0) 206 207 #define OC_MEMMAP_PROP_SIZE(c, prop_name, name) \ 208 do { \ 209 object_class_property_add(c, prop_name "-size", "uint64_t", \ 210 xen_pvh_get_ ## name ## _size, \ 211 xen_pvh_set_ ## name ## _size, NULL, NULL); \ 212 object_class_property_set_description(oc, prop_name "-size", \ 213 "Set memory range size for " prop_name); \ 214 } while (0) 215 216 #define OC_MEMMAP_PROP(c, prop_name, name) \ 217 do { \ 218 OC_MEMMAP_PROP_BASE(c, prop_name, name); \ 219 OC_MEMMAP_PROP_SIZE(c, prop_name, name); \ 220 } while (0) 221 222 /* 223 * We provide memmap properties to allow Xen to move things to other 224 * addresses for example when users need to accomodate the memory-map 225 * for 1:1 mapped devices/memory. 226 */ 227 OC_MEMMAP_PROP(oc, "ram-low", ram_low); 228 OC_MEMMAP_PROP(oc, "ram-high", ram_high); 229 230 if (xpc->has_virtio_mmio) { 231 OC_MEMMAP_PROP(oc, "virtio-mmio", virtio_mmio); 232 } 233 234 #ifdef CONFIG_TPM 235 if (xpc->has_tpm) { 236 object_class_property_add(oc, "tpm-base-addr", "uint64_t", 237 xen_pvh_get_tpm_base, 238 xen_pvh_set_tpm_base, 239 NULL, NULL); 240 object_class_property_set_description(oc, "tpm-base-addr", 241 "Set Base address for TPM device."); 242 243 machine_class_allow_dynamic_sysbus_dev(mc, TYPE_TPM_TIS_SYSBUS); 244 } 245 #endif 246 } 247 248 static void xen_pvh_class_init(ObjectClass *oc, void *data) 249 { 250 MachineClass *mc = MACHINE_CLASS(oc); 251 252 mc->init = xen_pvh_init; 253 254 mc->desc = "Xen PVH machine"; 255 mc->max_cpus = 1; 256 mc->default_machine_opts = "accel=xen"; 257 /* Set to zero to make sure that the real ram size is passed. */ 258 mc->default_ram_size = 0; 259 } 260 261 static const TypeInfo xen_pvh_info = { 262 .name = TYPE_XEN_PVH_MACHINE, 263 .parent = TYPE_MACHINE, 264 .abstract = true, 265 .instance_size = sizeof(XenPVHMachineState), 266 .class_size = sizeof(XenPVHMachineClass), 267 .class_init = xen_pvh_class_init, 268 }; 269 270 static void xen_pvh_register_types(void) 271 { 272 type_register_static(&xen_pvh_info); 273 } 274 275 type_init(xen_pvh_register_types); 276