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