xref: /openbmc/qemu/hw/arm/xen_arm.c (revision 2df1eb27)
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(&ram_memory, NULL, "xen.ram", block_len,
118                            &error_fatal);
119 
120     memory_region_init_alias(&ram_lo, NULL, "xen.ram.lo", &ram_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", &ram_memory,
125                                  GUEST_RAM1_BASE, ram_size[1]);
126         memory_region_add_subregion(sysmem, GUEST_RAM1_BASE, &ram_hi);
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         error_report("Couldn't find tmp0 backend");
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     trace_xen_enable_tpm(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         warn_report("%s non-zero ram size not specified. QEMU machine started"
181                     " without IOREQ (no emulated devices including virtio)",
182                     MACHINE_CLASS(object_get_class(OBJECT(machine)))->desc);
183         return;
184     }
185 
186     xen_init_ram(machine);
187 
188     xen_register_ioreq(xam->state, machine->smp.cpus, &xen_memory_listener);
189 
190     xen_create_virtio_mmio_devices(xam);
191 
192 #ifdef CONFIG_TPM
193     if (xam->cfg.tpm_base_addr) {
194         xen_enable_tpm(xam);
195     } else {
196         warn_report("tpm-base-addr is not provided. TPM will not be enabled");
197     }
198 #endif
199 }
200 
201 #ifdef CONFIG_TPM
202 static void xen_arm_get_tpm_base_addr(Object *obj, Visitor *v,
203                                       const char *name, void *opaque,
204                                       Error **errp)
205 {
206     XenArmState *xam = XEN_ARM(obj);
207     uint64_t value = xam->cfg.tpm_base_addr;
208 
209     visit_type_uint64(v, name, &value, errp);
210 }
211 
212 static void xen_arm_set_tpm_base_addr(Object *obj, Visitor *v,
213                                       const char *name, void *opaque,
214                                       Error **errp)
215 {
216     XenArmState *xam = XEN_ARM(obj);
217     uint64_t value;
218 
219     if (!visit_type_uint64(v, name, &value, errp)) {
220         return;
221     }
222 
223     xam->cfg.tpm_base_addr = value;
224 }
225 #endif
226 
227 static void xen_arm_machine_class_init(ObjectClass *oc, void *data)
228 {
229 
230     MachineClass *mc = MACHINE_CLASS(oc);
231     mc->desc = "Xen Para-virtualized PC";
232     mc->init = xen_arm_init;
233     mc->max_cpus = 1;
234     mc->default_machine_opts = "accel=xen";
235     /* Set explicitly here to make sure that real ram_size is passed */
236     mc->default_ram_size = 0;
237 
238 #ifdef CONFIG_TPM
239     object_class_property_add(oc, "tpm-base-addr", "uint64_t",
240                               xen_arm_get_tpm_base_addr,
241                               xen_arm_set_tpm_base_addr,
242                               NULL, NULL);
243     object_class_property_set_description(oc, "tpm-base-addr",
244                                           "Set Base address for TPM device.");
245 
246     machine_class_allow_dynamic_sysbus_dev(mc, TYPE_TPM_TIS_SYSBUS);
247 #endif
248 }
249 
250 static const TypeInfo xen_arm_machine_type = {
251     .name = TYPE_XEN_ARM,
252     .parent = TYPE_MACHINE,
253     .class_init = xen_arm_machine_class_init,
254     .instance_size = sizeof(XenArmState),
255 };
256 
257 static void xen_arm_machine_register_types(void)
258 {
259     type_register_static(&xen_arm_machine_type);
260 }
261 
262 type_init(xen_arm_machine_register_types)
263