2
0

gpex-acpi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #include "qemu/osdep.h"
  2. #include "hw/acpi/aml-build.h"
  3. #include "hw/pci-host/gpex.h"
  4. #include "hw/arm/virt.h"
  5. #include "hw/pci/pci_bus.h"
  6. #include "hw/pci/pci_bridge.h"
  7. #include "hw/pci/pcie_host.h"
  8. #include "hw/acpi/cxl.h"
  9. static void acpi_dsdt_add_pci_route_table(Aml *dev, uint32_t irq)
  10. {
  11. Aml *method, *crs;
  12. int i, slot_no;
  13. /* Declare the PCI Routing Table. */
  14. Aml *rt_pkg = aml_varpackage(PCI_SLOT_MAX * PCI_NUM_PINS);
  15. for (slot_no = 0; slot_no < PCI_SLOT_MAX; slot_no++) {
  16. for (i = 0; i < PCI_NUM_PINS; i++) {
  17. int gsi = (i + slot_no) % PCI_NUM_PINS;
  18. Aml *pkg = aml_package(4);
  19. aml_append(pkg, aml_int((slot_no << 16) | 0xFFFF));
  20. aml_append(pkg, aml_int(i));
  21. aml_append(pkg, aml_name("GSI%d", gsi));
  22. aml_append(pkg, aml_int(0));
  23. aml_append(rt_pkg, pkg);
  24. }
  25. }
  26. aml_append(dev, aml_name_decl("_PRT", rt_pkg));
  27. /* Create GSI link device */
  28. for (i = 0; i < PCI_NUM_PINS; i++) {
  29. uint32_t irqs = irq + i;
  30. Aml *dev_gsi = aml_device("GSI%d", i);
  31. aml_append(dev_gsi, aml_name_decl("_HID", aml_string("PNP0C0F")));
  32. aml_append(dev_gsi, aml_name_decl("_UID", aml_int(i)));
  33. crs = aml_resource_template();
  34. aml_append(crs,
  35. aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH,
  36. AML_EXCLUSIVE, &irqs, 1));
  37. aml_append(dev_gsi, aml_name_decl("_PRS", crs));
  38. crs = aml_resource_template();
  39. aml_append(crs,
  40. aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH,
  41. AML_EXCLUSIVE, &irqs, 1));
  42. aml_append(dev_gsi, aml_name_decl("_CRS", crs));
  43. method = aml_method("_SRS", 1, AML_NOTSERIALIZED);
  44. aml_append(dev_gsi, method);
  45. aml_append(dev, dev_gsi);
  46. }
  47. }
  48. static void acpi_dsdt_add_pci_osc(Aml *dev)
  49. {
  50. Aml *method, *UUID, *ifctx, *ifctx1, *elsectx, *buf;
  51. /* Declare an _OSC (OS Control Handoff) method */
  52. aml_append(dev, aml_name_decl("SUPP", aml_int(0)));
  53. aml_append(dev, aml_name_decl("CTRL", aml_int(0)));
  54. method = aml_method("_OSC", 4, AML_NOTSERIALIZED);
  55. aml_append(method,
  56. aml_create_dword_field(aml_arg(3), aml_int(0), "CDW1"));
  57. /* PCI Firmware Specification 3.0
  58. * 4.5.1. _OSC Interface for PCI Host Bridge Devices
  59. * The _OSC interface for a PCI/PCI-X/PCI Express hierarchy is
  60. * identified by the Universal Unique IDentifier (UUID)
  61. * 33DB4D5B-1FF7-401C-9657-7441C03DD766
  62. */
  63. UUID = aml_touuid("33DB4D5B-1FF7-401C-9657-7441C03DD766");
  64. ifctx = aml_if(aml_equal(aml_arg(0), UUID));
  65. aml_append(ifctx,
  66. aml_create_dword_field(aml_arg(3), aml_int(4), "CDW2"));
  67. aml_append(ifctx,
  68. aml_create_dword_field(aml_arg(3), aml_int(8), "CDW3"));
  69. aml_append(ifctx, aml_store(aml_name("CDW2"), aml_name("SUPP")));
  70. aml_append(ifctx, aml_store(aml_name("CDW3"), aml_name("CTRL")));
  71. /*
  72. * Allow OS control for all 5 features:
  73. * PCIeHotplug SHPCHotplug PME AER PCIeCapability.
  74. */
  75. aml_append(ifctx, aml_and(aml_name("CTRL"), aml_int(0x1F),
  76. aml_name("CTRL")));
  77. ifctx1 = aml_if(aml_lnot(aml_equal(aml_arg(1), aml_int(0x1))));
  78. aml_append(ifctx1, aml_or(aml_name("CDW1"), aml_int(0x08),
  79. aml_name("CDW1")));
  80. aml_append(ifctx, ifctx1);
  81. ifctx1 = aml_if(aml_lnot(aml_equal(aml_name("CDW3"), aml_name("CTRL"))));
  82. aml_append(ifctx1, aml_or(aml_name("CDW1"), aml_int(0x10),
  83. aml_name("CDW1")));
  84. aml_append(ifctx, ifctx1);
  85. aml_append(ifctx, aml_store(aml_name("CTRL"), aml_name("CDW3")));
  86. aml_append(ifctx, aml_return(aml_arg(3)));
  87. aml_append(method, ifctx);
  88. elsectx = aml_else();
  89. aml_append(elsectx, aml_or(aml_name("CDW1"), aml_int(4),
  90. aml_name("CDW1")));
  91. aml_append(elsectx, aml_return(aml_arg(3)));
  92. aml_append(method, elsectx);
  93. aml_append(dev, method);
  94. method = aml_method("_DSM", 4, AML_NOTSERIALIZED);
  95. /* PCI Firmware Specification 3.0
  96. * 4.6.1. _DSM for PCI Express Slot Information
  97. * The UUID in _DSM in this context is
  98. * {E5C937D0-3553-4D7A-9117-EA4D19C3434D}
  99. */
  100. UUID = aml_touuid("E5C937D0-3553-4D7A-9117-EA4D19C3434D");
  101. ifctx = aml_if(aml_equal(aml_arg(0), UUID));
  102. ifctx1 = aml_if(aml_equal(aml_arg(2), aml_int(0)));
  103. uint8_t byte_list[1] = {1};
  104. buf = aml_buffer(1, byte_list);
  105. aml_append(ifctx1, aml_return(buf));
  106. aml_append(ifctx, ifctx1);
  107. aml_append(method, ifctx);
  108. byte_list[0] = 0;
  109. buf = aml_buffer(1, byte_list);
  110. aml_append(method, aml_return(buf));
  111. aml_append(dev, method);
  112. }
  113. void acpi_dsdt_add_gpex(Aml *scope, struct GPEXConfig *cfg)
  114. {
  115. int nr_pcie_buses = cfg->ecam.size / PCIE_MMCFG_SIZE_MIN;
  116. Aml *method, *crs, *dev, *rbuf;
  117. PCIBus *bus = cfg->bus;
  118. CrsRangeSet crs_range_set;
  119. CrsRangeEntry *entry;
  120. int i;
  121. /* start to construct the tables for pxb */
  122. crs_range_set_init(&crs_range_set);
  123. if (bus) {
  124. QLIST_FOREACH(bus, &bus->child, sibling) {
  125. uint8_t bus_num = pci_bus_num(bus);
  126. uint8_t numa_node = pci_bus_numa_node(bus);
  127. bool is_cxl = pci_bus_is_cxl(bus);
  128. if (!pci_bus_is_root(bus)) {
  129. continue;
  130. }
  131. /*
  132. * 0 - (nr_pcie_buses - 1) is the bus range for the main
  133. * host-bridge and it equals the MIN of the
  134. * busNr defined for pxb-pcie.
  135. */
  136. if (bus_num < nr_pcie_buses) {
  137. nr_pcie_buses = bus_num;
  138. }
  139. dev = aml_device("PC%.02X", bus_num);
  140. if (is_cxl) {
  141. struct Aml *pkg = aml_package(2);
  142. aml_append(dev, aml_name_decl("_HID", aml_string("ACPI0016")));
  143. aml_append(pkg, aml_eisaid("PNP0A08"));
  144. aml_append(pkg, aml_eisaid("PNP0A03"));
  145. aml_append(dev, aml_name_decl("_CID", pkg));
  146. } else {
  147. aml_append(dev, aml_name_decl("_HID", aml_string("PNP0A08")));
  148. aml_append(dev, aml_name_decl("_CID", aml_string("PNP0A03")));
  149. }
  150. aml_append(dev, aml_name_decl("_BBN", aml_int(bus_num)));
  151. aml_append(dev, aml_name_decl("_UID", aml_int(bus_num)));
  152. aml_append(dev, aml_name_decl("_STR", aml_unicode("pxb Device")));
  153. aml_append(dev, aml_name_decl("_CCA", aml_int(1)));
  154. if (numa_node != NUMA_NODE_UNASSIGNED) {
  155. aml_append(dev, aml_name_decl("_PXM", aml_int(numa_node)));
  156. }
  157. acpi_dsdt_add_pci_route_table(dev, cfg->irq);
  158. /*
  159. * Resources defined for PXBs are composed by the folling parts:
  160. * 1. The resources the pci-brige/pcie-root-port need.
  161. * 2. The resources the devices behind pxb need.
  162. */
  163. crs = build_crs(PCI_HOST_BRIDGE(BUS(bus)->parent), &crs_range_set,
  164. cfg->pio.base, 0, 0, 0);
  165. aml_append(dev, aml_name_decl("_CRS", crs));
  166. if (is_cxl) {
  167. build_cxl_osc_method(dev);
  168. } else {
  169. acpi_dsdt_add_pci_osc(dev);
  170. }
  171. aml_append(scope, dev);
  172. }
  173. }
  174. /* tables for the main */
  175. dev = aml_device("%s", "PCI0");
  176. aml_append(dev, aml_name_decl("_HID", aml_string("PNP0A08")));
  177. aml_append(dev, aml_name_decl("_CID", aml_string("PNP0A03")));
  178. aml_append(dev, aml_name_decl("_SEG", aml_int(0)));
  179. aml_append(dev, aml_name_decl("_BBN", aml_int(0)));
  180. aml_append(dev, aml_name_decl("_UID", aml_int(0)));
  181. aml_append(dev, aml_name_decl("_STR", aml_unicode("PCIe 0 Device")));
  182. aml_append(dev, aml_name_decl("_CCA", aml_int(1)));
  183. acpi_dsdt_add_pci_route_table(dev, cfg->irq);
  184. method = aml_method("_CBA", 0, AML_NOTSERIALIZED);
  185. aml_append(method, aml_return(aml_int(cfg->ecam.base)));
  186. aml_append(dev, method);
  187. /*
  188. * At this point crs_range_set has all the ranges used by pci
  189. * busses *other* than PCI0. These ranges will be excluded from
  190. * the PCI0._CRS.
  191. */
  192. rbuf = aml_resource_template();
  193. aml_append(rbuf,
  194. aml_word_bus_number(AML_MIN_FIXED, AML_MAX_FIXED, AML_POS_DECODE,
  195. 0x0000, 0x0000, nr_pcie_buses - 1, 0x0000,
  196. nr_pcie_buses));
  197. if (cfg->mmio32.size) {
  198. crs_replace_with_free_ranges(crs_range_set.mem_ranges,
  199. cfg->mmio32.base,
  200. cfg->mmio32.base + cfg->mmio32.size - 1);
  201. for (i = 0; i < crs_range_set.mem_ranges->len; i++) {
  202. entry = g_ptr_array_index(crs_range_set.mem_ranges, i);
  203. aml_append(rbuf,
  204. aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED,
  205. AML_NON_CACHEABLE, AML_READ_WRITE, 0x0000,
  206. entry->base, entry->limit,
  207. 0x0000, entry->limit - entry->base + 1));
  208. }
  209. }
  210. if (cfg->pio.size) {
  211. crs_replace_with_free_ranges(crs_range_set.io_ranges,
  212. 0x0000,
  213. cfg->pio.size - 1);
  214. for (i = 0; i < crs_range_set.io_ranges->len; i++) {
  215. entry = g_ptr_array_index(crs_range_set.io_ranges, i);
  216. aml_append(rbuf,
  217. aml_dword_io(AML_MIN_FIXED, AML_MAX_FIXED, AML_POS_DECODE,
  218. AML_ENTIRE_RANGE, 0x0000, entry->base,
  219. entry->limit, cfg->pio.base,
  220. entry->limit - entry->base + 1));
  221. }
  222. }
  223. if (cfg->mmio64.size) {
  224. crs_replace_with_free_ranges(crs_range_set.mem_64bit_ranges,
  225. cfg->mmio64.base,
  226. cfg->mmio64.base + cfg->mmio64.size - 1);
  227. for (i = 0; i < crs_range_set.mem_64bit_ranges->len; i++) {
  228. entry = g_ptr_array_index(crs_range_set.mem_64bit_ranges, i);
  229. aml_append(rbuf,
  230. aml_qword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED,
  231. AML_NON_CACHEABLE, AML_READ_WRITE, 0x0000,
  232. entry->base,
  233. entry->limit, 0x0000,
  234. entry->limit - entry->base + 1));
  235. }
  236. }
  237. aml_append(dev, aml_name_decl("_CRS", rbuf));
  238. acpi_dsdt_add_pci_osc(dev);
  239. Aml *dev_res0 = aml_device("%s", "RES0");
  240. aml_append(dev_res0, aml_name_decl("_HID", aml_string("PNP0C02")));
  241. crs = aml_resource_template();
  242. aml_append(crs,
  243. aml_qword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED,
  244. AML_NON_CACHEABLE, AML_READ_WRITE, 0x0000,
  245. cfg->ecam.base,
  246. cfg->ecam.base + cfg->ecam.size - 1,
  247. 0x0000,
  248. cfg->ecam.size));
  249. aml_append(dev_res0, aml_name_decl("_CRS", crs));
  250. aml_append(dev, dev_res0);
  251. aml_append(scope, dev);
  252. crs_range_set_free(&crs_range_set);
  253. }