crypto/rsakey-builtin.c.inc: Clean up two error paths
[qemu/armbru.git] / hw / arm / xen_arm.c
blobf83b983ec58dbc184699dca5704880799594ad74
1 /*
2 * QEMU ARM Xen PVH Machine
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
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"
38 #define TYPE_XEN_ARM MACHINE_TYPE_NAME("xenpvh")
39 OBJECT_DECLARE_SIMPLE_TYPE(XenArmState, XEN_ARM)
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,
52 struct XenArmState {
53 /*< private >*/
54 MachineState parent;
56 XenIOState *state;
58 struct {
59 uint64_t tpm_base_addr;
60 } cfg;
63 static MemoryRegion ram_lo, ram_hi;
66 * VIRTIO_MMIO_DEV_SIZE is imported from tools/libs/light/libxl_arm.c under Xen
67 * repository.
69 * Origin: git://xenbits.xen.org/xen.git 2128143c114c
71 #define VIRTIO_MMIO_DEV_SIZE 0x200
73 #define NR_VIRTIO_MMIO_DEVICES \
74 (GUEST_VIRTIO_MMIO_SPI_LAST - GUEST_VIRTIO_MMIO_SPI_FIRST)
76 static void xen_set_irq(void *opaque, int irq, int level)
78 xendevicemodel_set_irq_level(xen_dmod, xen_domid, irq, level);
81 static void xen_create_virtio_mmio_devices(XenArmState *xam)
83 int i;
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);
90 sysbus_create_simple("virtio-mmio", base, irq);
92 DPRINTF("Created virtio-mmio device %d: irq %d base 0x%lx\n",
93 i, GUEST_VIRTIO_MMIO_SPI_FIRST + i, base);
97 static void xen_init_ram(MachineState *machine)
99 MemoryRegion *sysmem = get_system_memory();
100 ram_addr_t block_len, ram_size[GUEST_RAM_BANKS];
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];
112 memory_region_init_ram(&ram_memory, NULL, "xen.ram", block_len,
113 &error_fatal);
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]);
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]);
130 void arch_handle_ioreq(XenIOState *state, ioreq_t *req)
132 hw_error("Invalid ioreq type 0x%x\n", req->type);
134 return;
137 void arch_xen_set_memory(XenIOState *state, MemoryRegionSection *section,
138 bool add)
142 void xen_hvm_modified_memory(ram_addr_t start, ram_addr_t length)
146 void qmp_xen_set_global_dirty_log(bool enable, Error **errp)
150 #ifdef CONFIG_TPM
151 static void xen_enable_tpm(XenArmState *xam)
153 Error *errp = NULL;
154 DeviceState *dev;
155 SysBusDevice *busdev;
157 TPMBackend *be = qemu_find_tpm_be("tpm0");
158 if (be == NULL) {
159 DPRINTF("Couldn't fine the backend for tpm0\n");
160 return;
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);
169 DPRINTF("Connected tpmdev at address 0x%lx\n", xam->cfg.tpm_base_addr);
171 #endif
173 static void xen_arm_init(MachineState *machine)
175 XenArmState *xam = XEN_ARM(machine);
177 xam->state = g_new0(XenIOState, 1);
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;
185 xen_init_ram(machine);
187 xen_register_ioreq(xam->state, machine->smp.cpus, &xen_memory_listener);
189 xen_create_virtio_mmio_devices(xam);
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");
197 #endif
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)
205 XenArmState *xam = XEN_ARM(obj);
206 uint64_t value = xam->cfg.tpm_base_addr;
208 visit_type_uint64(v, name, &value, errp);
211 static void xen_arm_set_tpm_base_addr(Object *obj, Visitor *v,
212 const char *name, void *opaque,
213 Error **errp)
215 XenArmState *xam = XEN_ARM(obj);
216 uint64_t value;
218 if (!visit_type_uint64(v, name, &value, errp)) {
219 return;
222 xam->cfg.tpm_base_addr = value;
224 #endif
226 static void xen_arm_machine_class_init(ObjectClass *oc, void *data)
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;
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.");
245 machine_class_allow_dynamic_sysbus_dev(mc, TYPE_TPM_TIS_SYSBUS);
246 #endif
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),
256 static void xen_arm_machine_register_types(void)
258 type_register_static(&xen_arm_machine_type);
261 type_init(xen_arm_machine_register_types)