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 "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
  29typedef 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
  43typedef struct PXBDev {
  44    /*< private >*/
  45    PCIDevice parent_obj;
  46    /*< public >*/
  47
  48    uint8_t bus_nr;
  49    uint16_t numa_node;
  50} PXBDev;
  51
  52static PXBDev *convert_to_pxb(PCIDevice *dev)
  53{
  54    return pci_bus_is_express(pci_get_bus(dev))
  55        ? 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    /* Reason: Internal part of the pxb/pxb-pcie device, not usable by itself */
 153    dc->user_creatable = false;
 154    sbc->explicit_ofw_unit_address = pxb_host_ofw_unit_address;
 155    hc->root_bus_path = pxb_host_root_bus_path;
 156}
 157
 158static const TypeInfo pxb_host_info = {
 159    .name          = TYPE_PXB_HOST,
 160    .parent        = TYPE_PCI_HOST_BRIDGE,
 161    .class_init    = pxb_host_class_init,
 162};
 163
 164/*
 165 * Registers the PXB bus as a child of pci host root bus.
 166 */
 167static void pxb_register_bus(PCIDevice *dev, PCIBus *pxb_bus, Error **errp)
 168{
 169    PCIBus *bus = pci_get_bus(dev);
 170    int pxb_bus_num = pci_bus_num(pxb_bus);
 171
 172    if (bus->parent_dev) {
 173        error_setg(errp, "PXB devices can be attached only to root bus");
 174        return;
 175    }
 176
 177    QLIST_FOREACH(bus, &bus->child, sibling) {
 178        if (pci_bus_num(bus) == pxb_bus_num) {
 179            error_setg(errp, "Bus %d is already in use", pxb_bus_num);
 180            return;
 181        }
 182    }
 183    QLIST_INSERT_HEAD(&pci_get_bus(dev)->child, pxb_bus, sibling);
 184}
 185
 186static int pxb_map_irq_fn(PCIDevice *pci_dev, int pin)
 187{
 188    PCIDevice *pxb = pci_get_bus(pci_dev)->parent_dev;
 189
 190    /*
 191     * The bios does not index the pxb slot number when
 192     * it computes the IRQ because it resides on bus 0
 193     * and not on the current bus.
 194     * However QEMU routes the irq through bus 0 and adds
 195     * the pxb slot to the IRQ computation of the PXB
 196     * device.
 197     *
 198     * Synchronize between bios and QEMU by canceling
 199     * pxb's effect.
 200     */
 201    return pin - PCI_SLOT(pxb->devfn);
 202}
 203
 204static gint pxb_compare(gconstpointer a, gconstpointer b)
 205{
 206    const PXBDev *pxb_a = a, *pxb_b = b;
 207
 208    return pxb_a->bus_nr < pxb_b->bus_nr ? -1 :
 209           pxb_a->bus_nr > pxb_b->bus_nr ?  1 :
 210           0;
 211}
 212
 213static void pxb_dev_realize_common(PCIDevice *dev, bool pcie, Error **errp)
 214{
 215    PXBDev *pxb = convert_to_pxb(dev);
 216    DeviceState *ds, *bds = NULL;
 217    PCIBus *bus;
 218    const char *dev_name = NULL;
 219    Error *local_err = NULL;
 220
 221    if (pxb->numa_node != NUMA_NODE_UNASSIGNED &&
 222        pxb->numa_node >= nb_numa_nodes) {
 223        error_setg(errp, "Illegal numa node %d", pxb->numa_node);
 224        return;
 225    }
 226
 227    if (dev->qdev.id && *dev->qdev.id) {
 228        dev_name = dev->qdev.id;
 229    }
 230
 231    ds = qdev_create(NULL, TYPE_PXB_HOST);
 232    if (pcie) {
 233        bus = pci_root_bus_new(ds, dev_name, NULL, NULL, 0, TYPE_PXB_PCIE_BUS);
 234    } else {
 235        bus = pci_root_bus_new(ds, "pxb-internal", NULL, NULL, 0, TYPE_PXB_BUS);
 236        bds = qdev_create(BUS(bus), "pci-bridge");
 237        bds->id = dev_name;
 238        qdev_prop_set_uint8(bds, PCI_BRIDGE_DEV_PROP_CHASSIS_NR, pxb->bus_nr);
 239        qdev_prop_set_bit(bds, PCI_BRIDGE_DEV_PROP_SHPC, false);
 240    }
 241
 242    bus->parent_dev = dev;
 243    bus->address_space_mem = pci_get_bus(dev)->address_space_mem;
 244    bus->address_space_io = pci_get_bus(dev)->address_space_io;
 245    bus->map_irq = pxb_map_irq_fn;
 246
 247    PCI_HOST_BRIDGE(ds)->bus = bus;
 248
 249    pxb_register_bus(dev, bus, &local_err);
 250    if (local_err) {
 251        error_propagate(errp, local_err);
 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;
 266
 267err_register_bus:
 268    object_unref(OBJECT(bds));
 269    object_unparent(OBJECT(bus));
 270    object_unref(OBJECT(ds));
 271}
 272
 273static void pxb_dev_realize(PCIDevice *dev, Error **errp)
 274{
 275    if (pci_bus_is_express(pci_get_bus(dev))) {
 276        error_setg(errp, "pxb devices cannot reside on a PCIe bus");
 277        return;
 278    }
 279
 280    pxb_dev_realize_common(dev, false, errp);
 281}
 282
 283static void pxb_dev_exitfn(PCIDevice *pci_dev)
 284{
 285    PXBDev *pxb = convert_to_pxb(pci_dev);
 286
 287    pxb_dev_list = g_list_remove(pxb_dev_list, pxb);
 288}
 289
 290static Property pxb_dev_properties[] = {
 291    /* Note: 0 is not a legal PXB bus number. */
 292    DEFINE_PROP_UINT8("bus_nr", PXBDev, bus_nr, 0),
 293    DEFINE_PROP_UINT16("numa_node", PXBDev, numa_node, NUMA_NODE_UNASSIGNED),
 294    DEFINE_PROP_END_OF_LIST(),
 295};
 296
 297static void pxb_dev_class_init(ObjectClass *klass, void *data)
 298{
 299    DeviceClass *dc = DEVICE_CLASS(klass);
 300    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
 301
 302    k->realize = pxb_dev_realize;
 303    k->exit = pxb_dev_exitfn;
 304    k->vendor_id = PCI_VENDOR_ID_REDHAT;
 305    k->device_id = PCI_DEVICE_ID_REDHAT_PXB;
 306    k->class_id = PCI_CLASS_BRIDGE_HOST;
 307
 308    dc->desc = "PCI Expander Bridge";
 309    dc->props = pxb_dev_properties;
 310    dc->hotpluggable = false;
 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    .interfaces = (InterfaceInfo[]) {
 320        { INTERFACE_CONVENTIONAL_PCI_DEVICE },
 321        { },
 322    },
 323};
 324
 325static void pxb_pcie_dev_realize(PCIDevice *dev, Error **errp)
 326{
 327    if (!pci_bus_is_express(pci_get_bus(dev))) {
 328        error_setg(errp, "pxb-pcie devices cannot reside on a PCI bus");
 329        return;
 330    }
 331
 332    pxb_dev_realize_common(dev, true, errp);
 333}
 334
 335static void pxb_pcie_dev_class_init(ObjectClass *klass, void *data)
 336{
 337    DeviceClass *dc = DEVICE_CLASS(klass);
 338    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
 339
 340    k->realize = pxb_pcie_dev_realize;
 341    k->exit = pxb_dev_exitfn;
 342    k->vendor_id = PCI_VENDOR_ID_REDHAT;
 343    k->device_id = PCI_DEVICE_ID_REDHAT_PXB_PCIE;
 344    k->class_id = PCI_CLASS_BRIDGE_HOST;
 345
 346    dc->desc = "PCI Express Expander Bridge";
 347    dc->props = pxb_dev_properties;
 348    dc->hotpluggable = false;
 349    set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
 350}
 351
 352static const TypeInfo pxb_pcie_dev_info = {
 353    .name          = TYPE_PXB_PCIE_DEVICE,
 354    .parent        = TYPE_PCI_DEVICE,
 355    .instance_size = sizeof(PXBDev),
 356    .class_init    = pxb_pcie_dev_class_init,
 357    .interfaces = (InterfaceInfo[]) {
 358        { INTERFACE_CONVENTIONAL_PCI_DEVICE },
 359        { },
 360    },
 361};
 362
 363static void pxb_register_types(void)
 364{
 365    type_register_static(&pxb_bus_info);
 366    type_register_static(&pxb_pcie_bus_info);
 367    type_register_static(&pxb_host_info);
 368    type_register_static(&pxb_dev_info);
 369    type_register_static(&pxb_pcie_dev_info);
 370}
 371
 372type_init(pxb_register_types)
 373