2
0

microvm-dt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 "qapi/error.h"
  36. #include "system/device_tree.h"
  37. #include "hw/char/serial-isa.h"
  38. #include "hw/i386/fw_cfg.h"
  39. #include "hw/rtc/mc146818rtc.h"
  40. #include "hw/sysbus.h"
  41. #include "hw/virtio/virtio-mmio.h"
  42. #include "hw/usb/xhci.h"
  43. #include "microvm-dt.h"
  44. static bool debug;
  45. static void dt_add_microvm_irq(MicrovmMachineState *mms,
  46. const char *nodename, uint32_t irq)
  47. {
  48. int index = 0;
  49. if (irq >= IO_APIC_SECONDARY_IRQBASE) {
  50. irq -= IO_APIC_SECONDARY_IRQBASE;
  51. index++;
  52. }
  53. qemu_fdt_setprop_cell(mms->fdt, nodename, "interrupt-parent",
  54. mms->ioapic_phandle[index]);
  55. qemu_fdt_setprop_cells(mms->fdt, nodename, "interrupts", irq, 0);
  56. }
  57. static void dt_add_virtio(MicrovmMachineState *mms, VirtIOMMIOProxy *mmio)
  58. {
  59. SysBusDevice *dev = SYS_BUS_DEVICE(mmio);
  60. VirtioBusState *mmio_virtio_bus = &mmio->bus;
  61. BusState *mmio_bus = &mmio_virtio_bus->parent_obj;
  62. char *nodename;
  63. if (QTAILQ_EMPTY(&mmio_bus->children)) {
  64. return;
  65. }
  66. hwaddr base = dev->mmio[0].addr;
  67. hwaddr size = 512;
  68. unsigned index = (base - VIRTIO_MMIO_BASE) / size;
  69. uint32_t irq = mms->virtio_irq_base + index;
  70. nodename = g_strdup_printf("/virtio_mmio@%" PRIx64, base);
  71. qemu_fdt_add_subnode(mms->fdt, nodename);
  72. qemu_fdt_setprop_string(mms->fdt, nodename, "compatible", "virtio,mmio");
  73. qemu_fdt_setprop_sized_cells(mms->fdt, nodename, "reg", 2, base, 2, size);
  74. qemu_fdt_setprop(mms->fdt, nodename, "dma-coherent", NULL, 0);
  75. dt_add_microvm_irq(mms, nodename, irq);
  76. g_free(nodename);
  77. }
  78. static void dt_add_xhci(MicrovmMachineState *mms)
  79. {
  80. const char compat[] = "generic-xhci";
  81. uint32_t irq = MICROVM_XHCI_IRQ;
  82. hwaddr base = MICROVM_XHCI_BASE;
  83. hwaddr size = XHCI_LEN_REGS;
  84. char *nodename;
  85. nodename = g_strdup_printf("/usb@%" PRIx64, base);
  86. qemu_fdt_add_subnode(mms->fdt, nodename);
  87. qemu_fdt_setprop(mms->fdt, nodename, "compatible", compat, sizeof(compat));
  88. qemu_fdt_setprop_sized_cells(mms->fdt, nodename, "reg", 2, base, 2, size);
  89. qemu_fdt_setprop(mms->fdt, nodename, "dma-coherent", NULL, 0);
  90. dt_add_microvm_irq(mms, nodename, irq);
  91. g_free(nodename);
  92. }
  93. static void dt_add_pcie(MicrovmMachineState *mms)
  94. {
  95. hwaddr base = PCIE_MMIO_BASE;
  96. int nr_pcie_buses;
  97. char *nodename;
  98. nodename = g_strdup_printf("/pcie@%" PRIx64, base);
  99. qemu_fdt_add_subnode(mms->fdt, nodename);
  100. qemu_fdt_setprop_string(mms->fdt, nodename,
  101. "compatible", "pci-host-ecam-generic");
  102. qemu_fdt_setprop_string(mms->fdt, nodename, "device_type", "pci");
  103. qemu_fdt_setprop_cell(mms->fdt, nodename, "#address-cells", 3);
  104. qemu_fdt_setprop_cell(mms->fdt, nodename, "#size-cells", 2);
  105. qemu_fdt_setprop_cell(mms->fdt, nodename, "linux,pci-domain", 0);
  106. qemu_fdt_setprop(mms->fdt, nodename, "dma-coherent", NULL, 0);
  107. qemu_fdt_setprop_sized_cells(mms->fdt, nodename, "reg",
  108. 2, PCIE_ECAM_BASE, 2, PCIE_ECAM_SIZE);
  109. if (mms->gpex.mmio64.size) {
  110. qemu_fdt_setprop_sized_cells(mms->fdt, nodename, "ranges",
  111. 1, FDT_PCI_RANGE_MMIO,
  112. 2, mms->gpex.mmio32.base,
  113. 2, mms->gpex.mmio32.base,
  114. 2, mms->gpex.mmio32.size,
  115. 1, FDT_PCI_RANGE_MMIO_64BIT,
  116. 2, mms->gpex.mmio64.base,
  117. 2, mms->gpex.mmio64.base,
  118. 2, mms->gpex.mmio64.size);
  119. } else {
  120. qemu_fdt_setprop_sized_cells(mms->fdt, nodename, "ranges",
  121. 1, FDT_PCI_RANGE_MMIO,
  122. 2, mms->gpex.mmio32.base,
  123. 2, mms->gpex.mmio32.base,
  124. 2, mms->gpex.mmio32.size);
  125. }
  126. nr_pcie_buses = PCIE_ECAM_SIZE / PCIE_MMCFG_SIZE_MIN;
  127. qemu_fdt_setprop_cells(mms->fdt, nodename, "bus-range", 0,
  128. nr_pcie_buses - 1);
  129. g_free(nodename);
  130. }
  131. static void dt_add_ioapic(MicrovmMachineState *mms, SysBusDevice *dev)
  132. {
  133. hwaddr base = dev->mmio[0].addr;
  134. char *nodename;
  135. uint32_t ph;
  136. int index;
  137. switch (base) {
  138. case IO_APIC_DEFAULT_ADDRESS:
  139. index = 0;
  140. break;
  141. case IO_APIC_SECONDARY_ADDRESS:
  142. index = 1;
  143. break;
  144. default:
  145. fprintf(stderr, "unknown ioapic @ %" PRIx64 "\n", base);
  146. return;
  147. }
  148. nodename = g_strdup_printf("/ioapic%d@%" PRIx64, index + 1, base);
  149. qemu_fdt_add_subnode(mms->fdt, nodename);
  150. qemu_fdt_setprop_string(mms->fdt, nodename,
  151. "compatible", "intel,ce4100-ioapic");
  152. qemu_fdt_setprop(mms->fdt, nodename, "interrupt-controller", NULL, 0);
  153. qemu_fdt_setprop_cell(mms->fdt, nodename, "#interrupt-cells", 0x2);
  154. qemu_fdt_setprop_cell(mms->fdt, nodename, "#address-cells", 0x2);
  155. qemu_fdt_setprop_sized_cells(mms->fdt, nodename, "reg",
  156. 2, base, 2, 0x1000);
  157. ph = qemu_fdt_alloc_phandle(mms->fdt);
  158. qemu_fdt_setprop_cell(mms->fdt, nodename, "phandle", ph);
  159. qemu_fdt_setprop_cell(mms->fdt, nodename, "linux,phandle", ph);
  160. mms->ioapic_phandle[index] = ph;
  161. g_free(nodename);
  162. }
  163. static void dt_add_isa_serial(MicrovmMachineState *mms, ISADevice *dev)
  164. {
  165. const char compat[] = "ns16550";
  166. uint32_t irq = object_property_get_int(OBJECT(dev), "irq", &error_fatal);
  167. hwaddr base = object_property_get_int(OBJECT(dev), "iobase", &error_fatal);
  168. hwaddr size = 8;
  169. char *nodename;
  170. nodename = g_strdup_printf("/serial@%" PRIx64, base);
  171. qemu_fdt_add_subnode(mms->fdt, nodename);
  172. qemu_fdt_setprop(mms->fdt, nodename, "compatible", compat, sizeof(compat));
  173. qemu_fdt_setprop_sized_cells(mms->fdt, nodename, "reg", 2, base, 2, size);
  174. dt_add_microvm_irq(mms, nodename, irq);
  175. if (base == 0x3f8 /* com1 */) {
  176. qemu_fdt_setprop_string(mms->fdt, "/chosen", "stdout-path", nodename);
  177. }
  178. g_free(nodename);
  179. }
  180. static void dt_add_isa_rtc(MicrovmMachineState *mms, ISADevice *dev)
  181. {
  182. const char compat[] = "motorola,mc146818";
  183. uint32_t irq = object_property_get_uint(OBJECT(dev), "irq", &error_fatal);
  184. hwaddr base = object_property_get_uint(OBJECT(dev), "iobase", &error_fatal);
  185. hwaddr size = 8;
  186. char *nodename;
  187. nodename = g_strdup_printf("/rtc@%" PRIx64, base);
  188. qemu_fdt_add_subnode(mms->fdt, nodename);
  189. qemu_fdt_setprop(mms->fdt, nodename, "compatible", compat, sizeof(compat));
  190. qemu_fdt_setprop_sized_cells(mms->fdt, nodename, "reg", 2, base, 2, size);
  191. dt_add_microvm_irq(mms, nodename, irq);
  192. g_free(nodename);
  193. }
  194. static void dt_setup_isa_bus(MicrovmMachineState *mms, DeviceState *bridge)
  195. {
  196. BusState *bus = qdev_get_child_bus(bridge, "isa.0");
  197. BusChild *kid;
  198. Object *obj;
  199. QTAILQ_FOREACH(kid, &bus->children, sibling) {
  200. DeviceState *dev = kid->child;
  201. /* serial */
  202. obj = object_dynamic_cast(OBJECT(dev), TYPE_ISA_SERIAL);
  203. if (obj) {
  204. dt_add_isa_serial(mms, ISA_DEVICE(obj));
  205. continue;
  206. }
  207. /* rtc */
  208. obj = object_dynamic_cast(OBJECT(dev), TYPE_MC146818_RTC);
  209. if (obj) {
  210. dt_add_isa_rtc(mms, ISA_DEVICE(obj));
  211. continue;
  212. }
  213. if (debug) {
  214. fprintf(stderr, "%s: unhandled: %s\n", __func__,
  215. object_get_typename(OBJECT(dev)));
  216. }
  217. }
  218. }
  219. static void dt_setup_sys_bus(MicrovmMachineState *mms)
  220. {
  221. BusState *bus;
  222. BusChild *kid;
  223. Object *obj;
  224. /* sysbus devices */
  225. bus = sysbus_get_default();
  226. QTAILQ_FOREACH(kid, &bus->children, sibling) {
  227. DeviceState *dev = kid->child;
  228. /* ioapic */
  229. obj = object_dynamic_cast(OBJECT(dev), TYPE_IOAPIC);
  230. if (obj) {
  231. dt_add_ioapic(mms, SYS_BUS_DEVICE(obj));
  232. continue;
  233. }
  234. }
  235. QTAILQ_FOREACH(kid, &bus->children, sibling) {
  236. DeviceState *dev = kid->child;
  237. /* virtio */
  238. obj = object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MMIO);
  239. if (obj) {
  240. dt_add_virtio(mms, VIRTIO_MMIO(obj));
  241. continue;
  242. }
  243. /* xhci */
  244. obj = object_dynamic_cast(OBJECT(dev), TYPE_XHCI_SYSBUS);
  245. if (obj) {
  246. dt_add_xhci(mms);
  247. continue;
  248. }
  249. /* pcie */
  250. obj = object_dynamic_cast(OBJECT(dev), TYPE_GPEX_HOST);
  251. if (obj) {
  252. dt_add_pcie(mms);
  253. continue;
  254. }
  255. /* isa */
  256. obj = object_dynamic_cast(OBJECT(dev), "isabus-bridge");
  257. if (obj) {
  258. dt_setup_isa_bus(mms, DEVICE(obj));
  259. continue;
  260. }
  261. if (debug) {
  262. obj = object_dynamic_cast(OBJECT(dev), TYPE_IOAPIC);
  263. if (obj) {
  264. /* ioapic already added in first pass */
  265. continue;
  266. }
  267. fprintf(stderr, "%s: unhandled: %s\n", __func__,
  268. object_get_typename(OBJECT(dev)));
  269. }
  270. }
  271. }
  272. void dt_setup_microvm(MicrovmMachineState *mms)
  273. {
  274. X86MachineState *x86ms = X86_MACHINE(mms);
  275. int size = 0;
  276. mms->fdt = create_device_tree(&size);
  277. /* root node */
  278. qemu_fdt_setprop_string(mms->fdt, "/", "compatible", "linux,microvm");
  279. qemu_fdt_setprop_cell(mms->fdt, "/", "#address-cells", 0x2);
  280. qemu_fdt_setprop_cell(mms->fdt, "/", "#size-cells", 0x2);
  281. qemu_fdt_add_subnode(mms->fdt, "/chosen");
  282. dt_setup_sys_bus(mms);
  283. /* add to fw_cfg */
  284. if (debug) {
  285. fprintf(stderr, "%s: add etc/fdt to fw_cfg\n", __func__);
  286. }
  287. fw_cfg_add_file(x86ms->fw_cfg, "etc/fdt", mms->fdt, size);
  288. if (debug) {
  289. fprintf(stderr, "%s: writing microvm.fdt\n", __func__);
  290. if (!g_file_set_contents("microvm.fdt", mms->fdt, size, NULL)) {
  291. fprintf(stderr, "%s: writing microvm.fdt failed\n", __func__);
  292. return;
  293. }
  294. int ret = system("dtc -I dtb -O dts microvm.fdt");
  295. if (ret != 0) {
  296. fprintf(stderr, "%s: oops, dtc not installed?\n", __func__);
  297. }
  298. }
  299. }