pci_bridge_dev.c 8.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Standard PCI Bridge Device
 *
 * Copyright (c) 2011 Red Hat Inc. Author: Michael S. Tsirkin <mst@redhat.com>
 *
 * http://www.pcisig.com/specifications/conventional/pci_to_pci_bridge_architecture/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, see <http://www.gnu.org/licenses/>.
 */

P
Peter Maydell 已提交
22
#include "qemu/osdep.h"
23
#include "qapi/error.h"
24 25 26 27 28
#include "hw/pci/pci_bridge.h"
#include "hw/pci/pci_ids.h"
#include "hw/pci/msi.h"
#include "hw/pci/shpc.h"
#include "hw/pci/slotid_cap.h"
29
#include "exec/memory.h"
30
#include "hw/pci/pci_bus.h"
31
#include "hw/hotplug.h"
32

G
Gerd Hoffmann 已提交
33 34
#define TYPE_PCI_BRIDGE_DEV      "pci-bridge"
#define TYPE_PCI_BRIDGE_SEAT_DEV "pci-bridge-seat"
35 36 37
#define PCI_BRIDGE_DEV(obj) \
    OBJECT_CHECK(PCIBridgeDev, (obj), TYPE_PCI_BRIDGE_DEV)

38
struct PCIBridgeDev {
39 40 41 42
    /*< private >*/
    PCIBridge parent_obj;
    /*< public >*/

43 44
    MemoryRegion bar;
    uint8_t chassis_nr;
45
#define PCI_BRIDGE_DEV_F_SHPC_REQ 0
46
    uint32_t flags;
47 48

    OnOffAuto msi;
49 50 51 52 53
};
typedef struct PCIBridgeDev PCIBridgeDev;

static int pci_bridge_dev_initfn(PCIDevice *dev)
{
54
    PCIBridge *br = PCI_BRIDGE(dev);
55
    PCIBridgeDev *bridge_dev = PCI_BRIDGE_DEV(dev);
56
    int err;
57
    Error *local_err = NULL;
58

59 60
    pci_bridge_initfn(dev, TYPE_PCI_BUS);

61 62 63 64 65 66 67 68 69 70
    if (bridge_dev->flags & (1 << PCI_BRIDGE_DEV_F_SHPC_REQ)) {
        dev->config[PCI_INTERRUPT_PIN] = 0x1;
        memory_region_init(&bridge_dev->bar, OBJECT(dev), "shpc-bar",
                           shpc_bar_size(dev));
        err = shpc_init(dev, &br->sec_bus, &bridge_dev->bar, 0);
        if (err) {
            goto shpc_error;
        }
    } else {
        /* MSI is not applicable without SHPC */
71
        bridge_dev->msi = ON_OFF_AUTO_OFF;
72
    }
C
Cao jin 已提交
73

74 75 76 77
    err = slotid_cap_init(dev, 0, bridge_dev->chassis_nr, 0);
    if (err) {
        goto slotid_error;
    }
C
Cao jin 已提交
78

79 80 81 82 83 84 85 86 87 88 89 90
    if (bridge_dev->msi != ON_OFF_AUTO_OFF) {
        /* it means SHPC exists, because MSI is needed by SHPC */

        err = msi_init(dev, 0, 1, true, true, &local_err);
        /* Any error other than -ENOTSUP(board's MSI support is broken)
         * is a programming error */
        assert(!err || err == -ENOTSUP);
        if (err && bridge_dev->msi == ON_OFF_AUTO_ON) {
            /* Can't satisfy user's explicit msi=on request, fail */
            error_append_hint(&local_err, "You have to use msi=auto (default) "
                    "or msi=off with this machine type.\n");
            error_report_err(local_err);
91 92
            goto msi_error;
        }
93 94 95
        assert(!local_err || bridge_dev->msi == ON_OFF_AUTO_AUTO);
        /* With msi=auto, we fall back to MSI off silently */
        error_free(local_err);
96
    }
C
Cao jin 已提交
97

98 99 100 101 102 103
    if (shpc_present(dev)) {
        /* TODO: spec recommends using 64 bit prefetcheable BAR.
         * Check whether that works well. */
        pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY |
                         PCI_BASE_ADDRESS_MEM_TYPE_64, &bridge_dev->bar);
    }
104
    return 0;
C
Cao jin 已提交
105

106 107 108
msi_error:
    slotid_cap_cleanup(dev);
slotid_error:
109 110 111
    if (shpc_present(dev)) {
        shpc_cleanup(dev, &bridge_dev->bar);
    }
112
shpc_error:
113
    pci_bridge_exitfn(dev);
114

115 116 117
    return err;
}

118
static void pci_bridge_dev_exitfn(PCIDevice *dev)
119
{
120
    PCIBridgeDev *bridge_dev = PCI_BRIDGE_DEV(dev);
121 122 123 124
    if (msi_present(dev)) {
        msi_uninit(dev);
    }
    slotid_cap_cleanup(dev);
125 126 127
    if (shpc_present(dev)) {
        shpc_cleanup(dev, &bridge_dev->bar);
    }
128
    pci_bridge_exitfn(dev);
129 130
}

131 132
static void pci_bridge_dev_instance_finalize(Object *obj)
{
133
    /* this function is idempotent and handles (PCIDevice.shpc == NULL) */
134 135 136
    shpc_free(PCI_DEVICE(obj));
}

137 138 139 140 141 142 143
static void pci_bridge_dev_write_config(PCIDevice *d,
                                        uint32_t address, uint32_t val, int len)
{
    pci_bridge_write_config(d, address, val, len);
    if (msi_present(d)) {
        msi_write_config(d, address, val, len);
    }
144 145 146
    if (shpc_present(d)) {
        shpc_cap_write_config(d, address, val, len);
    }
147 148 149 150
}

static void qdev_pci_bridge_dev_reset(DeviceState *qdev)
{
151
    PCIDevice *dev = PCI_DEVICE(qdev);
152

153
    pci_bridge_reset(qdev);
154 155 156
    if (shpc_present(dev)) {
        shpc_reset(dev);
    }
157 158 159 160
}

static Property pci_bridge_dev_properties[] = {
                    /* Note: 0 is not a legal chassis number. */
161 162
    DEFINE_PROP_UINT8(PCI_BRIDGE_DEV_PROP_CHASSIS_NR, PCIBridgeDev, chassis_nr,
                      0),
163 164
    DEFINE_PROP_ON_OFF_AUTO(PCI_BRIDGE_DEV_PROP_MSI, PCIBridgeDev, msi,
                            ON_OFF_AUTO_AUTO),
165 166
    DEFINE_PROP_BIT(PCI_BRIDGE_DEV_PROP_SHPC, PCIBridgeDev, flags,
                    PCI_BRIDGE_DEV_F_SHPC_REQ, true),
167 168 169
    DEFINE_PROP_END_OF_LIST(),
};

170 171 172 173 174 175 176
static bool pci_device_shpc_present(void *opaque, int version_id)
{
    PCIDevice *dev = opaque;

    return shpc_present(dev);
}

177 178 179
static const VMStateDescription pci_bridge_dev_vmstate = {
    .name = "pci_bridge",
    .fields = (VMStateField[]) {
180
        VMSTATE_PCI_DEVICE(parent_obj, PCIBridge),
181
        SHPC_VMSTATE(shpc, PCIDevice, pci_device_shpc_present),
182 183 184 185
        VMSTATE_END_OF_LIST()
    }
};

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
static void pci_bridge_dev_hotplug_cb(HotplugHandler *hotplug_dev,
                                      DeviceState *dev, Error **errp)
{
    PCIDevice *pci_hotplug_dev = PCI_DEVICE(hotplug_dev);

    if (!shpc_present(pci_hotplug_dev)) {
        error_setg(errp, "standard hotplug controller has been disabled for "
                   "this %s", TYPE_PCI_BRIDGE_DEV);
        return;
    }
    shpc_device_hotplug_cb(hotplug_dev, dev, errp);
}

static void pci_bridge_dev_hot_unplug_request_cb(HotplugHandler *hotplug_dev,
                                                 DeviceState *dev,
                                                 Error **errp)
{
    PCIDevice *pci_hotplug_dev = PCI_DEVICE(hotplug_dev);

    if (!shpc_present(pci_hotplug_dev)) {
        error_setg(errp, "standard hotplug controller has been disabled for "
                   "this %s", TYPE_PCI_BRIDGE_DEV);
        return;
    }
    shpc_device_hot_unplug_request_cb(hotplug_dev, dev, errp);
}

213 214 215 216
static void pci_bridge_dev_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
217 218
    HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);

219 220 221
    k->init = pci_bridge_dev_initfn;
    k->exit = pci_bridge_dev_exitfn;
    k->config_write = pci_bridge_dev_write_config;
222 223
    k->vendor_id = PCI_VENDOR_ID_REDHAT;
    k->device_id = PCI_DEVICE_ID_REDHAT_BRIDGE;
224 225 226 227 228 229
    k->class_id = PCI_CLASS_BRIDGE_PCI;
    k->is_bridge = 1,
    dc->desc = "Standard PCI Bridge";
    dc->reset = qdev_pci_bridge_dev_reset;
    dc->props = pci_bridge_dev_properties;
    dc->vmsd = &pci_bridge_dev_vmstate;
230
    set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
231 232
    hc->plug = pci_bridge_dev_hotplug_cb;
    hc->unplug_request = pci_bridge_dev_hot_unplug_request_cb;
233 234
}

235
static const TypeInfo pci_bridge_dev_info = {
236 237 238 239 240
    .name              = TYPE_PCI_BRIDGE_DEV,
    .parent            = TYPE_PCI_BRIDGE,
    .instance_size     = sizeof(PCIBridgeDev),
    .class_init        = pci_bridge_dev_class_init,
    .instance_finalize = pci_bridge_dev_instance_finalize,
241 242 243 244
    .interfaces = (InterfaceInfo[]) {
        { TYPE_HOTPLUG_HANDLER },
        { }
    }
245 246
};

G
Gerd Hoffmann 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
/*
 * Multiseat bridge.  Same as the standard pci bridge, only with a
 * different pci id, so we can match it easily in the guest for
 * automagic multiseat configuration.  See docs/multiseat.txt for more.
 */
static void pci_bridge_dev_seat_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);

    k->device_id = PCI_DEVICE_ID_REDHAT_BRIDGE_SEAT;
    dc->desc = "Standard PCI Bridge (multiseat)";
}

static const TypeInfo pci_bridge_dev_seat_info = {
    .name              = TYPE_PCI_BRIDGE_SEAT_DEV,
    .parent            = TYPE_PCI_BRIDGE_DEV,
    .instance_size     = sizeof(PCIBridgeDev),
    .class_init        = pci_bridge_dev_seat_class_init,
};

268 269 270
static void pci_bridge_dev_register(void)
{
    type_register_static(&pci_bridge_dev_info);
G
Gerd Hoffmann 已提交
271
    type_register_static(&pci_bridge_dev_seat_info);
272 273 274
}

type_init(pci_bridge_dev_register);