viot.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * ACPI Virtual I/O Translation table implementation
  3. *
  4. * SPDX-License-Identifier: GPL-2.0-or-later
  5. */
  6. #include "qemu/osdep.h"
  7. #include "hw/acpi/acpi.h"
  8. #include "hw/acpi/aml-build.h"
  9. #include "hw/acpi/viot.h"
  10. #include "hw/pci/pci.h"
  11. #include "hw/pci/pci_host.h"
  12. struct viot_pci_host_range {
  13. int min_bus;
  14. int max_bus;
  15. };
  16. static void build_pci_host_range(GArray *table_data, int min_bus, int max_bus,
  17. uint16_t output_node)
  18. {
  19. /* Type */
  20. build_append_int_noprefix(table_data, 1 /* PCI range */, 1);
  21. /* Reserved */
  22. build_append_int_noprefix(table_data, 0, 1);
  23. /* Length */
  24. build_append_int_noprefix(table_data, 24, 2);
  25. /* Endpoint start */
  26. build_append_int_noprefix(table_data, PCI_BUILD_BDF(min_bus, 0), 4);
  27. /* PCI Segment start */
  28. build_append_int_noprefix(table_data, 0, 2);
  29. /* PCI Segment end */
  30. build_append_int_noprefix(table_data, 0, 2);
  31. /* PCI BDF start */
  32. build_append_int_noprefix(table_data, PCI_BUILD_BDF(min_bus, 0), 2);
  33. /* PCI BDF end */
  34. build_append_int_noprefix(table_data, PCI_BUILD_BDF(max_bus, 0xff), 2);
  35. /* Output node */
  36. build_append_int_noprefix(table_data, output_node, 2);
  37. /* Reserved */
  38. build_append_int_noprefix(table_data, 0, 6);
  39. }
  40. /* Build PCI range for a given PCI host bridge */
  41. static int enumerate_pci_host_bridges(Object *obj, void *opaque)
  42. {
  43. GArray *pci_host_ranges = opaque;
  44. if (object_dynamic_cast(obj, TYPE_PCI_HOST_BRIDGE)) {
  45. PCIBus *bus = PCI_HOST_BRIDGE(obj)->bus;
  46. if (bus && !pci_bus_bypass_iommu(bus)) {
  47. int min_bus, max_bus;
  48. pci_bus_range(bus, &min_bus, &max_bus);
  49. const struct viot_pci_host_range pci_host_range = {
  50. .min_bus = min_bus,
  51. .max_bus = max_bus,
  52. };
  53. g_array_append_val(pci_host_ranges, pci_host_range);
  54. }
  55. }
  56. return 0;
  57. }
  58. static gint pci_host_range_compare(gconstpointer a, gconstpointer b)
  59. {
  60. struct viot_pci_host_range *range_a = (struct viot_pci_host_range *)a;
  61. struct viot_pci_host_range *range_b = (struct viot_pci_host_range *)b;
  62. if (range_a->min_bus < range_b->min_bus) {
  63. return -1;
  64. } else if (range_a->min_bus > range_b->min_bus) {
  65. return 1;
  66. } else {
  67. return 0;
  68. }
  69. }
  70. /*
  71. * Generate a VIOT table with one PCI-based virtio-iommu that manages PCI
  72. * endpoints.
  73. *
  74. * Defined in the ACPI Specification (Version TBD)
  75. */
  76. void build_viot(MachineState *ms, GArray *table_data, BIOSLinker *linker,
  77. uint16_t virtio_iommu_bdf, const char *oem_id,
  78. const char *oem_table_id)
  79. {
  80. /* The virtio-iommu node follows the 48-bytes header */
  81. int viommu_off = 48;
  82. AcpiTable table = { .sig = "VIOT", .rev = 0,
  83. .oem_id = oem_id, .oem_table_id = oem_table_id };
  84. GArray *pci_host_ranges = g_array_new(false, true,
  85. sizeof(struct viot_pci_host_range));
  86. struct viot_pci_host_range *pci_host_range;
  87. int i;
  88. /* Build the list of PCI ranges that this viommu manages */
  89. object_child_foreach_recursive(OBJECT(ms), enumerate_pci_host_bridges,
  90. pci_host_ranges);
  91. /* Sort the pci host ranges by min_bus */
  92. g_array_sort(pci_host_ranges, pci_host_range_compare);
  93. /* ACPI table header */
  94. acpi_table_begin(&table, table_data);
  95. /* Node count */
  96. build_append_int_noprefix(table_data, pci_host_ranges->len + 1, 2);
  97. /* Node offset */
  98. build_append_int_noprefix(table_data, viommu_off, 2);
  99. /* Reserved */
  100. build_append_int_noprefix(table_data, 0, 8);
  101. /* Virtio-iommu node */
  102. /* Type */
  103. build_append_int_noprefix(table_data, 3 /* virtio-pci IOMMU */, 1);
  104. /* Reserved */
  105. build_append_int_noprefix(table_data, 0, 1);
  106. /* Length */
  107. build_append_int_noprefix(table_data, 16, 2);
  108. /* PCI Segment */
  109. build_append_int_noprefix(table_data, 0, 2);
  110. /* PCI BDF number */
  111. build_append_int_noprefix(table_data, virtio_iommu_bdf, 2);
  112. /* Reserved */
  113. build_append_int_noprefix(table_data, 0, 8);
  114. /* PCI ranges found above */
  115. for (i = 0; i < pci_host_ranges->len; i++) {
  116. pci_host_range = &g_array_index(pci_host_ranges,
  117. struct viot_pci_host_range, i);
  118. build_pci_host_range(table_data, pci_host_range->min_bus,
  119. pci_host_range->max_bus, viommu_off);
  120. }
  121. g_array_free(pci_host_ranges, true);
  122. acpi_table_end(linker, &table);
  123. }