xref: /openbmc/qemu/hw/i386/microvm-dt.c (revision b5ab62b3c0050612c7f9b0b4baeb44ebab42775a)
1f5918a99SGerd Hoffmann /*
2f5918a99SGerd Hoffmann  * microvm device tree support
3f5918a99SGerd Hoffmann  *
4f5918a99SGerd Hoffmann  * This generates an device tree for microvm and exports it via fw_cfg
5f5918a99SGerd Hoffmann  * as "etc/fdt" to the firmware (edk2 specifically).
6f5918a99SGerd Hoffmann  *
7f5918a99SGerd Hoffmann  * The use case is to allow edk2 find the pcie ecam and the virtio
8f5918a99SGerd Hoffmann  * devices, without adding an ACPI parser, reusing the fdt parser
9f5918a99SGerd Hoffmann  * which is needed anyway for the arm platform.
10f5918a99SGerd Hoffmann  *
11f5918a99SGerd Hoffmann  * Note 1: The device tree is incomplete. CPUs and memory is missing
12f5918a99SGerd Hoffmann  *         for example, those can be detected using other fw_cfg files.
13f5918a99SGerd Hoffmann  *         Also pci ecam irq routing is not there, edk2 doesn't use
14f5918a99SGerd Hoffmann  *         interrupts.
15f5918a99SGerd Hoffmann  *
16f5918a99SGerd Hoffmann  * Note 2: This is for firmware only. OSes should use the more
17f5918a99SGerd Hoffmann  *         complete ACPI tables for hardware discovery.
18f5918a99SGerd Hoffmann  *
19f5918a99SGerd Hoffmann  * ----------------------------------------------------------------------
20f5918a99SGerd Hoffmann  *
21f5918a99SGerd Hoffmann  * This program is free software; you can redistribute it and/or modify it
22f5918a99SGerd Hoffmann  * under the terms and conditions of the GNU General Public License,
23f5918a99SGerd Hoffmann  * version 2 or later, as published by the Free Software Foundation.
24f5918a99SGerd Hoffmann  *
25f5918a99SGerd Hoffmann  * This program is distributed in the hope it will be useful, but WITHOUT
26f5918a99SGerd Hoffmann  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
27f5918a99SGerd Hoffmann  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
28f5918a99SGerd Hoffmann  * more details.
29f5918a99SGerd Hoffmann  *
30f5918a99SGerd Hoffmann  * You should have received a copy of the GNU General Public License along with
31f5918a99SGerd Hoffmann  * this program.  If not, see <http://www.gnu.org/licenses/>.
32f5918a99SGerd Hoffmann  */
33f5918a99SGerd Hoffmann #include "qemu/osdep.h"
34f5918a99SGerd Hoffmann #include "qemu/cutils.h"
3594c720f3SBernhard Beschow #include "qapi/error.h"
36f5918a99SGerd Hoffmann #include "sysemu/device_tree.h"
37*37b724cdSBernhard Beschow #include "hw/char/serial-isa.h"
38f5918a99SGerd Hoffmann #include "hw/i386/fw_cfg.h"
39f5918a99SGerd Hoffmann #include "hw/rtc/mc146818rtc.h"
40f5918a99SGerd Hoffmann #include "hw/sysbus.h"
41f5918a99SGerd Hoffmann #include "hw/virtio/virtio-mmio.h"
42f5918a99SGerd Hoffmann #include "hw/usb/xhci.h"
43f5918a99SGerd Hoffmann 
44f5918a99SGerd Hoffmann #include "microvm-dt.h"
45f5918a99SGerd Hoffmann 
46f5918a99SGerd Hoffmann static bool debug;
47f5918a99SGerd Hoffmann 
dt_add_microvm_irq(MicrovmMachineState * mms,const char * nodename,uint32_t irq)48f5918a99SGerd Hoffmann static void dt_add_microvm_irq(MicrovmMachineState *mms,
49f5918a99SGerd Hoffmann                                const char *nodename, uint32_t irq)
50f5918a99SGerd Hoffmann {
51f5918a99SGerd Hoffmann     int index = 0;
52f5918a99SGerd Hoffmann 
53f5918a99SGerd Hoffmann     if (irq >= IO_APIC_SECONDARY_IRQBASE) {
54f5918a99SGerd Hoffmann         irq -= IO_APIC_SECONDARY_IRQBASE;
55f5918a99SGerd Hoffmann         index++;
56f5918a99SGerd Hoffmann     }
57f5918a99SGerd Hoffmann 
58f5918a99SGerd Hoffmann     qemu_fdt_setprop_cell(mms->fdt, nodename, "interrupt-parent",
59f5918a99SGerd Hoffmann                           mms->ioapic_phandle[index]);
60f5918a99SGerd Hoffmann     qemu_fdt_setprop_cells(mms->fdt, nodename, "interrupts", irq, 0);
61f5918a99SGerd Hoffmann }
62f5918a99SGerd Hoffmann 
dt_add_virtio(MicrovmMachineState * mms,VirtIOMMIOProxy * mmio)63f5918a99SGerd Hoffmann static void dt_add_virtio(MicrovmMachineState *mms, VirtIOMMIOProxy *mmio)
64f5918a99SGerd Hoffmann {
65f5918a99SGerd Hoffmann     SysBusDevice *dev = SYS_BUS_DEVICE(mmio);
66f5918a99SGerd Hoffmann     VirtioBusState *mmio_virtio_bus = &mmio->bus;
67f5918a99SGerd Hoffmann     BusState *mmio_bus = &mmio_virtio_bus->parent_obj;
68f5918a99SGerd Hoffmann     char *nodename;
69f5918a99SGerd Hoffmann 
70f5918a99SGerd Hoffmann     if (QTAILQ_EMPTY(&mmio_bus->children)) {
71f5918a99SGerd Hoffmann         return;
72f5918a99SGerd Hoffmann     }
73f5918a99SGerd Hoffmann 
74f5918a99SGerd Hoffmann     hwaddr base = dev->mmio[0].addr;
75f5918a99SGerd Hoffmann     hwaddr size = 512;
76f5918a99SGerd Hoffmann     unsigned index = (base - VIRTIO_MMIO_BASE) / size;
77f5918a99SGerd Hoffmann     uint32_t irq = mms->virtio_irq_base + index;
78f5918a99SGerd Hoffmann 
79f5918a99SGerd Hoffmann     nodename = g_strdup_printf("/virtio_mmio@%" PRIx64, base);
80f5918a99SGerd Hoffmann     qemu_fdt_add_subnode(mms->fdt, nodename);
81f5918a99SGerd Hoffmann     qemu_fdt_setprop_string(mms->fdt, nodename, "compatible", "virtio,mmio");
82f5918a99SGerd Hoffmann     qemu_fdt_setprop_sized_cells(mms->fdt, nodename, "reg", 2, base, 2, size);
83f5918a99SGerd Hoffmann     qemu_fdt_setprop(mms->fdt, nodename, "dma-coherent", NULL, 0);
84f5918a99SGerd Hoffmann     dt_add_microvm_irq(mms, nodename, irq);
85f5918a99SGerd Hoffmann     g_free(nodename);
86f5918a99SGerd Hoffmann }
87f5918a99SGerd Hoffmann 
dt_add_xhci(MicrovmMachineState * mms)88f5918a99SGerd Hoffmann static void dt_add_xhci(MicrovmMachineState *mms)
89f5918a99SGerd Hoffmann {
90f5918a99SGerd Hoffmann     const char compat[] = "generic-xhci";
91f5918a99SGerd Hoffmann     uint32_t irq = MICROVM_XHCI_IRQ;
92f5918a99SGerd Hoffmann     hwaddr base = MICROVM_XHCI_BASE;
93f5918a99SGerd Hoffmann     hwaddr size = XHCI_LEN_REGS;
94f5918a99SGerd Hoffmann     char *nodename;
95f5918a99SGerd Hoffmann 
96f5918a99SGerd Hoffmann     nodename = g_strdup_printf("/usb@%" PRIx64, base);
97f5918a99SGerd Hoffmann     qemu_fdt_add_subnode(mms->fdt, nodename);
98f5918a99SGerd Hoffmann     qemu_fdt_setprop(mms->fdt, nodename, "compatible", compat, sizeof(compat));
99f5918a99SGerd Hoffmann     qemu_fdt_setprop_sized_cells(mms->fdt, nodename, "reg", 2, base, 2, size);
100f5918a99SGerd Hoffmann     qemu_fdt_setprop(mms->fdt, nodename, "dma-coherent", NULL, 0);
101f5918a99SGerd Hoffmann     dt_add_microvm_irq(mms, nodename, irq);
102f5918a99SGerd Hoffmann     g_free(nodename);
103f5918a99SGerd Hoffmann }
104f5918a99SGerd Hoffmann 
dt_add_pcie(MicrovmMachineState * mms)105f5918a99SGerd Hoffmann static void dt_add_pcie(MicrovmMachineState *mms)
106f5918a99SGerd Hoffmann {
107f5918a99SGerd Hoffmann     hwaddr base = PCIE_MMIO_BASE;
108f5918a99SGerd Hoffmann     int nr_pcie_buses;
109f5918a99SGerd Hoffmann     char *nodename;
110f5918a99SGerd Hoffmann 
111f5918a99SGerd Hoffmann     nodename = g_strdup_printf("/pcie@%" PRIx64, base);
112f5918a99SGerd Hoffmann     qemu_fdt_add_subnode(mms->fdt, nodename);
113f5918a99SGerd Hoffmann     qemu_fdt_setprop_string(mms->fdt, nodename,
114f5918a99SGerd Hoffmann                             "compatible", "pci-host-ecam-generic");
115f5918a99SGerd Hoffmann     qemu_fdt_setprop_string(mms->fdt, nodename, "device_type", "pci");
116f5918a99SGerd Hoffmann     qemu_fdt_setprop_cell(mms->fdt, nodename, "#address-cells", 3);
117f5918a99SGerd Hoffmann     qemu_fdt_setprop_cell(mms->fdt, nodename, "#size-cells", 2);
118f5918a99SGerd Hoffmann     qemu_fdt_setprop_cell(mms->fdt, nodename, "linux,pci-domain", 0);
119f5918a99SGerd Hoffmann     qemu_fdt_setprop(mms->fdt, nodename, "dma-coherent", NULL, 0);
120f5918a99SGerd Hoffmann 
121f5918a99SGerd Hoffmann     qemu_fdt_setprop_sized_cells(mms->fdt, nodename, "reg",
122f5918a99SGerd Hoffmann                                  2, PCIE_ECAM_BASE, 2, PCIE_ECAM_SIZE);
123f5918a99SGerd Hoffmann     if (mms->gpex.mmio64.size) {
124f5918a99SGerd Hoffmann         qemu_fdt_setprop_sized_cells(mms->fdt, nodename, "ranges",
125f5918a99SGerd Hoffmann 
126f5918a99SGerd Hoffmann                                      1, FDT_PCI_RANGE_MMIO,
127f5918a99SGerd Hoffmann                                      2, mms->gpex.mmio32.base,
128f5918a99SGerd Hoffmann                                      2, mms->gpex.mmio32.base,
129f5918a99SGerd Hoffmann                                      2, mms->gpex.mmio32.size,
130f5918a99SGerd Hoffmann 
131f5918a99SGerd Hoffmann                                      1, FDT_PCI_RANGE_MMIO_64BIT,
132f5918a99SGerd Hoffmann                                      2, mms->gpex.mmio64.base,
133f5918a99SGerd Hoffmann                                      2, mms->gpex.mmio64.base,
134f5918a99SGerd Hoffmann                                      2, mms->gpex.mmio64.size);
135f5918a99SGerd Hoffmann     } else {
136f5918a99SGerd Hoffmann         qemu_fdt_setprop_sized_cells(mms->fdt, nodename, "ranges",
137f5918a99SGerd Hoffmann 
138f5918a99SGerd Hoffmann                                      1, FDT_PCI_RANGE_MMIO,
139f5918a99SGerd Hoffmann                                      2, mms->gpex.mmio32.base,
140f5918a99SGerd Hoffmann                                      2, mms->gpex.mmio32.base,
141f5918a99SGerd Hoffmann                                      2, mms->gpex.mmio32.size);
142f5918a99SGerd Hoffmann     }
143f5918a99SGerd Hoffmann 
144f5918a99SGerd Hoffmann     nr_pcie_buses = PCIE_ECAM_SIZE / PCIE_MMCFG_SIZE_MIN;
145f5918a99SGerd Hoffmann     qemu_fdt_setprop_cells(mms->fdt, nodename, "bus-range", 0,
146f5918a99SGerd Hoffmann                            nr_pcie_buses - 1);
1471b38ccc9SGerd Hoffmann 
1481b38ccc9SGerd Hoffmann     g_free(nodename);
149f5918a99SGerd Hoffmann }
150f5918a99SGerd Hoffmann 
dt_add_ioapic(MicrovmMachineState * mms,SysBusDevice * dev)151f5918a99SGerd Hoffmann static void dt_add_ioapic(MicrovmMachineState *mms, SysBusDevice *dev)
152f5918a99SGerd Hoffmann {
153f5918a99SGerd Hoffmann     hwaddr base = dev->mmio[0].addr;
154f5918a99SGerd Hoffmann     char *nodename;
155f5918a99SGerd Hoffmann     uint32_t ph;
156f5918a99SGerd Hoffmann     int index;
157f5918a99SGerd Hoffmann 
158f5918a99SGerd Hoffmann     switch (base) {
159f5918a99SGerd Hoffmann     case IO_APIC_DEFAULT_ADDRESS:
160f5918a99SGerd Hoffmann         index = 0;
161f5918a99SGerd Hoffmann         break;
162f5918a99SGerd Hoffmann     case IO_APIC_SECONDARY_ADDRESS:
163f5918a99SGerd Hoffmann         index = 1;
164f5918a99SGerd Hoffmann         break;
165f5918a99SGerd Hoffmann     default:
166f5918a99SGerd Hoffmann         fprintf(stderr, "unknown ioapic @ %" PRIx64 "\n", base);
167f5918a99SGerd Hoffmann         return;
168f5918a99SGerd Hoffmann     }
169f5918a99SGerd Hoffmann 
170f5918a99SGerd Hoffmann     nodename = g_strdup_printf("/ioapic%d@%" PRIx64, index + 1, base);
171f5918a99SGerd Hoffmann     qemu_fdt_add_subnode(mms->fdt, nodename);
172f5918a99SGerd Hoffmann     qemu_fdt_setprop_string(mms->fdt, nodename,
173f5918a99SGerd Hoffmann                             "compatible", "intel,ce4100-ioapic");
174f5918a99SGerd Hoffmann     qemu_fdt_setprop(mms->fdt, nodename, "interrupt-controller", NULL, 0);
175f5918a99SGerd Hoffmann     qemu_fdt_setprop_cell(mms->fdt, nodename, "#interrupt-cells", 0x2);
176f5918a99SGerd Hoffmann     qemu_fdt_setprop_cell(mms->fdt, nodename, "#address-cells", 0x2);
177f5918a99SGerd Hoffmann     qemu_fdt_setprop_sized_cells(mms->fdt, nodename, "reg",
178f5918a99SGerd Hoffmann                                  2, base, 2, 0x1000);
179f5918a99SGerd Hoffmann 
180f5918a99SGerd Hoffmann     ph = qemu_fdt_alloc_phandle(mms->fdt);
181f5918a99SGerd Hoffmann     qemu_fdt_setprop_cell(mms->fdt, nodename, "phandle", ph);
182f5918a99SGerd Hoffmann     qemu_fdt_setprop_cell(mms->fdt, nodename, "linux,phandle", ph);
183f5918a99SGerd Hoffmann     mms->ioapic_phandle[index] = ph;
184f5918a99SGerd Hoffmann 
185f5918a99SGerd Hoffmann     g_free(nodename);
186f5918a99SGerd Hoffmann }
187f5918a99SGerd Hoffmann 
dt_add_isa_serial(MicrovmMachineState * mms,ISADevice * dev)188f5918a99SGerd Hoffmann static void dt_add_isa_serial(MicrovmMachineState *mms, ISADevice *dev)
189f5918a99SGerd Hoffmann {
190f5918a99SGerd Hoffmann     const char compat[] = "ns16550";
19194c720f3SBernhard Beschow     uint32_t irq = object_property_get_int(OBJECT(dev), "irq", &error_fatal);
19294c720f3SBernhard Beschow     hwaddr base = object_property_get_int(OBJECT(dev), "iobase", &error_fatal);
193f5918a99SGerd Hoffmann     hwaddr size = 8;
194f5918a99SGerd Hoffmann     char *nodename;
195f5918a99SGerd Hoffmann 
196f5918a99SGerd Hoffmann     nodename = g_strdup_printf("/serial@%" PRIx64, base);
197f5918a99SGerd Hoffmann     qemu_fdt_add_subnode(mms->fdt, nodename);
198f5918a99SGerd Hoffmann     qemu_fdt_setprop(mms->fdt, nodename, "compatible", compat, sizeof(compat));
199f5918a99SGerd Hoffmann     qemu_fdt_setprop_sized_cells(mms->fdt, nodename, "reg", 2, base, 2, size);
200f5918a99SGerd Hoffmann     dt_add_microvm_irq(mms, nodename, irq);
201f5918a99SGerd Hoffmann 
202f5918a99SGerd Hoffmann     if (base == 0x3f8 /* com1 */) {
203f5918a99SGerd Hoffmann         qemu_fdt_setprop_string(mms->fdt, "/chosen", "stdout-path", nodename);
204f5918a99SGerd Hoffmann     }
205f5918a99SGerd Hoffmann 
206f5918a99SGerd Hoffmann     g_free(nodename);
207f5918a99SGerd Hoffmann }
208f5918a99SGerd Hoffmann 
dt_add_isa_rtc(MicrovmMachineState * mms,ISADevice * dev)209f5918a99SGerd Hoffmann static void dt_add_isa_rtc(MicrovmMachineState *mms, ISADevice *dev)
210f5918a99SGerd Hoffmann {
211f5918a99SGerd Hoffmann     const char compat[] = "motorola,mc146818";
2128f3428ccSBernhard Beschow     uint32_t irq = object_property_get_uint(OBJECT(dev), "irq", &error_fatal);
2135b21b331SBernhard Beschow     hwaddr base = object_property_get_uint(OBJECT(dev), "iobase", &error_fatal);
214f5918a99SGerd Hoffmann     hwaddr size = 8;
215f5918a99SGerd Hoffmann     char *nodename;
216f5918a99SGerd Hoffmann 
217f5918a99SGerd Hoffmann     nodename = g_strdup_printf("/rtc@%" PRIx64, base);
218f5918a99SGerd Hoffmann     qemu_fdt_add_subnode(mms->fdt, nodename);
219f5918a99SGerd Hoffmann     qemu_fdt_setprop(mms->fdt, nodename, "compatible", compat, sizeof(compat));
220f5918a99SGerd Hoffmann     qemu_fdt_setprop_sized_cells(mms->fdt, nodename, "reg", 2, base, 2, size);
221f5918a99SGerd Hoffmann     dt_add_microvm_irq(mms, nodename, irq);
222f5918a99SGerd Hoffmann     g_free(nodename);
223f5918a99SGerd Hoffmann }
224f5918a99SGerd Hoffmann 
dt_setup_isa_bus(MicrovmMachineState * mms,DeviceState * bridge)225f5918a99SGerd Hoffmann static void dt_setup_isa_bus(MicrovmMachineState *mms, DeviceState *bridge)
226f5918a99SGerd Hoffmann {
227f5918a99SGerd Hoffmann     BusState *bus = qdev_get_child_bus(bridge, "isa.0");
228f5918a99SGerd Hoffmann     BusChild *kid;
229f5918a99SGerd Hoffmann     Object *obj;
230f5918a99SGerd Hoffmann 
231f5918a99SGerd Hoffmann     QTAILQ_FOREACH(kid, &bus->children, sibling) {
232f5918a99SGerd Hoffmann         DeviceState *dev = kid->child;
233f5918a99SGerd Hoffmann 
234f5918a99SGerd Hoffmann         /* serial */
235f5918a99SGerd Hoffmann         obj = object_dynamic_cast(OBJECT(dev), TYPE_ISA_SERIAL);
236f5918a99SGerd Hoffmann         if (obj) {
237f5918a99SGerd Hoffmann             dt_add_isa_serial(mms, ISA_DEVICE(obj));
238f5918a99SGerd Hoffmann             continue;
239f5918a99SGerd Hoffmann         }
240f5918a99SGerd Hoffmann 
241f5918a99SGerd Hoffmann         /* rtc */
242f5918a99SGerd Hoffmann         obj = object_dynamic_cast(OBJECT(dev), TYPE_MC146818_RTC);
243f5918a99SGerd Hoffmann         if (obj) {
244f5918a99SGerd Hoffmann             dt_add_isa_rtc(mms, ISA_DEVICE(obj));
245f5918a99SGerd Hoffmann             continue;
246f5918a99SGerd Hoffmann         }
247f5918a99SGerd Hoffmann 
248f5918a99SGerd Hoffmann         if (debug) {
249f5918a99SGerd Hoffmann             fprintf(stderr, "%s: unhandled: %s\n", __func__,
250f5918a99SGerd Hoffmann                     object_get_typename(OBJECT(dev)));
251f5918a99SGerd Hoffmann         }
252f5918a99SGerd Hoffmann     }
253f5918a99SGerd Hoffmann }
254f5918a99SGerd Hoffmann 
dt_setup_sys_bus(MicrovmMachineState * mms)255f5918a99SGerd Hoffmann static void dt_setup_sys_bus(MicrovmMachineState *mms)
256f5918a99SGerd Hoffmann {
257f5918a99SGerd Hoffmann     BusState *bus;
258f5918a99SGerd Hoffmann     BusChild *kid;
259f5918a99SGerd Hoffmann     Object *obj;
260f5918a99SGerd Hoffmann 
261f5918a99SGerd Hoffmann     /* sysbus devices */
262f5918a99SGerd Hoffmann     bus = sysbus_get_default();
263f5918a99SGerd Hoffmann     QTAILQ_FOREACH(kid, &bus->children, sibling) {
264f5918a99SGerd Hoffmann         DeviceState *dev = kid->child;
265f5918a99SGerd Hoffmann 
266f5918a99SGerd Hoffmann         /* ioapic */
267f5918a99SGerd Hoffmann         obj = object_dynamic_cast(OBJECT(dev), TYPE_IOAPIC);
268f5918a99SGerd Hoffmann         if (obj) {
269f5918a99SGerd Hoffmann             dt_add_ioapic(mms, SYS_BUS_DEVICE(obj));
270f5918a99SGerd Hoffmann             continue;
271f5918a99SGerd Hoffmann         }
272f5918a99SGerd Hoffmann     }
273f5918a99SGerd Hoffmann 
274f5918a99SGerd Hoffmann     QTAILQ_FOREACH(kid, &bus->children, sibling) {
275f5918a99SGerd Hoffmann         DeviceState *dev = kid->child;
276f5918a99SGerd Hoffmann 
277f5918a99SGerd Hoffmann         /* virtio */
278f5918a99SGerd Hoffmann         obj = object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MMIO);
279f5918a99SGerd Hoffmann         if (obj) {
280f5918a99SGerd Hoffmann             dt_add_virtio(mms, VIRTIO_MMIO(obj));
281f5918a99SGerd Hoffmann             continue;
282f5918a99SGerd Hoffmann         }
283f5918a99SGerd Hoffmann 
284f5918a99SGerd Hoffmann         /* xhci */
285f5918a99SGerd Hoffmann         obj = object_dynamic_cast(OBJECT(dev), TYPE_XHCI_SYSBUS);
286f5918a99SGerd Hoffmann         if (obj) {
287f5918a99SGerd Hoffmann             dt_add_xhci(mms);
288f5918a99SGerd Hoffmann             continue;
289f5918a99SGerd Hoffmann         }
290f5918a99SGerd Hoffmann 
291f5918a99SGerd Hoffmann         /* pcie */
292f5918a99SGerd Hoffmann         obj = object_dynamic_cast(OBJECT(dev), TYPE_GPEX_HOST);
293f5918a99SGerd Hoffmann         if (obj) {
294f5918a99SGerd Hoffmann             dt_add_pcie(mms);
295f5918a99SGerd Hoffmann             continue;
296f5918a99SGerd Hoffmann         }
297f5918a99SGerd Hoffmann 
298f5918a99SGerd Hoffmann         /* isa */
299f5918a99SGerd Hoffmann         obj = object_dynamic_cast(OBJECT(dev), "isabus-bridge");
300f5918a99SGerd Hoffmann         if (obj) {
301f5918a99SGerd Hoffmann             dt_setup_isa_bus(mms, DEVICE(obj));
302f5918a99SGerd Hoffmann             continue;
303f5918a99SGerd Hoffmann         }
304f5918a99SGerd Hoffmann 
305f5918a99SGerd Hoffmann         if (debug) {
306f5918a99SGerd Hoffmann             obj = object_dynamic_cast(OBJECT(dev), TYPE_IOAPIC);
307f5918a99SGerd Hoffmann             if (obj) {
308f5918a99SGerd Hoffmann                 /* ioapic already added in first pass */
309f5918a99SGerd Hoffmann                 continue;
310f5918a99SGerd Hoffmann             }
311f5918a99SGerd Hoffmann             fprintf(stderr, "%s: unhandled: %s\n", __func__,
312f5918a99SGerd Hoffmann                     object_get_typename(OBJECT(dev)));
313f5918a99SGerd Hoffmann         }
314f5918a99SGerd Hoffmann     }
315f5918a99SGerd Hoffmann }
316f5918a99SGerd Hoffmann 
dt_setup_microvm(MicrovmMachineState * mms)317f5918a99SGerd Hoffmann void dt_setup_microvm(MicrovmMachineState *mms)
318f5918a99SGerd Hoffmann {
319f5918a99SGerd Hoffmann     X86MachineState *x86ms = X86_MACHINE(mms);
320f5918a99SGerd Hoffmann     int size = 0;
321f5918a99SGerd Hoffmann 
322f5918a99SGerd Hoffmann     mms->fdt = create_device_tree(&size);
323f5918a99SGerd Hoffmann 
324f5918a99SGerd Hoffmann     /* root node */
325f5918a99SGerd Hoffmann     qemu_fdt_setprop_string(mms->fdt, "/", "compatible", "linux,microvm");
326f5918a99SGerd Hoffmann     qemu_fdt_setprop_cell(mms->fdt, "/", "#address-cells", 0x2);
327f5918a99SGerd Hoffmann     qemu_fdt_setprop_cell(mms->fdt, "/", "#size-cells", 0x2);
328f5918a99SGerd Hoffmann 
329f5918a99SGerd Hoffmann     qemu_fdt_add_subnode(mms->fdt, "/chosen");
330f5918a99SGerd Hoffmann     dt_setup_sys_bus(mms);
331f5918a99SGerd Hoffmann 
332f5918a99SGerd Hoffmann     /* add to fw_cfg */
333d612405eSPhilippe Mathieu-Daudé     if (debug) {
334f5918a99SGerd Hoffmann         fprintf(stderr, "%s: add etc/fdt to fw_cfg\n", __func__);
335d612405eSPhilippe Mathieu-Daudé     }
336f5918a99SGerd Hoffmann     fw_cfg_add_file(x86ms->fw_cfg, "etc/fdt", mms->fdt, size);
337f5918a99SGerd Hoffmann 
338f5918a99SGerd Hoffmann     if (debug) {
339f5918a99SGerd Hoffmann         fprintf(stderr, "%s: writing microvm.fdt\n", __func__);
340b9e5628cSGerd Hoffmann         if (!g_file_set_contents("microvm.fdt", mms->fdt, size, NULL)) {
341b9e5628cSGerd Hoffmann             fprintf(stderr, "%s: writing microvm.fdt failed\n", __func__);
342b9e5628cSGerd Hoffmann             return;
343b9e5628cSGerd Hoffmann         }
344f5918a99SGerd Hoffmann         int ret = system("dtc -I dtb -O dts microvm.fdt");
345f5918a99SGerd Hoffmann         if (ret != 0) {
346f5918a99SGerd Hoffmann             fprintf(stderr, "%s: oops, dtc not installed?\n", __func__);
347f5918a99SGerd Hoffmann         }
348f5918a99SGerd Hoffmann     }
349f5918a99SGerd Hoffmann }
350