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