2011-07-03 17:00:09 +04:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2013-02-04 18:40:22 +04:00
|
|
|
#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"
|
2012-12-17 21:19:49 +04:00
|
|
|
#include "exec/memory.h"
|
2013-02-04 18:40:22 +04:00
|
|
|
#include "hw/pci/pci_bus.h"
|
2014-02-05 19:36:50 +04:00
|
|
|
#include "hw/hotplug.h"
|
2011-07-03 17:00:09 +04:00
|
|
|
|
2013-07-12 21:16:46 +04:00
|
|
|
#define TYPE_PCI_BRIDGE_DEV "pci-bridge"
|
|
|
|
#define PCI_BRIDGE_DEV(obj) \
|
|
|
|
OBJECT_CHECK(PCIBridgeDev, (obj), TYPE_PCI_BRIDGE_DEV)
|
|
|
|
|
2011-07-03 17:00:09 +04:00
|
|
|
struct PCIBridgeDev {
|
2013-07-12 21:16:46 +04:00
|
|
|
/*< private >*/
|
|
|
|
PCIBridge parent_obj;
|
|
|
|
/*< public >*/
|
|
|
|
|
2011-07-03 17:00:09 +04:00
|
|
|
MemoryRegion bar;
|
|
|
|
uint8_t chassis_nr;
|
|
|
|
#define PCI_BRIDGE_DEV_F_MSI_REQ 0
|
|
|
|
uint32_t flags;
|
|
|
|
};
|
|
|
|
typedef struct PCIBridgeDev PCIBridgeDev;
|
|
|
|
|
|
|
|
static int pci_bridge_dev_initfn(PCIDevice *dev)
|
|
|
|
{
|
2013-07-11 19:13:43 +04:00
|
|
|
PCIBridge *br = PCI_BRIDGE(dev);
|
2013-07-12 21:16:46 +04:00
|
|
|
PCIBridgeDev *bridge_dev = PCI_BRIDGE_DEV(dev);
|
2012-07-04 08:39:27 +04:00
|
|
|
int err;
|
|
|
|
|
2013-03-15 02:01:11 +04:00
|
|
|
err = pci_bridge_initfn(dev, TYPE_PCI_BUS);
|
2011-07-03 17:00:09 +04:00
|
|
|
if (err) {
|
|
|
|
goto bridge_error;
|
|
|
|
}
|
2013-10-07 11:36:36 +04:00
|
|
|
dev->config[PCI_INTERRUPT_PIN] = 0x1;
|
2013-06-07 05:25:08 +04:00
|
|
|
memory_region_init(&bridge_dev->bar, OBJECT(dev), "shpc-bar", shpc_bar_size(dev));
|
2011-07-03 17:00:09 +04:00
|
|
|
err = shpc_init(dev, &br->sec_bus, &bridge_dev->bar, 0);
|
|
|
|
if (err) {
|
|
|
|
goto shpc_error;
|
|
|
|
}
|
|
|
|
err = slotid_cap_init(dev, 0, bridge_dev->chassis_nr, 0);
|
|
|
|
if (err) {
|
|
|
|
goto slotid_error;
|
|
|
|
}
|
|
|
|
if ((bridge_dev->flags & (1 << PCI_BRIDGE_DEV_F_MSI_REQ)) &&
|
|
|
|
msi_supported) {
|
|
|
|
err = msi_init(dev, 0, 1, true, true);
|
|
|
|
if (err < 0) {
|
|
|
|
goto msi_error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* 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);
|
|
|
|
return 0;
|
|
|
|
msi_error:
|
|
|
|
slotid_cap_cleanup(dev);
|
|
|
|
slotid_error:
|
|
|
|
shpc_cleanup(dev, &bridge_dev->bar);
|
|
|
|
shpc_error:
|
2012-07-04 08:39:27 +04:00
|
|
|
pci_bridge_exitfn(dev);
|
2011-07-03 17:00:09 +04:00
|
|
|
bridge_error:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2012-07-04 08:39:27 +04:00
|
|
|
static void pci_bridge_dev_exitfn(PCIDevice *dev)
|
2011-07-03 17:00:09 +04:00
|
|
|
{
|
2013-07-12 21:16:46 +04:00
|
|
|
PCIBridgeDev *bridge_dev = PCI_BRIDGE_DEV(dev);
|
2011-07-03 17:00:09 +04:00
|
|
|
if (msi_present(dev)) {
|
|
|
|
msi_uninit(dev);
|
|
|
|
}
|
|
|
|
slotid_cap_cleanup(dev);
|
|
|
|
shpc_cleanup(dev, &bridge_dev->bar);
|
2012-07-04 08:39:27 +04:00
|
|
|
pci_bridge_exitfn(dev);
|
2011-07-03 17:00:09 +04:00
|
|
|
}
|
|
|
|
|
2015-02-12 23:39:20 +03:00
|
|
|
static void pci_bridge_dev_instance_finalize(Object *obj)
|
|
|
|
{
|
|
|
|
shpc_free(PCI_DEVICE(obj));
|
|
|
|
}
|
|
|
|
|
2011-07-03 17:00:09 +04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
shpc_cap_write_config(d, address, val, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qdev_pci_bridge_dev_reset(DeviceState *qdev)
|
|
|
|
{
|
2013-07-12 21:16:46 +04:00
|
|
|
PCIDevice *dev = PCI_DEVICE(qdev);
|
2012-05-16 03:09:56 +04:00
|
|
|
|
2011-07-03 17:00:09 +04:00
|
|
|
pci_bridge_reset(qdev);
|
|
|
|
shpc_reset(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Property pci_bridge_dev_properties[] = {
|
|
|
|
/* Note: 0 is not a legal chassis number. */
|
|
|
|
DEFINE_PROP_UINT8("chassis_nr", PCIBridgeDev, chassis_nr, 0),
|
|
|
|
DEFINE_PROP_BIT("msi", PCIBridgeDev, flags, PCI_BRIDGE_DEV_F_MSI_REQ, true),
|
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
|
|
|
static const VMStateDescription pci_bridge_dev_vmstate = {
|
|
|
|
.name = "pci_bridge",
|
|
|
|
.fields = (VMStateField[]) {
|
2013-07-12 21:16:46 +04:00
|
|
|
VMSTATE_PCI_DEVICE(parent_obj, PCIBridge),
|
|
|
|
SHPC_VMSTATE(shpc, PCIDevice),
|
2011-07-03 17:00:09 +04:00
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void pci_bridge_dev_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
|
|
|
PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
|
2014-02-05 19:36:50 +04:00
|
|
|
HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
|
|
|
|
|
2011-07-03 17:00:09 +04:00
|
|
|
k->init = pci_bridge_dev_initfn;
|
|
|
|
k->exit = pci_bridge_dev_exitfn;
|
|
|
|
k->config_write = pci_bridge_dev_write_config;
|
2012-12-13 13:19:38 +04:00
|
|
|
k->vendor_id = PCI_VENDOR_ID_REDHAT;
|
|
|
|
k->device_id = PCI_DEVICE_ID_REDHAT_BRIDGE;
|
2011-07-03 17:00:09 +04:00
|
|
|
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;
|
2013-07-29 18:17:45 +04:00
|
|
|
set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
|
2014-02-05 19:36:50 +04:00
|
|
|
hc->plug = shpc_device_hotplug_cb;
|
2014-09-26 13:28:19 +04:00
|
|
|
hc->unplug_request = shpc_device_hot_unplug_request_cb;
|
2011-07-03 17:00:09 +04:00
|
|
|
}
|
|
|
|
|
2013-01-10 19:19:07 +04:00
|
|
|
static const TypeInfo pci_bridge_dev_info = {
|
2015-02-12 23:39:20 +03:00
|
|
|
.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,
|
2014-02-05 19:36:50 +04:00
|
|
|
.interfaces = (InterfaceInfo[]) {
|
|
|
|
{ TYPE_HOTPLUG_HANDLER },
|
|
|
|
{ }
|
|
|
|
}
|
2011-07-03 17:00:09 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static void pci_bridge_dev_register(void)
|
|
|
|
{
|
|
|
|
type_register_static(&pci_bridge_dev_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(pci_bridge_dev_register);
|