1 /* 2 * QEMU ARM Xen PVH Machine 3 * 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a copy 6 * of this software and associated documentation files (the "Software"), to deal 7 * in the Software without restriction, including without limitation the rights 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 * copies of the Software, and to permit persons to whom the Software is 10 * furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be included in 13 * all copies or substantial portions of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 * THE SOFTWARE. 22 */ 23 24 #include "qemu/osdep.h" 25 #include "qemu/error-report.h" 26 #include "qapi/qapi-commands-migration.h" 27 #include "qapi/visitor.h" 28 #include "hw/boards.h" 29 #include "hw/irq.h" 30 #include "hw/sysbus.h" 31 #include "sysemu/block-backend.h" 32 #include "sysemu/tpm_backend.h" 33 #include "sysemu/sysemu.h" 34 #include "hw/xen/xen-hvm-common.h" 35 #include "sysemu/tpm.h" 36 #include "hw/xen/arch_hvm.h" 37 38 #define TYPE_XEN_ARM MACHINE_TYPE_NAME("xenpvh") 39 OBJECT_DECLARE_SIMPLE_TYPE(XenArmState, XEN_ARM) 40 41 static const MemoryListener xen_memory_listener = { 42 .region_add = xen_region_add, 43 .region_del = xen_region_del, 44 .log_start = NULL, 45 .log_stop = NULL, 46 .log_sync = NULL, 47 .log_global_start = NULL, 48 .log_global_stop = NULL, 49 .priority = MEMORY_LISTENER_PRIORITY_ACCEL, 50 }; 51 52 struct XenArmState { 53 /*< private >*/ 54 MachineState parent; 55 56 XenIOState *state; 57 58 struct { 59 uint64_t tpm_base_addr; 60 } cfg; 61 }; 62 63 static MemoryRegion ram_lo, ram_hi; 64 65 /* 66 * VIRTIO_MMIO_DEV_SIZE is imported from tools/libs/light/libxl_arm.c under Xen 67 * repository. 68 * 69 * Origin: git://xenbits.xen.org/xen.git 2128143c114c 70 */ 71 #define VIRTIO_MMIO_DEV_SIZE 0x200 72 73 #define NR_VIRTIO_MMIO_DEVICES \ 74 (GUEST_VIRTIO_MMIO_SPI_LAST - GUEST_VIRTIO_MMIO_SPI_FIRST) 75 76 static void xen_set_irq(void *opaque, int irq, int level) 77 { 78 if (xendevicemodel_set_irq_level(xen_dmod, xen_domid, irq, level)) { 79 error_report("xendevicemodel_set_irq_level failed"); 80 } 81 } 82 83 static void xen_create_virtio_mmio_devices(XenArmState *xam) 84 { 85 int i; 86 87 for (i = 0; i < NR_VIRTIO_MMIO_DEVICES; i++) { 88 hwaddr base = GUEST_VIRTIO_MMIO_BASE + i * VIRTIO_MMIO_DEV_SIZE; 89 qemu_irq irq = qemu_allocate_irq(xen_set_irq, NULL, 90 GUEST_VIRTIO_MMIO_SPI_FIRST + i); 91 92 sysbus_create_simple("virtio-mmio", base, irq); 93 94 DPRINTF("Created virtio-mmio device %d: irq %d base 0x%lx\n", 95 i, GUEST_VIRTIO_MMIO_SPI_FIRST + i, base); 96 } 97 } 98 99 static void xen_init_ram(MachineState *machine) 100 { 101 MemoryRegion *sysmem = get_system_memory(); 102 ram_addr_t block_len, ram_size[GUEST_RAM_BANKS]; 103 104 if (machine->ram_size <= GUEST_RAM0_SIZE) { 105 ram_size[0] = machine->ram_size; 106 ram_size[1] = 0; 107 block_len = GUEST_RAM0_BASE + ram_size[0]; 108 } else { 109 ram_size[0] = GUEST_RAM0_SIZE; 110 ram_size[1] = machine->ram_size - GUEST_RAM0_SIZE; 111 block_len = GUEST_RAM1_BASE + ram_size[1]; 112 } 113 114 memory_region_init_ram(&ram_memory, NULL, "xen.ram", block_len, 115 &error_fatal); 116 117 memory_region_init_alias(&ram_lo, NULL, "xen.ram.lo", &ram_memory, 118 GUEST_RAM0_BASE, ram_size[0]); 119 memory_region_add_subregion(sysmem, GUEST_RAM0_BASE, &ram_lo); 120 DPRINTF("Initialized region xen.ram.lo: base 0x%llx size 0x%lx\n", 121 GUEST_RAM0_BASE, ram_size[0]); 122 123 if (ram_size[1] > 0) { 124 memory_region_init_alias(&ram_hi, NULL, "xen.ram.hi", &ram_memory, 125 GUEST_RAM1_BASE, ram_size[1]); 126 memory_region_add_subregion(sysmem, GUEST_RAM1_BASE, &ram_hi); 127 DPRINTF("Initialized region xen.ram.hi: base 0x%llx size 0x%lx\n", 128 GUEST_RAM1_BASE, ram_size[1]); 129 } 130 } 131 132 void arch_handle_ioreq(XenIOState *state, ioreq_t *req) 133 { 134 hw_error("Invalid ioreq type 0x%x\n", req->type); 135 136 return; 137 } 138 139 void arch_xen_set_memory(XenIOState *state, MemoryRegionSection *section, 140 bool add) 141 { 142 } 143 144 void xen_hvm_modified_memory(ram_addr_t start, ram_addr_t length) 145 { 146 } 147 148 void qmp_xen_set_global_dirty_log(bool enable, Error **errp) 149 { 150 } 151 152 #ifdef CONFIG_TPM 153 static void xen_enable_tpm(XenArmState *xam) 154 { 155 Error *errp = NULL; 156 DeviceState *dev; 157 SysBusDevice *busdev; 158 159 TPMBackend *be = qemu_find_tpm_be("tpm0"); 160 if (be == NULL) { 161 DPRINTF("Couldn't fine the backend for tpm0\n"); 162 return; 163 } 164 dev = qdev_new(TYPE_TPM_TIS_SYSBUS); 165 object_property_set_link(OBJECT(dev), "tpmdev", OBJECT(be), &errp); 166 object_property_set_str(OBJECT(dev), "tpmdev", be->id, &errp); 167 busdev = SYS_BUS_DEVICE(dev); 168 sysbus_realize_and_unref(busdev, &error_fatal); 169 sysbus_mmio_map(busdev, 0, xam->cfg.tpm_base_addr); 170 171 DPRINTF("Connected tpmdev at address 0x%lx\n", xam->cfg.tpm_base_addr); 172 } 173 #endif 174 175 static void xen_arm_init(MachineState *machine) 176 { 177 XenArmState *xam = XEN_ARM(machine); 178 179 xam->state = g_new0(XenIOState, 1); 180 181 if (machine->ram_size == 0) { 182 DPRINTF("ram_size not specified. QEMU machine started without IOREQ" 183 "(no emulated devices including Virtio)\n"); 184 return; 185 } 186 187 xen_init_ram(machine); 188 189 xen_register_ioreq(xam->state, machine->smp.cpus, &xen_memory_listener); 190 191 xen_create_virtio_mmio_devices(xam); 192 193 #ifdef CONFIG_TPM 194 if (xam->cfg.tpm_base_addr) { 195 xen_enable_tpm(xam); 196 } else { 197 DPRINTF("tpm-base-addr is not provided. TPM will not be enabled\n"); 198 } 199 #endif 200 } 201 202 #ifdef CONFIG_TPM 203 static void xen_arm_get_tpm_base_addr(Object *obj, Visitor *v, 204 const char *name, void *opaque, 205 Error **errp) 206 { 207 XenArmState *xam = XEN_ARM(obj); 208 uint64_t value = xam->cfg.tpm_base_addr; 209 210 visit_type_uint64(v, name, &value, errp); 211 } 212 213 static void xen_arm_set_tpm_base_addr(Object *obj, Visitor *v, 214 const char *name, void *opaque, 215 Error **errp) 216 { 217 XenArmState *xam = XEN_ARM(obj); 218 uint64_t value; 219 220 if (!visit_type_uint64(v, name, &value, errp)) { 221 return; 222 } 223 224 xam->cfg.tpm_base_addr = value; 225 } 226 #endif 227 228 static void xen_arm_machine_class_init(ObjectClass *oc, void *data) 229 { 230 231 MachineClass *mc = MACHINE_CLASS(oc); 232 mc->desc = "Xen Para-virtualized PC"; 233 mc->init = xen_arm_init; 234 mc->max_cpus = 1; 235 mc->default_machine_opts = "accel=xen"; 236 /* Set explicitly here to make sure that real ram_size is passed */ 237 mc->default_ram_size = 0; 238 239 #ifdef CONFIG_TPM 240 object_class_property_add(oc, "tpm-base-addr", "uint64_t", 241 xen_arm_get_tpm_base_addr, 242 xen_arm_set_tpm_base_addr, 243 NULL, NULL); 244 object_class_property_set_description(oc, "tpm-base-addr", 245 "Set Base address for TPM device."); 246 247 machine_class_allow_dynamic_sysbus_dev(mc, TYPE_TPM_TIS_SYSBUS); 248 #endif 249 } 250 251 static const TypeInfo xen_arm_machine_type = { 252 .name = TYPE_XEN_ARM, 253 .parent = TYPE_MACHINE, 254 .class_init = xen_arm_machine_class_init, 255 .instance_size = sizeof(XenArmState), 256 }; 257 258 static void xen_arm_machine_register_types(void) 259 { 260 type_register_static(&xen_arm_machine_type); 261 } 262 263 type_init(xen_arm_machine_register_types) 264