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 "hw/pci/pci.h" 14 #include "hw/pci/pci_bus.h" 15 #include "hw/pci/pci_host.h" 16 #include "hw/pci/pci_bus.h" 17 #include "hw/pci/pci_bridge.h" 18 #include "hw/i386/pc.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 typedef struct PXBBus { 27 /*< private >*/ 28 PCIBus parent_obj; 29 /*< public >*/ 30 31 char bus_path[8]; 32 } PXBBus; 33 34 #define TYPE_PXB_DEVICE "pxb" 35 #define PXB_DEV(obj) OBJECT_CHECK(PXBDev, (obj), TYPE_PXB_DEVICE) 36 37 typedef struct PXBDev { 38 /*< private >*/ 39 PCIDevice parent_obj; 40 /*< public >*/ 41 42 uint8_t bus_nr; 43 uint16_t numa_node; 44 } PXBDev; 45 46 #define TYPE_PXB_HOST "pxb-host" 47 48 static int pxb_bus_num(PCIBus *bus) 49 { 50 PXBDev *pxb = PXB_DEV(bus->parent_dev); 51 52 return pxb->bus_nr; 53 } 54 55 static bool pxb_is_root(PCIBus *bus) 56 { 57 return true; /* by definition */ 58 } 59 60 static uint16_t pxb_bus_numa_node(PCIBus *bus) 61 { 62 PXBDev *pxb = PXB_DEV(bus->parent_dev); 63 64 return pxb->numa_node; 65 } 66 67 static void pxb_bus_class_init(ObjectClass *class, void *data) 68 { 69 PCIBusClass *pbc = PCI_BUS_CLASS(class); 70 71 pbc->bus_num = pxb_bus_num; 72 pbc->is_root = pxb_is_root; 73 pbc->numa_node = pxb_bus_numa_node; 74 } 75 76 static const TypeInfo pxb_bus_info = { 77 .name = TYPE_PXB_BUS, 78 .parent = TYPE_PCI_BUS, 79 .instance_size = sizeof(PXBBus), 80 .class_init = pxb_bus_class_init, 81 }; 82 83 static const char *pxb_host_root_bus_path(PCIHostState *host_bridge, 84 PCIBus *rootbus) 85 { 86 PXBBus *bus = PXB_BUS(rootbus); 87 88 snprintf(bus->bus_path, 8, "0000:%02x", pxb_bus_num(rootbus)); 89 return bus->bus_path; 90 } 91 92 static void pxb_host_class_init(ObjectClass *class, void *data) 93 { 94 DeviceClass *dc = DEVICE_CLASS(class); 95 PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_CLASS(class); 96 97 dc->fw_name = "pci"; 98 hc->root_bus_path = pxb_host_root_bus_path; 99 } 100 101 static const TypeInfo pxb_host_info = { 102 .name = TYPE_PXB_HOST, 103 .parent = TYPE_PCI_HOST_BRIDGE, 104 .class_init = pxb_host_class_init, 105 }; 106 107 /* 108 * Registers the PXB bus as a child of the i440fx root bus. 109 * 110 * Returns 0 on successs, -1 if i440fx host was not 111 * found or the bus number is already in use. 112 */ 113 static int pxb_register_bus(PCIDevice *dev, PCIBus *pxb_bus) 114 { 115 PCIBus *bus = dev->bus; 116 int pxb_bus_num = pci_bus_num(pxb_bus); 117 118 if (bus->parent_dev) { 119 error_report("PXB devices can be attached only to root bus."); 120 return -1; 121 } 122 123 QLIST_FOREACH(bus, &bus->child, sibling) { 124 if (pci_bus_num(bus) == pxb_bus_num) { 125 error_report("Bus %d is already in use.", pxb_bus_num); 126 return -1; 127 } 128 } 129 QLIST_INSERT_HEAD(&dev->bus->child, pxb_bus, sibling); 130 131 return 0; 132 } 133 134 static int pxb_map_irq_fn(PCIDevice *pci_dev, int pin) 135 { 136 PCIDevice *pxb = pci_dev->bus->parent_dev; 137 138 /* 139 * The bios does not index the pxb slot number when 140 * it computes the IRQ because it resides on bus 0 141 * and not on the current bus. 142 * However QEMU routes the irq through bus 0 and adds 143 * the pxb slot to the IRQ computation of the PXB 144 * device. 145 * 146 * Synchronize between bios and QEMU by canceling 147 * pxb's effect. 148 */ 149 return pin - PCI_SLOT(pxb->devfn); 150 } 151 152 static int pxb_dev_initfn(PCIDevice *dev) 153 { 154 PXBDev *pxb = PXB_DEV(dev); 155 DeviceState *ds, *bds; 156 PCIBus *bus; 157 const char *dev_name = NULL; 158 159 if (pxb->numa_node != NUMA_NODE_UNASSIGNED && 160 pxb->numa_node >= nb_numa_nodes) { 161 error_report("Illegal numa node %d.", pxb->numa_node); 162 return -EINVAL; 163 } 164 165 if (dev->qdev.id && *dev->qdev.id) { 166 dev_name = dev->qdev.id; 167 } 168 169 ds = qdev_create(NULL, TYPE_PXB_HOST); 170 bus = pci_bus_new(ds, "pxb-internal", NULL, NULL, 0, TYPE_PXB_BUS); 171 172 bus->parent_dev = dev; 173 bus->address_space_mem = dev->bus->address_space_mem; 174 bus->address_space_io = dev->bus->address_space_io; 175 bus->map_irq = pxb_map_irq_fn; 176 177 bds = qdev_create(BUS(bus), "pci-bridge"); 178 bds->id = dev_name; 179 qdev_prop_set_uint8(bds, PCI_BRIDGE_DEV_PROP_CHASSIS_NR, pxb->bus_nr); 180 qdev_prop_set_bit(bds, PCI_BRIDGE_DEV_PROP_SHPC, false); 181 182 PCI_HOST_BRIDGE(ds)->bus = bus; 183 184 if (pxb_register_bus(dev, bus)) { 185 return -EINVAL; 186 } 187 188 qdev_init_nofail(ds); 189 qdev_init_nofail(bds); 190 191 pci_word_test_and_set_mask(dev->config + PCI_STATUS, 192 PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK); 193 pci_config_set_class(dev->config, PCI_CLASS_BRIDGE_HOST); 194 195 return 0; 196 } 197 198 static Property pxb_dev_properties[] = { 199 /* Note: 0 is not a legal a PXB bus number. */ 200 DEFINE_PROP_UINT8("bus_nr", PXBDev, bus_nr, 0), 201 DEFINE_PROP_UINT16("numa_node", PXBDev, numa_node, NUMA_NODE_UNASSIGNED), 202 DEFINE_PROP_END_OF_LIST(), 203 }; 204 205 static void pxb_dev_class_init(ObjectClass *klass, void *data) 206 { 207 DeviceClass *dc = DEVICE_CLASS(klass); 208 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 209 210 k->init = pxb_dev_initfn; 211 k->vendor_id = PCI_VENDOR_ID_REDHAT; 212 k->device_id = PCI_DEVICE_ID_REDHAT_PXB; 213 k->class_id = PCI_CLASS_BRIDGE_HOST; 214 215 dc->desc = "PCI Expander Bridge"; 216 dc->props = pxb_dev_properties; 217 } 218 219 static const TypeInfo pxb_dev_info = { 220 .name = TYPE_PXB_DEVICE, 221 .parent = TYPE_PCI_DEVICE, 222 .instance_size = sizeof(PXBDev), 223 .class_init = pxb_dev_class_init, 224 }; 225 226 static void pxb_register_types(void) 227 { 228 type_register_static(&pxb_bus_info); 229 type_register_static(&pxb_host_info); 230 type_register_static(&pxb_dev_info); 231 } 232 233 type_init(pxb_register_types) 234