1 /* 2 * PCI Expander Bridge Device Emulation 3 * 4 * Copyright (C) 2015 Red Hat Inc 5 * 6 * Authors: 7 * Marcel Apfelbaum <marcel@redhat.com> 8 * 9 * This work is licensed under the terms of the GNU GPL, version 2 or later. 10 * See the COPYING file in the top-level directory. 11 */ 12 13 #include "qemu/osdep.h" 14 #include "qapi/error.h" 15 #include "hw/pci/pci.h" 16 #include "hw/pci/pci_bus.h" 17 #include "hw/pci/pci_host.h" 18 #include "hw/qdev-properties.h" 19 #include "hw/pci/pci_bridge.h" 20 #include "qemu/range.h" 21 #include "qemu/error-report.h" 22 #include "qemu/module.h" 23 #include "sysemu/numa.h" 24 #include "hw/boards.h" 25 #include "qom/object.h" 26 27 enum BusType { PCI, PCIE, CXL }; 28 29 #define TYPE_PXB_BUS "pxb-bus" 30 typedef struct PXBBus PXBBus; 31 DECLARE_INSTANCE_CHECKER(PXBBus, PXB_BUS, 32 TYPE_PXB_BUS) 33 34 #define TYPE_PXB_PCIE_BUS "pxb-pcie-bus" 35 DECLARE_INSTANCE_CHECKER(PXBBus, PXB_PCIE_BUS, 36 TYPE_PXB_PCIE_BUS) 37 38 #define TYPE_PXB_CXL_BUS "pxb-cxl-bus" 39 DECLARE_INSTANCE_CHECKER(PXBBus, PXB_CXL_BUS, 40 TYPE_PXB_CXL_BUS) 41 42 struct PXBBus { 43 /*< private >*/ 44 PCIBus parent_obj; 45 /*< public >*/ 46 47 char bus_path[8]; 48 }; 49 50 #define TYPE_PXB_DEVICE "pxb" 51 typedef struct PXBDev PXBDev; 52 DECLARE_INSTANCE_CHECKER(PXBDev, PXB_DEV, 53 TYPE_PXB_DEVICE) 54 55 #define TYPE_PXB_PCIE_DEVICE "pxb-pcie" 56 DECLARE_INSTANCE_CHECKER(PXBDev, PXB_PCIE_DEV, 57 TYPE_PXB_PCIE_DEVICE) 58 59 struct PXBDev { 60 /*< private >*/ 61 PCIDevice parent_obj; 62 /*< public >*/ 63 64 uint8_t bus_nr; 65 uint16_t numa_node; 66 bool bypass_iommu; 67 }; 68 69 static PXBDev *convert_to_pxb(PCIDevice *dev) 70 { 71 return pci_bus_is_express(pci_get_bus(dev)) 72 ? PXB_PCIE_DEV(dev) : PXB_DEV(dev); 73 } 74 75 static GList *pxb_dev_list; 76 77 #define TYPE_PXB_HOST "pxb-host" 78 79 static int pxb_bus_num(PCIBus *bus) 80 { 81 PXBDev *pxb = convert_to_pxb(bus->parent_dev); 82 83 return pxb->bus_nr; 84 } 85 86 static uint16_t pxb_bus_numa_node(PCIBus *bus) 87 { 88 PXBDev *pxb = convert_to_pxb(bus->parent_dev); 89 90 return pxb->numa_node; 91 } 92 93 static void pxb_bus_class_init(ObjectClass *class, void *data) 94 { 95 PCIBusClass *pbc = PCI_BUS_CLASS(class); 96 97 pbc->bus_num = pxb_bus_num; 98 pbc->numa_node = pxb_bus_numa_node; 99 } 100 101 static const TypeInfo pxb_bus_info = { 102 .name = TYPE_PXB_BUS, 103 .parent = TYPE_PCI_BUS, 104 .instance_size = sizeof(PXBBus), 105 .class_init = pxb_bus_class_init, 106 }; 107 108 static const TypeInfo pxb_pcie_bus_info = { 109 .name = TYPE_PXB_PCIE_BUS, 110 .parent = TYPE_PCIE_BUS, 111 .instance_size = sizeof(PXBBus), 112 .class_init = pxb_bus_class_init, 113 }; 114 115 static const char *pxb_host_root_bus_path(PCIHostState *host_bridge, 116 PCIBus *rootbus) 117 { 118 PXBBus *bus = pci_bus_is_express(rootbus) ? 119 PXB_PCIE_BUS(rootbus) : PXB_BUS(rootbus); 120 121 snprintf(bus->bus_path, 8, "0000:%02x", pxb_bus_num(rootbus)); 122 return bus->bus_path; 123 } 124 125 static char *pxb_host_ofw_unit_address(const SysBusDevice *dev) 126 { 127 const PCIHostState *pxb_host; 128 const PCIBus *pxb_bus; 129 const PXBDev *pxb_dev; 130 int position; 131 const DeviceState *pxb_dev_base; 132 const PCIHostState *main_host; 133 const SysBusDevice *main_host_sbd; 134 135 pxb_host = PCI_HOST_BRIDGE(dev); 136 pxb_bus = pxb_host->bus; 137 pxb_dev = convert_to_pxb(pxb_bus->parent_dev); 138 position = g_list_index(pxb_dev_list, pxb_dev); 139 assert(position >= 0); 140 141 pxb_dev_base = DEVICE(pxb_dev); 142 main_host = PCI_HOST_BRIDGE(pxb_dev_base->parent_bus->parent); 143 main_host_sbd = SYS_BUS_DEVICE(main_host); 144 145 if (main_host_sbd->num_mmio > 0) { 146 return g_strdup_printf(TARGET_FMT_plx ",%x", 147 main_host_sbd->mmio[0].addr, position + 1); 148 } 149 if (main_host_sbd->num_pio > 0) { 150 return g_strdup_printf("i%04x,%x", 151 main_host_sbd->pio[0], position + 1); 152 } 153 return NULL; 154 } 155 156 static void pxb_host_class_init(ObjectClass *class, void *data) 157 { 158 DeviceClass *dc = DEVICE_CLASS(class); 159 SysBusDeviceClass *sbc = SYS_BUS_DEVICE_CLASS(class); 160 PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_CLASS(class); 161 162 dc->fw_name = "pci"; 163 /* Reason: Internal part of the pxb/pxb-pcie device, not usable by itself */ 164 dc->user_creatable = false; 165 sbc->explicit_ofw_unit_address = pxb_host_ofw_unit_address; 166 hc->root_bus_path = pxb_host_root_bus_path; 167 } 168 169 static const TypeInfo pxb_host_info = { 170 .name = TYPE_PXB_HOST, 171 .parent = TYPE_PCI_HOST_BRIDGE, 172 .class_init = pxb_host_class_init, 173 }; 174 175 /* 176 * Registers the PXB bus as a child of pci host root bus. 177 */ 178 static void pxb_register_bus(PCIDevice *dev, PCIBus *pxb_bus, Error **errp) 179 { 180 PCIBus *bus = pci_get_bus(dev); 181 int pxb_bus_num = pci_bus_num(pxb_bus); 182 183 if (bus->parent_dev) { 184 error_setg(errp, "PXB devices can be attached only to root bus"); 185 return; 186 } 187 188 QLIST_FOREACH(bus, &bus->child, sibling) { 189 if (pci_bus_num(bus) == pxb_bus_num) { 190 error_setg(errp, "Bus %d is already in use", pxb_bus_num); 191 return; 192 } 193 } 194 QLIST_INSERT_HEAD(&pci_get_bus(dev)->child, pxb_bus, sibling); 195 } 196 197 static int pxb_map_irq_fn(PCIDevice *pci_dev, int pin) 198 { 199 PCIDevice *pxb = pci_get_bus(pci_dev)->parent_dev; 200 201 /* 202 * First carry out normal swizzle to handle 203 * multple root ports on a pxb instance. 204 */ 205 pin = pci_swizzle_map_irq_fn(pci_dev, pin); 206 207 /* 208 * The bios does not index the pxb slot number when 209 * it computes the IRQ because it resides on bus 0 210 * and not on the current bus. 211 * However QEMU routes the irq through bus 0 and adds 212 * the pxb slot to the IRQ computation of the PXB 213 * device. 214 * 215 * Synchronize between bios and QEMU by canceling 216 * pxb's effect. 217 */ 218 return pin - PCI_SLOT(pxb->devfn); 219 } 220 221 static gint pxb_compare(gconstpointer a, gconstpointer b) 222 { 223 const PXBDev *pxb_a = a, *pxb_b = b; 224 225 return pxb_a->bus_nr < pxb_b->bus_nr ? -1 : 226 pxb_a->bus_nr > pxb_b->bus_nr ? 1 : 227 0; 228 } 229 230 static void pxb_dev_realize_common(PCIDevice *dev, enum BusType type, 231 Error **errp) 232 { 233 PXBDev *pxb = convert_to_pxb(dev); 234 DeviceState *ds, *bds = NULL; 235 PCIBus *bus; 236 const char *dev_name = NULL; 237 Error *local_err = NULL; 238 MachineState *ms = MACHINE(qdev_get_machine()); 239 240 if (ms->numa_state == NULL) { 241 error_setg(errp, "NUMA is not supported by this machine-type"); 242 return; 243 } 244 245 if (pxb->numa_node != NUMA_NODE_UNASSIGNED && 246 pxb->numa_node >= ms->numa_state->num_nodes) { 247 error_setg(errp, "Illegal numa node %d", pxb->numa_node); 248 return; 249 } 250 251 if (dev->qdev.id && *dev->qdev.id) { 252 dev_name = dev->qdev.id; 253 } 254 255 ds = qdev_new(TYPE_PXB_HOST); 256 if (type == PCIE) { 257 bus = pci_root_bus_new(ds, dev_name, NULL, NULL, 0, TYPE_PXB_PCIE_BUS); 258 } else if (type == CXL) { 259 bus = pci_root_bus_new(ds, dev_name, NULL, NULL, 0, TYPE_PXB_CXL_BUS); 260 bus->flags |= PCI_BUS_CXL; 261 } else { 262 bus = pci_root_bus_new(ds, "pxb-internal", NULL, NULL, 0, TYPE_PXB_BUS); 263 bds = qdev_new("pci-bridge"); 264 bds->id = g_strdup(dev_name); 265 qdev_prop_set_uint8(bds, PCI_BRIDGE_DEV_PROP_CHASSIS_NR, pxb->bus_nr); 266 qdev_prop_set_bit(bds, PCI_BRIDGE_DEV_PROP_SHPC, false); 267 } 268 269 bus->parent_dev = dev; 270 bus->address_space_mem = pci_get_bus(dev)->address_space_mem; 271 bus->address_space_io = pci_get_bus(dev)->address_space_io; 272 bus->map_irq = pxb_map_irq_fn; 273 274 PCI_HOST_BRIDGE(ds)->bus = bus; 275 PCI_HOST_BRIDGE(ds)->bypass_iommu = pxb->bypass_iommu; 276 277 pxb_register_bus(dev, bus, &local_err); 278 if (local_err) { 279 error_propagate(errp, local_err); 280 goto err_register_bus; 281 } 282 283 sysbus_realize_and_unref(SYS_BUS_DEVICE(ds), &error_fatal); 284 if (bds) { 285 qdev_realize_and_unref(bds, &bus->qbus, &error_fatal); 286 } 287 288 pci_word_test_and_set_mask(dev->config + PCI_STATUS, 289 PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK); 290 pci_config_set_class(dev->config, PCI_CLASS_BRIDGE_HOST); 291 292 pxb_dev_list = g_list_insert_sorted(pxb_dev_list, pxb, pxb_compare); 293 return; 294 295 err_register_bus: 296 object_unref(OBJECT(bds)); 297 object_unparent(OBJECT(bus)); 298 object_unref(OBJECT(ds)); 299 } 300 301 static void pxb_dev_realize(PCIDevice *dev, Error **errp) 302 { 303 if (pci_bus_is_express(pci_get_bus(dev))) { 304 error_setg(errp, "pxb devices cannot reside on a PCIe bus"); 305 return; 306 } 307 308 pxb_dev_realize_common(dev, PCI, errp); 309 } 310 311 static void pxb_dev_exitfn(PCIDevice *pci_dev) 312 { 313 PXBDev *pxb = convert_to_pxb(pci_dev); 314 315 pxb_dev_list = g_list_remove(pxb_dev_list, pxb); 316 } 317 318 static Property pxb_dev_properties[] = { 319 /* Note: 0 is not a legal PXB bus number. */ 320 DEFINE_PROP_UINT8("bus_nr", PXBDev, bus_nr, 0), 321 DEFINE_PROP_UINT16("numa_node", PXBDev, numa_node, NUMA_NODE_UNASSIGNED), 322 DEFINE_PROP_BOOL("bypass_iommu", PXBDev, bypass_iommu, false), 323 DEFINE_PROP_END_OF_LIST(), 324 }; 325 326 static void pxb_dev_class_init(ObjectClass *klass, void *data) 327 { 328 DeviceClass *dc = DEVICE_CLASS(klass); 329 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 330 331 k->realize = pxb_dev_realize; 332 k->exit = pxb_dev_exitfn; 333 k->vendor_id = PCI_VENDOR_ID_REDHAT; 334 k->device_id = PCI_DEVICE_ID_REDHAT_PXB; 335 k->class_id = PCI_CLASS_BRIDGE_HOST; 336 337 dc->desc = "PCI Expander Bridge"; 338 device_class_set_props(dc, pxb_dev_properties); 339 dc->hotpluggable = false; 340 set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories); 341 } 342 343 static const TypeInfo pxb_dev_info = { 344 .name = TYPE_PXB_DEVICE, 345 .parent = TYPE_PCI_DEVICE, 346 .instance_size = sizeof(PXBDev), 347 .class_init = pxb_dev_class_init, 348 .interfaces = (InterfaceInfo[]) { 349 { INTERFACE_CONVENTIONAL_PCI_DEVICE }, 350 { }, 351 }, 352 }; 353 354 static void pxb_pcie_dev_realize(PCIDevice *dev, Error **errp) 355 { 356 if (!pci_bus_is_express(pci_get_bus(dev))) { 357 error_setg(errp, "pxb-pcie devices cannot reside on a PCI bus"); 358 return; 359 } 360 361 pxb_dev_realize_common(dev, PCIE, errp); 362 } 363 364 static void pxb_pcie_dev_class_init(ObjectClass *klass, void *data) 365 { 366 DeviceClass *dc = DEVICE_CLASS(klass); 367 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 368 369 k->realize = pxb_pcie_dev_realize; 370 k->exit = pxb_dev_exitfn; 371 k->vendor_id = PCI_VENDOR_ID_REDHAT; 372 k->device_id = PCI_DEVICE_ID_REDHAT_PXB_PCIE; 373 k->class_id = PCI_CLASS_BRIDGE_HOST; 374 375 dc->desc = "PCI Express Expander Bridge"; 376 device_class_set_props(dc, pxb_dev_properties); 377 dc->hotpluggable = false; 378 set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories); 379 } 380 381 static const TypeInfo pxb_pcie_dev_info = { 382 .name = TYPE_PXB_PCIE_DEVICE, 383 .parent = TYPE_PCI_DEVICE, 384 .instance_size = sizeof(PXBDev), 385 .class_init = pxb_pcie_dev_class_init, 386 .interfaces = (InterfaceInfo[]) { 387 { INTERFACE_CONVENTIONAL_PCI_DEVICE }, 388 { }, 389 }, 390 }; 391 392 static void pxb_register_types(void) 393 { 394 type_register_static(&pxb_bus_info); 395 type_register_static(&pxb_pcie_bus_info); 396 type_register_static(&pxb_host_info); 397 type_register_static(&pxb_dev_info); 398 type_register_static(&pxb_pcie_dev_info); 399 } 400 401 type_init(pxb_register_types) 402