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 #include "trace.h" 38 39 #define TYPE_XEN_ARM MACHINE_TYPE_NAME("xenpvh") 40 OBJECT_DECLARE_SIMPLE_TYPE(XenArmState, XEN_ARM) 41 42 static const MemoryListener xen_memory_listener = { 43 .region_add = xen_region_add, 44 .region_del = xen_region_del, 45 .log_start = NULL, 46 .log_stop = NULL, 47 .log_sync = NULL, 48 .log_global_start = NULL, 49 .log_global_stop = NULL, 50 .priority = MEMORY_LISTENER_PRIORITY_ACCEL, 51 }; 52 53 struct XenArmState { 54 /*< private >*/ 55 MachineState parent; 56 57 XenIOState *state; 58 59 struct { 60 uint64_t tpm_base_addr; 61 } cfg; 62 }; 63 64 static MemoryRegion ram_lo, ram_hi; 65 66 /* 67 * VIRTIO_MMIO_DEV_SIZE is imported from tools/libs/light/libxl_arm.c under Xen 68 * repository. 69 * 70 * Origin: git://xenbits.xen.org/xen.git 2128143c114c 71 */ 72 #define VIRTIO_MMIO_DEV_SIZE 0x200 73 74 #define NR_VIRTIO_MMIO_DEVICES \ 75 (GUEST_VIRTIO_MMIO_SPI_LAST - GUEST_VIRTIO_MMIO_SPI_FIRST) 76 77 static void xen_set_irq(void *opaque, int irq, int level) 78 { 79 if (xendevicemodel_set_irq_level(xen_dmod, xen_domid, irq, level)) { 80 error_report("xendevicemodel_set_irq_level failed"); 81 } 82 } 83 84 static void xen_create_virtio_mmio_devices(XenArmState *xam) 85 { 86 int i; 87 88 for (i = 0; i < NR_VIRTIO_MMIO_DEVICES; i++) { 89 hwaddr base = GUEST_VIRTIO_MMIO_BASE + i * VIRTIO_MMIO_DEV_SIZE; 90 qemu_irq irq = qemu_allocate_irq(xen_set_irq, NULL, 91 GUEST_VIRTIO_MMIO_SPI_FIRST + i); 92 93 sysbus_create_simple("virtio-mmio", base, irq); 94 95 trace_xen_create_virtio_mmio_devices(i, 96 GUEST_VIRTIO_MMIO_SPI_FIRST + i, 97 base); 98 } 99 } 100 101 static void xen_init_ram(MachineState *machine) 102 { 103 MemoryRegion *sysmem = get_system_memory(); 104 ram_addr_t block_len, ram_size[GUEST_RAM_BANKS]; 105 106 trace_xen_init_ram(machine->ram_size); 107 if (machine->ram_size <= GUEST_RAM0_SIZE) { 108 ram_size[0] = machine->ram_size; 109 ram_size[1] = 0; 110 block_len = GUEST_RAM0_BASE + ram_size[0]; 111 } else { 112 ram_size[0] = GUEST_RAM0_SIZE; 113 ram_size[1] = machine->ram_size - GUEST_RAM0_SIZE; 114 block_len = GUEST_RAM1_BASE + ram_size[1]; 115 } 116 117 memory_region_init_ram(&xen_memory, NULL, "xen.ram", block_len, 118 &error_fatal); 119 120 memory_region_init_alias(&ram_lo, NULL, "xen.ram.lo", &xen_memory, 121 GUEST_RAM0_BASE, ram_size[0]); 122 memory_region_add_subregion(sysmem, GUEST_RAM0_BASE, &ram_lo); 123 if (ram_size[1] > 0) { 124 memory_region_init_alias(&ram_hi, NULL, "xen.ram.hi", &xen_memory, 125 GUEST_RAM1_BASE, ram_size[1]); 126 memory_region_add_subregion(sysmem, GUEST_RAM1_BASE, &ram_hi); 127 } 128 129 /* Setup support for grants. */ 130 memory_region_init_ram(&xen_grants, NULL, "xen.grants", block_len, 131 &error_fatal); 132 memory_region_add_subregion(sysmem, XEN_GRANT_ADDR_OFF, &xen_grants); 133 } 134 135 void arch_handle_ioreq(XenIOState *state, ioreq_t *req) 136 { 137 hw_error("Invalid ioreq type 0x%x\n", req->type); 138 139 return; 140 } 141 142 void arch_xen_set_memory(XenIOState *state, MemoryRegionSection *section, 143 bool add) 144 { 145 } 146 147 void xen_hvm_modified_memory(ram_addr_t start, ram_addr_t length) 148 { 149 } 150 151 void qmp_xen_set_global_dirty_log(bool enable, Error **errp) 152 { 153 } 154 155 #ifdef CONFIG_TPM 156 static void xen_enable_tpm(XenArmState *xam) 157 { 158 Error *errp = NULL; 159 DeviceState *dev; 160 SysBusDevice *busdev; 161 162 TPMBackend *be = qemu_find_tpm_be("tpm0"); 163 if (be == NULL) { 164 error_report("Couldn't find tmp0 backend"); 165 return; 166 } 167 dev = qdev_new(TYPE_TPM_TIS_SYSBUS); 168 object_property_set_link(OBJECT(dev), "tpmdev", OBJECT(be), &errp); 169 object_property_set_str(OBJECT(dev), "tpmdev", be->id, &errp); 170 busdev = SYS_BUS_DEVICE(dev); 171 sysbus_realize_and_unref(busdev, &error_fatal); 172 sysbus_mmio_map(busdev, 0, xam->cfg.tpm_base_addr); 173 174 trace_xen_enable_tpm(xam->cfg.tpm_base_addr); 175 } 176 #endif 177 178 static void xen_arm_init(MachineState *machine) 179 { 180 XenArmState *xam = XEN_ARM(machine); 181 182 xam->state = g_new0(XenIOState, 1); 183 184 if (machine->ram_size == 0) { 185 warn_report("%s non-zero ram size not specified. QEMU machine started" 186 " without IOREQ (no emulated devices including virtio)", 187 MACHINE_CLASS(object_get_class(OBJECT(machine)))->desc); 188 return; 189 } 190 191 xen_init_ram(machine); 192 193 xen_register_ioreq(xam->state, machine->smp.cpus, &xen_memory_listener); 194 195 xen_create_virtio_mmio_devices(xam); 196 197 #ifdef CONFIG_TPM 198 if (xam->cfg.tpm_base_addr) { 199 xen_enable_tpm(xam); 200 } else { 201 warn_report("tpm-base-addr is not provided. TPM will not be enabled"); 202 } 203 #endif 204 } 205 206 #ifdef CONFIG_TPM 207 static void xen_arm_get_tpm_base_addr(Object *obj, Visitor *v, 208 const char *name, void *opaque, 209 Error **errp) 210 { 211 XenArmState *xam = XEN_ARM(obj); 212 uint64_t value = xam->cfg.tpm_base_addr; 213 214 visit_type_uint64(v, name, &value, errp); 215 } 216 217 static void xen_arm_set_tpm_base_addr(Object *obj, Visitor *v, 218 const char *name, void *opaque, 219 Error **errp) 220 { 221 XenArmState *xam = XEN_ARM(obj); 222 uint64_t value; 223 224 if (!visit_type_uint64(v, name, &value, errp)) { 225 return; 226 } 227 228 xam->cfg.tpm_base_addr = value; 229 } 230 #endif 231 232 static void xen_arm_machine_class_init(ObjectClass *oc, void *data) 233 { 234 235 MachineClass *mc = MACHINE_CLASS(oc); 236 mc->desc = "Xen Para-virtualized PC"; 237 mc->init = xen_arm_init; 238 mc->max_cpus = 1; 239 mc->default_machine_opts = "accel=xen"; 240 /* Set explicitly here to make sure that real ram_size is passed */ 241 mc->default_ram_size = 0; 242 243 #ifdef CONFIG_TPM 244 object_class_property_add(oc, "tpm-base-addr", "uint64_t", 245 xen_arm_get_tpm_base_addr, 246 xen_arm_set_tpm_base_addr, 247 NULL, NULL); 248 object_class_property_set_description(oc, "tpm-base-addr", 249 "Set Base address for TPM device."); 250 251 machine_class_allow_dynamic_sysbus_dev(mc, TYPE_TPM_TIS_SYSBUS); 252 #endif 253 } 254 255 static const TypeInfo xen_arm_machine_type = { 256 .name = TYPE_XEN_ARM, 257 .parent = TYPE_MACHINE, 258 .class_init = xen_arm_machine_class_init, 259 .instance_size = sizeof(XenArmState), 260 }; 261 262 static void xen_arm_machine_register_types(void) 263 { 264 type_register_static(&xen_arm_machine_type); 265 } 266 267 type_init(xen_arm_machine_register_types) 268