microvm-dt.c 11 KB

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