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