xref: /openbmc/qemu/hw/xen/xen-pvh-common.c (revision 692ec933)
1258b2a40SEdgar E. Iglesias /*
2258b2a40SEdgar E. Iglesias  * QEMU Xen PVH machine - common code.
3258b2a40SEdgar E. Iglesias  *
4258b2a40SEdgar E. Iglesias  * Copyright (c) 2024 Advanced Micro Devices, Inc.
5258b2a40SEdgar E. Iglesias  *
6258b2a40SEdgar E. Iglesias  * SPDX-License-Identifier: GPL-2.0-or-later
7258b2a40SEdgar E. Iglesias  */
8258b2a40SEdgar E. Iglesias 
9258b2a40SEdgar E. Iglesias #include "qemu/osdep.h"
10258b2a40SEdgar E. Iglesias #include "qemu/error-report.h"
11258b2a40SEdgar E. Iglesias #include "qapi/error.h"
12258b2a40SEdgar E. Iglesias #include "qapi/visitor.h"
13258b2a40SEdgar E. Iglesias #include "hw/boards.h"
14258b2a40SEdgar E. Iglesias #include "hw/irq.h"
15258b2a40SEdgar E. Iglesias #include "hw/sysbus.h"
16258b2a40SEdgar E. Iglesias #include "sysemu/sysemu.h"
17258b2a40SEdgar E. Iglesias #include "sysemu/tpm.h"
18258b2a40SEdgar E. Iglesias #include "sysemu/tpm_backend.h"
19258b2a40SEdgar E. Iglesias #include "hw/xen/xen-pvh-common.h"
20258b2a40SEdgar E. Iglesias #include "trace.h"
21258b2a40SEdgar E. Iglesias 
22258b2a40SEdgar E. Iglesias static const MemoryListener xen_memory_listener = {
23258b2a40SEdgar E. Iglesias     .region_add = xen_region_add,
24258b2a40SEdgar E. Iglesias     .region_del = xen_region_del,
25258b2a40SEdgar E. Iglesias     .log_start = NULL,
26258b2a40SEdgar E. Iglesias     .log_stop = NULL,
27258b2a40SEdgar E. Iglesias     .log_sync = NULL,
28258b2a40SEdgar E. Iglesias     .log_global_start = NULL,
29258b2a40SEdgar E. Iglesias     .log_global_stop = NULL,
30258b2a40SEdgar E. Iglesias     .priority = MEMORY_LISTENER_PRIORITY_ACCEL,
31258b2a40SEdgar E. Iglesias };
32258b2a40SEdgar E. Iglesias 
33258b2a40SEdgar E. Iglesias static void xen_pvh_init_ram(XenPVHMachineState *s,
34258b2a40SEdgar E. Iglesias                              MemoryRegion *sysmem)
35258b2a40SEdgar E. Iglesias {
36258b2a40SEdgar E. Iglesias     MachineState *ms = MACHINE(s);
37258b2a40SEdgar E. Iglesias     ram_addr_t block_len, ram_size[2];
38258b2a40SEdgar E. Iglesias 
39258b2a40SEdgar E. Iglesias     if (ms->ram_size <= s->cfg.ram_low.size) {
40258b2a40SEdgar E. Iglesias         ram_size[0] = ms->ram_size;
41258b2a40SEdgar E. Iglesias         ram_size[1] = 0;
42258b2a40SEdgar E. Iglesias         block_len = s->cfg.ram_low.base + ram_size[0];
43258b2a40SEdgar E. Iglesias     } else {
44258b2a40SEdgar E. Iglesias         ram_size[0] = s->cfg.ram_low.size;
45258b2a40SEdgar E. Iglesias         ram_size[1] = ms->ram_size - s->cfg.ram_low.size;
46258b2a40SEdgar E. Iglesias         block_len = s->cfg.ram_high.base + ram_size[1];
47258b2a40SEdgar E. Iglesias     }
48258b2a40SEdgar E. Iglesias 
49258b2a40SEdgar E. Iglesias     memory_region_init_ram(&xen_memory, NULL, "xen.ram", block_len,
50258b2a40SEdgar E. Iglesias                            &error_fatal);
51258b2a40SEdgar E. Iglesias 
52258b2a40SEdgar E. Iglesias     memory_region_init_alias(&s->ram.low, NULL, "xen.ram.lo", &xen_memory,
53258b2a40SEdgar E. Iglesias                              s->cfg.ram_low.base, ram_size[0]);
54258b2a40SEdgar E. Iglesias     memory_region_add_subregion(sysmem, s->cfg.ram_low.base, &s->ram.low);
55258b2a40SEdgar E. Iglesias     if (ram_size[1] > 0) {
56258b2a40SEdgar E. Iglesias         memory_region_init_alias(&s->ram.high, NULL, "xen.ram.hi", &xen_memory,
57258b2a40SEdgar E. Iglesias                                  s->cfg.ram_high.base, ram_size[1]);
58258b2a40SEdgar E. Iglesias         memory_region_add_subregion(sysmem, s->cfg.ram_high.base, &s->ram.high);
59258b2a40SEdgar E. Iglesias     }
60258b2a40SEdgar E. Iglesias 
61258b2a40SEdgar E. Iglesias     /* Setup support for grants.  */
62258b2a40SEdgar E. Iglesias     memory_region_init_ram(&xen_grants, NULL, "xen.grants", block_len,
63258b2a40SEdgar E. Iglesias                            &error_fatal);
64258b2a40SEdgar E. Iglesias     memory_region_add_subregion(sysmem, XEN_GRANT_ADDR_OFF, &xen_grants);
65258b2a40SEdgar E. Iglesias }
66258b2a40SEdgar E. Iglesias 
67258b2a40SEdgar E. Iglesias static void xen_set_irq(void *opaque, int irq, int level)
68258b2a40SEdgar E. Iglesias {
69258b2a40SEdgar E. Iglesias     if (xendevicemodel_set_irq_level(xen_dmod, xen_domid, irq, level)) {
70258b2a40SEdgar E. Iglesias         error_report("xendevicemodel_set_irq_level failed");
71258b2a40SEdgar E. Iglesias     }
72258b2a40SEdgar E. Iglesias }
73258b2a40SEdgar E. Iglesias 
74258b2a40SEdgar E. Iglesias static void xen_create_virtio_mmio_devices(XenPVHMachineState *s)
75258b2a40SEdgar E. Iglesias {
76258b2a40SEdgar E. Iglesias     int i;
77258b2a40SEdgar E. Iglesias 
78*692ec933SEdgar E. Iglesias     /*
79*692ec933SEdgar E. Iglesias      * We create the transports in reverse order. Since qbus_realize()
80*692ec933SEdgar E. Iglesias      * prepends (not appends) new child buses, the decrementing loop below will
81*692ec933SEdgar E. Iglesias      * create a list of virtio-mmio buses with increasing base addresses.
82*692ec933SEdgar E. Iglesias      *
83*692ec933SEdgar E. Iglesias      * When a -device option is processed from the command line,
84*692ec933SEdgar E. Iglesias      * qbus_find_recursive() picks the next free virtio-mmio bus in forwards
85*692ec933SEdgar E. Iglesias      * order.
86*692ec933SEdgar E. Iglesias      *
87*692ec933SEdgar E. Iglesias      * This is what the Xen tools expect.
88*692ec933SEdgar E. Iglesias      */
89*692ec933SEdgar E. Iglesias     for (i = s->cfg.virtio_mmio_num - 1; i >= 0; i--) {
90258b2a40SEdgar E. Iglesias         hwaddr base = s->cfg.virtio_mmio.base + i * s->cfg.virtio_mmio.size;
91258b2a40SEdgar E. Iglesias         qemu_irq irq = qemu_allocate_irq(xen_set_irq, NULL,
92258b2a40SEdgar E. Iglesias                                          s->cfg.virtio_mmio_irq_base + i);
93258b2a40SEdgar E. Iglesias 
94258b2a40SEdgar E. Iglesias         sysbus_create_simple("virtio-mmio", base, irq);
95258b2a40SEdgar E. Iglesias 
96258b2a40SEdgar E. Iglesias         trace_xen_create_virtio_mmio_devices(i,
97258b2a40SEdgar E. Iglesias                                              s->cfg.virtio_mmio_irq_base + i,
98258b2a40SEdgar E. Iglesias                                              base);
99258b2a40SEdgar E. Iglesias     }
100258b2a40SEdgar E. Iglesias }
101258b2a40SEdgar E. Iglesias 
102258b2a40SEdgar E. Iglesias #ifdef CONFIG_TPM
103258b2a40SEdgar E. Iglesias static void xen_enable_tpm(XenPVHMachineState *s)
104258b2a40SEdgar E. Iglesias {
105258b2a40SEdgar E. Iglesias     Error *errp = NULL;
106258b2a40SEdgar E. Iglesias     DeviceState *dev;
107258b2a40SEdgar E. Iglesias     SysBusDevice *busdev;
108258b2a40SEdgar E. Iglesias 
109258b2a40SEdgar E. Iglesias     TPMBackend *be = qemu_find_tpm_be("tpm0");
110258b2a40SEdgar E. Iglesias     if (be == NULL) {
111258b2a40SEdgar E. Iglesias         error_report("Couldn't find tmp0 backend");
112258b2a40SEdgar E. Iglesias         return;
113258b2a40SEdgar E. Iglesias     }
114258b2a40SEdgar E. Iglesias     dev = qdev_new(TYPE_TPM_TIS_SYSBUS);
115258b2a40SEdgar E. Iglesias     object_property_set_link(OBJECT(dev), "tpmdev", OBJECT(be), &errp);
116258b2a40SEdgar E. Iglesias     object_property_set_str(OBJECT(dev), "tpmdev", be->id, &errp);
117258b2a40SEdgar E. Iglesias     busdev = SYS_BUS_DEVICE(dev);
118258b2a40SEdgar E. Iglesias     sysbus_realize_and_unref(busdev, &error_fatal);
119258b2a40SEdgar E. Iglesias     sysbus_mmio_map(busdev, 0, s->cfg.tpm.base);
120258b2a40SEdgar E. Iglesias 
121258b2a40SEdgar E. Iglesias     trace_xen_enable_tpm(s->cfg.tpm.base);
122258b2a40SEdgar E. Iglesias }
123258b2a40SEdgar E. Iglesias #endif
124258b2a40SEdgar E. Iglesias 
125258b2a40SEdgar E. Iglesias static void xen_pvh_init(MachineState *ms)
126258b2a40SEdgar E. Iglesias {
127258b2a40SEdgar E. Iglesias     XenPVHMachineState *s = XEN_PVH_MACHINE(ms);
128258b2a40SEdgar E. Iglesias     XenPVHMachineClass *xpc = XEN_PVH_MACHINE_GET_CLASS(s);
129258b2a40SEdgar E. Iglesias     MemoryRegion *sysmem = get_system_memory();
130258b2a40SEdgar E. Iglesias 
131258b2a40SEdgar E. Iglesias     if (ms->ram_size == 0) {
132258b2a40SEdgar E. Iglesias         warn_report("%s: ram size not specified. QEMU machine started"
133258b2a40SEdgar E. Iglesias                     " without IOREQ (no emulated devices including virtio)",
134258b2a40SEdgar E. Iglesias                     MACHINE_CLASS(object_get_class(OBJECT(ms)))->desc);
135258b2a40SEdgar E. Iglesias         return;
136258b2a40SEdgar E. Iglesias     }
137258b2a40SEdgar E. Iglesias 
138258b2a40SEdgar E. Iglesias     xen_pvh_init_ram(s, sysmem);
139258b2a40SEdgar E. Iglesias     xen_register_ioreq(&s->ioreq, ms->smp.max_cpus, &xen_memory_listener);
140258b2a40SEdgar E. Iglesias 
141258b2a40SEdgar E. Iglesias     if (s->cfg.virtio_mmio_num) {
142258b2a40SEdgar E. Iglesias         xen_create_virtio_mmio_devices(s);
143258b2a40SEdgar E. Iglesias     }
144258b2a40SEdgar E. Iglesias 
145258b2a40SEdgar E. Iglesias #ifdef CONFIG_TPM
146258b2a40SEdgar E. Iglesias     if (xpc->has_tpm) {
147258b2a40SEdgar E. Iglesias         if (s->cfg.tpm.base) {
148258b2a40SEdgar E. Iglesias             xen_enable_tpm(s);
149258b2a40SEdgar E. Iglesias         } else {
150258b2a40SEdgar E. Iglesias             warn_report("tpm-base-addr is not set. TPM will not be enabled");
151258b2a40SEdgar E. Iglesias         }
152258b2a40SEdgar E. Iglesias     }
153258b2a40SEdgar E. Iglesias #endif
154258b2a40SEdgar E. Iglesias 
155258b2a40SEdgar E. Iglesias     /* Call the implementation specific init.  */
156258b2a40SEdgar E. Iglesias     if (xpc->init) {
157258b2a40SEdgar E. Iglesias         xpc->init(ms);
158258b2a40SEdgar E. Iglesias     }
159258b2a40SEdgar E. Iglesias }
160258b2a40SEdgar E. Iglesias 
161258b2a40SEdgar E. Iglesias #define XEN_PVH_PROP_MEMMAP_SETTER(n, f)                                   \
162258b2a40SEdgar E. Iglesias static void xen_pvh_set_ ## n ## _ ## f(Object *obj, Visitor *v,           \
163258b2a40SEdgar E. Iglesias                                        const char *name, void *opaque,     \
164258b2a40SEdgar E. Iglesias                                        Error **errp)                       \
165258b2a40SEdgar E. Iglesias {                                                                          \
166258b2a40SEdgar E. Iglesias     XenPVHMachineState *xp = XEN_PVH_MACHINE(obj);                         \
167258b2a40SEdgar E. Iglesias     uint64_t value;                                                        \
168258b2a40SEdgar E. Iglesias                                                                            \
169258b2a40SEdgar E. Iglesias     if (!visit_type_size(v, name, &value, errp)) {                         \
170258b2a40SEdgar E. Iglesias         return;                                                            \
171258b2a40SEdgar E. Iglesias     }                                                                      \
172258b2a40SEdgar E. Iglesias     xp->cfg.n.f = value;                                                   \
173258b2a40SEdgar E. Iglesias }
174258b2a40SEdgar E. Iglesias 
175258b2a40SEdgar E. Iglesias #define XEN_PVH_PROP_MEMMAP_GETTER(n, f)                                   \
176258b2a40SEdgar E. Iglesias static void xen_pvh_get_ ## n ## _ ## f(Object *obj, Visitor *v,           \
177258b2a40SEdgar E. Iglesias                                        const char *name, void *opaque,     \
178258b2a40SEdgar E. Iglesias                                        Error **errp)                       \
179258b2a40SEdgar E. Iglesias {                                                                          \
180258b2a40SEdgar E. Iglesias     XenPVHMachineState *xp = XEN_PVH_MACHINE(obj);                         \
181258b2a40SEdgar E. Iglesias     uint64_t value = xp->cfg.n.f;                                          \
182258b2a40SEdgar E. Iglesias                                                                            \
183258b2a40SEdgar E. Iglesias     visit_type_uint64(v, name, &value, errp);                              \
184258b2a40SEdgar E. Iglesias }
185258b2a40SEdgar E. Iglesias 
186258b2a40SEdgar E. Iglesias #define XEN_PVH_PROP_MEMMAP_BASE(n)        \
187258b2a40SEdgar E. Iglesias     XEN_PVH_PROP_MEMMAP_SETTER(n, base)    \
188258b2a40SEdgar E. Iglesias     XEN_PVH_PROP_MEMMAP_GETTER(n, base)    \
189258b2a40SEdgar E. Iglesias 
190258b2a40SEdgar E. Iglesias #define XEN_PVH_PROP_MEMMAP_SIZE(n)        \
191258b2a40SEdgar E. Iglesias     XEN_PVH_PROP_MEMMAP_SETTER(n, size)    \
192258b2a40SEdgar E. Iglesias     XEN_PVH_PROP_MEMMAP_GETTER(n, size)
193258b2a40SEdgar E. Iglesias 
194258b2a40SEdgar E. Iglesias #define XEN_PVH_PROP_MEMMAP(n)             \
195258b2a40SEdgar E. Iglesias     XEN_PVH_PROP_MEMMAP_BASE(n)            \
196258b2a40SEdgar E. Iglesias     XEN_PVH_PROP_MEMMAP_SIZE(n)
197258b2a40SEdgar E. Iglesias 
198258b2a40SEdgar E. Iglesias XEN_PVH_PROP_MEMMAP(ram_low)
199258b2a40SEdgar E. Iglesias XEN_PVH_PROP_MEMMAP(ram_high)
200258b2a40SEdgar E. Iglesias /* TPM only has a base-addr option.  */
201258b2a40SEdgar E. Iglesias XEN_PVH_PROP_MEMMAP_BASE(tpm)
202258b2a40SEdgar E. Iglesias XEN_PVH_PROP_MEMMAP(virtio_mmio)
203258b2a40SEdgar E. Iglesias 
204258b2a40SEdgar E. Iglesias void xen_pvh_class_setup_common_props(XenPVHMachineClass *xpc)
205258b2a40SEdgar E. Iglesias {
206258b2a40SEdgar E. Iglesias     ObjectClass *oc = OBJECT_CLASS(xpc);
207258b2a40SEdgar E. Iglesias     MachineClass *mc = MACHINE_CLASS(xpc);
208258b2a40SEdgar E. Iglesias 
209258b2a40SEdgar E. Iglesias #define OC_MEMMAP_PROP_BASE(c, prop_name, name)                           \
210258b2a40SEdgar E. Iglesias do {                                                                      \
211258b2a40SEdgar E. Iglesias     object_class_property_add(c, prop_name "-base", "uint64_t",           \
212258b2a40SEdgar E. Iglesias                               xen_pvh_get_ ## name ## _base,              \
213258b2a40SEdgar E. Iglesias                               xen_pvh_set_ ## name ## _base, NULL, NULL); \
214258b2a40SEdgar E. Iglesias     object_class_property_set_description(oc, prop_name "-base",          \
215258b2a40SEdgar E. Iglesias                               "Set base address for " prop_name);         \
216258b2a40SEdgar E. Iglesias } while (0)
217258b2a40SEdgar E. Iglesias 
218258b2a40SEdgar E. Iglesias #define OC_MEMMAP_PROP_SIZE(c, prop_name, name)                           \
219258b2a40SEdgar E. Iglesias do {                                                                      \
220258b2a40SEdgar E. Iglesias     object_class_property_add(c, prop_name "-size", "uint64_t",           \
221258b2a40SEdgar E. Iglesias                               xen_pvh_get_ ## name ## _size,              \
222258b2a40SEdgar E. Iglesias                               xen_pvh_set_ ## name ## _size, NULL, NULL); \
223258b2a40SEdgar E. Iglesias     object_class_property_set_description(oc, prop_name "-size",          \
224258b2a40SEdgar E. Iglesias                               "Set memory range size for " prop_name);    \
225258b2a40SEdgar E. Iglesias } while (0)
226258b2a40SEdgar E. Iglesias 
227258b2a40SEdgar E. Iglesias #define OC_MEMMAP_PROP(c, prop_name, name)                                \
228258b2a40SEdgar E. Iglesias do {                                                                      \
229258b2a40SEdgar E. Iglesias         OC_MEMMAP_PROP_BASE(c, prop_name, name);                          \
230258b2a40SEdgar E. Iglesias         OC_MEMMAP_PROP_SIZE(c, prop_name, name);                          \
231258b2a40SEdgar E. Iglesias } while (0)
232258b2a40SEdgar E. Iglesias 
233258b2a40SEdgar E. Iglesias     /*
234258b2a40SEdgar E. Iglesias      * We provide memmap properties to allow Xen to move things to other
235258b2a40SEdgar E. Iglesias      * addresses for example when users need to accomodate the memory-map
236258b2a40SEdgar E. Iglesias      * for 1:1 mapped devices/memory.
237258b2a40SEdgar E. Iglesias      */
238258b2a40SEdgar E. Iglesias     OC_MEMMAP_PROP(oc, "ram-low", ram_low);
239258b2a40SEdgar E. Iglesias     OC_MEMMAP_PROP(oc, "ram-high", ram_high);
240258b2a40SEdgar E. Iglesias 
241258b2a40SEdgar E. Iglesias     if (xpc->has_virtio_mmio) {
242258b2a40SEdgar E. Iglesias         OC_MEMMAP_PROP(oc, "virtio-mmio", virtio_mmio);
243258b2a40SEdgar E. Iglesias     }
244258b2a40SEdgar E. Iglesias 
245258b2a40SEdgar E. Iglesias #ifdef CONFIG_TPM
246258b2a40SEdgar E. Iglesias     if (xpc->has_tpm) {
247258b2a40SEdgar E. Iglesias         object_class_property_add(oc, "tpm-base-addr", "uint64_t",
248258b2a40SEdgar E. Iglesias                                   xen_pvh_get_tpm_base,
249258b2a40SEdgar E. Iglesias                                   xen_pvh_set_tpm_base,
250258b2a40SEdgar E. Iglesias                                   NULL, NULL);
251258b2a40SEdgar E. Iglesias         object_class_property_set_description(oc, "tpm-base-addr",
252258b2a40SEdgar E. Iglesias                                   "Set Base address for TPM device.");
253258b2a40SEdgar E. Iglesias 
254258b2a40SEdgar E. Iglesias         machine_class_allow_dynamic_sysbus_dev(mc, TYPE_TPM_TIS_SYSBUS);
255258b2a40SEdgar E. Iglesias     }
256258b2a40SEdgar E. Iglesias #endif
257258b2a40SEdgar E. Iglesias }
258258b2a40SEdgar E. Iglesias 
259258b2a40SEdgar E. Iglesias static void xen_pvh_class_init(ObjectClass *oc, void *data)
260258b2a40SEdgar E. Iglesias {
261258b2a40SEdgar E. Iglesias     MachineClass *mc = MACHINE_CLASS(oc);
262258b2a40SEdgar E. Iglesias 
263258b2a40SEdgar E. Iglesias     mc->init = xen_pvh_init;
264258b2a40SEdgar E. Iglesias 
265258b2a40SEdgar E. Iglesias     mc->desc = "Xen PVH machine";
266258b2a40SEdgar E. Iglesias     mc->max_cpus = 1;
267258b2a40SEdgar E. Iglesias     mc->default_machine_opts = "accel=xen";
268258b2a40SEdgar E. Iglesias     /* Set to zero to make sure that the real ram size is passed. */
269258b2a40SEdgar E. Iglesias     mc->default_ram_size = 0;
270258b2a40SEdgar E. Iglesias }
271258b2a40SEdgar E. Iglesias 
272258b2a40SEdgar E. Iglesias static const TypeInfo xen_pvh_info = {
273258b2a40SEdgar E. Iglesias     .name = TYPE_XEN_PVH_MACHINE,
274258b2a40SEdgar E. Iglesias     .parent = TYPE_MACHINE,
275258b2a40SEdgar E. Iglesias     .abstract = true,
276258b2a40SEdgar E. Iglesias     .instance_size = sizeof(XenPVHMachineState),
277258b2a40SEdgar E. Iglesias     .class_size = sizeof(XenPVHMachineClass),
278258b2a40SEdgar E. Iglesias     .class_init = xen_pvh_class_init,
279258b2a40SEdgar E. Iglesias };
280258b2a40SEdgar E. Iglesias 
281258b2a40SEdgar E. Iglesias static void xen_pvh_register_types(void)
282258b2a40SEdgar E. Iglesias {
283258b2a40SEdgar E. Iglesias     type_register_static(&xen_pvh_info);
284258b2a40SEdgar E. Iglesias }
285258b2a40SEdgar E. Iglesias 
286258b2a40SEdgar E. Iglesias type_init(xen_pvh_register_types);
287