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 xendevicemodel_set_irq_level(xen_dmod, xen_domid, irq, level); 79 } 80 81 static void xen_create_virtio_mmio_devices(XenArmState *xam) 82 { 83 int i; 84 85 for (i = 0; i < NR_VIRTIO_MMIO_DEVICES; i++) { 86 hwaddr base = GUEST_VIRTIO_MMIO_BASE + i * VIRTIO_MMIO_DEV_SIZE; 87 qemu_irq irq = qemu_allocate_irq(xen_set_irq, NULL, 88 GUEST_VIRTIO_MMIO_SPI_FIRST + i); 89 90 sysbus_create_simple("virtio-mmio", base, irq); 91 92 DPRINTF("Created virtio-mmio device %d: irq %d base 0x%lx\n", 93 i, GUEST_VIRTIO_MMIO_SPI_FIRST + i, base); 94 } 95 } 96 97 static void xen_init_ram(MachineState *machine) 98 { 99 MemoryRegion *sysmem = get_system_memory(); 100 ram_addr_t block_len, ram_size[GUEST_RAM_BANKS]; 101 102 if (machine->ram_size <= GUEST_RAM0_SIZE) { 103 ram_size[0] = machine->ram_size; 104 ram_size[1] = 0; 105 block_len = GUEST_RAM0_BASE + ram_size[0]; 106 } else { 107 ram_size[0] = GUEST_RAM0_SIZE; 108 ram_size[1] = machine->ram_size - GUEST_RAM0_SIZE; 109 block_len = GUEST_RAM1_BASE + ram_size[1]; 110 } 111 112 memory_region_init_ram(&ram_memory, NULL, "xen.ram", block_len, 113 &error_fatal); 114 115 memory_region_init_alias(&ram_lo, NULL, "xen.ram.lo", &ram_memory, 116 GUEST_RAM0_BASE, ram_size[0]); 117 memory_region_add_subregion(sysmem, GUEST_RAM0_BASE, &ram_lo); 118 DPRINTF("Initialized region xen.ram.lo: base 0x%llx size 0x%lx\n", 119 GUEST_RAM0_BASE, ram_size[0]); 120 121 if (ram_size[1] > 0) { 122 memory_region_init_alias(&ram_hi, NULL, "xen.ram.hi", &ram_memory, 123 GUEST_RAM1_BASE, ram_size[1]); 124 memory_region_add_subregion(sysmem, GUEST_RAM1_BASE, &ram_hi); 125 DPRINTF("Initialized region xen.ram.hi: base 0x%llx size 0x%lx\n", 126 GUEST_RAM1_BASE, ram_size[1]); 127 } 128 } 129 130 void arch_handle_ioreq(XenIOState *state, ioreq_t *req) 131 { 132 hw_error("Invalid ioreq type 0x%x\n", req->type); 133 134 return; 135 } 136 137 void arch_xen_set_memory(XenIOState *state, MemoryRegionSection *section, 138 bool add) 139 { 140 } 141 142 void xen_hvm_modified_memory(ram_addr_t start, ram_addr_t length) 143 { 144 } 145 146 void qmp_xen_set_global_dirty_log(bool enable, Error **errp) 147 { 148 } 149 150 #ifdef CONFIG_TPM 151 static void xen_enable_tpm(XenArmState *xam) 152 { 153 Error *errp = NULL; 154 DeviceState *dev; 155 SysBusDevice *busdev; 156 157 TPMBackend *be = qemu_find_tpm_be("tpm0"); 158 if (be == NULL) { 159 DPRINTF("Couldn't fine the backend for tpm0\n"); 160 return; 161 } 162 dev = qdev_new(TYPE_TPM_TIS_SYSBUS); 163 object_property_set_link(OBJECT(dev), "tpmdev", OBJECT(be), &errp); 164 object_property_set_str(OBJECT(dev), "tpmdev", be->id, &errp); 165 busdev = SYS_BUS_DEVICE(dev); 166 sysbus_realize_and_unref(busdev, &error_fatal); 167 sysbus_mmio_map(busdev, 0, xam->cfg.tpm_base_addr); 168 169 DPRINTF("Connected tpmdev at address 0x%lx\n", xam->cfg.tpm_base_addr); 170 } 171 #endif 172 173 static void xen_arm_init(MachineState *machine) 174 { 175 XenArmState *xam = XEN_ARM(machine); 176 177 xam->state = g_new0(XenIOState, 1); 178 179 if (machine->ram_size == 0) { 180 DPRINTF("ram_size not specified. QEMU machine started without IOREQ" 181 "(no emulated devices including Virtio)\n"); 182 return; 183 } 184 185 xen_init_ram(machine); 186 187 xen_register_ioreq(xam->state, machine->smp.cpus, &xen_memory_listener); 188 189 xen_create_virtio_mmio_devices(xam); 190 191 #ifdef CONFIG_TPM 192 if (xam->cfg.tpm_base_addr) { 193 xen_enable_tpm(xam); 194 } else { 195 DPRINTF("tpm-base-addr is not provided. TPM will not be enabled\n"); 196 } 197 #endif 198 } 199 200 #ifdef CONFIG_TPM 201 static void xen_arm_get_tpm_base_addr(Object *obj, Visitor *v, 202 const char *name, void *opaque, 203 Error **errp) 204 { 205 XenArmState *xam = XEN_ARM(obj); 206 uint64_t value = xam->cfg.tpm_base_addr; 207 208 visit_type_uint64(v, name, &value, errp); 209 } 210 211 static void xen_arm_set_tpm_base_addr(Object *obj, Visitor *v, 212 const char *name, void *opaque, 213 Error **errp) 214 { 215 XenArmState *xam = XEN_ARM(obj); 216 uint64_t value; 217 218 if (!visit_type_uint64(v, name, &value, errp)) { 219 return; 220 } 221 222 xam->cfg.tpm_base_addr = value; 223 } 224 #endif 225 226 static void xen_arm_machine_class_init(ObjectClass *oc, void *data) 227 { 228 229 MachineClass *mc = MACHINE_CLASS(oc); 230 mc->desc = "Xen Para-virtualized PC"; 231 mc->init = xen_arm_init; 232 mc->max_cpus = 1; 233 mc->default_machine_opts = "accel=xen"; 234 /* Set explicitly here to make sure that real ram_size is passed */ 235 mc->default_ram_size = 0; 236 237 #ifdef CONFIG_TPM 238 object_class_property_add(oc, "tpm-base-addr", "uint64_t", 239 xen_arm_get_tpm_base_addr, 240 xen_arm_set_tpm_base_addr, 241 NULL, NULL); 242 object_class_property_set_description(oc, "tpm-base-addr", 243 "Set Base address for TPM device."); 244 245 machine_class_allow_dynamic_sysbus_dev(mc, TYPE_TPM_TIS_SYSBUS); 246 #endif 247 } 248 249 static const TypeInfo xen_arm_machine_type = { 250 .name = TYPE_XEN_ARM, 251 .parent = TYPE_MACHINE, 252 .class_init = xen_arm_machine_class_init, 253 .instance_size = sizeof(XenArmState), 254 }; 255 256 static void xen_arm_machine_register_types(void) 257 { 258 type_register_static(&xen_arm_machine_type); 259 } 260 261 type_init(xen_arm_machine_register_types) 262